mirror of
https://github.com/denoland/deno.git
synced 2024-12-24 08:09:08 -05:00
refactor(lsp): use deno_graph and single document struct (#12535)
Closes #12473
This commit is contained in:
parent
74a93fdf63
commit
34a9ddff09
17 changed files with 1673 additions and 2710 deletions
13
Cargo.lock
generated
13
Cargo.lock
generated
|
@ -781,9 +781,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "deno_doc"
|
||||
version = "0.17.1"
|
||||
version = "0.18.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "96b60f0447a17ed4fc413bd7abad29e7d31e76f13422813f2e7978b0c4bdca7a"
|
||||
checksum = "81744eb79bda23580020b2df68a150197312b54c47e11160a65add0534c03ec5"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"deno_ast",
|
||||
|
@ -826,9 +826,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "deno_graph"
|
||||
version = "0.8.2"
|
||||
version = "0.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d3d7197f490abd2a1e7d1edc3a1c4db1f4e294d7067dbbf65c4714932c7c3c70"
|
||||
checksum = "d7513c22ec28dd1a4eeb82a99fc543c92d7ee2f1a146a0dd6bea7427020eb863"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"cfg-if 1.0.0",
|
||||
|
@ -836,7 +836,7 @@ dependencies = [
|
|||
"deno_ast",
|
||||
"futures",
|
||||
"lazy_static",
|
||||
"parking_lot_core",
|
||||
"parking_lot",
|
||||
"regex",
|
||||
"ring",
|
||||
"serde",
|
||||
|
@ -1887,6 +1887,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "716d3d89f35ac6a34fd0eed635395f4c3b76fa889338a4632e5231a8684216bd"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
"web-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -41,8 +41,8 @@ winres = "0.1.11"
|
|||
[dependencies]
|
||||
deno_ast = { version = "0.4.1", features = ["bundler", "codegen", "dep_graph", "module_specifier", "proposal", "react", "sourcemap", "transforms", "typescript", "view", "visit"] }
|
||||
deno_core = { version = "0.105.0", path = "../core" }
|
||||
deno_doc = "0.17.1"
|
||||
deno_graph = "0.8.2"
|
||||
deno_doc = "0.18.0"
|
||||
deno_graph = "0.9.1"
|
||||
deno_lint = { version = "0.18.1", features = ["docs"] }
|
||||
deno_runtime = { version = "0.31.0", path = "../runtime" }
|
||||
deno_tls = { version = "0.10.0", path = "../ext/tls" }
|
||||
|
|
|
@ -91,7 +91,6 @@ impl Metadata {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn read(cache_filename: &Path) -> Result<Metadata, AnyError> {
|
||||
let metadata_filename = Metadata::filename(cache_filename);
|
||||
let metadata = fs::read_to_string(metadata_filename)?;
|
||||
|
|
|
@ -3,23 +3,10 @@
|
|||
use super::language_server;
|
||||
use super::tsc;
|
||||
|
||||
use crate::ast;
|
||||
use crate::ast::Location;
|
||||
use crate::config_file::LintConfig;
|
||||
use crate::lsp::documents::DocumentData;
|
||||
use crate::tools::lint::create_linter;
|
||||
use crate::tools::lint::get_configured_rules;
|
||||
|
||||
use deno_ast::swc::ast as swc_ast;
|
||||
use deno_ast::swc::common::comments::Comment;
|
||||
use deno_ast::swc::common::BytePos;
|
||||
use deno_ast::swc::common::Span;
|
||||
use deno_ast::swc::common::DUMMY_SP;
|
||||
use deno_ast::swc::visit::Node;
|
||||
use deno_ast::swc::visit::Visit;
|
||||
use deno_ast::swc::visit::VisitWith;
|
||||
use deno_ast::Diagnostic;
|
||||
use deno_ast::MediaType;
|
||||
use deno_ast::SourceTextInfo;
|
||||
use deno_core::error::anyhow;
|
||||
use deno_core::error::custom_error;
|
||||
|
@ -27,17 +14,13 @@ use deno_core::error::AnyError;
|
|||
use deno_core::serde::Deserialize;
|
||||
use deno_core::serde_json;
|
||||
use deno_core::serde_json::json;
|
||||
use deno_core::url;
|
||||
use deno_core::ModuleResolutionError;
|
||||
use deno_core::ModuleSpecifier;
|
||||
use import_map::ImportMap;
|
||||
use lspower::lsp;
|
||||
use lspower::lsp::Position;
|
||||
use lspower::lsp::Range;
|
||||
use regex::Regex;
|
||||
use std::cmp::Ordering;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
/// Diagnostic error codes which actually are the same, and so when grouping
|
||||
|
@ -84,56 +67,6 @@ lazy_static::lazy_static! {
|
|||
|
||||
const SUPPORTED_EXTENSIONS: &[&str] = &[".ts", ".tsx", ".js", ".jsx", ".mjs"];
|
||||
|
||||
// TODO(@kitsonk) remove after deno_graph migration
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
enum TypeScriptReference {
|
||||
Path(String),
|
||||
Types(String),
|
||||
}
|
||||
|
||||
fn match_to_span(comment: &Comment, m: ®ex::Match) -> Span {
|
||||
Span {
|
||||
lo: comment.span.lo + BytePos((m.start() + 1) as u32),
|
||||
hi: comment.span.lo + BytePos((m.end() + 1) as u32),
|
||||
ctxt: comment.span.ctxt,
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(@kitsonk) remove after deno_graph migration
|
||||
fn parse_deno_types(comment: &Comment) -> Option<(String, Span)> {
|
||||
let captures = DENO_TYPES_RE.captures(&comment.text)?;
|
||||
if let Some(m) = captures.get(1) {
|
||||
Some((m.as_str().to_string(), match_to_span(comment, &m)))
|
||||
} else if let Some(m) = captures.get(2) {
|
||||
Some((m.as_str().to_string(), match_to_span(comment, &m)))
|
||||
} else {
|
||||
unreachable!();
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(@kitsonk) remove after deno_graph migration
|
||||
fn parse_ts_reference(
|
||||
comment: &Comment,
|
||||
) -> Option<(TypeScriptReference, Span)> {
|
||||
if !TRIPLE_SLASH_REFERENCE_RE.is_match(&comment.text) {
|
||||
None
|
||||
} else if let Some(captures) = PATH_REFERENCE_RE.captures(&comment.text) {
|
||||
let m = captures.get(1).unwrap();
|
||||
Some((
|
||||
TypeScriptReference::Path(m.as_str().to_string()),
|
||||
match_to_span(comment, &m),
|
||||
))
|
||||
} else {
|
||||
TYPES_REFERENCE_RE.captures(&comment.text).map(|captures| {
|
||||
let m = captures.get(1).unwrap();
|
||||
(
|
||||
TypeScriptReference::Types(m.as_str().to_string()),
|
||||
match_to_span(comment, &m),
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Category of self-generated diagnostic messages (those not coming from)
|
||||
/// TypeScript.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
|
@ -219,266 +152,6 @@ pub fn get_lint_references(
|
|||
)
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
||||
pub struct Dependency {
|
||||
pub is_dynamic: bool,
|
||||
pub maybe_code: Option<ResolvedDependency>,
|
||||
pub maybe_code_specifier_range: Option<Range>,
|
||||
pub maybe_type: Option<ResolvedDependency>,
|
||||
pub maybe_type_specifier_range: Option<Range>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum ResolvedDependencyErr {
|
||||
InvalidDowngrade,
|
||||
InvalidLocalImport,
|
||||
InvalidSpecifier(ModuleResolutionError),
|
||||
Missing,
|
||||
}
|
||||
|
||||
impl ResolvedDependencyErr {
|
||||
pub fn as_code(&self) -> lsp::NumberOrString {
|
||||
match self {
|
||||
Self::InvalidDowngrade => {
|
||||
lsp::NumberOrString::String("invalid-downgrade".to_string())
|
||||
}
|
||||
Self::InvalidLocalImport => {
|
||||
lsp::NumberOrString::String("invalid-local-import".to_string())
|
||||
}
|
||||
Self::InvalidSpecifier(error) => match error {
|
||||
ModuleResolutionError::ImportPrefixMissing(_, _) => {
|
||||
lsp::NumberOrString::String("import-prefix-missing".to_string())
|
||||
}
|
||||
ModuleResolutionError::InvalidBaseUrl(_) => {
|
||||
lsp::NumberOrString::String("invalid-base-url".to_string())
|
||||
}
|
||||
ModuleResolutionError::InvalidPath(_) => {
|
||||
lsp::NumberOrString::String("invalid-path".to_string())
|
||||
}
|
||||
ModuleResolutionError::InvalidUrl(_) => {
|
||||
lsp::NumberOrString::String("invalid-url".to_string())
|
||||
}
|
||||
},
|
||||
Self::Missing => lsp::NumberOrString::String("missing".to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ResolvedDependencyErr {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::InvalidDowngrade => {
|
||||
write!(f, "HTTPS modules cannot import HTTP modules.")
|
||||
}
|
||||
Self::InvalidLocalImport => {
|
||||
write!(f, "Remote modules cannot import local modules.")
|
||||
}
|
||||
Self::InvalidSpecifier(err) => write!(f, "{}", err),
|
||||
Self::Missing => write!(f, "The module is unexpectedly missing."),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum ResolvedDependency {
|
||||
Resolved(ModuleSpecifier),
|
||||
Err(ResolvedDependencyErr),
|
||||
}
|
||||
|
||||
impl ResolvedDependency {
|
||||
pub fn as_hover_text(&self) -> String {
|
||||
match self {
|
||||
Self::Resolved(specifier) => match specifier.scheme() {
|
||||
"data" => "_(a data url)_".to_string(),
|
||||
"blob" => "_(a blob url)_".to_string(),
|
||||
_ => format!(
|
||||
"{}​{}",
|
||||
specifier[..url::Position::AfterScheme].to_string(),
|
||||
specifier[url::Position::AfterScheme..].to_string()
|
||||
),
|
||||
},
|
||||
Self::Err(_) => "_[errored]_".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resolve_import(
|
||||
specifier: &str,
|
||||
referrer: &ModuleSpecifier,
|
||||
maybe_import_map: &Option<ImportMap>,
|
||||
) -> ResolvedDependency {
|
||||
let maybe_mapped = if let Some(import_map) = maybe_import_map {
|
||||
import_map.resolve(specifier, referrer.as_str()).ok()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let remapped = maybe_mapped.is_some();
|
||||
let specifier = if let Some(remapped) = maybe_mapped {
|
||||
remapped
|
||||
} else {
|
||||
match deno_core::resolve_import(specifier, referrer.as_str()) {
|
||||
Ok(resolved) => resolved,
|
||||
Err(err) => {
|
||||
return ResolvedDependency::Err(
|
||||
ResolvedDependencyErr::InvalidSpecifier(err),
|
||||
)
|
||||
}
|
||||
}
|
||||
};
|
||||
let referrer_scheme = referrer.scheme();
|
||||
let specifier_scheme = specifier.scheme();
|
||||
if referrer_scheme == "https" && specifier_scheme == "http" {
|
||||
return ResolvedDependency::Err(ResolvedDependencyErr::InvalidDowngrade);
|
||||
}
|
||||
if (referrer_scheme == "https" || referrer_scheme == "http")
|
||||
&& !(specifier_scheme == "https" || specifier_scheme == "http")
|
||||
&& !remapped
|
||||
{
|
||||
return ResolvedDependency::Err(ResolvedDependencyErr::InvalidLocalImport);
|
||||
}
|
||||
|
||||
ResolvedDependency::Resolved(specifier)
|
||||
}
|
||||
|
||||
pub fn parse_module(
|
||||
specifier: &ModuleSpecifier,
|
||||
source: SourceTextInfo,
|
||||
media_type: MediaType,
|
||||
) -> Result<deno_ast::ParsedSource, Diagnostic> {
|
||||
deno_ast::parse_module(deno_ast::ParseParams {
|
||||
specifier: specifier.as_str().to_string(),
|
||||
source,
|
||||
media_type,
|
||||
// capture the tokens for linting and formatting
|
||||
capture_tokens: true,
|
||||
maybe_syntax: None,
|
||||
scope_analysis: true, // for deno_lint
|
||||
})
|
||||
}
|
||||
|
||||
// TODO(@kitsonk) a lot of this logic is duplicated in module_graph.rs in
|
||||
// Module::parse() and should be refactored out to a common function.
|
||||
pub fn analyze_dependencies(
|
||||
specifier: &ModuleSpecifier,
|
||||
media_type: MediaType,
|
||||
parsed_source: &deno_ast::ParsedSource,
|
||||
maybe_import_map: &Option<ImportMap>,
|
||||
) -> (HashMap<String, Dependency>, Option<ResolvedDependency>) {
|
||||
let mut maybe_type = None;
|
||||
let mut dependencies = HashMap::<String, Dependency>::new();
|
||||
|
||||
// Parse leading comments for supported triple slash references.
|
||||
for comment in parsed_source.get_leading_comments().iter() {
|
||||
if let Some((ts_reference, span)) = parse_ts_reference(comment) {
|
||||
let loc = parsed_source.source().line_and_column_index(span.lo);
|
||||
match ts_reference {
|
||||
TypeScriptReference::Path(import) => {
|
||||
let dep = dependencies.entry(import.clone()).or_default();
|
||||
let resolved_import =
|
||||
resolve_import(&import, specifier, maybe_import_map);
|
||||
dep.maybe_code = Some(resolved_import);
|
||||
dep.maybe_code_specifier_range = Some(Range {
|
||||
start: Position {
|
||||
line: loc.line_index as u32,
|
||||
character: loc.column_index as u32,
|
||||
},
|
||||
end: Position {
|
||||
line: loc.line_index as u32,
|
||||
character: (loc.column_index + import.chars().count() + 2) as u32,
|
||||
},
|
||||
});
|
||||
}
|
||||
TypeScriptReference::Types(import) => {
|
||||
let resolved_import =
|
||||
resolve_import(&import, specifier, maybe_import_map);
|
||||
if media_type == MediaType::JavaScript || media_type == MediaType::Jsx
|
||||
{
|
||||
maybe_type = Some(resolved_import.clone());
|
||||
}
|
||||
let dep = dependencies.entry(import.clone()).or_default();
|
||||
dep.maybe_type = Some(resolved_import);
|
||||
dep.maybe_type_specifier_range = Some(Range {
|
||||
start: Position {
|
||||
line: loc.line_index as u32,
|
||||
character: loc.column_index as u32,
|
||||
},
|
||||
end: Position {
|
||||
line: loc.line_index as u32,
|
||||
character: (loc.column_index + import.chars().count() + 2) as u32,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Parse ES and type only imports
|
||||
let descriptors = deno_graph::analyze_dependencies(parsed_source);
|
||||
for desc in descriptors.into_iter().filter(|desc| {
|
||||
desc.kind != deno_ast::swc::dep_graph::DependencyKind::Require
|
||||
}) {
|
||||
let resolved_import =
|
||||
resolve_import(&desc.specifier, specifier, maybe_import_map);
|
||||
|
||||
let maybe_resolved_type_dependency =
|
||||
// Check for `@deno-types` pragmas that affect the import
|
||||
if let Some(comment) = desc.leading_comments.last() {
|
||||
parse_deno_types(comment).as_ref().map(|(deno_types, span)| {
|
||||
(
|
||||
resolve_import(deno_types, specifier, maybe_import_map),
|
||||
deno_types.clone(),
|
||||
parsed_source.source().line_and_column_index(span.lo)
|
||||
)
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let dep = dependencies.entry(desc.specifier.to_string()).or_default();
|
||||
dep.is_dynamic = desc.is_dynamic;
|
||||
let start = parsed_source
|
||||
.source()
|
||||
.line_and_column_index(desc.specifier_span.lo);
|
||||
let end = parsed_source
|
||||
.source()
|
||||
.line_and_column_index(desc.specifier_span.hi);
|
||||
let range = Range {
|
||||
start: Position {
|
||||
line: start.line_index as u32,
|
||||
character: start.column_index as u32,
|
||||
},
|
||||
end: Position {
|
||||
line: end.line_index as u32,
|
||||
character: end.column_index as u32,
|
||||
},
|
||||
};
|
||||
dep.maybe_code_specifier_range = Some(range);
|
||||
dep.maybe_code = Some(resolved_import);
|
||||
if dep.maybe_type.is_none() {
|
||||
if let Some((resolved_dependency, specifier, loc)) =
|
||||
maybe_resolved_type_dependency
|
||||
{
|
||||
dep.maybe_type_specifier_range = Some(Range {
|
||||
start: Position {
|
||||
line: loc.line_index as u32,
|
||||
// +1 to skip quote
|
||||
character: (loc.column_index + 1) as u32,
|
||||
},
|
||||
end: Position {
|
||||
line: loc.line_index as u32,
|
||||
// +1 to skip quote
|
||||
character: (loc.column_index + 1 + specifier.chars().count())
|
||||
as u32,
|
||||
},
|
||||
});
|
||||
dep.maybe_type = Some(resolved_dependency);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(dependencies, maybe_type)
|
||||
}
|
||||
|
||||
fn code_as_string(code: &Option<lsp::NumberOrString>) -> String {
|
||||
match code {
|
||||
Some(lsp::NumberOrString::String(str)) => str.clone(),
|
||||
|
@ -494,21 +167,16 @@ fn check_specifier(
|
|||
specifier: &str,
|
||||
referrer: &ModuleSpecifier,
|
||||
snapshot: &language_server::StateSnapshot,
|
||||
maybe_import_map: &Option<ImportMap>,
|
||||
) -> Option<String> {
|
||||
for ext in SUPPORTED_EXTENSIONS {
|
||||
let specifier_with_ext = format!("{}{}", specifier, ext);
|
||||
if let ResolvedDependency::Resolved(resolved_specifier) =
|
||||
resolve_import(&specifier_with_ext, referrer, maybe_import_map)
|
||||
{
|
||||
if snapshot.documents.contains_key(&resolved_specifier)
|
||||
|| snapshot.sources.contains_key(&resolved_specifier)
|
||||
if snapshot
|
||||
.documents
|
||||
.contains_import(&specifier_with_ext, referrer)
|
||||
{
|
||||
return Some(specifier_with_ext);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
|
@ -531,12 +199,9 @@ pub(crate) fn fix_ts_import_changes(
|
|||
.get(1)
|
||||
.ok_or_else(|| anyhow!("Missing capture."))?
|
||||
.as_str();
|
||||
if let Some(new_specifier) = check_specifier(
|
||||
specifier,
|
||||
referrer,
|
||||
&snapshot,
|
||||
&language_server.maybe_import_map,
|
||||
) {
|
||||
if let Some(new_specifier) =
|
||||
check_specifier(specifier, referrer, &snapshot)
|
||||
{
|
||||
let new_text =
|
||||
text_change.new_text.replace(specifier, &new_specifier);
|
||||
text_changes.push(tsc::TextChange {
|
||||
|
@ -582,12 +247,9 @@ fn fix_ts_import_action(
|
|||
.ok_or_else(|| anyhow!("Missing capture."))?
|
||||
.as_str();
|
||||
let snapshot = language_server.snapshot()?;
|
||||
if let Some(new_specifier) = check_specifier(
|
||||
specifier,
|
||||
referrer,
|
||||
&snapshot,
|
||||
&language_server.maybe_import_map,
|
||||
) {
|
||||
if let Some(new_specifier) =
|
||||
check_specifier(specifier, referrer, &snapshot)
|
||||
{
|
||||
let description = action.description.replace(specifier, &new_specifier);
|
||||
let changes = action
|
||||
.changes
|
||||
|
@ -755,8 +417,9 @@ impl CodeActionCollection {
|
|||
pub(crate) fn add_deno_lint_ignore_action(
|
||||
&mut self,
|
||||
specifier: &ModuleSpecifier,
|
||||
document: Option<&DocumentData>,
|
||||
diagnostic: &lsp::Diagnostic,
|
||||
maybe_text_info: Option<SourceTextInfo>,
|
||||
maybe_parsed_source: Option<deno_ast::ParsedSource>,
|
||||
) -> Result<(), AnyError> {
|
||||
let code = diagnostic
|
||||
.code
|
||||
|
@ -767,11 +430,8 @@ impl CodeActionCollection {
|
|||
})
|
||||
.unwrap();
|
||||
|
||||
let document_source = document.map(|d| d.source());
|
||||
|
||||
let line_content = document_source.map(|d| {
|
||||
d.text_info()
|
||||
.line_text(diagnostic.range.start.line as usize)
|
||||
let line_content = maybe_text_info.map(|ti| {
|
||||
ti.line_text(diagnostic.range.start.line as usize)
|
||||
.to_string()
|
||||
});
|
||||
|
||||
|
@ -814,9 +474,7 @@ impl CodeActionCollection {
|
|||
.push(CodeActionKind::DenoLint(ignore_error_action));
|
||||
|
||||
// Disable a lint error for the entire file.
|
||||
let parsed_source =
|
||||
document_source.and_then(|d| d.module().and_then(|r| r.as_ref().ok()));
|
||||
let maybe_ignore_comment = parsed_source.and_then(|ps| {
|
||||
let maybe_ignore_comment = maybe_parsed_source.clone().and_then(|ps| {
|
||||
// Note: we can use ps.get_leading_comments() but it doesn't
|
||||
// work when shebang is present at the top of the file.
|
||||
ps.comments().get_vec().iter().find_map(|c| {
|
||||
|
@ -847,7 +505,7 @@ impl CodeActionCollection {
|
|||
if let Some(ignore_comment) = maybe_ignore_comment {
|
||||
new_text = format!(" {}", code);
|
||||
// Get the end position of the comment.
|
||||
let line = parsed_source
|
||||
let line = maybe_parsed_source
|
||||
.unwrap()
|
||||
.source()
|
||||
.line_and_column_index(ignore_comment.span.hi());
|
||||
|
@ -1099,157 +757,9 @@ fn prepend_whitespace(content: String, line_content: Option<String>) -> String {
|
|||
}
|
||||
}
|
||||
|
||||
/// Get LSP range from the provided start and end locations.
|
||||
fn get_range_from_location(
|
||||
start: &ast::Location,
|
||||
end: &ast::Location,
|
||||
) -> lsp::Range {
|
||||
lsp::Range {
|
||||
start: lsp::Position {
|
||||
line: (start.line - 1) as u32,
|
||||
character: start.col as u32,
|
||||
},
|
||||
end: lsp::Position {
|
||||
line: (end.line - 1) as u32,
|
||||
character: end.col as u32,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Narrow the range to only include the text of the specifier, excluding the
|
||||
/// quotes.
|
||||
fn narrow_range(range: lsp::Range) -> lsp::Range {
|
||||
lsp::Range {
|
||||
start: lsp::Position {
|
||||
line: range.start.line,
|
||||
character: range.start.character + 1,
|
||||
},
|
||||
end: lsp::Position {
|
||||
line: range.end.line,
|
||||
character: range.end.character - 1,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct DependencyRange {
|
||||
/// The LSP Range is inclusive of the quotes around the specifier.
|
||||
pub range: lsp::Range,
|
||||
/// The text of the specifier within the document.
|
||||
pub specifier: String,
|
||||
}
|
||||
|
||||
impl DependencyRange {
|
||||
/// Determine if the position is within the range
|
||||
fn within(&self, position: &lsp::Position) -> bool {
|
||||
(position.line > self.range.start.line
|
||||
|| position.line == self.range.start.line
|
||||
&& position.character >= self.range.start.character)
|
||||
&& (position.line < self.range.end.line
|
||||
|| position.line == self.range.end.line
|
||||
&& position.character <= self.range.end.character)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct DependencyRanges(Vec<DependencyRange>);
|
||||
|
||||
impl DependencyRanges {
|
||||
pub fn contains(&self, position: &lsp::Position) -> Option<DependencyRange> {
|
||||
self.0.iter().find(|r| r.within(position)).cloned()
|
||||
}
|
||||
}
|
||||
|
||||
struct DependencyRangeCollector<'a> {
|
||||
import_ranges: DependencyRanges,
|
||||
parsed_source: &'a deno_ast::ParsedSource,
|
||||
}
|
||||
|
||||
impl<'a> DependencyRangeCollector<'a> {
|
||||
pub fn new(parsed_source: &'a deno_ast::ParsedSource) -> Self {
|
||||
Self {
|
||||
import_ranges: DependencyRanges::default(),
|
||||
parsed_source,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn take(self) -> DependencyRanges {
|
||||
self.import_ranges
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Visit for DependencyRangeCollector<'a> {
|
||||
fn visit_import_decl(
|
||||
&mut self,
|
||||
node: &swc_ast::ImportDecl,
|
||||
_parent: &dyn Node,
|
||||
) {
|
||||
let start = Location::from_pos(self.parsed_source, node.src.span.lo);
|
||||
let end = Location::from_pos(self.parsed_source, node.src.span.hi);
|
||||
self.import_ranges.0.push(DependencyRange {
|
||||
range: narrow_range(get_range_from_location(&start, &end)),
|
||||
specifier: node.src.value.to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
fn visit_named_export(
|
||||
&mut self,
|
||||
node: &swc_ast::NamedExport,
|
||||
_parent: &dyn Node,
|
||||
) {
|
||||
if let Some(src) = &node.src {
|
||||
let start = Location::from_pos(self.parsed_source, src.span.lo);
|
||||
let end = Location::from_pos(self.parsed_source, src.span.hi);
|
||||
self.import_ranges.0.push(DependencyRange {
|
||||
range: narrow_range(get_range_from_location(&start, &end)),
|
||||
specifier: src.value.to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_export_all(
|
||||
&mut self,
|
||||
node: &swc_ast::ExportAll,
|
||||
_parent: &dyn Node,
|
||||
) {
|
||||
let start = Location::from_pos(self.parsed_source, node.src.span.lo);
|
||||
let end = Location::from_pos(self.parsed_source, node.src.span.hi);
|
||||
self.import_ranges.0.push(DependencyRange {
|
||||
range: narrow_range(get_range_from_location(&start, &end)),
|
||||
specifier: node.src.value.to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
fn visit_ts_import_type(
|
||||
&mut self,
|
||||
node: &swc_ast::TsImportType,
|
||||
_parent: &dyn Node,
|
||||
) {
|
||||
let start = Location::from_pos(self.parsed_source, node.arg.span.lo);
|
||||
let end = Location::from_pos(self.parsed_source, node.arg.span.hi);
|
||||
self.import_ranges.0.push(DependencyRange {
|
||||
range: narrow_range(get_range_from_location(&start, &end)),
|
||||
specifier: node.arg.value.to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Analyze a document for import ranges, which then can be used to identify if
|
||||
/// a particular position within the document as inside an import range.
|
||||
pub fn analyze_dependency_ranges(
|
||||
parsed_source: &deno_ast::ParsedSource,
|
||||
) -> Result<DependencyRanges, AnyError> {
|
||||
let mut collector = DependencyRangeCollector::new(parsed_source);
|
||||
parsed_source
|
||||
.module()
|
||||
.visit_with(&swc_ast::Invalid { span: DUMMY_SP }, &mut collector);
|
||||
Ok(collector.take())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use deno_core::resolve_url;
|
||||
|
||||
#[test]
|
||||
fn test_reference_to_diagnostic() {
|
||||
|
@ -1338,209 +848,4 @@ mod tests {
|
|||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_lint_references() {
|
||||
let specifier = resolve_url("file:///a.ts").expect("bad specifier");
|
||||
let source = "const foo = 42;";
|
||||
let parsed_module = parse_module(
|
||||
&specifier,
|
||||
SourceTextInfo::from_string(source.to_string()),
|
||||
MediaType::TypeScript,
|
||||
)
|
||||
.unwrap();
|
||||
let actual = get_lint_references(&parsed_module, None).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
actual,
|
||||
vec![Reference {
|
||||
category: Category::Lint {
|
||||
message: "`foo` is never used".to_string(),
|
||||
code: "no-unused-vars".to_string(),
|
||||
hint: Some(
|
||||
"If this is intentional, prefix it with an underscore like `_foo`"
|
||||
.to_string()
|
||||
),
|
||||
},
|
||||
range: Range {
|
||||
start: Position {
|
||||
line: 0,
|
||||
character: 6,
|
||||
},
|
||||
end: Position {
|
||||
line: 0,
|
||||
character: 9,
|
||||
}
|
||||
}
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_analyze_dependencies() {
|
||||
let specifier = resolve_url("file:///a.ts").expect("bad specifier");
|
||||
let source = r#"import {
|
||||
Application,
|
||||
Context,
|
||||
Router,
|
||||
Status,
|
||||
} from "https://deno.land/x/oak@v6.3.2/mod.ts";
|
||||
|
||||
import type { Component } from "https://esm.sh/preact";
|
||||
import { h, Fragment } from "https://esm.sh/preact";
|
||||
|
||||
// @deno-types="https://deno.land/x/types/react/index.d.ts";
|
||||
import React from "https://cdn.skypack.dev/react";
|
||||
"#;
|
||||
let parsed_module = parse_module(
|
||||
&specifier,
|
||||
SourceTextInfo::from_string(source.to_string()),
|
||||
MediaType::TypeScript,
|
||||
)
|
||||
.unwrap();
|
||||
let (actual, maybe_type) = analyze_dependencies(
|
||||
&specifier,
|
||||
MediaType::TypeScript,
|
||||
&parsed_module,
|
||||
&None,
|
||||
);
|
||||
assert!(maybe_type.is_none());
|
||||
assert_eq!(actual.len(), 3);
|
||||
assert_eq!(
|
||||
actual.get("https://cdn.skypack.dev/react").cloned(),
|
||||
Some(Dependency {
|
||||
is_dynamic: false,
|
||||
maybe_code: Some(ResolvedDependency::Resolved(
|
||||
resolve_url("https://cdn.skypack.dev/react").unwrap()
|
||||
)),
|
||||
maybe_type: Some(ResolvedDependency::Resolved(
|
||||
resolve_url("https://deno.land/x/types/react/index.d.ts").unwrap()
|
||||
)),
|
||||
maybe_code_specifier_range: Some(Range {
|
||||
start: Position {
|
||||
line: 11,
|
||||
character: 22,
|
||||
},
|
||||
end: Position {
|
||||
line: 11,
|
||||
character: 53,
|
||||
}
|
||||
}),
|
||||
maybe_type_specifier_range: Some(Range {
|
||||
start: Position {
|
||||
line: 10,
|
||||
character: 20,
|
||||
},
|
||||
end: Position {
|
||||
line: 10,
|
||||
character: 62,
|
||||
}
|
||||
})
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
actual.get("https://deno.land/x/oak@v6.3.2/mod.ts").cloned(),
|
||||
Some(Dependency {
|
||||
is_dynamic: false,
|
||||
maybe_code: Some(ResolvedDependency::Resolved(
|
||||
resolve_url("https://deno.land/x/oak@v6.3.2/mod.ts").unwrap()
|
||||
)),
|
||||
maybe_type: None,
|
||||
maybe_code_specifier_range: Some(Range {
|
||||
start: Position {
|
||||
line: 5,
|
||||
character: 11,
|
||||
},
|
||||
end: Position {
|
||||
line: 5,
|
||||
character: 50,
|
||||
}
|
||||
}),
|
||||
maybe_type_specifier_range: None,
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
actual.get("https://esm.sh/preact").cloned(),
|
||||
Some(Dependency {
|
||||
is_dynamic: false,
|
||||
maybe_code: Some(ResolvedDependency::Resolved(
|
||||
resolve_url("https://esm.sh/preact").unwrap()
|
||||
)),
|
||||
maybe_type: None,
|
||||
maybe_code_specifier_range: Some(Range {
|
||||
start: Position {
|
||||
line: 8,
|
||||
character: 32
|
||||
},
|
||||
end: Position {
|
||||
line: 8,
|
||||
character: 55
|
||||
}
|
||||
}),
|
||||
maybe_type_specifier_range: None,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_analyze_dependency_ranges() {
|
||||
let specifier = resolve_url("file:///a.ts").unwrap();
|
||||
let source =
|
||||
"import * as a from \"./b.ts\";\nexport * as a from \"./c.ts\";\n";
|
||||
let media_type = MediaType::TypeScript;
|
||||
let parsed_module = parse_module(
|
||||
&specifier,
|
||||
SourceTextInfo::from_string(source.to_string()),
|
||||
media_type,
|
||||
)
|
||||
.unwrap();
|
||||
let result = analyze_dependency_ranges(&parsed_module);
|
||||
assert!(result.is_ok());
|
||||
let actual = result.unwrap();
|
||||
assert_eq!(
|
||||
actual.contains(&lsp::Position {
|
||||
line: 0,
|
||||
character: 0,
|
||||
}),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
actual.contains(&lsp::Position {
|
||||
line: 0,
|
||||
character: 22,
|
||||
}),
|
||||
Some(DependencyRange {
|
||||
range: lsp::Range {
|
||||
start: lsp::Position {
|
||||
line: 0,
|
||||
character: 20,
|
||||
},
|
||||
end: lsp::Position {
|
||||
line: 0,
|
||||
character: 26,
|
||||
},
|
||||
},
|
||||
specifier: "./b.ts".to_string(),
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
actual.contains(&lsp::Position {
|
||||
line: 1,
|
||||
character: 22,
|
||||
}),
|
||||
Some(DependencyRange {
|
||||
range: lsp::Range {
|
||||
start: lsp::Position {
|
||||
line: 1,
|
||||
character: 20,
|
||||
},
|
||||
end: lsp::Position {
|
||||
line: 1,
|
||||
character: 26,
|
||||
},
|
||||
},
|
||||
specifier: "./c.ts".to_string(),
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ use regex::Regex;
|
|||
use std::cell::RefCell;
|
||||
use std::collections::HashSet;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
static ref ABSTRACT_MODIFIER: Regex = Regex::new(r"\babstract\b").unwrap();
|
||||
|
@ -61,18 +62,15 @@ fn span_to_range(span: &Span, parsed_source: &ParsedSource) -> lsp::Range {
|
|||
}
|
||||
}
|
||||
|
||||
struct DenoTestCollector<'a> {
|
||||
struct DenoTestCollector {
|
||||
code_lenses: Vec<lsp::CodeLens>,
|
||||
parsed_source: &'a ParsedSource,
|
||||
parsed_source: ParsedSource,
|
||||
specifier: ModuleSpecifier,
|
||||
test_vars: HashSet<String>,
|
||||
}
|
||||
|
||||
impl<'a> DenoTestCollector<'a> {
|
||||
pub fn new(
|
||||
specifier: ModuleSpecifier,
|
||||
parsed_source: &'a ParsedSource,
|
||||
) -> Self {
|
||||
impl DenoTestCollector {
|
||||
pub fn new(specifier: ModuleSpecifier, parsed_source: ParsedSource) -> Self {
|
||||
Self {
|
||||
code_lenses: Vec::new(),
|
||||
parsed_source,
|
||||
|
@ -82,7 +80,7 @@ impl<'a> DenoTestCollector<'a> {
|
|||
}
|
||||
|
||||
fn add_code_lens<N: AsRef<str>>(&mut self, name: N, span: &Span) {
|
||||
let range = span_to_range(span, self.parsed_source);
|
||||
let range = span_to_range(span, &self.parsed_source);
|
||||
self.code_lenses.push(lsp::CodeLens {
|
||||
range,
|
||||
command: Some(lsp::Command {
|
||||
|
@ -130,7 +128,7 @@ impl<'a> DenoTestCollector<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Visit for DenoTestCollector<'a> {
|
||||
impl Visit for DenoTestCollector {
|
||||
fn visit_call_expr(&mut self, node: &ast::CallExpr, _parent: &dyn Node) {
|
||||
if let ast::ExprOrSuper::Expr(callee_expr) = &node.callee {
|
||||
match callee_expr.as_ref() {
|
||||
|
@ -238,7 +236,7 @@ async fn resolve_implementation_code_lens(
|
|||
let implementation_specifier =
|
||||
resolve_url(&implementation.document_span.file_name)?;
|
||||
let implementation_location =
|
||||
implementation.to_location(&line_index, language_server);
|
||||
implementation.to_location(line_index.clone(), language_server);
|
||||
if !(implementation_specifier == data.specifier
|
||||
&& implementation_location.range.start == code_lens.range.start)
|
||||
{
|
||||
|
@ -311,7 +309,7 @@ async fn resolve_references_code_lens(
|
|||
resolve_url(&reference.document_span.file_name)?;
|
||||
let line_index =
|
||||
language_server.get_line_index(reference_specifier).await?;
|
||||
locations.push(reference.to_location(&line_index, language_server));
|
||||
locations.push(reference.to_location(line_index, language_server));
|
||||
}
|
||||
let command = if !locations.is_empty() {
|
||||
let title = if locations.len() > 1 {
|
||||
|
@ -372,9 +370,9 @@ pub(crate) async fn resolve_code_lens(
|
|||
|
||||
pub(crate) async fn collect(
|
||||
specifier: &ModuleSpecifier,
|
||||
parsed_source: Option<&ParsedSource>,
|
||||
parsed_source: Option<ParsedSource>,
|
||||
config: &Config,
|
||||
line_index: &LineIndex,
|
||||
line_index: Arc<LineIndex>,
|
||||
navigation_tree: &NavigationTree,
|
||||
) -> Result<Vec<lsp::CodeLens>, AnyError> {
|
||||
let mut code_lenses = collect_test(specifier, parsed_source, config)?;
|
||||
|
@ -393,13 +391,13 @@ pub(crate) async fn collect(
|
|||
|
||||
fn collect_test(
|
||||
specifier: &ModuleSpecifier,
|
||||
parsed_source: Option<&ParsedSource>,
|
||||
parsed_source: Option<ParsedSource>,
|
||||
config: &Config,
|
||||
) -> Result<Vec<lsp::CodeLens>, AnyError> {
|
||||
if config.specifier_code_lens_test(specifier) {
|
||||
if let Some(parsed_source) = parsed_source {
|
||||
let mut collector =
|
||||
DenoTestCollector::new(specifier.clone(), parsed_source);
|
||||
DenoTestCollector::new(specifier.clone(), parsed_source.clone());
|
||||
parsed_source.module().visit_with(
|
||||
&ast::Invalid {
|
||||
span: deno_ast::swc::common::DUMMY_SP,
|
||||
|
@ -416,7 +414,7 @@ fn collect_test(
|
|||
async fn collect_tsc(
|
||||
specifier: &ModuleSpecifier,
|
||||
workspace_settings: &WorkspaceSettings,
|
||||
line_index: &LineIndex,
|
||||
line_index: Arc<LineIndex>,
|
||||
navigation_tree: &NavigationTree,
|
||||
) -> Result<Vec<lsp::CodeLens>, AnyError> {
|
||||
let code_lenses = Rc::new(RefCell::new(Vec::new()));
|
||||
|
@ -428,7 +426,11 @@ async fn collect_tsc(
|
|||
let source = CodeLensSource::Implementations;
|
||||
match i.kind {
|
||||
tsc::ScriptElementKind::InterfaceElement => {
|
||||
code_lenses.push(i.to_code_lens(line_index, specifier, &source));
|
||||
code_lenses.push(i.to_code_lens(
|
||||
line_index.clone(),
|
||||
specifier,
|
||||
&source,
|
||||
));
|
||||
}
|
||||
tsc::ScriptElementKind::ClassElement
|
||||
| tsc::ScriptElementKind::MemberFunctionElement
|
||||
|
@ -436,7 +438,11 @@ async fn collect_tsc(
|
|||
| tsc::ScriptElementKind::MemberGetAccessorElement
|
||||
| tsc::ScriptElementKind::MemberSetAccessorElement => {
|
||||
if ABSTRACT_MODIFIER.is_match(&i.kind_modifiers) {
|
||||
code_lenses.push(i.to_code_lens(line_index, specifier, &source));
|
||||
code_lenses.push(i.to_code_lens(
|
||||
line_index.clone(),
|
||||
specifier,
|
||||
&source,
|
||||
));
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
|
@ -448,31 +454,51 @@ async fn collect_tsc(
|
|||
let source = CodeLensSource::References;
|
||||
if let Some(parent) = &mp {
|
||||
if parent.kind == tsc::ScriptElementKind::EnumElement {
|
||||
code_lenses.push(i.to_code_lens(line_index, specifier, &source));
|
||||
code_lenses.push(i.to_code_lens(
|
||||
line_index.clone(),
|
||||
specifier,
|
||||
&source,
|
||||
));
|
||||
}
|
||||
}
|
||||
match i.kind {
|
||||
tsc::ScriptElementKind::FunctionElement => {
|
||||
if workspace_settings.code_lens.references_all_functions {
|
||||
code_lenses.push(i.to_code_lens(line_index, specifier, &source));
|
||||
code_lenses.push(i.to_code_lens(
|
||||
line_index.clone(),
|
||||
specifier,
|
||||
&source,
|
||||
));
|
||||
}
|
||||
}
|
||||
tsc::ScriptElementKind::ConstElement
|
||||
| tsc::ScriptElementKind::LetElement
|
||||
| tsc::ScriptElementKind::VariableElement => {
|
||||
if EXPORT_MODIFIER.is_match(&i.kind_modifiers) {
|
||||
code_lenses.push(i.to_code_lens(line_index, specifier, &source));
|
||||
code_lenses.push(i.to_code_lens(
|
||||
line_index.clone(),
|
||||
specifier,
|
||||
&source,
|
||||
));
|
||||
}
|
||||
}
|
||||
tsc::ScriptElementKind::ClassElement => {
|
||||
if i.text != "<class>" {
|
||||
code_lenses.push(i.to_code_lens(line_index, specifier, &source));
|
||||
code_lenses.push(i.to_code_lens(
|
||||
line_index.clone(),
|
||||
specifier,
|
||||
&source,
|
||||
));
|
||||
}
|
||||
}
|
||||
tsc::ScriptElementKind::InterfaceElement
|
||||
| tsc::ScriptElementKind::TypeElement
|
||||
| tsc::ScriptElementKind::EnumElement => {
|
||||
code_lenses.push(i.to_code_lens(line_index, specifier, &source));
|
||||
code_lenses.push(i.to_code_lens(
|
||||
line_index.clone(),
|
||||
specifier,
|
||||
&source,
|
||||
));
|
||||
}
|
||||
tsc::ScriptElementKind::LocalFunctionElement
|
||||
| tsc::ScriptElementKind::MemberGetAccessorElement
|
||||
|
@ -485,8 +511,11 @@ async fn collect_tsc(
|
|||
tsc::ScriptElementKind::ClassElement
|
||||
| tsc::ScriptElementKind::InterfaceElement
|
||||
| tsc::ScriptElementKind::TypeElement => {
|
||||
code_lenses
|
||||
.push(i.to_code_lens(line_index, specifier, &source));
|
||||
code_lenses.push(i.to_code_lens(
|
||||
line_index.clone(),
|
||||
specifier,
|
||||
&source,
|
||||
));
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
@ -510,21 +539,28 @@ mod tests {
|
|||
#[test]
|
||||
fn test_deno_test_collector() {
|
||||
let specifier = resolve_url("https://deno.land/x/mod.ts").unwrap();
|
||||
let source = r#"
|
||||
let source = Arc::new(
|
||||
r#"
|
||||
Deno.test({
|
||||
name: "test a",
|
||||
fn() {}
|
||||
});
|
||||
|
||||
Deno.test("test b", function anotherTest() {});
|
||||
"#;
|
||||
let parsed_module = crate::lsp::analysis::parse_module(
|
||||
&specifier,
|
||||
SourceTextInfo::from_string(source.to_string()),
|
||||
MediaType::TypeScript,
|
||||
)
|
||||
"#
|
||||
.to_string(),
|
||||
);
|
||||
let parsed_module = deno_ast::parse_module(deno_ast::ParseParams {
|
||||
specifier: specifier.to_string(),
|
||||
source: SourceTextInfo::new(source),
|
||||
media_type: MediaType::TypeScript,
|
||||
capture_tokens: true,
|
||||
scope_analysis: true,
|
||||
maybe_syntax: None,
|
||||
})
|
||||
.unwrap();
|
||||
let mut collector = DenoTestCollector::new(specifier, &parsed_module);
|
||||
let mut collector =
|
||||
DenoTestCollector::new(specifier, parsed_module.clone());
|
||||
parsed_module.module().visit_with(
|
||||
&ast::Invalid {
|
||||
span: deno_ast::swc::common::DUMMY_SP,
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use super::analysis;
|
||||
use super::language_server;
|
||||
use super::lsp_custom;
|
||||
use super::tsc;
|
||||
|
@ -85,21 +84,34 @@ async fn check_auto_config_registry(
|
|||
}
|
||||
}
|
||||
|
||||
/// Ranges from the graph for specifiers include the leading and trailing quote,
|
||||
/// which we want to ignore when replacing text.
|
||||
fn to_narrow_lsp_range(range: &deno_graph::Range) -> lsp::Range {
|
||||
lsp::Range {
|
||||
start: lsp::Position {
|
||||
line: range.start.line as u32,
|
||||
character: (range.start.character + 1) as u32,
|
||||
},
|
||||
end: lsp::Position {
|
||||
line: range.end.line as u32,
|
||||
character: (range.end.character - 1) as u32,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Given a specifier, a position, and a snapshot, optionally return a
|
||||
/// completion response, which will be valid import completions for the specific
|
||||
/// context.
|
||||
pub async fn get_import_completions(
|
||||
pub(crate) async fn get_import_completions(
|
||||
specifier: &ModuleSpecifier,
|
||||
position: &lsp::Position,
|
||||
state_snapshot: &language_server::StateSnapshot,
|
||||
client: lspower::Client,
|
||||
) -> Option<lsp::CompletionResponse> {
|
||||
let analysis::DependencyRange {
|
||||
range,
|
||||
specifier: text,
|
||||
} = state_snapshot
|
||||
let (text, _, range) = state_snapshot
|
||||
.documents
|
||||
.is_specifier_position(specifier, position)?;
|
||||
.get_maybe_dependency(specifier, position)?;
|
||||
let range = to_narrow_lsp_range(&range);
|
||||
// completions for local relative modules
|
||||
if text.starts_with("./") || text.starts_with("../") {
|
||||
Some(lsp::CompletionResponse::List(lsp::CompletionList {
|
||||
|
@ -258,7 +270,7 @@ fn get_workspace_completions(
|
|||
range: &lsp::Range,
|
||||
state_snapshot: &language_server::StateSnapshot,
|
||||
) -> Vec<lsp::CompletionItem> {
|
||||
let workspace_specifiers = state_snapshot.sources.specifiers();
|
||||
let workspace_specifiers = state_snapshot.documents.specifiers(false, true);
|
||||
let specifier_strings =
|
||||
get_relative_specifiers(specifier, workspace_specifiers);
|
||||
specifier_strings
|
||||
|
@ -399,11 +411,8 @@ fn relative_specifier(
|
|||
mod tests {
|
||||
use super::*;
|
||||
use crate::http_cache::HttpCache;
|
||||
use crate::lsp::analysis;
|
||||
use crate::lsp::documents::DocumentCache;
|
||||
use crate::lsp::documents::Documents;
|
||||
use crate::lsp::documents::LanguageId;
|
||||
use crate::lsp::sources::Sources;
|
||||
use deno_ast::MediaType;
|
||||
use deno_core::resolve_url;
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
|
@ -415,37 +424,17 @@ mod tests {
|
|||
source_fixtures: &[(&str, &str)],
|
||||
location: &Path,
|
||||
) -> language_server::StateSnapshot {
|
||||
let mut documents = DocumentCache::default();
|
||||
let documents = Documents::new(location);
|
||||
for (specifier, source, version, language_id) in fixtures {
|
||||
let specifier =
|
||||
resolve_url(specifier).expect("failed to create specifier");
|
||||
documents.open(
|
||||
specifier.clone(),
|
||||
*version,
|
||||
*language_id,
|
||||
language_id.clone(),
|
||||
Arc::new(source.to_string()),
|
||||
);
|
||||
let media_type = MediaType::from(&specifier);
|
||||
let parsed_module = documents
|
||||
.get(&specifier)
|
||||
.unwrap()
|
||||
.source()
|
||||
.module()
|
||||
.map(|r| r.as_ref())
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let (deps, _) = analysis::analyze_dependencies(
|
||||
&specifier,
|
||||
media_type,
|
||||
parsed_module,
|
||||
&None,
|
||||
);
|
||||
let dep_ranges = analysis::analyze_dependency_ranges(parsed_module).ok();
|
||||
documents
|
||||
.set_dependencies(&specifier, Some(deps), dep_ranges)
|
||||
.unwrap();
|
||||
}
|
||||
let sources = Sources::new(location);
|
||||
let http_cache = HttpCache::new(location);
|
||||
for (specifier, source) in source_fixtures {
|
||||
let specifier =
|
||||
|
@ -454,13 +443,12 @@ mod tests {
|
|||
.set(&specifier, HashMap::default(), source.as_bytes())
|
||||
.expect("could not cache file");
|
||||
assert!(
|
||||
sources.get_source(&specifier).is_some(),
|
||||
documents.content(&specifier).is_some(),
|
||||
"source could not be setup"
|
||||
);
|
||||
}
|
||||
language_server::StateSnapshot {
|
||||
documents,
|
||||
sources,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,14 @@
|
|||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use super::analysis;
|
||||
use super::documents::DocumentCache;
|
||||
use super::documents;
|
||||
use super::documents::Documents;
|
||||
use super::language_server;
|
||||
use super::sources::Sources;
|
||||
use super::tsc;
|
||||
|
||||
use crate::diagnostics;
|
||||
use crate::tokio_util::create_basic_runtime;
|
||||
|
||||
use analysis::ResolvedDependency;
|
||||
use deno_core::error::anyhow;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::resolve_url;
|
||||
|
@ -90,21 +89,13 @@ impl DiagnosticCollection {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Default)]
|
||||
pub(crate) struct DiagnosticsServer {
|
||||
channel: Option<mpsc::UnboundedSender<()>>,
|
||||
collection: Arc<Mutex<DiagnosticCollection>>,
|
||||
}
|
||||
|
||||
impl DiagnosticsServer {
|
||||
pub(crate) fn new() -> Self {
|
||||
let collection = Arc::new(Mutex::new(DiagnosticCollection::default()));
|
||||
Self {
|
||||
channel: None,
|
||||
collection,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn get(
|
||||
&self,
|
||||
specifier: &ModuleSpecifier,
|
||||
|
@ -318,24 +309,17 @@ async fn generate_lint_diagnostics(
|
|||
tokio::task::spawn(async move {
|
||||
let mut diagnostics_vec = Vec::new();
|
||||
if workspace_settings.lint {
|
||||
for specifier in documents.open_specifiers() {
|
||||
if !documents.is_diagnosable(specifier) {
|
||||
continue;
|
||||
}
|
||||
let version = documents.version(specifier);
|
||||
for specifier in documents.specifiers(true, true) {
|
||||
let version = documents.lsp_version(&specifier);
|
||||
let current_version = collection
|
||||
.lock()
|
||||
.await
|
||||
.get_version(specifier, &DiagnosticSource::DenoLint);
|
||||
.get_version(&specifier, &DiagnosticSource::DenoLint);
|
||||
if version != current_version {
|
||||
let module = documents
|
||||
.get(specifier)
|
||||
.map(|d| d.source().module())
|
||||
.flatten();
|
||||
let diagnostics = match module {
|
||||
Some(Ok(module)) => {
|
||||
let diagnostics = match documents.parsed_source(&specifier) {
|
||||
Some(Ok(parsed_source)) => {
|
||||
if let Ok(references) = analysis::get_lint_references(
|
||||
module,
|
||||
&parsed_source,
|
||||
maybe_lint_config.as_ref(),
|
||||
) {
|
||||
references
|
||||
|
@ -372,11 +356,10 @@ async fn generate_ts_diagnostics(
|
|||
let collection = collection.lock().await;
|
||||
snapshot
|
||||
.documents
|
||||
.open_specifiers()
|
||||
.specifiers(true, true)
|
||||
.iter()
|
||||
.filter_map(|&s| {
|
||||
if snapshot.documents.is_diagnosable(s) {
|
||||
let version = snapshot.documents.version(s);
|
||||
.filter_map(|s| {
|
||||
let version = snapshot.documents.lsp_version(s);
|
||||
let current_version =
|
||||
collection.get_version(s, &DiagnosticSource::TypeScript);
|
||||
if version != current_version {
|
||||
|
@ -384,9 +367,6 @@ async fn generate_ts_diagnostics(
|
|||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
|
@ -396,7 +376,7 @@ async fn generate_ts_diagnostics(
|
|||
ts_server.request(snapshot.clone(), req).await?;
|
||||
for (specifier_str, ts_diagnostics) in ts_diagnostics_map {
|
||||
let specifier = resolve_url(&specifier_str)?;
|
||||
let version = snapshot.documents.version(&specifier);
|
||||
let version = snapshot.documents.lsp_version(&specifier);
|
||||
diagnostics_vec.push((
|
||||
specifier,
|
||||
version,
|
||||
|
@ -407,32 +387,41 @@ async fn generate_ts_diagnostics(
|
|||
Ok(diagnostics_vec)
|
||||
}
|
||||
|
||||
fn resolution_error_as_code(
|
||||
err: &deno_graph::ResolutionError,
|
||||
) -> lsp::NumberOrString {
|
||||
use deno_graph::ResolutionError;
|
||||
use deno_graph::SpecifierError;
|
||||
|
||||
match err {
|
||||
ResolutionError::InvalidDowngrade(_, _) => {
|
||||
lsp::NumberOrString::String("invalid-downgrade".to_string())
|
||||
}
|
||||
ResolutionError::InvalidLocalImport(_, _) => {
|
||||
lsp::NumberOrString::String("invalid-local-import".to_string())
|
||||
}
|
||||
ResolutionError::InvalidSpecifier(err, _) => match err {
|
||||
SpecifierError::ImportPrefixMissing(_, _) => {
|
||||
lsp::NumberOrString::String("import-prefix-missing".to_string())
|
||||
}
|
||||
SpecifierError::InvalidUrl(_) => {
|
||||
lsp::NumberOrString::String("invalid-url".to_string())
|
||||
}
|
||||
},
|
||||
ResolutionError::ResolverError(_, _, _) => {
|
||||
lsp::NumberOrString::String("resolver-error".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn diagnose_dependency(
|
||||
diagnostics: &mut Vec<lsp::Diagnostic>,
|
||||
documents: &DocumentCache,
|
||||
sources: &Sources,
|
||||
maybe_dependency: &Option<ResolvedDependency>,
|
||||
maybe_range: &Option<lsp::Range>,
|
||||
documents: &Documents,
|
||||
resolved: &deno_graph::Resolved,
|
||||
) {
|
||||
if let (Some(dep), Some(range)) = (maybe_dependency, *maybe_range) {
|
||||
match dep {
|
||||
analysis::ResolvedDependency::Err(err) => {
|
||||
diagnostics.push(lsp::Diagnostic {
|
||||
range,
|
||||
severity: Some(lsp::DiagnosticSeverity::Error),
|
||||
code: Some(err.as_code()),
|
||||
code_description: None,
|
||||
source: Some("deno".to_string()),
|
||||
message: err.to_string(),
|
||||
related_information: None,
|
||||
tags: None,
|
||||
data: None,
|
||||
})
|
||||
}
|
||||
analysis::ResolvedDependency::Resolved(specifier) => {
|
||||
if !(documents.contains_key(specifier)
|
||||
|| sources.contains_key(specifier))
|
||||
{
|
||||
match resolved {
|
||||
Some(Ok((specifier, range))) => {
|
||||
if !documents.contains_specifier(specifier) {
|
||||
let (code, message) = match specifier.scheme() {
|
||||
"file" => (Some(lsp::NumberOrString::String("no-local".to_string())), format!("Unable to load a local module: \"{}\".\n Please check the file path.", specifier)),
|
||||
"data" => (Some(lsp::NumberOrString::String("no-cache-data".to_string())), "Uncached data URL.".to_string()),
|
||||
|
@ -440,7 +429,7 @@ fn diagnose_dependency(
|
|||
_ => (Some(lsp::NumberOrString::String("no-cache".to_string())), format!("Uncached or missing remote URL: \"{}\".", specifier)),
|
||||
};
|
||||
diagnostics.push(lsp::Diagnostic {
|
||||
range,
|
||||
range: documents::to_lsp_range(range),
|
||||
severity: Some(lsp::DiagnosticSeverity::Error),
|
||||
code,
|
||||
source: Some("deno".to_string()),
|
||||
|
@ -448,10 +437,9 @@ fn diagnose_dependency(
|
|||
data: Some(json!({ "specifier": specifier })),
|
||||
..Default::default()
|
||||
});
|
||||
} else if sources.contains_key(specifier) {
|
||||
if let Some(message) = sources.get_maybe_warning(specifier) {
|
||||
} else if let Some(message) = documents.maybe_warning(specifier) {
|
||||
diagnostics.push(lsp::Diagnostic {
|
||||
range,
|
||||
range: documents::to_lsp_range(range),
|
||||
severity: Some(lsp::DiagnosticSeverity::Warning),
|
||||
code: Some(lsp::NumberOrString::String("deno-warn".to_string())),
|
||||
source: Some("deno".to_string()),
|
||||
|
@ -460,8 +448,15 @@ fn diagnose_dependency(
|
|||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(Err(err)) => diagnostics.push(lsp::Diagnostic {
|
||||
range: documents::to_lsp_range(err.range()),
|
||||
severity: Some(lsp::DiagnosticSeverity::Error),
|
||||
code: Some(resolution_error_as_code(err)),
|
||||
source: Some("deno".to_string()),
|
||||
message: err.to_string(),
|
||||
..Default::default()
|
||||
}),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -473,36 +468,31 @@ async fn generate_deps_diagnostics(
|
|||
) -> Result<DiagnosticVec, AnyError> {
|
||||
let config = snapshot.config.clone();
|
||||
let documents = snapshot.documents.clone();
|
||||
let sources = snapshot.sources.clone();
|
||||
tokio::task::spawn(async move {
|
||||
let mut diagnostics_vec = Vec::new();
|
||||
|
||||
for specifier in documents.open_specifiers() {
|
||||
if !config.specifier_enabled(specifier) {
|
||||
for specifier in documents.specifiers(true, true) {
|
||||
if !config.specifier_enabled(&specifier) {
|
||||
continue;
|
||||
}
|
||||
let version = documents.version(specifier);
|
||||
let version = documents.lsp_version(&specifier);
|
||||
let current_version = collection
|
||||
.lock()
|
||||
.await
|
||||
.get_version(specifier, &DiagnosticSource::Deno);
|
||||
.get_version(&specifier, &DiagnosticSource::Deno);
|
||||
if version != current_version {
|
||||
let mut diagnostics = Vec::new();
|
||||
if let Some(dependencies) = documents.dependencies(specifier) {
|
||||
if let Some(dependencies) = documents.dependencies(&specifier) {
|
||||
for (_, dependency) in dependencies {
|
||||
diagnose_dependency(
|
||||
&mut diagnostics,
|
||||
&documents,
|
||||
&sources,
|
||||
&dependency.maybe_code,
|
||||
&dependency.maybe_code_specifier_range,
|
||||
);
|
||||
diagnose_dependency(
|
||||
&mut diagnostics,
|
||||
&documents,
|
||||
&sources,
|
||||
&dependency.maybe_type,
|
||||
&dependency.maybe_type_specifier_range,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -544,7 +534,7 @@ async fn publish_diagnostics(
|
|||
.extend(collection.get(&specifier, DiagnosticSource::Deno).cloned());
|
||||
}
|
||||
let uri = specifier.clone();
|
||||
let version = snapshot.documents.version(&specifier);
|
||||
let version = snapshot.documents.lsp_version(&specifier);
|
||||
client.publish_diagnostics(uri, diagnostics, version).await;
|
||||
}
|
||||
}
|
||||
|
@ -627,3 +617,79 @@ async fn update_diagnostics(
|
|||
tokio::join!(lint, ts, deps);
|
||||
snapshot.performance.measure(mark);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::lsp::config::ConfigSnapshot;
|
||||
use crate::lsp::config::Settings;
|
||||
use crate::lsp::config::WorkspaceSettings;
|
||||
use crate::lsp::documents::LanguageId;
|
||||
use crate::lsp::language_server::StateSnapshot;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use tempfile::TempDir;
|
||||
|
||||
fn mock_state_snapshot(
|
||||
fixtures: &[(&str, &str, i32, LanguageId)],
|
||||
location: &Path,
|
||||
) -> StateSnapshot {
|
||||
let documents = Documents::new(location);
|
||||
for (specifier, source, version, language_id) in fixtures {
|
||||
let specifier =
|
||||
resolve_url(specifier).expect("failed to create specifier");
|
||||
documents.open(
|
||||
specifier.clone(),
|
||||
*version,
|
||||
language_id.clone(),
|
||||
Arc::new(source.to_string()),
|
||||
);
|
||||
}
|
||||
let config = ConfigSnapshot {
|
||||
settings: Settings {
|
||||
workspace: WorkspaceSettings {
|
||||
enable: true,
|
||||
lint: true,
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
};
|
||||
StateSnapshot {
|
||||
config,
|
||||
documents,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn setup(
|
||||
sources: &[(&str, &str, i32, LanguageId)],
|
||||
) -> (StateSnapshot, Arc<Mutex<DiagnosticCollection>>, PathBuf) {
|
||||
let temp_dir = TempDir::new().expect("could not create temp dir");
|
||||
let location = temp_dir.path().join("deps");
|
||||
let state_snapshot = mock_state_snapshot(sources, &location);
|
||||
let collection = Arc::new(Mutex::new(DiagnosticCollection::default()));
|
||||
(state_snapshot, collection, location)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_generate_lint_diagnostics() {
|
||||
let (snapshot, collection, _) = setup(&[(
|
||||
"file:///a.ts",
|
||||
r#"import * as b from "./b.ts";
|
||||
|
||||
let a = "a";
|
||||
console.log(a);
|
||||
"#,
|
||||
1,
|
||||
LanguageId::TypeScript,
|
||||
)]);
|
||||
let result = generate_lint_diagnostics(&snapshot, collection).await;
|
||||
assert!(result.is_ok());
|
||||
let diagnostics = result.unwrap();
|
||||
assert_eq!(diagnostics.len(), 1);
|
||||
let (_, _, diagnostics) = &diagnostics[0];
|
||||
assert_eq!(diagnostics.len(), 2);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,75 +0,0 @@
|
|||
use deno_ast::Diagnostic;
|
||||
use deno_ast::MediaType;
|
||||
use deno_ast::ParsedSource;
|
||||
use deno_ast::SourceTextInfo;
|
||||
use deno_core::ModuleSpecifier;
|
||||
use once_cell::sync::OnceCell;
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::analysis;
|
||||
use super::text::LineIndex;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct DocumentSourceInner {
|
||||
specifier: ModuleSpecifier,
|
||||
media_type: MediaType,
|
||||
text_info: SourceTextInfo,
|
||||
parsed_module: OnceCell<Result<ParsedSource, Diagnostic>>,
|
||||
line_index: LineIndex,
|
||||
}
|
||||
|
||||
/// Immutable information about a document.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DocumentSource {
|
||||
inner: Arc<DocumentSourceInner>,
|
||||
}
|
||||
|
||||
impl DocumentSource {
|
||||
pub fn new(
|
||||
specifier: &ModuleSpecifier,
|
||||
media_type: MediaType,
|
||||
text: Arc<String>,
|
||||
line_index: LineIndex,
|
||||
) -> Self {
|
||||
Self {
|
||||
inner: Arc::new(DocumentSourceInner {
|
||||
specifier: specifier.clone(),
|
||||
media_type,
|
||||
text_info: SourceTextInfo::new(text),
|
||||
parsed_module: OnceCell::new(),
|
||||
line_index,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn text_info(&self) -> &SourceTextInfo {
|
||||
&self.inner.text_info
|
||||
}
|
||||
|
||||
pub fn line_index(&self) -> &LineIndex {
|
||||
&self.inner.line_index
|
||||
}
|
||||
|
||||
pub fn module(&self) -> Option<&Result<ParsedSource, Diagnostic>> {
|
||||
let is_parsable = matches!(
|
||||
self.inner.media_type,
|
||||
MediaType::JavaScript
|
||||
| MediaType::Jsx
|
||||
| MediaType::TypeScript
|
||||
| MediaType::Tsx
|
||||
| MediaType::Dts,
|
||||
);
|
||||
if is_parsable {
|
||||
// lazily parse the module
|
||||
Some(self.inner.parsed_module.get_or_init(|| {
|
||||
analysis::parse_module(
|
||||
&self.inner.specifier,
|
||||
self.inner.text_info.clone(),
|
||||
self.inner.media_type,
|
||||
)
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
1357
cli/lsp/documents.rs
1357
cli/lsp/documents.rs
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
@ -11,7 +11,6 @@ mod code_lens;
|
|||
mod completions;
|
||||
mod config;
|
||||
mod diagnostics;
|
||||
mod document_source;
|
||||
mod documents;
|
||||
pub(crate) mod language_server;
|
||||
mod lsp_custom;
|
||||
|
@ -20,8 +19,8 @@ mod path_to_regex;
|
|||
mod performance;
|
||||
mod refactor;
|
||||
mod registries;
|
||||
mod resolver;
|
||||
mod semantic_tokens;
|
||||
mod sources;
|
||||
mod text;
|
||||
mod tsc;
|
||||
mod urls;
|
||||
|
|
|
@ -424,7 +424,7 @@ impl ModuleRegistry {
|
|||
|
||||
/// For a string specifier from the client, provide a set of completions, if
|
||||
/// any, for the specifier.
|
||||
pub async fn get_completions(
|
||||
pub(crate) async fn get_completions(
|
||||
&self,
|
||||
current_specifier: &str,
|
||||
offset: usize,
|
||||
|
@ -520,8 +520,8 @@ impl ModuleRegistry {
|
|||
));
|
||||
let command = if key.name == last_key_name
|
||||
&& !state_snapshot
|
||||
.sources
|
||||
.contains_key(&item_specifier)
|
||||
.documents
|
||||
.contains_specifier(&item_specifier)
|
||||
{
|
||||
Some(lsp::Command {
|
||||
title: "".to_string(),
|
||||
|
@ -610,8 +610,8 @@ impl ModuleRegistry {
|
|||
);
|
||||
let command = if k.name == last_key_name
|
||||
&& !state_snapshot
|
||||
.sources
|
||||
.contains_key(&item_specifier)
|
||||
.documents
|
||||
.contains_specifier(&item_specifier)
|
||||
{
|
||||
Some(lsp::Command {
|
||||
title: "".to_string(),
|
||||
|
@ -761,16 +761,14 @@ impl ModuleRegistry {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::lsp::documents::DocumentCache;
|
||||
use crate::lsp::sources::Sources;
|
||||
use crate::lsp::documents::Documents;
|
||||
use tempfile::TempDir;
|
||||
|
||||
fn mock_state_snapshot(
|
||||
source_fixtures: &[(&str, &str)],
|
||||
location: &Path,
|
||||
) -> language_server::StateSnapshot {
|
||||
let documents = DocumentCache::default();
|
||||
let sources = Sources::new(location);
|
||||
let documents = Documents::new(location);
|
||||
let http_cache = HttpCache::new(location);
|
||||
for (specifier, source) in source_fixtures {
|
||||
let specifier =
|
||||
|
@ -779,13 +777,12 @@ mod tests {
|
|||
.set(&specifier, HashMap::default(), source.as_bytes())
|
||||
.expect("could not cache file");
|
||||
assert!(
|
||||
sources.get_source(&specifier).is_some(),
|
||||
documents.content(&specifier).is_some(),
|
||||
"source could not be setup"
|
||||
);
|
||||
}
|
||||
language_server::StateSnapshot {
|
||||
documents,
|
||||
sources,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
|
33
cli/lsp/resolver.rs
Normal file
33
cli/lsp/resolver.rs
Normal file
|
@ -0,0 +1,33 @@
|
|||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::ModuleSpecifier;
|
||||
use deno_graph::source::Resolver;
|
||||
use import_map::ImportMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct ImportMapResolver(Arc<ImportMap>);
|
||||
|
||||
impl ImportMapResolver {
|
||||
pub fn new(import_map: Arc<ImportMap>) -> Self {
|
||||
Self(import_map)
|
||||
}
|
||||
|
||||
pub fn as_resolver(&self) -> &dyn Resolver {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Resolver for ImportMapResolver {
|
||||
fn resolve(
|
||||
&self,
|
||||
specifier: &str,
|
||||
referrer: &ModuleSpecifier,
|
||||
) -> Result<ModuleSpecifier, AnyError> {
|
||||
self
|
||||
.0
|
||||
.resolve(specifier, referrer.as_str())
|
||||
.map_err(|err| err.into())
|
||||
}
|
||||
}
|
|
@ -1,797 +0,0 @@
|
|||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use super::analysis;
|
||||
use super::document_source::DocumentSource;
|
||||
use super::text::LineIndex;
|
||||
use super::tsc;
|
||||
use super::urls::INVALID_SPECIFIER;
|
||||
|
||||
use crate::file_fetcher::get_source_from_bytes;
|
||||
use crate::file_fetcher::map_content_type;
|
||||
use crate::file_fetcher::SUPPORTED_SCHEMES;
|
||||
use crate::http_cache;
|
||||
use crate::http_cache::HttpCache;
|
||||
use crate::text_encoding;
|
||||
|
||||
use deno_ast::MediaType;
|
||||
use deno_core::error::anyhow;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::parking_lot::Mutex;
|
||||
use deno_core::serde_json;
|
||||
use deno_core::ModuleSpecifier;
|
||||
use import_map::ImportMap;
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::time::SystemTime;
|
||||
use tsc::NavigationTree;
|
||||
|
||||
fn get_remote_headers(
|
||||
cache_filename: &Path,
|
||||
) -> Option<HashMap<String, String>> {
|
||||
let metadata_path = http_cache::Metadata::filename(cache_filename);
|
||||
let metadata_str = fs::read_to_string(metadata_path).ok()?;
|
||||
let metadata: http_cache::Metadata =
|
||||
serde_json::from_str(&metadata_str).ok()?;
|
||||
Some(metadata.headers)
|
||||
}
|
||||
|
||||
fn resolve_remote_specifier(
|
||||
specifier: &ModuleSpecifier,
|
||||
http_cache: &HttpCache,
|
||||
redirect_limit: isize,
|
||||
) -> Option<ModuleSpecifier> {
|
||||
let cache_filename = http_cache.get_cache_filename(specifier)?;
|
||||
if redirect_limit >= 0 && cache_filename.is_file() {
|
||||
let headers = get_remote_headers(&cache_filename)?;
|
||||
if let Some(location) = headers.get("location") {
|
||||
let redirect =
|
||||
deno_core::resolve_import(location, specifier.as_str()).ok()?;
|
||||
resolve_remote_specifier(&redirect, http_cache, redirect_limit - 1)
|
||||
} else {
|
||||
Some(specifier.clone())
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_specifier(
|
||||
specifier: &ModuleSpecifier,
|
||||
redirects: &mut HashMap<ModuleSpecifier, ModuleSpecifier>,
|
||||
http_cache: &HttpCache,
|
||||
) -> Option<ModuleSpecifier> {
|
||||
let scheme = specifier.scheme();
|
||||
if !SUPPORTED_SCHEMES.contains(&scheme) {
|
||||
return None;
|
||||
}
|
||||
|
||||
if scheme == "data" {
|
||||
Some(specifier.clone())
|
||||
} else if scheme == "file" {
|
||||
let path = specifier.to_file_path().ok()?;
|
||||
if path.is_file() {
|
||||
Some(specifier.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else if let Some(specifier) = redirects.get(specifier) {
|
||||
Some(specifier.clone())
|
||||
} else {
|
||||
let redirect = resolve_remote_specifier(specifier, http_cache, 10)?;
|
||||
redirects.insert(specifier.clone(), redirect.clone());
|
||||
Some(redirect)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Metadata {
|
||||
dependencies: Option<HashMap<String, analysis::Dependency>>,
|
||||
length_utf16: usize,
|
||||
maybe_navigation_tree: Option<tsc::NavigationTree>,
|
||||
maybe_types: Option<analysis::ResolvedDependency>,
|
||||
maybe_warning: Option<String>,
|
||||
media_type: MediaType,
|
||||
source: DocumentSource,
|
||||
specifier: ModuleSpecifier,
|
||||
version: String,
|
||||
}
|
||||
|
||||
impl Default for Metadata {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
dependencies: None,
|
||||
length_utf16: 0,
|
||||
maybe_navigation_tree: None,
|
||||
maybe_types: None,
|
||||
maybe_warning: None,
|
||||
media_type: MediaType::default(),
|
||||
source: DocumentSource::new(
|
||||
&INVALID_SPECIFIER,
|
||||
MediaType::default(),
|
||||
Arc::new(String::default()),
|
||||
LineIndex::default(),
|
||||
),
|
||||
specifier: INVALID_SPECIFIER.clone(),
|
||||
version: String::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Metadata {
|
||||
fn new(
|
||||
specifier: &ModuleSpecifier,
|
||||
source: Arc<String>,
|
||||
version: &str,
|
||||
media_type: MediaType,
|
||||
maybe_warning: Option<String>,
|
||||
maybe_import_map: &Option<ImportMap>,
|
||||
) -> Self {
|
||||
let line_index = LineIndex::new(&source);
|
||||
let document_source =
|
||||
DocumentSource::new(specifier, media_type, source, line_index);
|
||||
let (dependencies, maybe_types) =
|
||||
if let Some(Ok(parsed_module)) = document_source.module() {
|
||||
let (deps, maybe_types) = analysis::analyze_dependencies(
|
||||
specifier,
|
||||
media_type,
|
||||
parsed_module,
|
||||
maybe_import_map,
|
||||
);
|
||||
(Some(deps), maybe_types)
|
||||
} else {
|
||||
(None, None)
|
||||
};
|
||||
|
||||
Self {
|
||||
dependencies,
|
||||
length_utf16: document_source
|
||||
.text_info()
|
||||
.text_str()
|
||||
.encode_utf16()
|
||||
.count(),
|
||||
maybe_navigation_tree: None,
|
||||
maybe_types,
|
||||
maybe_warning,
|
||||
media_type: media_type.to_owned(),
|
||||
source: document_source,
|
||||
specifier: specifier.clone(),
|
||||
version: version.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn refresh(&mut self, maybe_import_map: &Option<ImportMap>) {
|
||||
let (dependencies, maybe_types) =
|
||||
if let Some(Ok(parsed_module)) = self.source.module() {
|
||||
let (deps, maybe_types) = analysis::analyze_dependencies(
|
||||
&self.specifier,
|
||||
self.media_type,
|
||||
parsed_module,
|
||||
maybe_import_map,
|
||||
);
|
||||
(Some(deps), maybe_types)
|
||||
} else {
|
||||
(None, None)
|
||||
};
|
||||
self.dependencies = dependencies;
|
||||
self.maybe_types = maybe_types;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
struct Inner {
|
||||
http_cache: HttpCache,
|
||||
maybe_import_map: Option<ImportMap>,
|
||||
metadata: HashMap<ModuleSpecifier, Metadata>,
|
||||
redirects: HashMap<ModuleSpecifier, ModuleSpecifier>,
|
||||
remotes: HashMap<ModuleSpecifier, PathBuf>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct Sources(Arc<Mutex<Inner>>);
|
||||
|
||||
impl Sources {
|
||||
pub fn new(location: &Path) -> Self {
|
||||
Self(Arc::new(Mutex::new(Inner::new(location))))
|
||||
}
|
||||
|
||||
pub fn contains_key(&self, specifier: &ModuleSpecifier) -> bool {
|
||||
self.0.lock().contains_key(specifier)
|
||||
}
|
||||
|
||||
pub fn get_line_index(
|
||||
&self,
|
||||
specifier: &ModuleSpecifier,
|
||||
) -> Option<LineIndex> {
|
||||
self.0.lock().get_line_index(specifier)
|
||||
}
|
||||
|
||||
pub fn get_maybe_types(
|
||||
&self,
|
||||
specifier: &ModuleSpecifier,
|
||||
) -> Option<analysis::ResolvedDependency> {
|
||||
self.0.lock().get_maybe_types(specifier)
|
||||
}
|
||||
|
||||
pub fn get_maybe_warning(
|
||||
&self,
|
||||
specifier: &ModuleSpecifier,
|
||||
) -> Option<String> {
|
||||
self.0.lock().get_maybe_warning(specifier)
|
||||
}
|
||||
|
||||
pub fn get_media_type(
|
||||
&self,
|
||||
specifier: &ModuleSpecifier,
|
||||
) -> Option<MediaType> {
|
||||
self.0.lock().get_media_type(specifier)
|
||||
}
|
||||
|
||||
pub fn get_navigation_tree(
|
||||
&self,
|
||||
specifier: &ModuleSpecifier,
|
||||
) -> Option<tsc::NavigationTree> {
|
||||
self.0.lock().get_navigation_tree(specifier)
|
||||
}
|
||||
|
||||
pub fn get_script_version(
|
||||
&self,
|
||||
specifier: &ModuleSpecifier,
|
||||
) -> Option<String> {
|
||||
self.0.lock().get_script_version(specifier)
|
||||
}
|
||||
|
||||
pub fn get_source(&self, specifier: &ModuleSpecifier) -> Option<Arc<String>> {
|
||||
self.0.lock().get_source(specifier)
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.0.lock().metadata.len()
|
||||
}
|
||||
|
||||
pub fn resolve_import(
|
||||
&self,
|
||||
specifier: &str,
|
||||
referrer: &ModuleSpecifier,
|
||||
) -> Option<(ModuleSpecifier, MediaType)> {
|
||||
self.0.lock().resolve_import(specifier, referrer)
|
||||
}
|
||||
|
||||
pub fn specifiers(&self) -> Vec<ModuleSpecifier> {
|
||||
self.0.lock().metadata.keys().cloned().collect()
|
||||
}
|
||||
|
||||
pub fn set_import_map(&self, maybe_import_map: Option<ImportMap>) {
|
||||
self.0.lock().set_import_map(maybe_import_map)
|
||||
}
|
||||
|
||||
pub fn set_navigation_tree(
|
||||
&self,
|
||||
specifier: &ModuleSpecifier,
|
||||
navigation_tree: tsc::NavigationTree,
|
||||
) -> Result<(), AnyError> {
|
||||
self
|
||||
.0
|
||||
.lock()
|
||||
.set_navigation_tree(specifier, navigation_tree)
|
||||
}
|
||||
}
|
||||
|
||||
impl Inner {
|
||||
fn new(location: &Path) -> Self {
|
||||
Self {
|
||||
http_cache: HttpCache::new(location),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn calculate_script_version(
|
||||
&mut self,
|
||||
specifier: &ModuleSpecifier,
|
||||
) -> Option<String> {
|
||||
let path = self.get_path(specifier)?;
|
||||
let metadata = fs::metadata(path).ok()?;
|
||||
if let Ok(modified) = metadata.modified() {
|
||||
if let Ok(n) = modified.duration_since(SystemTime::UNIX_EPOCH) {
|
||||
Some(format!("{}", n.as_millis()))
|
||||
} else {
|
||||
Some("1".to_string())
|
||||
}
|
||||
} else {
|
||||
Some("1".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn contains_key(&mut self, specifier: &ModuleSpecifier) -> bool {
|
||||
if let Some(specifier) =
|
||||
resolve_specifier(specifier, &mut self.redirects, &self.http_cache)
|
||||
{
|
||||
if self.get_metadata(&specifier).is_some() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn get_line_index(
|
||||
&mut self,
|
||||
specifier: &ModuleSpecifier,
|
||||
) -> Option<LineIndex> {
|
||||
let specifier =
|
||||
resolve_specifier(specifier, &mut self.redirects, &self.http_cache)?;
|
||||
let metadata = self.get_metadata(&specifier)?;
|
||||
Some(metadata.source.line_index().clone())
|
||||
}
|
||||
|
||||
fn get_maybe_types(
|
||||
&mut self,
|
||||
specifier: &ModuleSpecifier,
|
||||
) -> Option<analysis::ResolvedDependency> {
|
||||
let specifier =
|
||||
resolve_specifier(specifier, &mut self.redirects, &self.http_cache)?;
|
||||
let metadata = self.get_metadata(&specifier)?;
|
||||
metadata.maybe_types
|
||||
}
|
||||
|
||||
fn get_maybe_warning(
|
||||
&mut self,
|
||||
specifier: &ModuleSpecifier,
|
||||
) -> Option<String> {
|
||||
let metadata = self.get_metadata(specifier)?;
|
||||
metadata.maybe_warning
|
||||
}
|
||||
|
||||
fn get_media_type(
|
||||
&mut self,
|
||||
specifier: &ModuleSpecifier,
|
||||
) -> Option<MediaType> {
|
||||
let specifier =
|
||||
resolve_specifier(specifier, &mut self.redirects, &self.http_cache)?;
|
||||
let metadata = self.get_metadata(&specifier)?;
|
||||
Some(metadata.media_type)
|
||||
}
|
||||
|
||||
fn get_metadata(&mut self, specifier: &ModuleSpecifier) -> Option<Metadata> {
|
||||
if let Some(metadata) = self.metadata.get(specifier).cloned() {
|
||||
if metadata.version == self.calculate_script_version(specifier)? {
|
||||
return Some(metadata);
|
||||
}
|
||||
}
|
||||
|
||||
let version = self.calculate_script_version(specifier)?;
|
||||
let path = self.get_path(specifier)?;
|
||||
let bytes = fs::read(path).ok()?;
|
||||
let scheme = specifier.scheme();
|
||||
let (source, media_type, maybe_types, maybe_warning) = if scheme == "file" {
|
||||
let maybe_charset =
|
||||
Some(text_encoding::detect_charset(&bytes).to_string());
|
||||
let source = get_source_from_bytes(bytes, maybe_charset).ok()?;
|
||||
(source, MediaType::from(specifier), None, None)
|
||||
} else {
|
||||
let cache_filename = self.http_cache.get_cache_filename(specifier)?;
|
||||
let headers = get_remote_headers(&cache_filename)?;
|
||||
let maybe_content_type = headers.get("content-type").cloned();
|
||||
let (media_type, maybe_charset) =
|
||||
map_content_type(specifier, maybe_content_type);
|
||||
let source = get_source_from_bytes(bytes, maybe_charset).ok()?;
|
||||
let maybe_types = headers.get("x-typescript-types").map(|s| {
|
||||
analysis::resolve_import(s, specifier, &self.maybe_import_map)
|
||||
});
|
||||
let maybe_warning = headers.get("x-deno-warning").cloned();
|
||||
(source, media_type, maybe_types, maybe_warning)
|
||||
};
|
||||
let mut metadata = Metadata::new(
|
||||
specifier,
|
||||
Arc::new(source),
|
||||
&version,
|
||||
media_type,
|
||||
maybe_warning,
|
||||
&self.maybe_import_map,
|
||||
);
|
||||
if maybe_types.is_some() {
|
||||
metadata.maybe_types = maybe_types;
|
||||
}
|
||||
self.metadata.insert(specifier.clone(), metadata.clone());
|
||||
Some(metadata)
|
||||
}
|
||||
|
||||
fn get_navigation_tree(
|
||||
&mut self,
|
||||
specifier: &ModuleSpecifier,
|
||||
) -> Option<tsc::NavigationTree> {
|
||||
let specifier =
|
||||
resolve_specifier(specifier, &mut self.redirects, &self.http_cache)?;
|
||||
let metadata = self.get_metadata(&specifier)?;
|
||||
metadata.maybe_navigation_tree
|
||||
}
|
||||
|
||||
fn get_path(&mut self, specifier: &ModuleSpecifier) -> Option<PathBuf> {
|
||||
if specifier.scheme() == "file" {
|
||||
specifier.to_file_path().ok()
|
||||
} else if let Some(path) = self.remotes.get(specifier) {
|
||||
Some(path.clone())
|
||||
} else {
|
||||
let path = self.http_cache.get_cache_filename(specifier)?;
|
||||
if path.is_file() {
|
||||
self.remotes.insert(specifier.clone(), path.clone());
|
||||
Some(path)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_script_version(
|
||||
&mut self,
|
||||
specifier: &ModuleSpecifier,
|
||||
) -> Option<String> {
|
||||
let specifier =
|
||||
resolve_specifier(specifier, &mut self.redirects, &self.http_cache)?;
|
||||
let metadata = self.get_metadata(&specifier)?;
|
||||
Some(metadata.version)
|
||||
}
|
||||
|
||||
fn get_source(&mut self, specifier: &ModuleSpecifier) -> Option<Arc<String>> {
|
||||
let specifier =
|
||||
resolve_specifier(specifier, &mut self.redirects, &self.http_cache)?;
|
||||
let metadata = self.get_metadata(&specifier)?;
|
||||
Some(metadata.source.text_info().text())
|
||||
}
|
||||
|
||||
fn resolution_result(
|
||||
&mut self,
|
||||
specifier: &ModuleSpecifier,
|
||||
) -> Option<(ModuleSpecifier, MediaType)> {
|
||||
let specifier =
|
||||
resolve_specifier(specifier, &mut self.redirects, &self.http_cache)?;
|
||||
let media_type = if let Some(metadata) = self.metadata.get(&specifier) {
|
||||
metadata.media_type
|
||||
} else {
|
||||
MediaType::from(&specifier)
|
||||
};
|
||||
Some((specifier, media_type))
|
||||
}
|
||||
|
||||
fn resolve_import(
|
||||
&mut self,
|
||||
specifier: &str,
|
||||
referrer: &ModuleSpecifier,
|
||||
) -> Option<(ModuleSpecifier, MediaType)> {
|
||||
let referrer =
|
||||
resolve_specifier(referrer, &mut self.redirects, &self.http_cache)?;
|
||||
let metadata = self.get_metadata(&referrer)?;
|
||||
let dependencies = &metadata.dependencies?;
|
||||
let dependency = dependencies.get(specifier)?;
|
||||
if let Some(type_dependency) = &dependency.maybe_type {
|
||||
if let analysis::ResolvedDependency::Resolved(resolved_specifier) =
|
||||
type_dependency
|
||||
{
|
||||
// even if we have a module in the maybe_types slot, it doesn't mean
|
||||
// that it is the actual module we should be using based on headers,
|
||||
// so we check here and update properly.
|
||||
if let Some(type_dependency) = self.get_maybe_types(resolved_specifier)
|
||||
{
|
||||
self.set_maybe_type(specifier, &referrer, &type_dependency);
|
||||
if let analysis::ResolvedDependency::Resolved(type_specifier) =
|
||||
type_dependency
|
||||
{
|
||||
self.resolution_result(&type_specifier)
|
||||
} else {
|
||||
self.resolution_result(resolved_specifier)
|
||||
}
|
||||
} else {
|
||||
self.resolution_result(resolved_specifier)
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
let code_dependency = &dependency.maybe_code.clone()?;
|
||||
if let analysis::ResolvedDependency::Resolved(resolved_specifier) =
|
||||
code_dependency
|
||||
{
|
||||
if let Some(type_dependency) = self.get_maybe_types(resolved_specifier)
|
||||
{
|
||||
self.set_maybe_type(specifier, &referrer, &type_dependency);
|
||||
if let analysis::ResolvedDependency::Resolved(type_specifier) =
|
||||
type_dependency
|
||||
{
|
||||
self.resolution_result(&type_specifier)
|
||||
} else {
|
||||
self.resolution_result(resolved_specifier)
|
||||
}
|
||||
} else {
|
||||
self.resolution_result(resolved_specifier)
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn set_import_map(&mut self, maybe_import_map: Option<ImportMap>) {
|
||||
for (_, metadata) in self.metadata.iter_mut() {
|
||||
metadata.refresh(&maybe_import_map);
|
||||
}
|
||||
self.maybe_import_map = maybe_import_map;
|
||||
}
|
||||
|
||||
fn set_maybe_type(
|
||||
&mut self,
|
||||
specifier: &str,
|
||||
referrer: &ModuleSpecifier,
|
||||
dependency: &analysis::ResolvedDependency,
|
||||
) {
|
||||
if let Some(metadata) = self.metadata.get_mut(referrer) {
|
||||
if let Some(dependencies) = &mut metadata.dependencies {
|
||||
if let Some(dep) = dependencies.get_mut(specifier) {
|
||||
dep.maybe_type = Some(dependency.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn set_navigation_tree(
|
||||
&mut self,
|
||||
specifier: &ModuleSpecifier,
|
||||
navigation_tree: NavigationTree,
|
||||
) -> Result<(), AnyError> {
|
||||
let mut metadata = self
|
||||
.metadata
|
||||
.get_mut(specifier)
|
||||
.ok_or_else(|| anyhow!("Specifier not found {}"))?;
|
||||
metadata.maybe_navigation_tree = Some(navigation_tree);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use deno_core::resolve_path;
|
||||
use deno_core::resolve_url;
|
||||
use deno_core::serde_json::json;
|
||||
use tempfile::TempDir;
|
||||
|
||||
fn setup() -> (Sources, PathBuf) {
|
||||
let temp_dir = TempDir::new().expect("could not create temp dir");
|
||||
let location = temp_dir.path().join("deps");
|
||||
let sources = Sources::new(&location);
|
||||
(sources, location)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sources_get_script_version() {
|
||||
let (sources, _) = setup();
|
||||
let tests = test_util::testdata_path();
|
||||
let specifier =
|
||||
resolve_path(&tests.join("001_hello.js").to_string_lossy()).unwrap();
|
||||
let actual = sources.get_script_version(&specifier);
|
||||
assert!(actual.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sources_get_text() {
|
||||
let (sources, _) = setup();
|
||||
let tests = test_util::testdata_path();
|
||||
let specifier =
|
||||
resolve_path(&tests.join("001_hello.js").to_string_lossy()).unwrap();
|
||||
let actual = sources.get_source(&specifier);
|
||||
assert!(actual.is_some());
|
||||
let actual = actual.unwrap().to_string();
|
||||
assert_eq!(actual, "console.log(\"Hello World\");\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_resolve_dependency_types() {
|
||||
let (sources, location) = setup();
|
||||
let cache = HttpCache::new(&location);
|
||||
let specifier_dep = resolve_url("https://deno.land/x/mod.ts").unwrap();
|
||||
cache
|
||||
.set(
|
||||
&specifier_dep,
|
||||
Default::default(),
|
||||
b"export * from \"https://deno.land/x/lib.js\";",
|
||||
)
|
||||
.unwrap();
|
||||
let specifier_code = resolve_url("https://deno.land/x/lib.js").unwrap();
|
||||
let mut headers_code = HashMap::new();
|
||||
headers_code
|
||||
.insert("x-typescript-types".to_string(), "./lib.d.ts".to_string());
|
||||
cache
|
||||
.set(&specifier_code, headers_code, b"export const a = 1;")
|
||||
.unwrap();
|
||||
let specifier_type = resolve_url("https://deno.land/x/lib.d.ts").unwrap();
|
||||
cache
|
||||
.set(
|
||||
&specifier_type,
|
||||
Default::default(),
|
||||
b"export const a: number;",
|
||||
)
|
||||
.unwrap();
|
||||
let actual =
|
||||
sources.resolve_import("https://deno.land/x/lib.js", &specifier_dep);
|
||||
assert_eq!(actual, Some((specifier_type, MediaType::Dts)))
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// This is a regression test for https://github.com/denoland/deno/issues/10031
|
||||
fn test_resolve_dependency_import_types() {
|
||||
let (sources, location) = setup();
|
||||
let cache = HttpCache::new(&location);
|
||||
let specifier_dep = resolve_url("https://deno.land/x/mod.ts").unwrap();
|
||||
cache
|
||||
.set(
|
||||
&specifier_dep,
|
||||
Default::default(),
|
||||
b"import type { A } from \"https://deno.land/x/lib.js\";\nconst a: A = { a: \"a\" };",
|
||||
)
|
||||
.unwrap();
|
||||
let specifier_code = resolve_url("https://deno.land/x/lib.js").unwrap();
|
||||
let mut headers_code = HashMap::new();
|
||||
headers_code
|
||||
.insert("x-typescript-types".to_string(), "./lib.d.ts".to_string());
|
||||
cache
|
||||
.set(&specifier_code, headers_code, b"export const a = 1;")
|
||||
.unwrap();
|
||||
let specifier_type = resolve_url("https://deno.land/x/lib.d.ts").unwrap();
|
||||
cache
|
||||
.set(
|
||||
&specifier_type,
|
||||
Default::default(),
|
||||
b"export const a: number;\nexport interface A { a: number; }\n",
|
||||
)
|
||||
.unwrap();
|
||||
let actual =
|
||||
sources.resolve_import("https://deno.land/x/lib.js", &specifier_dep);
|
||||
assert_eq!(actual, Some((specifier_type, MediaType::Dts)))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_warning_header() {
|
||||
let (sources, location) = setup();
|
||||
let cache = HttpCache::new(&location);
|
||||
let specifier = resolve_url("https://deno.land/x/lib.js").unwrap();
|
||||
let mut headers = HashMap::new();
|
||||
headers.insert(
|
||||
"x-deno-warning".to_string(),
|
||||
"this is a warning".to_string(),
|
||||
);
|
||||
cache
|
||||
.set(&specifier, headers, b"export const a = 1;")
|
||||
.unwrap();
|
||||
let actual = sources.get_maybe_warning(&specifier);
|
||||
assert_eq!(actual, Some("this is a warning".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_resolve_dependency_evil_redirect() {
|
||||
let (sources, location) = setup();
|
||||
let cache = HttpCache::new(&location);
|
||||
let evil_specifier = resolve_url("https://deno.land/x/evil.ts").unwrap();
|
||||
let mut evil_headers = HashMap::new();
|
||||
evil_headers
|
||||
.insert("location".to_string(), "file:///etc/passwd".to_string());
|
||||
cache.set(&evil_specifier, evil_headers, b"").unwrap();
|
||||
let remote_specifier = resolve_url("https://deno.land/x/mod.ts").unwrap();
|
||||
cache
|
||||
.set(
|
||||
&remote_specifier,
|
||||
Default::default(),
|
||||
b"export * from \"./evil.ts\";",
|
||||
)
|
||||
.unwrap();
|
||||
let actual = sources.resolve_import("./evil.ts", &remote_specifier);
|
||||
assert_eq!(actual, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_resolve_with_import_map() {
|
||||
let (sources, location) = setup();
|
||||
let import_map_json = json!({
|
||||
"imports": {
|
||||
"mylib": "https://deno.land/x/myLib/index.js"
|
||||
}
|
||||
});
|
||||
let import_map = ImportMap::from_json(
|
||||
"https://deno.land/x/",
|
||||
&import_map_json.to_string(),
|
||||
)
|
||||
.unwrap();
|
||||
sources.set_import_map(Some(import_map));
|
||||
let cache = HttpCache::new(&location);
|
||||
let mylib_specifier =
|
||||
resolve_url("https://deno.land/x/myLib/index.js").unwrap();
|
||||
let mut mylib_headers_map = HashMap::new();
|
||||
mylib_headers_map.insert(
|
||||
"content-type".to_string(),
|
||||
"application/javascript".to_string(),
|
||||
);
|
||||
cache
|
||||
.set(
|
||||
&mylib_specifier,
|
||||
mylib_headers_map,
|
||||
b"export const a = \"a\";\n",
|
||||
)
|
||||
.unwrap();
|
||||
let referrer = resolve_url("https://deno.land/x/mod.ts").unwrap();
|
||||
cache
|
||||
.set(
|
||||
&referrer,
|
||||
Default::default(),
|
||||
b"export { a } from \"mylib\";",
|
||||
)
|
||||
.unwrap();
|
||||
let actual = sources.resolve_import("mylib", &referrer);
|
||||
assert_eq!(actual, Some((mylib_specifier, MediaType::JavaScript)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_update_import_map() {
|
||||
let (sources, location) = setup();
|
||||
let import_map_json = json!({
|
||||
"imports": {
|
||||
"otherlib": "https://deno.land/x/otherlib/index.js"
|
||||
}
|
||||
});
|
||||
let import_map = ImportMap::from_json(
|
||||
"https://deno.land/x/",
|
||||
&import_map_json.to_string(),
|
||||
)
|
||||
.unwrap();
|
||||
sources.set_import_map(Some(import_map));
|
||||
let cache = HttpCache::new(&location);
|
||||
let mylib_specifier =
|
||||
resolve_url("https://deno.land/x/myLib/index.js").unwrap();
|
||||
let mut mylib_headers_map = HashMap::new();
|
||||
mylib_headers_map.insert(
|
||||
"content-type".to_string(),
|
||||
"application/javascript".to_string(),
|
||||
);
|
||||
cache
|
||||
.set(
|
||||
&mylib_specifier,
|
||||
mylib_headers_map,
|
||||
b"export const a = \"a\";\n",
|
||||
)
|
||||
.unwrap();
|
||||
let referrer = resolve_url("https://deno.land/x/mod.ts").unwrap();
|
||||
cache
|
||||
.set(
|
||||
&referrer,
|
||||
Default::default(),
|
||||
b"export { a } from \"mylib\";",
|
||||
)
|
||||
.unwrap();
|
||||
let actual = sources.resolve_import("mylib", &referrer);
|
||||
assert_eq!(actual, None);
|
||||
let import_map_json = json!({
|
||||
"imports": {
|
||||
"otherlib": "https://deno.land/x/otherlib/index.js",
|
||||
"mylib": "https://deno.land/x/myLib/index.js"
|
||||
}
|
||||
});
|
||||
let import_map = ImportMap::from_json(
|
||||
"https://deno.land/x/",
|
||||
&import_map_json.to_string(),
|
||||
)
|
||||
.unwrap();
|
||||
sources.set_import_map(Some(import_map));
|
||||
let actual = sources.resolve_import("mylib", &referrer);
|
||||
assert_eq!(actual, Some((mylib_specifier, MediaType::JavaScript)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sources_resolve_specifier_non_supported_schema() {
|
||||
let (sources, _) = setup();
|
||||
let specifier =
|
||||
resolve_url("foo://a/b/c.ts").expect("could not create specifier");
|
||||
let sources = sources.0.lock();
|
||||
let mut redirects = sources.redirects.clone();
|
||||
let http_cache = sources.http_cache.clone();
|
||||
let actual = resolve_specifier(&specifier, &mut redirects, &http_cache);
|
||||
assert!(actual.is_none());
|
||||
}
|
||||
}
|
278
cli/lsp/tsc.rs
278
cli/lsp/tsc.rs
|
@ -1,7 +1,5 @@
|
|||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use super::analysis::ResolvedDependency;
|
||||
use super::analysis::ResolvedDependencyErr;
|
||||
use super::code_lens;
|
||||
use super::config;
|
||||
use super::language_server;
|
||||
|
@ -22,7 +20,6 @@ use crate::tokio_util::create_basic_runtime;
|
|||
use crate::tsc;
|
||||
use crate::tsc::ResolveArgs;
|
||||
|
||||
use deno_ast::MediaType;
|
||||
use deno_core::error::anyhow;
|
||||
use deno_core::error::custom_error;
|
||||
use deno_core::error::AnyError;
|
||||
|
@ -106,7 +103,7 @@ impl TsServer {
|
|||
Self(tx)
|
||||
}
|
||||
|
||||
pub async fn request<R>(
|
||||
pub(crate) async fn request<R>(
|
||||
&self,
|
||||
snapshot: StateSnapshot,
|
||||
req: RequestMethod,
|
||||
|
@ -128,8 +125,8 @@ impl TsServer {
|
|||
pub struct AssetDocument {
|
||||
pub text: Arc<String>,
|
||||
pub length: usize,
|
||||
pub line_index: LineIndex,
|
||||
pub maybe_navigation_tree: Option<NavigationTree>,
|
||||
pub line_index: Arc<LineIndex>,
|
||||
pub maybe_navigation_tree: Option<Arc<NavigationTree>>,
|
||||
}
|
||||
|
||||
impl AssetDocument {
|
||||
|
@ -138,7 +135,7 @@ impl AssetDocument {
|
|||
Self {
|
||||
text: Arc::new(text.to_string()),
|
||||
length: text.encode_utf16().count(),
|
||||
line_index: LineIndex::new(text),
|
||||
line_index: Arc::new(LineIndex::new(text)),
|
||||
maybe_navigation_tree: None,
|
||||
}
|
||||
}
|
||||
|
@ -179,10 +176,18 @@ impl Assets {
|
|||
self.0.insert(k, v)
|
||||
}
|
||||
|
||||
pub fn get_navigation_tree(
|
||||
&self,
|
||||
specifier: &ModuleSpecifier,
|
||||
) -> Option<Arc<NavigationTree>> {
|
||||
let doc = self.0.get(specifier).map(|v| v.as_ref()).flatten()?;
|
||||
doc.maybe_navigation_tree.as_ref().cloned()
|
||||
}
|
||||
|
||||
pub fn set_navigation_tree(
|
||||
&mut self,
|
||||
specifier: &ModuleSpecifier,
|
||||
navigation_tree: NavigationTree,
|
||||
navigation_tree: Arc<NavigationTree>,
|
||||
) -> Result<(), AnyError> {
|
||||
let maybe_doc = self
|
||||
.0
|
||||
|
@ -199,7 +204,7 @@ impl Assets {
|
|||
/// Optionally returns an internal asset, first checking for any static assets
|
||||
/// in Rust, then checking any previously retrieved static assets from the
|
||||
/// isolate, and then finally, the tsc isolate itself.
|
||||
pub async fn get_asset(
|
||||
pub(crate) async fn get_asset(
|
||||
specifier: &ModuleSpecifier,
|
||||
ts_server: &TsServer,
|
||||
state_snapshot: StateSnapshot,
|
||||
|
@ -498,7 +503,7 @@ pub struct TextSpan {
|
|||
}
|
||||
|
||||
impl TextSpan {
|
||||
pub fn to_range(&self, line_index: &LineIndex) -> lsp::Range {
|
||||
pub fn to_range(&self, line_index: Arc<LineIndex>) -> lsp::Range {
|
||||
lsp::Range {
|
||||
start: line_index.position_tsc(self.start.into()),
|
||||
end: line_index.position_tsc(TextSize::from(self.start + self.length)),
|
||||
|
@ -532,7 +537,7 @@ pub struct QuickInfo {
|
|||
}
|
||||
|
||||
impl QuickInfo {
|
||||
pub fn to_hover(&self, line_index: &LineIndex) -> lsp::Hover {
|
||||
pub fn to_hover(&self, line_index: Arc<LineIndex>) -> lsp::Hover {
|
||||
let mut contents = Vec::<lsp::MarkedString>::new();
|
||||
if let Some(display_string) = self
|
||||
.display_parts
|
||||
|
@ -585,7 +590,7 @@ pub struct DocumentSpan {
|
|||
impl DocumentSpan {
|
||||
pub(crate) async fn to_link(
|
||||
&self,
|
||||
line_index: &LineIndex,
|
||||
line_index: Arc<LineIndex>,
|
||||
language_server: &mut language_server::Inner,
|
||||
) -> Option<lsp::LocationLink> {
|
||||
let target_specifier = normalize_specifier(&self.file_name).ok()?;
|
||||
|
@ -600,13 +605,13 @@ impl DocumentSpan {
|
|||
let (target_range, target_selection_range) =
|
||||
if let Some(context_span) = &self.context_span {
|
||||
(
|
||||
context_span.to_range(&target_line_index),
|
||||
self.text_span.to_range(&target_line_index),
|
||||
context_span.to_range(target_line_index.clone()),
|
||||
self.text_span.to_range(target_line_index),
|
||||
)
|
||||
} else {
|
||||
(
|
||||
self.text_span.to_range(&target_line_index),
|
||||
self.text_span.to_range(&target_line_index),
|
||||
self.text_span.to_range(target_line_index.clone()),
|
||||
self.text_span.to_range(target_line_index),
|
||||
)
|
||||
};
|
||||
let origin_selection_range =
|
||||
|
@ -642,7 +647,7 @@ pub struct NavigationTree {
|
|||
impl NavigationTree {
|
||||
pub fn to_code_lens(
|
||||
&self,
|
||||
line_index: &LineIndex,
|
||||
line_index: Arc<LineIndex>,
|
||||
specifier: &ModuleSpecifier,
|
||||
source: &code_lens::CodeLensSource,
|
||||
) -> lsp::CodeLens {
|
||||
|
@ -666,7 +671,7 @@ impl NavigationTree {
|
|||
|
||||
pub fn collect_document_symbols(
|
||||
&self,
|
||||
line_index: &LineIndex,
|
||||
line_index: Arc<LineIndex>,
|
||||
document_symbols: &mut Vec<lsp::DocumentSymbol>,
|
||||
) -> bool {
|
||||
let mut should_include = self.should_include_entry();
|
||||
|
@ -692,8 +697,8 @@ impl NavigationTree {
|
|||
})
|
||||
.any(|child_range| range.intersect(child_range).is_some());
|
||||
if should_traverse_child {
|
||||
let included_child =
|
||||
child.collect_document_symbols(line_index, &mut symbol_children);
|
||||
let included_child = child
|
||||
.collect_document_symbols(line_index.clone(), &mut symbol_children);
|
||||
should_include = should_include || included_child;
|
||||
}
|
||||
}
|
||||
|
@ -727,8 +732,8 @@ impl NavigationTree {
|
|||
document_symbols.push(lsp::DocumentSymbol {
|
||||
name: self.text.clone(),
|
||||
kind: self.kind.clone().into(),
|
||||
range: span.to_range(line_index),
|
||||
selection_range: selection_span.to_range(line_index),
|
||||
range: span.to_range(line_index.clone()),
|
||||
selection_range: selection_span.to_range(line_index.clone()),
|
||||
tags,
|
||||
children,
|
||||
detail: None,
|
||||
|
@ -786,7 +791,7 @@ pub struct ImplementationLocation {
|
|||
impl ImplementationLocation {
|
||||
pub(crate) fn to_location(
|
||||
&self,
|
||||
line_index: &LineIndex,
|
||||
line_index: Arc<LineIndex>,
|
||||
language_server: &mut language_server::Inner,
|
||||
) -> lsp::Location {
|
||||
let specifier = normalize_specifier(&self.document_span.file_name)
|
||||
|
@ -803,7 +808,7 @@ impl ImplementationLocation {
|
|||
|
||||
pub(crate) async fn to_link(
|
||||
&self,
|
||||
line_index: &LineIndex,
|
||||
line_index: Arc<LineIndex>,
|
||||
language_server: &mut language_server::Inner,
|
||||
) -> Option<lsp::LocationLink> {
|
||||
self
|
||||
|
@ -846,7 +851,7 @@ impl RenameLocations {
|
|||
lsp::TextDocumentEdit {
|
||||
text_document: lsp::OptionalVersionedTextDocumentIdentifier {
|
||||
uri: uri.clone(),
|
||||
version: language_server.document_version(specifier.clone()),
|
||||
version: language_server.document_version(&specifier),
|
||||
},
|
||||
edits:
|
||||
Vec::<lsp::OneOf<lsp::TextEdit, lsp::AnnotatedTextEdit>>::new(),
|
||||
|
@ -860,7 +865,7 @@ impl RenameLocations {
|
|||
range: location
|
||||
.document_span
|
||||
.text_span
|
||||
.to_range(&language_server.get_line_index(specifier.clone()).await?),
|
||||
.to_range(language_server.get_line_index(specifier.clone()).await?),
|
||||
new_text: new_name.to_string(),
|
||||
}));
|
||||
}
|
||||
|
@ -919,14 +924,16 @@ pub struct DefinitionInfoAndBoundSpan {
|
|||
impl DefinitionInfoAndBoundSpan {
|
||||
pub(crate) async fn to_definition(
|
||||
&self,
|
||||
line_index: &LineIndex,
|
||||
line_index: Arc<LineIndex>,
|
||||
language_server: &mut language_server::Inner,
|
||||
) -> Option<lsp::GotoDefinitionResponse> {
|
||||
if let Some(definitions) = &self.definitions {
|
||||
let mut location_links = Vec::<lsp::LocationLink>::new();
|
||||
for di in definitions {
|
||||
if let Some(link) =
|
||||
di.document_span.to_link(line_index, language_server).await
|
||||
if let Some(link) = di
|
||||
.document_span
|
||||
.to_link(line_index.clone(), language_server)
|
||||
.await
|
||||
{
|
||||
location_links.push(link);
|
||||
}
|
||||
|
@ -948,13 +955,13 @@ pub struct DocumentHighlights {
|
|||
impl DocumentHighlights {
|
||||
pub fn to_highlight(
|
||||
&self,
|
||||
line_index: &LineIndex,
|
||||
line_index: Arc<LineIndex>,
|
||||
) -> Vec<lsp::DocumentHighlight> {
|
||||
self
|
||||
.highlight_spans
|
||||
.iter()
|
||||
.map(|hs| lsp::DocumentHighlight {
|
||||
range: hs.text_span.to_range(line_index),
|
||||
range: hs.text_span.to_range(line_index.clone()),
|
||||
kind: match hs.kind {
|
||||
HighlightSpanKind::WrittenReference => {
|
||||
Some(lsp::DocumentHighlightKind::Write)
|
||||
|
@ -976,7 +983,7 @@ pub struct TextChange {
|
|||
impl TextChange {
|
||||
pub fn as_text_edit(
|
||||
&self,
|
||||
line_index: &LineIndex,
|
||||
line_index: Arc<LineIndex>,
|
||||
) -> lsp::OneOf<lsp::TextEdit, lsp::AnnotatedTextEdit> {
|
||||
lsp::OneOf::Left(lsp::TextEdit {
|
||||
range: self.span.to_range(line_index),
|
||||
|
@ -1004,12 +1011,12 @@ impl FileTextChanges {
|
|||
let edits = self
|
||||
.text_changes
|
||||
.iter()
|
||||
.map(|tc| tc.as_text_edit(&line_index))
|
||||
.map(|tc| tc.as_text_edit(line_index.clone()))
|
||||
.collect();
|
||||
Ok(lsp::TextDocumentEdit {
|
||||
text_document: lsp::OptionalVersionedTextDocumentIdentifier {
|
||||
uri: specifier.clone(),
|
||||
version: language_server.document_version(specifier),
|
||||
version: language_server.document_version(&specifier),
|
||||
},
|
||||
edits,
|
||||
})
|
||||
|
@ -1024,7 +1031,7 @@ impl FileTextChanges {
|
|||
let line_index = if !self.is_new_file.unwrap_or(false) {
|
||||
language_server.get_line_index(specifier.clone()).await?
|
||||
} else {
|
||||
LineIndex::new("")
|
||||
Arc::new(LineIndex::new(""))
|
||||
};
|
||||
|
||||
if self.is_new_file.unwrap_or(false) {
|
||||
|
@ -1043,12 +1050,12 @@ impl FileTextChanges {
|
|||
let edits = self
|
||||
.text_changes
|
||||
.iter()
|
||||
.map(|tc| tc.as_text_edit(&line_index))
|
||||
.map(|tc| tc.as_text_edit(line_index.clone()))
|
||||
.collect();
|
||||
ops.push(lsp::DocumentChangeOperation::Edit(lsp::TextDocumentEdit {
|
||||
text_document: lsp::OptionalVersionedTextDocumentIdentifier {
|
||||
uri: specifier.clone(),
|
||||
version: language_server.document_version(specifier),
|
||||
version: language_server.document_version(&specifier),
|
||||
},
|
||||
edits,
|
||||
}));
|
||||
|
@ -1066,7 +1073,7 @@ pub struct Classifications {
|
|||
impl Classifications {
|
||||
pub fn to_semantic_tokens(
|
||||
&self,
|
||||
line_index: &LineIndex,
|
||||
line_index: Arc<LineIndex>,
|
||||
) -> lsp::SemanticTokens {
|
||||
let token_count = self.spans.len() / 3;
|
||||
let mut builder = SemanticTokensBuilder::new();
|
||||
|
@ -1299,7 +1306,7 @@ pub struct ReferenceEntry {
|
|||
impl ReferenceEntry {
|
||||
pub(crate) fn to_location(
|
||||
&self,
|
||||
line_index: &LineIndex,
|
||||
line_index: Arc<LineIndex>,
|
||||
language_server: &mut language_server::Inner,
|
||||
) -> lsp::Location {
|
||||
let specifier = normalize_specifier(&self.document_span.file_name)
|
||||
|
@ -1342,7 +1349,7 @@ impl CallHierarchyItem {
|
|||
.ok()?;
|
||||
|
||||
Some(self.to_call_hierarchy_item(
|
||||
&target_line_index,
|
||||
target_line_index,
|
||||
language_server,
|
||||
maybe_root_path,
|
||||
))
|
||||
|
@ -1350,7 +1357,7 @@ impl CallHierarchyItem {
|
|||
|
||||
pub(crate) fn to_call_hierarchy_item(
|
||||
&self,
|
||||
line_index: &LineIndex,
|
||||
line_index: Arc<LineIndex>,
|
||||
language_server: &mut language_server::Inner,
|
||||
maybe_root_path: Option<&Path>,
|
||||
) -> lsp::CallHierarchyItem {
|
||||
|
@ -1410,7 +1417,7 @@ impl CallHierarchyItem {
|
|||
uri,
|
||||
detail: Some(detail),
|
||||
kind: self.kind.clone().into(),
|
||||
range: self.span.to_range(line_index),
|
||||
range: self.span.to_range(line_index.clone()),
|
||||
selection_range: self.selection_span.to_range(line_index),
|
||||
data: None,
|
||||
}
|
||||
|
@ -1444,14 +1451,14 @@ impl CallHierarchyIncomingCall {
|
|||
|
||||
Some(lsp::CallHierarchyIncomingCall {
|
||||
from: self.from.to_call_hierarchy_item(
|
||||
&target_line_index,
|
||||
target_line_index.clone(),
|
||||
language_server,
|
||||
maybe_root_path,
|
||||
),
|
||||
from_ranges: self
|
||||
.from_spans
|
||||
.iter()
|
||||
.map(|span| span.to_range(&target_line_index))
|
||||
.map(|span| span.to_range(target_line_index.clone()))
|
||||
.collect(),
|
||||
})
|
||||
}
|
||||
|
@ -1467,7 +1474,7 @@ pub struct CallHierarchyOutgoingCall {
|
|||
impl CallHierarchyOutgoingCall {
|
||||
pub(crate) async fn try_resolve_call_hierarchy_outgoing_call(
|
||||
&self,
|
||||
line_index: &LineIndex,
|
||||
line_index: Arc<LineIndex>,
|
||||
language_server: &mut language_server::Inner,
|
||||
maybe_root_path: Option<&Path>,
|
||||
) -> Option<lsp::CallHierarchyOutgoingCall> {
|
||||
|
@ -1479,14 +1486,14 @@ impl CallHierarchyOutgoingCall {
|
|||
|
||||
Some(lsp::CallHierarchyOutgoingCall {
|
||||
to: self.to.to_call_hierarchy_item(
|
||||
&target_line_index,
|
||||
target_line_index,
|
||||
language_server,
|
||||
maybe_root_path,
|
||||
),
|
||||
from_ranges: self
|
||||
.from_spans
|
||||
.iter()
|
||||
.map(|span| span.to_range(line_index))
|
||||
.map(|span| span.to_range(line_index.clone()))
|
||||
.collect(),
|
||||
})
|
||||
}
|
||||
|
@ -1560,7 +1567,7 @@ pub struct CompletionInfo {
|
|||
impl CompletionInfo {
|
||||
pub fn as_completion_response(
|
||||
&self,
|
||||
line_index: &LineIndex,
|
||||
line_index: Arc<LineIndex>,
|
||||
settings: &config::CompletionSettings,
|
||||
specifier: &ModuleSpecifier,
|
||||
position: u32,
|
||||
|
@ -1569,8 +1576,13 @@ impl CompletionInfo {
|
|||
.entries
|
||||
.iter()
|
||||
.map(|entry| {
|
||||
entry
|
||||
.as_completion_item(line_index, self, settings, specifier, position)
|
||||
entry.as_completion_item(
|
||||
line_index.clone(),
|
||||
self,
|
||||
settings,
|
||||
specifier,
|
||||
position,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let is_incomplete = self
|
||||
|
@ -1711,7 +1723,7 @@ impl CompletionEntry {
|
|||
|
||||
pub fn as_completion_item(
|
||||
&self,
|
||||
line_index: &LineIndex,
|
||||
line_index: Arc<LineIndex>,
|
||||
info: &CompletionInfo,
|
||||
settings: &config::CompletionSettings,
|
||||
specifier: &ModuleSpecifier,
|
||||
|
@ -1837,11 +1849,11 @@ const FOLD_END_PAIR_CHARACTERS: &[u8] = &[b'}', b']', b')', b'`'];
|
|||
impl OutliningSpan {
|
||||
pub fn to_folding_range(
|
||||
&self,
|
||||
line_index: &LineIndex,
|
||||
line_index: Arc<LineIndex>,
|
||||
content: &[u8],
|
||||
line_folding_only: bool,
|
||||
) -> lsp::FoldingRange {
|
||||
let range = self.text_span.to_range(line_index);
|
||||
let range = self.text_span.to_range(line_index.clone());
|
||||
lsp::FoldingRange {
|
||||
start_line: range.start.line,
|
||||
start_character: if line_folding_only {
|
||||
|
@ -1867,7 +1879,7 @@ impl OutliningSpan {
|
|||
fn adjust_folding_end_line(
|
||||
&self,
|
||||
range: &lsp::Range,
|
||||
line_index: &LineIndex,
|
||||
line_index: Arc<LineIndex>,
|
||||
content: &[u8],
|
||||
line_folding_only: bool,
|
||||
) -> u32 {
|
||||
|
@ -1991,10 +2003,10 @@ pub struct SelectionRange {
|
|||
impl SelectionRange {
|
||||
pub fn to_selection_range(
|
||||
&self,
|
||||
line_index: &LineIndex,
|
||||
line_index: Arc<LineIndex>,
|
||||
) -> lsp::SelectionRange {
|
||||
lsp::SelectionRange {
|
||||
range: self.text_span.to_range(line_index),
|
||||
range: self.text_span.to_range(line_index.clone()),
|
||||
parent: self.parent.as_ref().map(|parent_selection| {
|
||||
Box::new(parent_selection.to_selection_range(line_index))
|
||||
}),
|
||||
|
@ -2065,19 +2077,13 @@ fn cache_snapshot(
|
|||
.snapshots
|
||||
.contains_key(&(specifier.clone(), version.clone().into()))
|
||||
{
|
||||
let content = if state.state_snapshot.documents.contains_key(specifier) {
|
||||
state
|
||||
let content = state
|
||||
.state_snapshot
|
||||
.documents
|
||||
.content(specifier)
|
||||
.ok_or_else(|| {
|
||||
anyhow!("Specifier unexpectedly doesn't have content: {}", specifier)
|
||||
})?
|
||||
} else {
|
||||
state.state_snapshot.sources.get_source(specifier).ok_or_else(|| {
|
||||
anyhow!("Specifier (\"{}\") is not an in memory document or on disk resource.", specifier)
|
||||
})?
|
||||
};
|
||||
})?;
|
||||
state
|
||||
.snapshots
|
||||
.insert((specifier.clone(), version.into()), content.to_string());
|
||||
|
@ -2140,7 +2146,10 @@ fn op_exists(state: &mut State, args: SpecifierArgs) -> Result<bool, AnyError> {
|
|||
.performance
|
||||
.mark("op_exists", Some(&args));
|
||||
let specifier = state.normalize_specifier(args.specifier)?;
|
||||
let result = state.state_snapshot.sources.contains_key(&specifier);
|
||||
let result = state
|
||||
.state_snapshot
|
||||
.documents
|
||||
.contains_specifier(&specifier);
|
||||
state.state_snapshot.performance.measure(mark);
|
||||
Ok(result)
|
||||
}
|
||||
|
@ -2268,7 +2277,7 @@ fn op_load(
|
|||
.performance
|
||||
.mark("op_load", Some(&args));
|
||||
let specifier = state.normalize_specifier(args.specifier)?;
|
||||
let result = state.state_snapshot.sources.get_source(&specifier);
|
||||
let result = state.state_snapshot.documents.content(&specifier);
|
||||
state.state_snapshot.performance.measure(mark);
|
||||
Ok(result.map(|t| t.to_string()))
|
||||
}
|
||||
|
@ -2281,89 +2290,33 @@ fn op_resolve(
|
|||
.state_snapshot
|
||||
.performance
|
||||
.mark("op_resolve", Some(&args));
|
||||
let mut resolved = Vec::new();
|
||||
let referrer = state.normalize_specifier(&args.base)?;
|
||||
let sources = &mut state.state_snapshot.sources;
|
||||
|
||||
if state.state_snapshot.documents.contains_key(&referrer) {
|
||||
if let Some(dependencies) =
|
||||
state.state_snapshot.documents.dependencies(&referrer)
|
||||
{
|
||||
for specifier in &args.specifiers {
|
||||
if specifier.starts_with("asset:///") {
|
||||
resolved.push(Some((
|
||||
specifier.clone(),
|
||||
MediaType::from(specifier).as_ts_extension().into(),
|
||||
)))
|
||||
} else if let Some(dependency) = dependencies.get(specifier) {
|
||||
let resolved_import =
|
||||
if let Some(resolved_import) = &dependency.maybe_type {
|
||||
resolved_import.clone()
|
||||
} else if let Some(resolved_import) = &dependency.maybe_code {
|
||||
resolved_import.clone()
|
||||
} else {
|
||||
ResolvedDependency::Err(ResolvedDependencyErr::Missing)
|
||||
};
|
||||
if let ResolvedDependency::Resolved(resolved_specifier) =
|
||||
resolved_import
|
||||
{
|
||||
if state
|
||||
let result = if let Some(resolved) = state
|
||||
.state_snapshot
|
||||
.documents
|
||||
.contains_key(&resolved_specifier)
|
||||
.resolve(args.specifiers, &referrer)
|
||||
{
|
||||
let media_type = MediaType::from(&resolved_specifier);
|
||||
resolved.push(Some((
|
||||
resolved_specifier.to_string(),
|
||||
media_type.as_ts_extension().into(),
|
||||
)));
|
||||
} else if sources.contains_key(&resolved_specifier) {
|
||||
let media_type = if let Some(media_type) =
|
||||
sources.get_media_type(&resolved_specifier)
|
||||
{
|
||||
media_type
|
||||
Ok(
|
||||
resolved
|
||||
.into_iter()
|
||||
.map(|o| {
|
||||
o.map(|(s, mt)| (s.to_string(), mt.as_ts_extension().to_string()))
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
} else {
|
||||
MediaType::from(&resolved_specifier)
|
||||
};
|
||||
resolved.push(Some((
|
||||
resolved_specifier.to_string(),
|
||||
media_type.as_ts_extension().into(),
|
||||
)));
|
||||
} else {
|
||||
resolved.push(None);
|
||||
}
|
||||
} else {
|
||||
resolved.push(None);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if sources.contains_key(&referrer) {
|
||||
for specifier in &args.specifiers {
|
||||
if let Some((resolved_specifier, media_type)) =
|
||||
sources.resolve_import(specifier, &referrer)
|
||||
{
|
||||
resolved.push(Some((
|
||||
resolved_specifier.to_string(),
|
||||
media_type.as_ts_extension().into(),
|
||||
)));
|
||||
} else {
|
||||
resolved.push(None);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
state.state_snapshot.performance.measure(mark);
|
||||
return Err(custom_error(
|
||||
Err(custom_error(
|
||||
"NotFound",
|
||||
format!(
|
||||
"the referring ({}) specifier is unexpectedly missing",
|
||||
referrer
|
||||
"Error resolving. Referring specifier \"{}\" was not found.",
|
||||
args.base
|
||||
),
|
||||
));
|
||||
}
|
||||
))
|
||||
};
|
||||
|
||||
state.state_snapshot.performance.measure(mark);
|
||||
Ok(resolved)
|
||||
result
|
||||
}
|
||||
|
||||
fn op_respond(state: &mut State, args: Response) -> Result<bool, AnyError> {
|
||||
|
@ -2375,15 +2328,7 @@ fn op_script_names(
|
|||
state: &mut State,
|
||||
_args: Value,
|
||||
) -> Result<Vec<ModuleSpecifier>, AnyError> {
|
||||
Ok(
|
||||
state
|
||||
.state_snapshot
|
||||
.documents
|
||||
.open_specifiers()
|
||||
.into_iter()
|
||||
.cloned()
|
||||
.collect(),
|
||||
)
|
||||
Ok(state.state_snapshot.documents.specifiers(true, true))
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
|
@ -2407,17 +2352,8 @@ fn op_script_version(
|
|||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
} else if let Some(version) =
|
||||
state.state_snapshot.documents.version(&specifier)
|
||||
{
|
||||
Ok(Some(version.to_string()))
|
||||
} else {
|
||||
let sources = &mut state.state_snapshot.sources;
|
||||
if let Some(version) = sources.get_script_version(&specifier) {
|
||||
Ok(Some(version))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
Ok(state.state_snapshot.documents.version(&specifier))
|
||||
};
|
||||
|
||||
state.state_snapshot.performance.measure(mark);
|
||||
|
@ -2868,7 +2804,7 @@ impl RequestMethod {
|
|||
}
|
||||
|
||||
/// Send a request into a runtime and return the JSON value of the response.
|
||||
pub fn request(
|
||||
pub(crate) fn request(
|
||||
runtime: &mut JsRuntime,
|
||||
state_snapshot: StateSnapshot,
|
||||
method: RequestMethod,
|
||||
|
@ -2908,10 +2844,8 @@ mod tests {
|
|||
use super::*;
|
||||
use crate::http_cache::HttpCache;
|
||||
use crate::http_util::HeadersMap;
|
||||
use crate::lsp::analysis;
|
||||
use crate::lsp::documents::DocumentCache;
|
||||
use crate::lsp::documents::Documents;
|
||||
use crate::lsp::documents::LanguageId;
|
||||
use crate::lsp::sources::Sources;
|
||||
use crate::lsp::text::LineIndex;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
@ -2921,37 +2855,19 @@ mod tests {
|
|||
fixtures: &[(&str, &str, i32, LanguageId)],
|
||||
location: &Path,
|
||||
) -> StateSnapshot {
|
||||
let mut documents = DocumentCache::default();
|
||||
let documents = Documents::new(location);
|
||||
for (specifier, source, version, language_id) in fixtures {
|
||||
let specifier =
|
||||
resolve_url(specifier).expect("failed to create specifier");
|
||||
documents.open(
|
||||
specifier.clone(),
|
||||
*version,
|
||||
*language_id,
|
||||
language_id.clone(),
|
||||
Arc::new(source.to_string()),
|
||||
);
|
||||
let media_type = MediaType::from(&specifier);
|
||||
if let Some(Ok(parsed_module)) =
|
||||
documents.get(&specifier).unwrap().source().module()
|
||||
{
|
||||
let (deps, _) = analysis::analyze_dependencies(
|
||||
&specifier,
|
||||
media_type,
|
||||
parsed_module,
|
||||
&None,
|
||||
);
|
||||
let dep_ranges =
|
||||
analysis::analyze_dependency_ranges(parsed_module).ok();
|
||||
documents
|
||||
.set_dependencies(&specifier, Some(deps), dep_ranges)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
let sources = Sources::new(location);
|
||||
StateSnapshot {
|
||||
documents,
|
||||
sources,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -92,7 +92,9 @@ impl LspUrlMap {
|
|||
let url = if specifier.scheme() == "file" {
|
||||
specifier.clone()
|
||||
} else {
|
||||
let specifier_str = if specifier.scheme() == "data" {
|
||||
let specifier_str = if specifier.scheme() == "asset" {
|
||||
format!("deno:asset{}", specifier.path())
|
||||
} else if specifier.scheme() == "data" {
|
||||
let data_url = DataUrl::process(specifier.as_str())
|
||||
.map_err(|e| uri_error(format!("{:?}", e)))?;
|
||||
let mime = data_url.mime_type();
|
||||
|
|
|
@ -431,7 +431,7 @@ fn lsp_hover_asset() {
|
|||
"deno/virtualTextDocument",
|
||||
json!({
|
||||
"textDocument": {
|
||||
"uri": "deno:/asset//lib.deno.shared_globals.d.ts"
|
||||
"uri": "deno:asset/lib.deno.shared_globals.d.ts"
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
@ -442,7 +442,7 @@ fn lsp_hover_asset() {
|
|||
"textDocument/hover",
|
||||
json!({
|
||||
"textDocument": {
|
||||
"uri": "deno:/asset//lib.es2015.symbol.wellknown.d.ts"
|
||||
"uri": "deno:asset/lib.es2015.symbol.wellknown.d.ts"
|
||||
},
|
||||
"position": {
|
||||
"line": 109,
|
||||
|
@ -919,11 +919,11 @@ fn lsp_hover_dependency() {
|
|||
"range": {
|
||||
"start": {
|
||||
"line": 0,
|
||||
"character": 20
|
||||
"character": 19
|
||||
},
|
||||
"end":{
|
||||
"line": 0,
|
||||
"character": 61
|
||||
"character": 62
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
@ -953,11 +953,11 @@ fn lsp_hover_dependency() {
|
|||
"range": {
|
||||
"start": {
|
||||
"line": 3,
|
||||
"character": 20
|
||||
"character": 19
|
||||
},
|
||||
"end":{
|
||||
"line": 3,
|
||||
"character": 66
|
||||
"character": 67
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
@ -987,11 +987,11 @@ fn lsp_hover_dependency() {
|
|||
"range": {
|
||||
"start": {
|
||||
"line": 4,
|
||||
"character": 20
|
||||
"character": 19
|
||||
},
|
||||
"end":{
|
||||
"line": 4,
|
||||
"character": 56
|
||||
"character": 57
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
@ -1021,11 +1021,11 @@ fn lsp_hover_dependency() {
|
|||
"range": {
|
||||
"start": {
|
||||
"line": 5,
|
||||
"character": 20
|
||||
"character": 19
|
||||
},
|
||||
"end":{
|
||||
"line": 5,
|
||||
"character": 131
|
||||
"character": 132
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
@ -1055,11 +1055,11 @@ fn lsp_hover_dependency() {
|
|||
"range": {
|
||||
"start": {
|
||||
"line": 6,
|
||||
"character": 20
|
||||
"character": 19
|
||||
},
|
||||
"end":{
|
||||
"line": 6,
|
||||
"character": 32
|
||||
"character": 33
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
@ -1771,7 +1771,7 @@ fn lsp_code_lens_non_doc_nav_tree() {
|
|||
"deno/virtualTextDocument",
|
||||
json!({
|
||||
"textDocument": {
|
||||
"uri": "deno:/asset//lib.deno.shared_globals.d.ts"
|
||||
"uri": "deno:asset/lib.deno.shared_globals.d.ts"
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
@ -1783,7 +1783,7 @@ fn lsp_code_lens_non_doc_nav_tree() {
|
|||
"textDocument/codeLens",
|
||||
json!({
|
||||
"textDocument": {
|
||||
"uri": "deno:/asset//lib.deno.shared_globals.d.ts"
|
||||
"uri": "deno:asset/lib.deno.shared_globals.d.ts"
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
@ -2714,11 +2714,11 @@ fn lsp_cache_location() {
|
|||
"range": {
|
||||
"start": {
|
||||
"line": 0,
|
||||
"character": 20
|
||||
"character": 19
|
||||
},
|
||||
"end":{
|
||||
"line": 0,
|
||||
"character": 61
|
||||
"character": 62
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
|
Loading…
Reference in a new issue