summaryrefslogtreecommitdiff
path: root/cli/lsp/documents.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2023-03-29 16:25:48 -0400
committerGitHub <noreply@github.com>2023-03-29 16:25:48 -0400
commit89bbbd102c6050763105be39a49494a5ffdce35b (patch)
treec0b912058a193d12fc60fac91223e0fef9fc7637 /cli/lsp/documents.rs
parentbacbf949256e32ca84e7f11c0171db7d9a644b44 (diff)
refactor(lsp): remove boolean parameters on `documents.documents(...)` (#18493)
I think this makes things clearer at the call sites.
Diffstat (limited to 'cli/lsp/documents.rs')
-rw-r--r--cli/lsp/documents.rs76
1 files changed, 42 insertions, 34 deletions
diff --git a/cli/lsp/documents.rs b/cli/lsp/documents.rs
index 20ca755d9..d8a94e538 100644
--- a/cli/lsp/documents.rs
+++ b/cli/lsp/documents.rs
@@ -800,6 +800,17 @@ fn get_document_path(
}
}
+/// Specify the documents to include on a `documents.documents(...)` call.
+#[derive(Debug, Clone, Copy)]
+pub enum DocumentsFilter {
+ /// Includes all the documents (diagnosable & non-diagnosable, open & file system).
+ All,
+ /// Includes all the diagnosable documents (open & file system).
+ AllDiagnosable,
+ /// Includes only the diagnosable documents that are open.
+ OpenDiagnosable,
+}
+
#[derive(Debug, Clone, Default)]
pub struct Documents {
/// The DENO_DIR that the documents looks for non-file based modules.
@@ -1011,47 +1022,44 @@ impl Documents {
}
}
- /// Return a vector of documents that are contained in the document store,
- /// where `open_only` flag would provide only those documents currently open
- /// in the editor and `diagnosable_only` would provide only those documents
- /// that the language server can provide diagnostics for.
- pub fn documents(
- &self,
- open_only: bool,
- diagnosable_only: bool,
- ) -> Vec<Document> {
- if open_only {
- self
+ /// Return a collection of documents that are contained in the document store
+ /// based on the provided filter.
+ pub fn documents(&self, filter: DocumentsFilter) -> Vec<Document> {
+ match filter {
+ DocumentsFilter::OpenDiagnosable => self
.open_docs
.values()
.filter_map(|doc| {
- if !diagnosable_only || doc.is_diagnosable() {
+ if doc.is_diagnosable() {
Some(doc.clone())
} else {
None
}
})
- .collect()
- } else {
- // it is technically possible for a Document to end up in both the open
- // and closed documents so we need to ensure we don't return duplicates
- let mut seen_documents = HashSet::new();
- let file_system_docs = self.file_system_docs.lock();
- self
- .open_docs
- .values()
- .chain(file_system_docs.docs.values())
- .filter_map(|doc| {
- // this prefers the open documents
- if seen_documents.insert(doc.specifier().clone())
- && (!diagnosable_only || doc.is_diagnosable())
- {
- Some(doc.clone())
- } else {
- None
- }
- })
- .collect()
+ .collect(),
+ DocumentsFilter::AllDiagnosable | DocumentsFilter::All => {
+ let diagnosable_only =
+ matches!(filter, DocumentsFilter::AllDiagnosable);
+ // it is technically possible for a Document to end up in both the open
+ // and closed documents so we need to ensure we don't return duplicates
+ let mut seen_documents = HashSet::new();
+ let file_system_docs = self.file_system_docs.lock();
+ self
+ .open_docs
+ .values()
+ .chain(file_system_docs.docs.values())
+ .filter_map(|doc| {
+ // this prefers the open documents
+ if seen_documents.insert(doc.specifier().clone())
+ && (!diagnosable_only || doc.is_diagnosable())
+ {
+ Some(doc.clone())
+ } else {
+ None
+ }
+ })
+ .collect()
+ }
}
}
@@ -1592,7 +1600,7 @@ console.log(b, "hello deno");
// At this point the document will be in both documents and the shared file system documents.
// Now make sure that the original documents doesn't return both copies
- assert_eq!(documents.documents(false, false).len(), 1);
+ assert_eq!(documents.documents(DocumentsFilter::All).len(), 1);
}
#[test]