Skip to content

Commit 8a869e8

Browse files
bors[bot]Veykril
andauthored
Merge #7288
7288: Handle self/super/crate in PathSegment as NameRef r=matklad a=Veykril Wrapping self/super/crate in NameRef as per #7261 (comment) Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents 148e3d0 + cb86339 commit 8a869e8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+258
-254
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members = [ "xtask/", "lib/*", "crates/*" ]
2+
members = ["xtask/", "lib/*", "crates/*"]
33

44
[profile.dev]
55
# Disabling debug info speeds up builds a bunch,

crates/hir/src/semantics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ to_def_impls![
750750
(crate::ConstParam, ast::ConstParam, const_param_to_def),
751751
(crate::MacroDef, ast::MacroRules, macro_rules_to_def),
752752
(crate::Local, ast::IdentPat, bind_pat_to_def),
753+
(crate::Local, ast::SelfParam, self_param_to_def),
753754
(crate::Label, ast::Label, label_to_def),
754755
];
755756

crates/hir/src/semantics/source_to_def.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ impl SourceToDefCtx<'_, '_> {
114114
let pat_id = source_map.node_pat(src.as_ref())?;
115115
Some((container, pat_id))
116116
}
117+
pub(super) fn self_param_to_def(
118+
&mut self,
119+
src: InFile<ast::SelfParam>,
120+
) -> Option<(DefWithBodyId, PatId)> {
121+
let container = self.find_pat_or_label_container(src.as_ref().map(|it| it.syntax()))?;
122+
let (_body, source_map) = self.db.body_with_source_map(container);
123+
let pat_id = source_map.node_self_param(src.as_ref())?;
124+
Some((container, pat_id))
125+
}
117126
pub(super) fn label_to_def(
118127
&mut self,
119128
src: InFile<ast::Label>,

crates/hir_def/src/resolver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ impl Resolver {
258258
) -> Option<ResolveValueResult> {
259259
let n_segments = path.segments.len();
260260
let tmp = name![self];
261-
let first_name = if path.is_self() { &tmp } else { &path.segments.first()? };
261+
let first_name = if path.is_self() { &tmp } else { path.segments.first()? };
262262
let skip_to_mod = path.kind != PathKind::Plain && !path.is_self();
263263
for scope in self.scopes.iter().rev() {
264264
match scope {

crates/ide/src/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use itertools::Itertools;
1818
use rustc_hash::FxHashSet;
1919
use syntax::{
2020
ast::{self, AstNode},
21-
SyntaxNode, TextRange, T,
21+
SyntaxNode, TextRange,
2222
};
2323
use text_edit::TextEdit;
2424

@@ -232,7 +232,7 @@ fn text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(
232232
single_use_tree: &ast::UseTree,
233233
) -> Option<TextEdit> {
234234
let use_tree_list_node = single_use_tree.syntax().parent()?;
235-
if single_use_tree.path()?.segment()?.syntax().first_child_or_token()?.kind() == T![self] {
235+
if single_use_tree.path()?.segment()?.self_token().is_some() {
236236
let start = use_tree_list_node.prev_sibling_or_token()?.text_range().start();
237237
let end = use_tree_list_node.text_range().end();
238238
return Some(TextEdit::delete(TextRange::new(start, end)));

crates/ide/src/display/navigation_target.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,24 +400,33 @@ impl TryToNav for hir::GenericParam {
400400
impl ToNav for hir::Local {
401401
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
402402
let src = self.source(db);
403-
let node = match &src.value {
404-
Either::Left(bind_pat) => {
405-
bind_pat.name().map_or_else(|| bind_pat.syntax().clone(), |it| it.syntax().clone())
406-
}
407-
Either::Right(it) => it.syntax().clone(),
403+
let (node, focus_range) = match &src.value {
404+
Either::Left(bind_pat) => (
405+
bind_pat.syntax().clone(),
406+
bind_pat
407+
.name()
408+
.map(|it| src.with_value(&it.syntax().clone()).original_file_range(db).range),
409+
),
410+
Either::Right(it) => (it.syntax().clone(), it.self_token().map(|it| it.text_range())),
408411
};
409412
let full_range = src.with_value(&node).original_file_range(db);
410413
let name = match self.name(db) {
411414
Some(it) => it.to_string().into(),
412415
None => "".into(),
413416
};
414-
let kind = if self.is_param(db) { SymbolKind::ValueParam } else { SymbolKind::Local };
417+
let kind = if self.is_self(db) {
418+
SymbolKind::SelfParam
419+
} else if self.is_param(db) {
420+
SymbolKind::ValueParam
421+
} else {
422+
SymbolKind::Local
423+
};
415424
NavigationTarget {
416425
file_id: full_range.file_id,
417426
name,
418427
kind: Some(kind),
419428
full_range: full_range.range,
420-
focus_range: None,
429+
focus_range,
421430
container_name: None,
422431
description: None,
423432
docs: None,

crates/ide/src/goto_definition.rs

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use either::Either;
22
use hir::{HasAttrs, ModuleDef, Semantics};
33
use ide_db::{
4-
base_db::FileId,
54
defs::{Definition, NameClass, NameRefClass},
65
symbol_index, RootDatabase,
76
};
@@ -13,7 +12,7 @@ use crate::{
1312
display::{ToNav, TryToNav},
1413
doc_links::extract_definitions_from_markdown,
1514
runnables::doc_owner_to_def,
16-
FilePosition, NavigationTarget, RangeInfo, SymbolKind,
15+
FilePosition, NavigationTarget, RangeInfo,
1716
};
1817

1918
// Feature: Go to Definition
@@ -49,26 +48,18 @@ pub(crate) fn goto_definition(
4948
let nav = def.try_to_nav(sema.db)?;
5049
vec![nav]
5150
},
52-
ast::SelfParam(self_param) => {
53-
vec![self_to_nav_target(self_param, position.file_id)?]
54-
},
55-
ast::PathSegment(segment) => {
56-
segment.self_token()?;
57-
let path = segment.parent_path();
58-
if path.qualifier().is_some() && !ast::PathExpr::can_cast(path.syntax().parent()?.kind()) {
59-
return None;
60-
}
61-
let func = segment.syntax().ancestors().find_map(ast::Fn::cast)?;
62-
let self_param = func.param_list()?.self_param()?;
63-
vec![self_to_nav_target(self_param, position.file_id)?]
64-
},
6551
ast::Lifetime(lt) => if let Some(name_class) = NameClass::classify_lifetime(&sema, &lt) {
6652
let def = name_class.referenced_or_defined(sema.db);
6753
let nav = def.try_to_nav(sema.db)?;
6854
vec![nav]
6955
} else {
7056
reference_definition(&sema, Either::Left(&lt)).to_vec()
7157
},
58+
ast::SelfParam(self_param) => {
59+
let def = NameClass::classify_self_param(&sema, &self_param)?.referenced_or_defined(sema.db);
60+
let nav = def.try_to_nav(sema.db)?;
61+
vec![nav]
62+
},
7263
_ => return None,
7364
}
7465
};
@@ -134,20 +125,6 @@ fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
134125
}
135126
}
136127

137-
fn self_to_nav_target(self_param: ast::SelfParam, file_id: FileId) -> Option<NavigationTarget> {
138-
let self_token = self_param.self_token()?;
139-
Some(NavigationTarget {
140-
file_id,
141-
full_range: self_param.syntax().text_range(),
142-
focus_range: Some(self_token.text_range()),
143-
name: self_token.text().clone(),
144-
kind: Some(SymbolKind::SelfParam),
145-
container_name: None,
146-
description: None,
147-
docs: None,
148-
})
149-
}
150-
151128
#[derive(Debug)]
152129
pub(crate) enum ReferenceResult {
153130
Exact(NavigationTarget),

crates/ide/src/hover.rs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ pub(crate) fn hover(
9898
ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(sema.db)),
9999
ast::Lifetime(lifetime) => NameClass::classify_lifetime(&sema, &lifetime)
100100
.map_or_else(|| NameRefClass::classify_lifetime(&sema, &lifetime).map(|d| d.referenced(sema.db)), |d| d.defined(sema.db)),
101+
ast::SelfParam(self_param) => NameClass::classify_self_param(&sema, &self_param).and_then(|d| d.defined(sema.db)),
101102
_ => None,
102103
}
103104
};
@@ -134,17 +135,14 @@ pub(crate) fn hover(
134135
return None;
135136
}
136137

137-
let node = token.ancestors().find(|n| {
138-
ast::Expr::can_cast(n.kind())
139-
|| ast::Pat::can_cast(n.kind())
140-
|| ast::SelfParam::can_cast(n.kind())
141-
})?;
138+
let node = token
139+
.ancestors()
140+
.find(|n| ast::Expr::can_cast(n.kind()) || ast::Pat::can_cast(n.kind()))?;
142141

143142
let ty = match_ast! {
144143
match node {
145144
ast::Expr(it) => sema.type_of_expr(&it)?,
146145
ast::Pat(it) => sema.type_of_pat(&it)?,
147-
ast::SelfParam(self_param) => sema.type_of_self(&self_param)?,
148146
// If this node is a MACRO_CALL, it means that `descend_into_macros` failed to resolve.
149147
// (e.g expanding a builtin macro). So we give up here.
150148
ast::MacroCall(_it) => return None,
@@ -386,7 +384,7 @@ fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
386384
return tokens.max_by_key(priority);
387385
fn priority(n: &SyntaxToken) -> usize {
388386
match n.kind() {
389-
IDENT | INT_NUMBER | LIFETIME_IDENT => 3,
387+
IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] => 3,
390388
T!['('] | T![')'] => 2,
391389
kind if kind.is_trivia() => 0,
392390
_ => 1,
@@ -3129,6 +3127,39 @@ fn foo<T: Foo>(t: T$0){}
31293127
);
31303128
}
31313129

3130+
#[test]
3131+
fn test_hover_self_has_go_to_type() {
3132+
check_actions(
3133+
r#"
3134+
struct Foo;
3135+
impl Foo {
3136+
fn foo(&self$0) {}
3137+
}
3138+
"#,
3139+
expect![[r#"
3140+
[
3141+
GoToType(
3142+
[
3143+
HoverGotoTypeData {
3144+
mod_path: "test::Foo",
3145+
nav: NavigationTarget {
3146+
file_id: FileId(
3147+
0,
3148+
),
3149+
full_range: 0..11,
3150+
focus_range: 7..10,
3151+
name: "Foo",
3152+
kind: Struct,
3153+
description: "struct Foo",
3154+
},
3155+
},
3156+
],
3157+
),
3158+
]
3159+
"#]],
3160+
);
3161+
}
3162+
31323163
#[test]
31333164
fn hover_displays_normalized_crate_names() {
31343165
check(
@@ -3193,6 +3224,7 @@ impl Foo {
31933224
"#,
31943225
expect![[r#"
31953226
*&self*
3227+
31963228
```rust
31973229
&Foo
31983230
```
@@ -3212,6 +3244,7 @@ impl Foo {
32123244
"#,
32133245
expect![[r#"
32143246
*self: Arc<Foo>*
3247+
32153248
```rust
32163249
Arc<Foo>
32173250
```

0 commit comments

Comments
 (0)