mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
return ref ranges from gotodef
This commit is contained in:
parent
f9ed8d4d23
commit
3aaf20bd6e
4 changed files with 24 additions and 10 deletions
|
@ -4,19 +4,21 @@ use ra_syntax::{
|
||||||
algo::find_node_at_offset,
|
algo::find_node_at_offset,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{FilePosition, NavigationTarget, db::RootDatabase};
|
use crate::{FilePosition, NavigationTarget, db::RootDatabase, RangeInfo};
|
||||||
|
|
||||||
pub(crate) fn goto_definition(
|
pub(crate) fn goto_definition(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
) -> Cancelable<Option<Vec<NavigationTarget>>> {
|
) -> Cancelable<Option<RangeInfo<Vec<NavigationTarget>>>> {
|
||||||
let file = db.source_file(position.file_id);
|
let file = db.source_file(position.file_id);
|
||||||
let syntax = file.syntax();
|
let syntax = file.syntax();
|
||||||
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, position.offset) {
|
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(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::<ast::Name>(syntax, position.offset) {
|
if let Some(name) = find_node_at_offset::<ast::Name>(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)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
|
@ -251,7 +251,7 @@ pub struct RangeInfo<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> RangeInfo<T> {
|
impl<T> RangeInfo<T> {
|
||||||
fn new(range: TextRange, info: T) -> RangeInfo<T> {
|
pub fn new(range: TextRange, info: T) -> RangeInfo<T> {
|
||||||
RangeInfo { range, info }
|
RangeInfo { range, info }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -391,7 +391,7 @@ impl Analysis {
|
||||||
pub fn goto_definition(
|
pub fn goto_definition(
|
||||||
&self,
|
&self,
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
) -> Cancelable<Option<Vec<NavigationTarget>>> {
|
) -> Cancelable<Option<RangeInfo<Vec<NavigationTarget>>>> {
|
||||||
self.db
|
self.db
|
||||||
.catch_canceled(|db| goto_definition::goto_definition(db, position))?
|
.catch_canceled(|db| goto_definition::goto_definition(db, position))?
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ use languageserver_types::{
|
||||||
};
|
};
|
||||||
use ra_ide_api::{
|
use ra_ide_api::{
|
||||||
CompletionItem, CompletionItemKind, FileId, FilePosition, FileRange, FileSystemEdit,
|
CompletionItem, CompletionItemKind, FileId, FilePosition, FileRange, FileSystemEdit,
|
||||||
InsertText, NavigationTarget, SourceChange, SourceFileEdit,
|
InsertText, NavigationTarget, SourceChange, SourceFileEdit, RangeInfo,
|
||||||
LineCol, LineIndex, translate_offset_with_edit
|
LineCol, LineIndex, translate_offset_with_edit
|
||||||
};
|
};
|
||||||
use ra_syntax::{SyntaxKind, TextRange, TextUnit};
|
use ra_syntax::{SyntaxKind, TextRange, TextUnit};
|
||||||
|
@ -349,6 +349,15 @@ impl TryConvWith for &NavigationTarget {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TryConvWith for &RangeInfo<NavigationTarget> {
|
||||||
|
type Ctx = ServerWorld;
|
||||||
|
type Output = Location;
|
||||||
|
fn try_conv_with(self, world: &ServerWorld) -> Result<Location> {
|
||||||
|
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(
|
pub fn to_location(
|
||||||
file_id: FileId,
|
file_id: FileId,
|
||||||
range: TextRange,
|
range: TextRange,
|
||||||
|
|
|
@ -9,7 +9,7 @@ use languageserver_types::{
|
||||||
SignatureInformation, SymbolInformation, TextDocumentIdentifier, TextEdit, WorkspaceEdit,
|
SignatureInformation, SymbolInformation, TextDocumentIdentifier, TextEdit, WorkspaceEdit,
|
||||||
};
|
};
|
||||||
use ra_ide_api::{
|
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 ra_syntax::{TextUnit, AstNode};
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
@ -208,12 +208,15 @@ pub fn handle_goto_definition(
|
||||||
params: req::TextDocumentPositionParams,
|
params: req::TextDocumentPositionParams,
|
||||||
) -> Result<Option<req::GotoDefinitionResponse>> {
|
) -> Result<Option<req::GotoDefinitionResponse>> {
|
||||||
let position = params.try_conv_with(&world)?;
|
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),
|
None => return Ok(None),
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
};
|
};
|
||||||
let res = navs
|
let nav_range = nav_info.range;
|
||||||
|
let res = nav_info
|
||||||
|
.info
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
.map(|nav| RangeInfo::new(nav_range, nav))
|
||||||
.map(|nav| nav.try_conv_with(&world))
|
.map(|nav| nav.try_conv_with(&world))
|
||||||
.collect::<Result<Vec<_>>>()?;
|
.collect::<Result<Vec<_>>>()?;
|
||||||
Ok(Some(req::GotoDefinitionResponse::Array(res)))
|
Ok(Some(req::GotoDefinitionResponse::Array(res)))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue