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_lsp_server/src/conv.rs | 11 ++++++++++- crates/ra_lsp_server/src/main_loop/handlers.rs | 9 ++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) (limited to 'crates/ra_lsp_server') 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 From 8a5f74a24f726a839f3a0e154cfadec23040e14c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 11 Jan 2019 16:01:48 +0300 Subject: use location link in goto def --- crates/ra_lsp_server/src/conv.rs | 28 ++++++++++++++++++-------- crates/ra_lsp_server/src/main_loop/handlers.rs | 9 +++++---- 2 files changed, 25 insertions(+), 12 deletions(-) (limited to 'crates/ra_lsp_server') diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 0b9b18cbf..aad698da1 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -1,5 +1,5 @@ use languageserver_types::{ - self, CreateFile, DocumentChangeOperation, DocumentChanges, InsertTextFormat, Location, + self, CreateFile, DocumentChangeOperation, DocumentChanges, InsertTextFormat, Location, LocationLink, Position, Range, RenameFile, ResourceOp, SymbolKind, TextDocumentEdit, TextDocumentIdentifier, TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, WorkspaceEdit, @@ -349,13 +349,25 @@ 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_link( + target: &RangeInfo, + world: &ServerWorld, + // line index for original range file + line_index: &LineIndex, +) -> Result { + let url = target.info.file_id().try_conv_with(world)?; + let tgt_line_index = world.analysis().file_line_index(target.info.file_id()); + + let res = LocationLink { + origin_selection_range: Some(target.range.conv_with(line_index)), + target_uri: url.to_string(), + target_range: target.info.range().conv_with(&tgt_line_index), + target_selection_range: target + .info + .focus_range() + .map(|it| it.conv_with(&tgt_line_index)), + }; + Ok(res) } pub fn to_location( diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index e3bf55ae7..aad9d6568 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, SourceChange, RangeInfo, + FileId, FilePosition, FileRange, FoldKind, Query, RunnableKind, Severity, RangeInfo, }; use ra_syntax::{TextUnit, AstNode}; use rustc_hash::FxHashMap; @@ -17,7 +17,7 @@ use serde_json::to_value; use std::io::Write; use crate::{ - conv::{to_location, Conv, ConvWith, MapConvWith, TryConvWith}, + conv::{to_location, to_location_link, Conv, ConvWith, MapConvWith, TryConvWith}, project_model::TargetKind, req::{self, Decoration}, server_world::ServerWorld, @@ -208,6 +208,7 @@ pub fn handle_goto_definition( 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_definition(position)? { None => return Ok(None), Some(it) => it, @@ -217,9 +218,9 @@ pub fn handle_goto_definition( .info .into_iter() .map(|nav| RangeInfo::new(nav_range, nav)) - .map(|nav| nav.try_conv_with(&world)) + .map(|nav| to_location_link(&nav, &world, &line_index)) .collect::>>()?; - Ok(Some(req::GotoDefinitionResponse::Array(res))) + Ok(Some(req::GotoDefinitionResponse::Link(res))) } pub fn handle_parent_module( -- cgit v1.2.3 From dda916bc4d51383fcf84f736bd12c7a77c445fb0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 11 Jan 2019 18:17:20 +0300 Subject: fix tests --- crates/ra_lsp_server/src/conv.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'crates/ra_lsp_server') diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index aad698da1..76fa98cbe 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -345,7 +345,8 @@ impl TryConvWith for &NavigationTarget { 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) + let range = self.focus_range().unwrap_or(self.full_range()); + to_location(self.file_id(), range, &world, &line_index) } } @@ -361,7 +362,7 @@ pub fn to_location_link( let res = LocationLink { origin_selection_range: Some(target.range.conv_with(line_index)), target_uri: url.to_string(), - target_range: target.info.range().conv_with(&tgt_line_index), + target_range: target.info.full_range().conv_with(&tgt_line_index), target_selection_range: target .info .focus_range() -- cgit v1.2.3