1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(lsp): directly use file referrer when loading document (#24997)

This commit is contained in:
Nayeem Rahman 2024-08-12 15:45:27 +01:00 committed by GitHub
parent 56e8ed5eb1
commit 9bdab6fb6b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 27 additions and 34 deletions

View file

@ -858,8 +858,8 @@ mod tests {
.global() .global()
.set(&specifier, HashMap::default(), source.as_bytes()) .set(&specifier, HashMap::default(), source.as_bytes())
.expect("could not cache file"); .expect("could not cache file");
let document = let document = documents
documents.get_or_load(&specifier, &temp_dir.uri().join("$").unwrap()); .get_or_load(&specifier, Some(&temp_dir.uri().join("$").unwrap()));
assert!(document.is_some(), "source could not be setup"); assert!(document.is_some(), "source could not be setup");
} }
documents documents

View file

@ -1367,21 +1367,20 @@ fn diagnose_resolution(
let mut diagnostics = vec![]; let mut diagnostics = vec![];
match resolution { match resolution {
Resolution::Ok(resolved) => { Resolution::Ok(resolved) => {
let file_referrer = referrer_doc.file_referrer();
let specifier = &resolved.specifier; let specifier = &resolved.specifier;
let managed_npm_resolver = snapshot let managed_npm_resolver =
.resolver snapshot.resolver.maybe_managed_npm_resolver(file_referrer);
.maybe_managed_npm_resolver(referrer_doc.file_referrer());
for (_, headers) in snapshot for (_, headers) in snapshot
.resolver .resolver
.redirect_chain_headers(specifier, referrer_doc.file_referrer()) .redirect_chain_headers(specifier, file_referrer)
{ {
if let Some(message) = headers.get("x-deno-warning") { if let Some(message) = headers.get("x-deno-warning") {
diagnostics.push(DenoDiagnostic::DenoWarn(message.clone())); diagnostics.push(DenoDiagnostic::DenoWarn(message.clone()));
} }
} }
if let Some(doc) = snapshot if let Some(doc) =
.documents snapshot.documents.get_or_load(specifier, file_referrer)
.get_or_load(specifier, referrer_doc.specifier())
{ {
if let Some(headers) = doc.maybe_headers() { if let Some(headers) = doc.maybe_headers() {
if let Some(message) = headers.get("x-deno-warning") { if let Some(message) = headers.get("x-deno-warning") {

View file

@ -1180,11 +1180,10 @@ impl Documents {
pub fn get_or_load( pub fn get_or_load(
&self, &self,
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
referrer: &ModuleSpecifier, file_referrer: Option<&ModuleSpecifier>,
) -> Option<Arc<Document>> { ) -> Option<Arc<Document>> {
let file_referrer = self.get_file_referrer(referrer);
let specifier = let specifier =
self.resolve_document_specifier(specifier, file_referrer.as_deref())?; self.resolve_document_specifier(specifier, file_referrer)?;
if let Some(document) = self.open_docs.get(&specifier) { if let Some(document) = self.open_docs.get(&specifier) {
Some(document.clone()) Some(document.clone())
} else { } else {
@ -1193,7 +1192,7 @@ impl Documents {
&self.resolver, &self.resolver,
&self.config, &self.config,
&self.cache, &self.cache,
file_referrer.as_deref(), file_referrer,
) )
} }
} }
@ -1464,7 +1463,7 @@ impl Documents {
specifier = s; specifier = s;
media_type = Some(mt); media_type = Some(mt);
} }
let Some(doc) = self.get_or_load(&specifier, referrer) else { let Some(doc) = self.get_or_load(&specifier, file_referrer) else {
let media_type = let media_type =
media_type.unwrap_or_else(|| MediaType::from_specifier(&specifier)); media_type.unwrap_or_else(|| MediaType::from_specifier(&specifier));
return Some((specifier, media_type)); return Some((specifier, media_type));

View file

@ -1333,8 +1333,9 @@ impl Inner {
{ {
return Ok(None); return Ok(None);
} }
let document = let document = self
file_referrer.and_then(|r| self.documents.get_or_load(&specifier, &r)); .documents
.get_or_load(&specifier, file_referrer.as_ref());
let Some(document) = document else { let Some(document) = document else {
return Ok(None); return Ok(None);
}; };
@ -1448,7 +1449,7 @@ impl Inner {
{ {
let dep_doc = dep let dep_doc = dep
.get_code() .get_code()
.and_then(|s| self.documents.get_or_load(s, &specifier)); .and_then(|s| self.documents.get_or_load(s, file_referrer));
let dep_maybe_types_dependency = let dep_maybe_types_dependency =
dep_doc.as_ref().map(|d| d.maybe_types_dependency()); dep_doc.as_ref().map(|d| d.maybe_types_dependency());
let value = match (dep.maybe_code.is_none(), dep.maybe_type.is_none(), &dep_maybe_types_dependency) { let value = match (dep.maybe_code.is_none(), dep.maybe_type.is_none(), &dep_maybe_types_dependency) {

View file

@ -2043,12 +2043,10 @@ impl DocumentSpan {
let target_asset_or_doc = let target_asset_or_doc =
language_server.get_maybe_asset_or_document(&target_specifier)?; language_server.get_maybe_asset_or_document(&target_specifier)?;
let target_line_index = target_asset_or_doc.line_index(); let target_line_index = target_asset_or_doc.line_index();
let file_referrer = language_server let file_referrer = target_asset_or_doc.file_referrer();
.documents
.get_file_referrer(&target_specifier);
let target_uri = language_server let target_uri = language_server
.url_map .url_map
.normalize_specifier(&target_specifier, file_referrer.as_deref()) .normalize_specifier(&target_specifier, file_referrer)
.ok()?; .ok()?;
let (target_range, target_selection_range) = let (target_range, target_selection_range) =
if let Some(context_span) = &self.context_span { if let Some(context_span) = &self.context_span {
@ -2092,10 +2090,10 @@ impl DocumentSpan {
language_server.get_maybe_asset_or_document(&specifier)?; language_server.get_maybe_asset_or_document(&specifier)?;
let line_index = asset_or_doc.line_index(); let line_index = asset_or_doc.line_index();
let range = self.text_span.to_range(line_index); let range = self.text_span.to_range(line_index);
let file_referrer = language_server.documents.get_file_referrer(&specifier); let file_referrer = asset_or_doc.file_referrer();
let mut target = language_server let mut target = language_server
.url_map .url_map
.normalize_specifier(&specifier, file_referrer.as_deref()) .normalize_specifier(&specifier, file_referrer)
.ok()? .ok()?
.into_url(); .into_url();
target.set_fragment(Some(&format!( target.set_fragment(Some(&format!(
@ -2153,10 +2151,10 @@ impl NavigateToItem {
let asset_or_doc = let asset_or_doc =
language_server.get_asset_or_document(&specifier).ok()?; language_server.get_asset_or_document(&specifier).ok()?;
let line_index = asset_or_doc.line_index(); let line_index = asset_or_doc.line_index();
let file_referrer = language_server.documents.get_file_referrer(&specifier); let file_referrer = asset_or_doc.file_referrer();
let uri = language_server let uri = language_server
.url_map .url_map
.normalize_specifier(&specifier, file_referrer.as_deref()) .normalize_specifier(&specifier, file_referrer)
.ok()?; .ok()?;
let range = self.text_span.to_range(line_index); let range = self.text_span.to_range(line_index);
let location = lsp::Location { let location = lsp::Location {
@ -4232,14 +4230,10 @@ impl State {
} }
fn get_document(&self, specifier: &ModuleSpecifier) -> Option<Arc<Document>> { fn get_document(&self, specifier: &ModuleSpecifier) -> Option<Arc<Document>> {
if let Some(scope) = &self.last_scope {
self.state_snapshot.documents.get_or_load(specifier, scope)
} else {
self self
.state_snapshot .state_snapshot
.documents .documents
.get_or_load(specifier, &ModuleSpecifier::parse("file:///").unwrap()) .get_or_load(specifier, self.last_scope.as_ref())
}
} }
fn get_asset_or_document( fn get_asset_or_document(
@ -4561,7 +4555,7 @@ fn op_script_names(state: &mut OpState) -> ScriptNames {
specifier, specifier,
doc.file_referrer(), doc.file_referrer(),
)?; )?;
let types_doc = documents.get_or_load(&types, specifier)?; let types_doc = documents.get_or_load(&types, doc.file_referrer())?;
Some(types_doc.specifier().clone()) Some(types_doc.specifier().clone())
})(); })();
// If there is a types dep, use that as the root instead. But if the doc // If there is a types dep, use that as the root instead. But if the doc