From 7125192c1e46f2350707c4903a1679b2a0178ea6 Mon Sep 17 00:00:00 2001 From: kjeremy Date: Tue, 23 Apr 2019 14:11:27 -0400 Subject: Basic resolution for ADT --- crates/ra_ide_api/src/goto_type_definition.rs | 68 ++++++++++++++++++++++++++ crates/ra_ide_api/src/lib.rs | 8 +++ crates/ra_lsp_server/src/caps.rs | 4 +- crates/ra_lsp_server/src/main_loop.rs | 1 + crates/ra_lsp_server/src/main_loop/handlers.rs | 20 ++++++++ 5 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 crates/ra_ide_api/src/goto_type_definition.rs diff --git a/crates/ra_ide_api/src/goto_type_definition.rs b/crates/ra_ide_api/src/goto_type_definition.rs new file mode 100644 index 000000000..b6a3c1c3a --- /dev/null +++ b/crates/ra_ide_api/src/goto_type_definition.rs @@ -0,0 +1,68 @@ +use ra_db::SourceDatabase; +use ra_syntax::{ + AstNode, ast, + algo::find_token_at_offset +}; + +use crate::{FilePosition, NavigationTarget, db::RootDatabase, RangeInfo}; + +pub(crate) fn goto_type_definition( + db: &RootDatabase, + position: FilePosition, +) -> Option>> { + let file = db.parse(position.file_id); + + let node = find_token_at_offset(file.syntax(), position.offset).find_map(|token| { + token + .parent() + .ancestors() + .find(|n| ast::Expr::cast(*n).is_some() || ast::Pat::cast(*n).is_some()) + })?; + + let analyzer = hir::SourceAnalyzer::new(db, position.file_id, node, None); + + let ty: hir::Ty = if let Some(ty) = ast::Expr::cast(node).and_then(|e| analyzer.type_of(db, e)) + { + ty + } else if let Some(ty) = ast::Pat::cast(node).and_then(|p| analyzer.type_of_pat(db, p)) { + ty + } else { + return None; + }; + + if let Some((adt_def, _)) = ty.as_adt() { + let nav = NavigationTarget::from_adt_def(db, adt_def); + return Some(RangeInfo::new(node.range(), vec![nav])); + }; + + None +} + +#[cfg(test)] +mod tests { + use crate::mock_analysis::analysis_and_position; + + fn check_goto(fixture: &str, expected: &str) { + let (analysis, pos) = analysis_and_position(fixture); + + let mut navs = analysis.goto_type_definition(pos).unwrap().unwrap().info; + assert_eq!(navs.len(), 1); + let nav = navs.pop().unwrap(); + nav.assert_match(expected); + } + + #[test] + fn goto_type_definition_works_simple() { + check_goto( + " + //- /lib.rs + struct Foo; + fn foo() { + let f: Foo; + f<|> + } + ", + "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + ); + } +} diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index d25795adc..d4be8bd6c 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -19,6 +19,7 @@ mod status; mod completion; mod runnables; mod goto_definition; +mod goto_type_definition; mod extend_selection; mod hover; mod call_info; @@ -416,6 +417,13 @@ impl Analysis { self.with_db(|db| impls::goto_implementation(db, position)) } + pub fn goto_type_definition( + &self, + position: FilePosition, + ) -> Cancelable>>> { + self.with_db(|db| goto_type_definition::goto_type_definition(db, position)) + } + /// Finds all usages of the reference at point. pub fn find_all_refs( &self, diff --git a/crates/ra_lsp_server/src/caps.rs b/crates/ra_lsp_server/src/caps.rs index f6d2b75e7..9095bee89 100644 --- a/crates/ra_lsp_server/src/caps.rs +++ b/crates/ra_lsp_server/src/caps.rs @@ -2,7 +2,7 @@ use lsp_types::{ CodeActionProviderCapability, CodeLensOptions, CompletionOptions, DocumentOnTypeFormattingOptions, ExecuteCommandOptions, FoldingRangeProviderCapability, RenameOptions, RenameProviderCapability, ServerCapabilities, SignatureHelpOptions, TextDocumentSyncCapability, TextDocumentSyncKind, - TextDocumentSyncOptions, ImplementationProviderCapability, GenericCapability, + TextDocumentSyncOptions, ImplementationProviderCapability, GenericCapability, TypeDefinitionProviderCapability }; pub fn server_capabilities() -> ServerCapabilities { @@ -23,7 +23,7 @@ pub fn server_capabilities() -> ServerCapabilities { trigger_characters: Some(vec!["(".to_string(), ",".to_string(), ")".to_string()]), }), definition_provider: Some(true), - type_definition_provider: None, + type_definition_provider: Some(TypeDefinitionProviderCapability::Simple(true)), implementation_provider: Some(ImplementationProviderCapability::Simple(true)), references_provider: Some(true), document_highlight_provider: Some(true), diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs index dc1f8f3f7..87b4e3ac2 100644 --- a/crates/ra_lsp_server/src/main_loop.rs +++ b/crates/ra_lsp_server/src/main_loop.rs @@ -306,6 +306,7 @@ fn on_request( .on::(handlers::handle_workspace_symbol)? .on::(handlers::handle_goto_definition)? .on::(handlers::handle_goto_implementation)? + .on::(handlers::handle_goto_type_definition)? .on::(handlers::handle_parent_module)? .on::(handlers::handle_runnables)? .on::(handlers::handle_decorations)? diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 530081494..23802e5e1 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -288,6 +288,26 @@ pub fn handle_goto_implementation( Ok(Some(req::GotoDefinitionResponse::Link(res))) } +pub fn handle_goto_type_definition( + world: ServerWorld, + params: req::TextDocumentPositionParams, +) -> Result> { + let position = params.try_conv_with(&world)?; + let line_index = world.analysis().file_line_index(position.file_id); + let nav_info = match world.analysis().goto_type_definition(position)? { + None => return Ok(None), + Some(it) => it, + }; + let nav_range = nav_info.range; + let res = nav_info + .info + .into_iter() + .map(|nav| RangeInfo::new(nav_range, nav)) + .map(|nav| to_location_link(&nav, &world, &line_index)) + .collect::>>()?; + Ok(Some(req::GotoDefinitionResponse::Link(res))) +} + pub fn handle_parent_module( world: ServerWorld, params: req::TextDocumentPositionParams, -- cgit v1.2.3 From 6ba7667f2bf82aa2fbbef3823567fed0819a3fb9 Mon Sep 17 00:00:00 2001 From: kjeremy Date: Tue, 23 Apr 2019 15:41:31 -0400 Subject: Add to LSP features --- docs/dev/lsp-features.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/dev/lsp-features.md b/docs/dev/lsp-features.md index 57015293c..28bae59bb 100644 --- a/docs/dev/lsp-features.md +++ b/docs/dev/lsp-features.md @@ -46,7 +46,7 @@ This list documents LSP features, supported by rust-analyzer. - trigger characters: `(`, `,`, `)` - [ ] [textDocument/declaration](https://microsoft.github.io/language-server-protocol/specification#textDocument_declaration) - [x] [textDocument/definition](https://microsoft.github.io/language-server-protocol/specification#textDocument_definition) -- [ ] [textDocument/typeDefinition](https://microsoft.github.io/language-server-protocol/specification#textDocument_typeDefinition) +- [x] [textDocument/typeDefinition](https://microsoft.github.io/language-server-protocol/specification#textDocument_typeDefinition) - [x] [textDocument/implementation](https://microsoft.github.io/language-server-protocol/specification#textDocument_implementation) - [x] [textDocument/references](https://microsoft.github.io/language-server-protocol/specification#textDocument_references) - [x] [textDocument/documentHighlight](https://microsoft.github.io/language-server-protocol/specification#textDocument_documentHighlight) -- cgit v1.2.3 From a5bf0cdb97d082a024dcd6704795b816d7815b5f Mon Sep 17 00:00:00 2001 From: kjeremy Date: Tue, 23 Apr 2019 15:41:43 -0400 Subject: Simplify --- crates/ra_ide_api/src/goto_type_definition.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/crates/ra_ide_api/src/goto_type_definition.rs b/crates/ra_ide_api/src/goto_type_definition.rs index b6a3c1c3a..de2b9d3c3 100644 --- a/crates/ra_ide_api/src/goto_type_definition.rs +++ b/crates/ra_ide_api/src/goto_type_definition.rs @@ -30,12 +30,10 @@ pub(crate) fn goto_type_definition( return None; }; - if let Some((adt_def, _)) = ty.as_adt() { - let nav = NavigationTarget::from_adt_def(db, adt_def); - return Some(RangeInfo::new(node.range(), vec![nav])); - }; + let (adt_def, _) = ty.as_adt()?; + let nav = NavigationTarget::from_adt_def(db, adt_def); - None + Some(RangeInfo::new(node.range(), vec![nav])) } #[cfg(test)] -- cgit v1.2.3 From 9d25e5dea4a24272bd891c68e02309cc69792a07 Mon Sep 17 00:00:00 2001 From: kjeremy Date: Tue, 23 Apr 2019 16:32:30 -0400 Subject: Add to features --- docs/user/features.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/user/features.md b/docs/user/features.md index b44a3fce1..cbfc491b2 100644 --- a/docs/user/features.md +++ b/docs/user/features.md @@ -42,6 +42,18 @@ is a relatively new feature of LSP: https://github.com/Microsoft/language-server-protocol/issues/613, check your editor's LSP library to see if this feature is supported. +### Go to Definition + +Navigates to the definition of an identifier. + +### Go to Implementation + +Navigates to the impl block of structs, enums or traits. Also implemented as a code lens. + +### Go to Type Defintion + +Navigates to the type of an identifier. + ### Commands ctrl+shift+p #### Run -- cgit v1.2.3