diff options
author | Kitson Kelly <me@kitsonkelly.com> | 2021-02-01 14:30:41 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-01 14:30:41 +1100 |
commit | 534531e4dd4133ff22f775b385ba488ec747cead (patch) | |
tree | a3f4fcd57ab661acb3b05f4c54ad80af42a86b22 /cli/lsp/tsc.rs | |
parent | 46d5843f753548415c87f3c8a868bba49c203b92 (diff) |
feat(lsp): add references code lens (#9316)
Diffstat (limited to 'cli/lsp/tsc.rs')
-rw-r--r-- | cli/lsp/tsc.rs | 169 |
1 files changed, 115 insertions, 54 deletions
diff --git a/cli/lsp/tsc.rs b/cli/lsp/tsc.rs index ce9f31e68..3fee900c6 100644 --- a/cli/lsp/tsc.rs +++ b/cli/lsp/tsc.rs @@ -1,5 +1,6 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. +use super::analysis::CodeLensSource; use super::analysis::ResolvedDependency; use super::language_server::StateSnapshot; use super::text; @@ -241,7 +242,7 @@ fn replace_links(text: &str) -> String { .to_string() } -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, PartialEq, Eq)] pub enum ScriptElementKind { #[serde(rename = "")] Unknown, @@ -356,8 +357,8 @@ impl From<ScriptElementKind> for lsp::CompletionItemKind { #[derive(Debug, Clone, Deserialize)] #[serde(rename_all = "camelCase")] pub struct TextSpan { - start: u32, - length: u32, + pub start: u32, + pub length: u32, } impl TextSpan { @@ -480,6 +481,59 @@ impl DocumentSpan { } } +#[derive(Debug, Clone, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct NavigationTree { + pub text: String, + pub kind: ScriptElementKind, + pub kind_modifiers: String, + pub spans: Vec<TextSpan>, + pub name_span: Option<TextSpan>, + pub child_items: Option<Vec<NavigationTree>>, +} + +impl NavigationTree { + pub fn to_code_lens( + &self, + line_index: &LineIndex, + specifier: &ModuleSpecifier, + source: &CodeLensSource, + ) -> lsp::CodeLens { + lsp::CodeLens { + range: self.name_span.clone().unwrap().to_range(line_index), + command: None, + data: Some(json!({ + "specifier": specifier, + "source": source + })), + } + } + + pub fn walk<F>(&self, callback: &F) + where + F: Fn(&NavigationTree, Option<&NavigationTree>), + { + callback(self, None); + if let Some(child_items) = &self.child_items { + for child in child_items { + child.walk_child(callback, self); + } + } + } + + fn walk_child<F>(&self, callback: &F, parent: &NavigationTree) + where + F: Fn(&NavigationTree, Option<&NavigationTree>), + { + callback(self, Some(parent)); + if let Some(child_items) = &self.child_items { + for child in child_items { + child.walk_child(callback, self); + } + } + } +} + #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct ImplementationLocation { @@ -1157,24 +1211,26 @@ pub struct UserPreferences { pub enum RequestMethod { /// Configure the compilation settings for the server. Configure(TsConfig), + /// Get rename locations at a given position. + FindRenameLocations((ModuleSpecifier, u32, bool, bool, bool)), /// Retrieve the text of an assets that exists in memory in the isolate. GetAsset(ModuleSpecifier), + /// Get completion information at a given position (IntelliSense). + GetCompletions((ModuleSpecifier, u32, UserPreferences)), + /// Get declaration information for a specific position. + GetDefinition((ModuleSpecifier, u32)), /// Return diagnostics for given file. GetDiagnostics(Vec<ModuleSpecifier>), - /// Return quick info at position (hover information). - GetQuickInfo((ModuleSpecifier, u32)), /// Return document highlights at position. GetDocumentHighlights((ModuleSpecifier, u32, Vec<ModuleSpecifier>)), - /// Get document references for a specific position. - GetReferences((ModuleSpecifier, u32)), - /// Get declaration information for a specific position. - GetDefinition((ModuleSpecifier, u32)), - /// Get completion information at a given position (IntelliSense). - GetCompletions((ModuleSpecifier, u32, UserPreferences)), /// Get implementation information for a specific position. GetImplementation((ModuleSpecifier, u32)), - /// Get rename locations at a given position. - FindRenameLocations((ModuleSpecifier, u32, bool, bool, bool)), + /// Get a "navigation tree" for a specifier. + GetNavigationTree(ModuleSpecifier), + /// Return quick info at position (hover information). + GetQuickInfo((ModuleSpecifier, u32)), + /// Get document references for a specific position. + GetReferences((ModuleSpecifier, u32)), } impl RequestMethod { @@ -1185,22 +1241,48 @@ impl RequestMethod { "method": "configure", "compilerOptions": config, }), + RequestMethod::FindRenameLocations(( + specifier, + position, + find_in_strings, + find_in_comments, + provide_prefix_and_suffix_text_for_rename, + )) => { + json!({ + "id": id, + "method": "findRenameLocations", + "specifier": specifier, + "position": position, + "findInStrings": find_in_strings, + "findInComments": find_in_comments, + "providePrefixAndSuffixTextForRename": provide_prefix_and_suffix_text_for_rename + }) + } RequestMethod::GetAsset(specifier) => json!({ "id": id, "method": "getAsset", "specifier": specifier, }), + RequestMethod::GetCompletions((specifier, position, preferences)) => { + json!({ + "id": id, + "method": "getCompletions", + "specifier": specifier, + "position": position, + "preferences": preferences, + }) + } + RequestMethod::GetDefinition((specifier, position)) => json!({ + "id": id, + "method": "getDefinition", + "specifier": specifier, + "position": position, + }), RequestMethod::GetDiagnostics(specifiers) => json!({ "id": id, "method": "getDiagnostics", "specifiers": specifiers, }), - RequestMethod::GetQuickInfo((specifier, position)) => json!({ - "id": id, - "method": "getQuickInfo", - "specifier": specifier, - "position": position, - }), RequestMethod::GetDocumentHighlights(( specifier, position, @@ -1212,50 +1294,29 @@ impl RequestMethod { "position": position, "filesToSearch": files_to_search, }), - RequestMethod::GetReferences((specifier, position)) => json!({ + RequestMethod::GetImplementation((specifier, position)) => json!({ "id": id, - "method": "getReferences", + "method": "getImplementation", "specifier": specifier, "position": position, }), - RequestMethod::GetDefinition((specifier, position)) => json!({ + RequestMethod::GetNavigationTree(specifier) => json!({ "id": id, - "method": "getDefinition", + "method": "getNavigationTree", + "specifier": specifier, + }), + RequestMethod::GetQuickInfo((specifier, position)) => json!({ + "id": id, + "method": "getQuickInfo", "specifier": specifier, "position": position, }), - RequestMethod::GetCompletions((specifier, position, preferences)) => { - json!({ - "id": id, - "method": "getCompletions", - "specifier": specifier, - "position": position, - "preferences": preferences, - }) - } - RequestMethod::GetImplementation((specifier, position)) => json!({ - "id": id, - "method": "getImplementation", - "specifier": specifier, - "position": position, + RequestMethod::GetReferences((specifier, position)) => json!({ + "id": id, + "method": "getReferences", + "specifier": specifier, + "position": position, }), - RequestMethod::FindRenameLocations(( - specifier, - position, - find_in_strings, - find_in_comments, - provide_prefix_and_suffix_text_for_rename, - )) => { - json!({ - "id": id, - "method": "findRenameLocations", - "specifier": specifier, - "position": position, - "findInStrings": find_in_strings, - "findInComments": find_in_comments, - "providePrefixAndSuffixTextForRename": provide_prefix_and_suffix_text_for_rename - }) - } } } } |