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

refactor(lsp): Documents - combine duplicate exists methods (#13479)

This commit is contained in:
David Sherret 2022-01-25 09:21:59 -05:00 committed by Bartek Iwańczuk
parent df9095c64f
commit 7f7d65aa3a
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
2 changed files with 24 additions and 36 deletions

View file

@ -146,7 +146,7 @@ pub(crate) async fn get_import_completions(
};
let maybe_list = module_registries
.get_completions(&text, offset, &range, |specifier| {
documents.contains_specifier(specifier)
documents.exists(specifier)
})
.await;
let list = maybe_list.unwrap_or_else(|| lsp::CompletionList {

View file

@ -719,7 +719,8 @@ struct FileSystemDocuments {
}
impl FileSystemDocuments {
/// Adds or updates a document by reading the document from the file system.
/// Adds or updates a document by reading the document from the file system
/// returning the document.
fn refresh_document(
&mut self,
cache: &HttpCache,
@ -756,7 +757,8 @@ impl FileSystemDocuments {
)
};
self.dirty = true;
self.docs.insert(specifier.clone(), doc)
self.docs.insert(specifier.clone(), doc.clone());
Some(doc)
}
}
@ -780,12 +782,7 @@ fn get_document_path(
if specifier.scheme() == "file" {
specifier_to_file_path(specifier).ok()
} else {
let path = cache.get_cache_filename(specifier)?;
if path.is_file() {
Some(path)
} else {
None
}
cache.get_cache_filename(specifier)
}
}
@ -921,15 +918,25 @@ impl Documents {
deno_core::resolve_import(specifier, referrer.as_str()).ok()
};
if let Some(import_specifier) = maybe_specifier {
self.contains_specifier(&import_specifier)
self.exists(&import_specifier)
} else {
false
}
}
/// Return `true` if the specifier can be resolved to a document.
pub fn contains_specifier(&self, specifier: &ModuleSpecifier) -> bool {
self.get(specifier).is_some()
pub fn exists(&self, specifier: &ModuleSpecifier) -> bool {
// keep this fast because it's used by op_exists, which is a hot path in tsc
let specifier = self.specifier_resolver.resolve(specifier);
if let Some(specifier) = specifier {
if self.open_docs.contains_key(&specifier) {
return true;
}
if let Some(path) = get_document_path(&self.cache, &specifier) {
return path.is_file();
}
}
false
}
/// Return an array of specifiers, if any, that are dependent upon the
@ -949,22 +956,6 @@ impl Documents {
}
}
/// Used by the tsc op_exists to shortcut trying to load a document to provide
/// information to CLI without allocating a document.
pub(crate) fn exists(&self, specifier: &ModuleSpecifier) -> bool {
let specifier = self.specifier_resolver.resolve(specifier);
if let Some(specifier) = specifier {
if self.open_docs.contains_key(&specifier) {
return true;
}
if let Some(path) = get_document_path(&self.cache, &specifier) {
return path.is_file();
}
}
false
}
/// Return a document for the specifier.
pub fn get(&self, specifier: &ModuleSpecifier) -> Option<Document> {
let specifier = self.specifier_resolver.resolve(specifier)?;
@ -975,20 +966,17 @@ impl Documents {
let fs_version = get_document_path(&self.cache, &specifier)
.map(|path| calculate_fs_version(&path))
.flatten();
if file_system_docs
.docs
.get(&specifier)
.map(|d| d.fs_version().to_string())
!= fs_version
{
let file_system_doc = file_system_docs.docs.get(&specifier);
if file_system_doc.map(|d| d.fs_version().to_string()) != fs_version {
// attempt to update the file on the file system
file_system_docs.refresh_document(
&self.cache,
self.get_maybe_resolver(),
&specifier,
);
)
} else {
file_system_doc.cloned()
}
file_system_docs.docs.get(&specifier).cloned()
}
}