From 830abe0c1b9fdbc20a78771d0b3df37d9eabdc3e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 2 Jan 2019 17:09:39 +0300 Subject: use navigation target in API --- crates/ra_lsp_server/src/conv.rs | 11 ++++++++- crates/ra_lsp_server/src/main_loop/handlers.rs | 33 ++++++++++++-------------- 2 files changed, 25 insertions(+), 19 deletions(-) (limited to 'crates/ra_lsp_server/src') diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 618486250..1107ffc8b 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -2,7 +2,7 @@ use languageserver_types::{ self, Location, Position, Range, SymbolKind, TextDocumentEdit, TextDocumentIdentifier, TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, InsertTextFormat, }; -use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition,FileRange, CompletionItem, CompletionItemKind, InsertText}; +use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition,FileRange, CompletionItem, CompletionItemKind, InsertText, NavigationTarget}; use ra_editor::{LineCol, LineIndex, translate_offset_with_edit}; use ra_text_edit::{AtomTextEdit, TextEdit}; use ra_syntax::{SyntaxKind, TextRange, TextUnit}; @@ -322,6 +322,15 @@ impl TryConvWith for FileSystemEdit { } } +impl TryConvWith for &NavigationTarget { + type Ctx = ServerWorld; + type Output = Location; + fn try_conv_with(self, world: &ServerWorld) -> Result { + let line_index = world.analysis().file_line_index(self.file_id()); + to_location(self.file_id(), self.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 5ff6219f9..26b6c7d8a 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -188,12 +188,11 @@ pub fn handle_workspace_symbol( fn exec_query(world: &ServerWorld, query: Query) -> Result> { let mut res = Vec::new(); - for (file_id, symbol) in world.analysis().symbol_search(query)? { - let line_index = world.analysis().file_line_index(file_id); + for nav in world.analysis().symbol_search(query)? { let info = SymbolInformation { - name: symbol.name.to_string(), - kind: symbol.kind.conv(), - location: to_location(file_id, symbol.node_range, world, &line_index)?, + name: nav.name().into(), + kind: nav.kind().conv(), + location: nav.try_conv_with(world)?, container_name: None, deprecated: None, }; @@ -212,12 +211,11 @@ pub fn handle_goto_definition( None => return Ok(None), Some(it) => it, }; - let mut res = Vec::new(); - for nav in rr.resolves_to { - let line_index = world.analysis().file_line_index(nav.file_id()); - let location = to_location(nav.file_id(), nav.range(), &world, &line_index)?; - res.push(location) - } + let res = rr + .resolves_to + .into_iter() + .map(|nav| nav.try_conv_with(&world)) + .collect::>>()?; Ok(Some(req::GotoDefinitionResponse::Array(res))) } @@ -226,13 +224,12 @@ pub fn handle_parent_module( params: req::TextDocumentPositionParams, ) -> Result> { let position = params.try_conv_with(&world)?; - let mut res = Vec::new(); - for (file_id, symbol) in world.analysis().parent_module(position)? { - let line_index = world.analysis().file_line_index(file_id); - let location = to_location(file_id, symbol.node_range, &world, &line_index)?; - res.push(location); - } - Ok(res) + world + .analysis() + .parent_module(position)? + .into_iter() + .map(|nav| nav.try_conv_with(&world)) + .collect::>>() } pub fn handle_runnables( -- cgit v1.2.3