Skip to content

Commit 1dd1814

Browse files
bors[bot]Veykril
andauthored
Merge #9677
9677: fix: Correctly classify Rename Names r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents b744e33 + ef6fed0 commit 1dd1814

File tree

2 files changed

+78
-25
lines changed

2 files changed

+78
-25
lines changed

crates/ide/src/hover.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3780,4 +3780,63 @@ struct Foo;
37803780
"#]],
37813781
)
37823782
}
3783+
3784+
#[test]
3785+
fn hover_rename() {
3786+
check(
3787+
r#"
3788+
use self as foo$0;
3789+
"#,
3790+
expect![[r#"
3791+
*foo*
3792+
3793+
```rust
3794+
extern crate test
3795+
```
3796+
"#]],
3797+
);
3798+
check(
3799+
r#"
3800+
mod bar {}
3801+
use bar::{self as foo$0};
3802+
"#,
3803+
expect![[r#"
3804+
*foo*
3805+
3806+
```rust
3807+
test
3808+
```
3809+
3810+
```rust
3811+
mod bar
3812+
```
3813+
"#]],
3814+
);
3815+
check(
3816+
r#"
3817+
mod bar {
3818+
use super as foo$0;
3819+
}
3820+
"#,
3821+
expect![[r#"
3822+
*foo*
3823+
3824+
```rust
3825+
extern crate test
3826+
```
3827+
"#]],
3828+
);
3829+
check(
3830+
r#"
3831+
use crate as foo$0;
3832+
"#,
3833+
expect![[r#"
3834+
*foo*
3835+
3836+
```rust
3837+
extern crate test
3838+
```
3839+
"#]],
3840+
);
3841+
}
37833842
}

crates/ide_db/src/defs.rs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use hir::{
1010
PathResolution, Semantics, Visibility,
1111
};
1212
use syntax::{
13-
ast::{self, AstNode, PathSegmentKind},
14-
match_ast, SyntaxKind, SyntaxNode,
13+
ast::{self, AstNode},
14+
match_ast, SyntaxKind,
1515
};
1616

1717
use crate::RootDatabase;
@@ -134,29 +134,23 @@ impl NameClass {
134134
if let Some(use_tree) = it.syntax().parent().and_then(ast::UseTree::cast) {
135135
let path = use_tree.path()?;
136136
let path_segment = path.segment()?;
137-
let name_ref_class = path_segment
138-
.kind()
139-
.and_then(|kind| {
140-
match kind {
141-
// The rename might be from a `self` token, so fallback to the name higher
142-
// in the use tree.
143-
PathSegmentKind::SelfKw => {
144-
let use_tree = use_tree
145-
.syntax()
146-
.parent()
147-
.as_ref()
148-
// Skip over UseTreeList
149-
.and_then(SyntaxNode::parent)
150-
.and_then(ast::UseTree::cast)?;
151-
let path = use_tree.path()?;
152-
let path_segment = path.segment()?;
153-
path_segment.name_ref()
154-
},
155-
PathSegmentKind::Name(name_ref) => Some(name_ref),
156-
_ => None,
157-
}
158-
})
159-
.and_then(|name_ref| NameRefClass::classify(sema, &name_ref))?;
137+
let name_ref = path_segment.name_ref()?;
138+
let name_ref = if name_ref.self_token().is_some() {
139+
use_tree
140+
.syntax()
141+
.parent()
142+
.as_ref()
143+
// Skip over UseTreeList
144+
.and_then(|it| {
145+
let use_tree = it.parent().and_then(ast::UseTree::cast)?;
146+
let path = use_tree.path()?;
147+
let path_segment = path.segment()?;
148+
path_segment.name_ref()
149+
}).unwrap_or(name_ref)
150+
} else {
151+
name_ref
152+
};
153+
let name_ref_class = NameRefClass::classify(sema, &name_ref)?;
160154

161155
Some(NameClass::Definition(match name_ref_class {
162156
NameRefClass::Definition(def) => def,

0 commit comments

Comments
 (0)