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

refactor(lsp): remove boolean parameters on documents.documents(...) (#18493)

I think this makes things clearer at the call sites.
This commit is contained in:
David Sherret 2023-03-29 16:25:48 -04:00 committed by GitHub
parent bacbf94925
commit 89bbbd102c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 64 additions and 43 deletions

View file

@ -3,6 +3,7 @@
use super::client::Client; use super::client::Client;
use super::config::ConfigSnapshot; use super::config::ConfigSnapshot;
use super::documents::Documents; use super::documents::Documents;
use super::documents::DocumentsFilter;
use super::lsp_custom; use super::lsp_custom;
use super::registries::ModuleRegistry; use super::registries::ModuleRegistry;
use super::tsc; use super::tsc;
@ -278,7 +279,7 @@ fn get_import_map_completions(
if let Ok(resolved) = import_map.resolve(&key, specifier) { if let Ok(resolved) = import_map.resolve(&key, specifier) {
let resolved = resolved.to_string(); let resolved = resolved.to_string();
let workspace_items: Vec<lsp::CompletionItem> = documents let workspace_items: Vec<lsp::CompletionItem> = documents
.documents(false, true) .documents(DocumentsFilter::AllDiagnosable)
.into_iter() .into_iter()
.filter_map(|d| { .filter_map(|d| {
let specifier_str = d.specifier().to_string(); let specifier_str = d.specifier().to_string();
@ -460,7 +461,7 @@ fn get_workspace_completions(
documents: &Documents, documents: &Documents,
) -> Vec<lsp::CompletionItem> { ) -> Vec<lsp::CompletionItem> {
let workspace_specifiers = documents let workspace_specifiers = documents
.documents(false, true) .documents(DocumentsFilter::AllDiagnosable)
.into_iter() .into_iter()
.map(|d| d.specifier().clone()) .map(|d| d.specifier().clone())
.collect(); .collect();

View file

@ -6,6 +6,7 @@ use super::client::Client;
use super::config::ConfigSnapshot; use super::config::ConfigSnapshot;
use super::documents; use super::documents;
use super::documents::Document; use super::documents::Document;
use super::documents::DocumentsFilter;
use super::language_server; use super::language_server;
use super::language_server::StateSnapshot; use super::language_server::StateSnapshot;
use super::performance::Performance; use super::performance::Performance;
@ -454,7 +455,9 @@ async fn generate_lint_diagnostics(
lint_options: &LintOptions, lint_options: &LintOptions,
token: CancellationToken, token: CancellationToken,
) -> DiagnosticVec { ) -> DiagnosticVec {
let documents = snapshot.documents.documents(true, true); let documents = snapshot
.documents
.documents(DocumentsFilter::OpenDiagnosable);
let workspace_settings = config.settings.workspace.clone(); let workspace_settings = config.settings.workspace.clone();
let lint_rules = get_configured_rules(lint_options.rules.clone()); let lint_rules = get_configured_rules(lint_options.rules.clone());
let mut diagnostics_vec = Vec::new(); let mut diagnostics_vec = Vec::new();
@ -530,7 +533,7 @@ async fn generate_ts_diagnostics(
let mut diagnostics_vec = Vec::new(); let mut diagnostics_vec = Vec::new();
let specifiers = snapshot let specifiers = snapshot
.documents .documents
.documents(true, true) .documents(DocumentsFilter::OpenDiagnosable)
.into_iter() .into_iter()
.map(|d| d.specifier().clone()); .map(|d| d.specifier().clone());
let (enabled_specifiers, disabled_specifiers) = specifiers let (enabled_specifiers, disabled_specifiers) = specifiers
@ -1025,7 +1028,10 @@ async fn generate_deno_diagnostics(
) -> DiagnosticVec { ) -> DiagnosticVec {
let mut diagnostics_vec = Vec::new(); let mut diagnostics_vec = Vec::new();
for document in snapshot.documents.documents(true, true) { for document in snapshot
.documents
.documents(DocumentsFilter::OpenDiagnosable)
{
if token.is_cancelled() { if token.is_cancelled() {
break; break;
} }

View file

@ -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)] #[derive(Debug, Clone, Default)]
pub struct Documents { pub struct Documents {
/// The DENO_DIR that the documents looks for non-file based modules. /// 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, /// Return a collection of documents that are contained in the document store
/// where `open_only` flag would provide only those documents currently open /// based on the provided filter.
/// in the editor and `diagnosable_only` would provide only those documents pub fn documents(&self, filter: DocumentsFilter) -> Vec<Document> {
/// that the language server can provide diagnostics for. match filter {
pub fn documents( DocumentsFilter::OpenDiagnosable => self
&self,
open_only: bool,
diagnosable_only: bool,
) -> Vec<Document> {
if open_only {
self
.open_docs .open_docs
.values() .values()
.filter_map(|doc| { .filter_map(|doc| {
if !diagnosable_only || doc.is_diagnosable() { if doc.is_diagnosable() {
Some(doc.clone()) Some(doc.clone())
} else { } else {
None None
} }
}) })
.collect() .collect(),
} else { DocumentsFilter::AllDiagnosable | DocumentsFilter::All => {
// it is technically possible for a Document to end up in both the open let diagnosable_only =
// and closed documents so we need to ensure we don't return duplicates matches!(filter, DocumentsFilter::AllDiagnosable);
let mut seen_documents = HashSet::new(); // it is technically possible for a Document to end up in both the open
let file_system_docs = self.file_system_docs.lock(); // and closed documents so we need to ensure we don't return duplicates
self let mut seen_documents = HashSet::new();
.open_docs let file_system_docs = self.file_system_docs.lock();
.values() self
.chain(file_system_docs.docs.values()) .open_docs
.filter_map(|doc| { .values()
// this prefers the open documents .chain(file_system_docs.docs.values())
if seen_documents.insert(doc.specifier().clone()) .filter_map(|doc| {
&& (!diagnosable_only || doc.is_diagnosable()) // this prefers the open documents
{ if seen_documents.insert(doc.specifier().clone())
Some(doc.clone()) && (!diagnosable_only || doc.is_diagnosable())
} else { {
None Some(doc.clone())
} } else {
}) None
.collect() }
})
.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. // 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 // 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] #[test]

View file

@ -44,6 +44,7 @@ use super::documents::to_lsp_range;
use super::documents::AssetOrDocument; use super::documents::AssetOrDocument;
use super::documents::Document; use super::documents::Document;
use super::documents::Documents; use super::documents::Documents;
use super::documents::DocumentsFilter;
use super::documents::LanguageId; use super::documents::LanguageId;
use super::logging::lsp_log; use super::logging::lsp_log;
use super::lsp_custom; use super::lsp_custom;
@ -3223,7 +3224,7 @@ impl Inner {
)?; )?;
cli_options.set_import_map_specifier(self.maybe_import_map_uri.clone()); cli_options.set_import_map_specifier(self.maybe_import_map_uri.clone());
let open_docs = self.documents.documents(true, true); let open_docs = self.documents.documents(DocumentsFilter::OpenDiagnosable);
Ok(Some(PrepareCacheResult { Ok(Some(PrepareCacheResult {
cli_options, cli_options,
open_docs, open_docs,
@ -3341,7 +3342,7 @@ impl Inner {
let mut contents = String::new(); let mut contents = String::new();
let mut documents_specifiers = self let mut documents_specifiers = self
.documents .documents
.documents(false, false) .documents(DocumentsFilter::All)
.into_iter() .into_iter()
.map(|d| d.specifier().clone()) .map(|d| d.specifier().clone())
.collect::<Vec<_>>(); .collect::<Vec<_>>();

View file

@ -8,6 +8,7 @@ use super::lsp_custom;
use crate::lsp::client::Client; use crate::lsp::client::Client;
use crate::lsp::client::TestingNotification; use crate::lsp::client::TestingNotification;
use crate::lsp::config; use crate::lsp::config;
use crate::lsp::documents::DocumentsFilter;
use crate::lsp::language_server::StateSnapshot; use crate::lsp::language_server::StateSnapshot;
use crate::lsp::performance::Performance; use crate::lsp::performance::Performance;
@ -92,7 +93,10 @@ impl TestServer {
// eliminating any we go over when iterating over the document // eliminating any we go over when iterating over the document
let mut keys: HashSet<ModuleSpecifier> = let mut keys: HashSet<ModuleSpecifier> =
tests.keys().cloned().collect(); tests.keys().cloned().collect();
for document in snapshot.documents.documents(false, true) { for document in snapshot
.documents
.documents(DocumentsFilter::AllDiagnosable)
{
let specifier = document.specifier(); let specifier = document.specifier();
keys.remove(specifier); keys.remove(specifier);
let script_version = document.script_version(); let script_version = document.script_version();

View file

@ -3,6 +3,7 @@
use super::code_lens; use super::code_lens;
use super::config; use super::config;
use super::documents::AssetOrDocument; use super::documents::AssetOrDocument;
use super::documents::DocumentsFilter;
use super::language_server; use super::language_server;
use super::language_server::StateSnapshot; use super::language_server::StateSnapshot;
use super::performance::Performance; use super::performance::Performance;
@ -2760,7 +2761,7 @@ fn op_respond(state: &mut OpState, args: Response) -> bool {
fn op_script_names(state: &mut OpState) -> Vec<String> { fn op_script_names(state: &mut OpState) -> Vec<String> {
let state = state.borrow_mut::<State>(); let state = state.borrow_mut::<State>();
let documents = &state.state_snapshot.documents; let documents = &state.state_snapshot.documents;
let open_docs = documents.documents(true, true); let open_docs = documents.documents(DocumentsFilter::OpenDiagnosable);
let mut result = Vec::new(); let mut result = Vec::new();
let mut seen = HashSet::new(); let mut seen = HashSet::new();