From 3aaf20bd6ec75a572b13d020520d4df563a2891c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 11 Jan 2019 14:14:09 +0300 Subject: return ref ranges from gotodef --- crates/ra_ide_api/src/goto_definition.rs | 10 ++++++---- crates/ra_ide_api/src/lib.rs | 4 ++-- crates/ra_lsp_server/src/conv.rs | 11 ++++++++++- crates/ra_lsp_server/src/main_loop/handlers.rs | 9 ++++++--- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index 4706dc733..3fb29950b 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -4,19 +4,21 @@ use ra_syntax::{ algo::find_node_at_offset, }; -use crate::{FilePosition, NavigationTarget, db::RootDatabase}; +use crate::{FilePosition, NavigationTarget, db::RootDatabase, RangeInfo}; pub(crate) fn goto_definition( db: &RootDatabase, position: FilePosition, -) -> Cancelable>> { +) -> Cancelable>>> { let file = db.source_file(position.file_id); let syntax = file.syntax(); if let Some(name_ref) = find_node_at_offset::(syntax, position.offset) { - return Ok(Some(reference_definition(db, position.file_id, name_ref)?)); + let navs = reference_definition(db, position.file_id, name_ref)?; + return Ok(Some(RangeInfo::new(name_ref.syntax().range(), navs))); } if let Some(name) = find_node_at_offset::(syntax, position.offset) { - return name_definition(db, position.file_id, name); + let navs = ctry!(name_definition(db, position.file_id, name)?); + return Ok(Some(RangeInfo::new(name.syntax().range(), navs))); } Ok(None) } diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 2873bab36..2b02dab2a 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -251,7 +251,7 @@ pub struct RangeInfo { } impl RangeInfo { - fn new(range: TextRange, info: T) -> RangeInfo { + pub fn new(range: TextRange, info: T) -> RangeInfo { RangeInfo { range, info } } } @@ -391,7 +391,7 @@ impl Analysis { pub fn goto_definition( &self, position: FilePosition, - ) -> Cancelable>> { + ) -> Cancelable>>> { self.db .catch_canceled(|db| goto_definition::goto_definition(db, position))? } diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 35c679a4a..0b9b18cbf 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -6,7 +6,7 @@ use languageserver_types::{ }; use ra_ide_api::{ CompletionItem, CompletionItemKind, FileId, FilePosition, FileRange, FileSystemEdit, - InsertText, NavigationTarget, SourceChange, SourceFileEdit, + InsertText, NavigationTarget, SourceChange, SourceFileEdit, RangeInfo, LineCol, LineIndex, translate_offset_with_edit }; use ra_syntax::{SyntaxKind, TextRange, TextUnit}; @@ -349,6 +349,15 @@ impl TryConvWith for &NavigationTarget { } } +impl TryConvWith for &RangeInfo { + type Ctx = ServerWorld; + type Output = Location; + fn try_conv_with(self, world: &ServerWorld) -> Result { + let line_index = world.analysis().file_line_index(self.info.file_id()); + to_location(self.info.file_id(), self.info.range(), &world, &line_index) + } +} + pub fn to_location( file_id: FileId, range: TextRange, diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 5f4b27149..e3bf55ae7 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -9,7 +9,7 @@ use languageserver_types::{ SignatureInformation, SymbolInformation, TextDocumentIdentifier, TextEdit, WorkspaceEdit, }; use ra_ide_api::{ - FileId, FilePosition, FileRange, FoldKind, Query, RunnableKind, Severity, + FileId, FilePosition, FileRange, FoldKind, Query, RunnableKind, Severity, SourceChange, RangeInfo, }; use ra_syntax::{TextUnit, AstNode}; use rustc_hash::FxHashMap; @@ -208,12 +208,15 @@ pub fn handle_goto_definition( params: req::TextDocumentPositionParams, ) -> Result> { let position = params.try_conv_with(&world)?; - let navs = match world.analysis().goto_definition(position)? { + let nav_info = match world.analysis().goto_definition(position)? { None => return Ok(None), Some(it) => it, }; - let res = navs + let nav_range = nav_info.range; + let res = nav_info + .info .into_iter() + .map(|nav| RangeInfo::new(nav_range, nav)) .map(|nav| nav.try_conv_with(&world)) .collect::>>()?; Ok(Some(req::GotoDefinitionResponse::Array(res))) -- cgit v1.2.3