mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 04:51:22 +00:00
perf(lsp): avoid redundant getNavigationTree() calls (#21396)
This commit is contained in:
parent
c803c0eaab
commit
91626bac49
@ -1,8 +1,7 @@
|
||||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use super::analysis::source_range_to_lsp_range;
|
||||
use super::config::Config;
|
||||
use super::config::WorkspaceSettings;
|
||||
use super::config::CodeLensSettings;
|
||||
use super::language_server;
|
||||
use super::text::LineIndex;
|
||||
use super::tsc;
|
||||
@ -381,63 +380,29 @@ pub async fn resolve_code_lens(
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn collect(
|
||||
pub fn collect_test(
|
||||
specifier: &ModuleSpecifier,
|
||||
parsed_source: Option<ParsedSource>,
|
||||
config: &Config,
|
||||
line_index: Arc<LineIndex>,
|
||||
navigation_tree: &NavigationTree,
|
||||
parsed_source: ParsedSource,
|
||||
) -> Result<Vec<lsp::CodeLens>, AnyError> {
|
||||
let mut code_lenses = collect_test(specifier, parsed_source, config)?;
|
||||
code_lenses.extend(
|
||||
collect_tsc(
|
||||
specifier,
|
||||
config.workspace_settings(),
|
||||
line_index,
|
||||
navigation_tree,
|
||||
)
|
||||
.await?,
|
||||
);
|
||||
|
||||
Ok(code_lenses)
|
||||
}
|
||||
|
||||
fn collect_test(
|
||||
specifier: &ModuleSpecifier,
|
||||
parsed_source: Option<ParsedSource>,
|
||||
config: &Config,
|
||||
) -> Result<Vec<lsp::CodeLens>, AnyError> {
|
||||
if config.specifier_enabled_for_test(specifier)
|
||||
&& config.enabled_code_lens_test_for_specifier(specifier)
|
||||
{
|
||||
if let Some(parsed_source) = parsed_source {
|
||||
let mut collector =
|
||||
DenoTestCollector::new(specifier.clone(), parsed_source.clone());
|
||||
parsed_source.module().visit_with(&mut collector);
|
||||
return Ok(collector.take());
|
||||
}
|
||||
}
|
||||
Ok(Vec::new())
|
||||
let mut collector =
|
||||
DenoTestCollector::new(specifier.clone(), parsed_source.clone());
|
||||
parsed_source.module().visit_with(&mut collector);
|
||||
Ok(collector.take())
|
||||
}
|
||||
|
||||
/// Return tsc navigation tree code lenses.
|
||||
async fn collect_tsc(
|
||||
pub async fn collect_tsc(
|
||||
specifier: &ModuleSpecifier,
|
||||
workspace_settings: &WorkspaceSettings,
|
||||
code_lens_settings: &CodeLensSettings,
|
||||
line_index: Arc<LineIndex>,
|
||||
navigation_tree: &NavigationTree,
|
||||
) -> Result<Vec<lsp::CodeLens>, AnyError> {
|
||||
if !workspace_settings.code_lens.implementations
|
||||
&& !workspace_settings.code_lens.references
|
||||
{
|
||||
return Ok(vec![]);
|
||||
}
|
||||
let code_lenses = Rc::new(RefCell::new(Vec::new()));
|
||||
navigation_tree.walk(&|i, mp| {
|
||||
let mut code_lenses = code_lenses.borrow_mut();
|
||||
|
||||
// TSC Implementations Code Lens
|
||||
if workspace_settings.code_lens.implementations {
|
||||
if code_lens_settings.implementations {
|
||||
let source = CodeLensSource::Implementations;
|
||||
match i.kind {
|
||||
tsc::ScriptElementKind::InterfaceElement => {
|
||||
@ -465,7 +430,7 @@ async fn collect_tsc(
|
||||
}
|
||||
|
||||
// TSC References Code Lens
|
||||
if workspace_settings.code_lens.references {
|
||||
if code_lens_settings.references {
|
||||
let source = CodeLensSource::References;
|
||||
if let Some(parent) = &mp {
|
||||
if parent.kind == tsc::ScriptElementKind::EnumElement {
|
||||
@ -478,7 +443,7 @@ async fn collect_tsc(
|
||||
}
|
||||
match i.kind {
|
||||
tsc::ScriptElementKind::FunctionElement => {
|
||||
if workspace_settings.code_lens.references_all_functions {
|
||||
if code_lens_settings.references_all_functions {
|
||||
code_lenses.push(i.to_code_lens(
|
||||
line_index.clone(),
|
||||
specifier,
|
||||
|
@ -898,26 +898,6 @@ impl Config {
|
||||
|| settings.inlay_hints.enum_member_values.enabled
|
||||
}
|
||||
|
||||
/// Determine if any code lenses are enabled at all. This allows short
|
||||
/// circuiting when there are no code lenses enabled.
|
||||
pub fn enabled_code_lens_for_specifier(
|
||||
&self,
|
||||
specifier: &ModuleSpecifier,
|
||||
) -> bool {
|
||||
let settings = self.workspace_settings_for_specifier(specifier);
|
||||
settings.code_lens.implementations
|
||||
|| settings.code_lens.references
|
||||
|| settings.code_lens.test
|
||||
}
|
||||
|
||||
pub fn enabled_code_lens_test_for_specifier(
|
||||
&self,
|
||||
specifier: &ModuleSpecifier,
|
||||
) -> bool {
|
||||
let settings = self.workspace_settings_for_specifier(specifier);
|
||||
settings.code_lens.test
|
||||
}
|
||||
|
||||
pub fn root_uri(&self) -> Option<&Url> {
|
||||
self.workspace_folders.get(0).map(|p| &p.0)
|
||||
}
|
||||
|
@ -2147,34 +2147,60 @@ impl Inner {
|
||||
.normalize_url(¶ms.text_document.uri, LspUrlKind::File);
|
||||
if !self.is_diagnosable(&specifier)
|
||||
|| !self.config.specifier_enabled(&specifier)
|
||||
|| !self.config.enabled_code_lens_for_specifier(&specifier)
|
||||
{
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let mark = self.performance.mark("code_lens", Some(¶ms));
|
||||
let asset_or_doc = self.get_asset_or_document(&specifier)?;
|
||||
let navigation_tree =
|
||||
self.get_navigation_tree(&specifier).await.map_err(|err| {
|
||||
error!("Error getting code lenses for \"{}\": {}", specifier, err);
|
||||
LspError::internal_error()
|
||||
})?;
|
||||
let parsed_source = asset_or_doc.maybe_parsed_source().and_then(|r| r.ok());
|
||||
let line_index = asset_or_doc.line_index();
|
||||
let code_lenses = code_lens::collect(
|
||||
&specifier,
|
||||
parsed_source,
|
||||
&self.config,
|
||||
line_index,
|
||||
&navigation_tree,
|
||||
)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
error!("Error getting code lenses for \"{}\": {}", specifier, err);
|
||||
LspError::internal_error()
|
||||
})?;
|
||||
let settings = self.config.workspace_settings_for_specifier(&specifier);
|
||||
let mut code_lenses = Vec::new();
|
||||
if settings.code_lens.test
|
||||
&& self.config.specifier_enabled_for_test(&specifier)
|
||||
{
|
||||
if let Some(Ok(parsed_source)) = asset_or_doc.maybe_parsed_source() {
|
||||
code_lenses.extend(
|
||||
code_lens::collect_test(&specifier, parsed_source).map_err(
|
||||
|err| {
|
||||
error!(
|
||||
"Error getting test code lenses for \"{}\": {}",
|
||||
&specifier, err
|
||||
);
|
||||
LspError::internal_error()
|
||||
},
|
||||
)?,
|
||||
);
|
||||
}
|
||||
}
|
||||
if settings.code_lens.implementations || settings.code_lens.references {
|
||||
let navigation_tree =
|
||||
self.get_navigation_tree(&specifier).await.map_err(|err| {
|
||||
error!("Error getting code lenses for \"{}\": {}", specifier, err);
|
||||
LspError::internal_error()
|
||||
})?;
|
||||
let line_index = asset_or_doc.line_index();
|
||||
code_lenses.extend(
|
||||
code_lens::collect_tsc(
|
||||
&specifier,
|
||||
&settings.code_lens,
|
||||
line_index,
|
||||
&navigation_tree,
|
||||
)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
error!(
|
||||
"Error getting ts code lenses for \"{}\": {}",
|
||||
&specifier, err
|
||||
);
|
||||
LspError::internal_error()
|
||||
})?,
|
||||
);
|
||||
}
|
||||
self.performance.measure(mark);
|
||||
|
||||
if code_lenses.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
Ok(Some(code_lenses))
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user