Skip to content

Commit 05f75d6

Browse files
bors[bot]Veykril
andauthored
Merge #6666
6666: Support 'go to definition' for self r=jonas-schievink a=Veykril Also reverts #6660, instead of showing the type it now works like it does for names by returning the declaration we are already on. This for example enables VSCode to show all references(#6665) when executing `go to definition` on the declaration. Co-authored-by: Lukas Wirth <[email protected]>
2 parents c8a2ff6 + 37438c3 commit 05f75d6

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

crates/ide/src/goto_definition.rs

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use hir::Semantics;
22
use ide_db::{
3+
base_db::FileId,
34
defs::{NameClass, NameRefClass},
45
symbol_index, RootDatabase,
56
};
@@ -40,10 +41,17 @@ pub(crate) fn goto_definition(
4041
vec![nav]
4142
},
4243
ast::SelfParam(self_param) => {
43-
let ty = sema.type_of_self(&self_param)?;
44-
let adt_def = ty.autoderef(db).filter_map(|ty| ty.as_adt()).last()?;
45-
let nav = adt_def.to_nav(db);
46-
vec![nav]
44+
vec![self_to_nav_target(self_param, position.file_id)?]
45+
},
46+
ast::PathSegment(segment) => {
47+
segment.self_token()?;
48+
let path = segment.parent_path();
49+
if path.qualifier().is_some() && !ast::PathExpr::can_cast(path.syntax().parent()?.kind()) {
50+
return None;
51+
}
52+
let func = segment.syntax().ancestors().find_map(ast::Fn::cast)?;
53+
let self_param = func.param_list()?.self_param()?;
54+
vec![self_to_nav_target(self_param, position.file_id)?]
4755
},
4856
_ => return None,
4957
}
@@ -63,6 +71,20 @@ fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
6371
}
6472
}
6573

74+
fn self_to_nav_target(self_param: ast::SelfParam, file_id: FileId) -> Option<NavigationTarget> {
75+
let self_token = self_param.self_token()?;
76+
Some(NavigationTarget {
77+
file_id,
78+
full_range: self_param.syntax().text_range(),
79+
focus_range: Some(self_token.text_range()),
80+
name: self_token.text().clone(),
81+
kind: self_token.kind(),
82+
container_name: None,
83+
description: None,
84+
docs: None,
85+
})
86+
}
87+
6688
#[derive(Debug)]
6789
pub(crate) enum ReferenceResult {
6890
Exact(NavigationTarget),
@@ -987,31 +1009,31 @@ fn g() -> <() as Iterator<A = (), B<|> = u8>>::A {}
9871009
}
9881010

9891011
#[test]
990-
fn todo_def_type_for_self() {
1012+
fn goto_self_param_ty_specified() {
9911013
check(
9921014
r#"
9931015
struct Foo {}
994-
//^^^
9951016
9961017
impl Foo {
997-
fn bar(&self<|>) {}
998-
}
999-
"#,
1000-
);
1018+
fn bar(self: &Foo) {
1019+
//^^^^
1020+
let foo = sel<|>f;
1021+
}
1022+
}"#,
1023+
)
10011024
}
10021025

10031026
#[test]
1004-
fn todo_def_type_for_arbitrary_self() {
1027+
fn goto_self_param_on_decl() {
10051028
check(
10061029
r#"
1007-
struct Arc<T>(T);
1008-
//^^^
10091030
struct Foo {}
10101031
10111032
impl Foo {
1012-
fn bar(self<|>: Arc<Self>) {}
1013-
}
1014-
"#,
1015-
);
1033+
fn bar(&self<|>) {
1034+
//^^^^
1035+
}
1036+
}"#,
1037+
)
10161038
}
10171039
}

0 commit comments

Comments
 (0)