mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
fix goto definition when inbetween tokens
fixes both goto_definition and goto_type_definition. before, when running goto between some non-trivia token and an identifier, goto would be attempted for the non-trivia token. but this does not make sense for e.g. L_PAREN or COLONCOLON only for IDENTs. now only IDENTs will be searched for in goto actions.
This commit is contained in:
parent
ebc95af2b5
commit
4df741ecb2
2 changed files with 28 additions and 4 deletions
|
@ -3,7 +3,7 @@
|
||||||
use hir::{db::AstDatabase, InFile};
|
use hir::{db::AstDatabase, InFile};
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
ast::{self, DocCommentsOwner},
|
ast::{self, DocCommentsOwner},
|
||||||
match_ast, AstNode, SyntaxNode,
|
match_ast, AstNode, SyntaxKind, SyntaxNode,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -20,7 +20,7 @@ pub(crate) fn goto_definition(
|
||||||
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
|
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
|
||||||
let file = db.parse_or_expand(position.file_id.into())?;
|
let file = db.parse_or_expand(position.file_id.into())?;
|
||||||
let original_token =
|
let original_token =
|
||||||
file.token_at_offset(position.offset).filter(|it| !it.kind().is_trivia()).next()?;
|
file.token_at_offset(position.offset).find(|it| it.kind() == SyntaxKind::IDENT)?;
|
||||||
let token = descend_into_macros(db, position.file_id, original_token.clone());
|
let token = descend_into_macros(db, position.file_id, original_token.clone());
|
||||||
|
|
||||||
let nav_targets = match_ast! {
|
let nav_targets = match_ast! {
|
||||||
|
@ -234,6 +234,18 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn goto_definition_works_at_start_of_item() {
|
||||||
|
check_goto(
|
||||||
|
"
|
||||||
|
//- /lib.rs
|
||||||
|
struct Foo;
|
||||||
|
enum E { X(<|>Foo) }
|
||||||
|
",
|
||||||
|
"Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn goto_definition_resolves_correct_name() {
|
fn goto_definition_resolves_correct_name() {
|
||||||
check_goto(
|
check_goto(
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
//! FIXME: write short doc here
|
//! FIXME: write short doc here
|
||||||
|
|
||||||
use hir::db::AstDatabase;
|
use hir::db::AstDatabase;
|
||||||
use ra_syntax::{ast, AstNode};
|
use ra_syntax::{ast, AstNode, SyntaxKind};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::RootDatabase, display::ToNav, expand::descend_into_macros, FilePosition, NavigationTarget,
|
db::RootDatabase, display::ToNav, expand::descend_into_macros, FilePosition, NavigationTarget,
|
||||||
|
@ -13,7 +13,7 @@ pub(crate) fn goto_type_definition(
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
|
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
|
||||||
let file = db.parse_or_expand(position.file_id.into())?;
|
let file = db.parse_or_expand(position.file_id.into())?;
|
||||||
let token = file.token_at_offset(position.offset).filter(|it| !it.kind().is_trivia()).next()?;
|
let token = file.token_at_offset(position.offset).find(|it| it.kind() == SyntaxKind::IDENT)?;
|
||||||
let token = descend_into_macros(db, position.file_id, token);
|
let token = descend_into_macros(db, position.file_id, token);
|
||||||
|
|
||||||
let node = token.value.ancestors().find_map(|token| {
|
let node = token.value.ancestors().find_map(|token| {
|
||||||
|
@ -102,4 +102,16 @@ mod tests {
|
||||||
"Foo STRUCT_DEF FileId(1) [52; 65) [59; 62)",
|
"Foo STRUCT_DEF FileId(1) [52; 65) [59; 62)",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn goto_type_definition_works_param() {
|
||||||
|
check_goto(
|
||||||
|
"
|
||||||
|
//- /lib.rs
|
||||||
|
struct Foo;
|
||||||
|
fn foo(<|>f: Foo) {}
|
||||||
|
",
|
||||||
|
"Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)",
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue