Skip to content

Commit 98718e0

Browse files
committed
Wrap remaining self/super/crate in Name{Ref}
1 parent 8a869e8 commit 98718e0

28 files changed

+236
-168
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.

crates/assists/src/handlers/introduce_named_lifetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn generate_fn_def_assist(
6161
// compute the location which implicitly has the same lifetime as the anonymous lifetime
6262
let loc_needing_lifetime = if let Some(self_param) = self_param {
6363
// if we have a self reference, use that
64-
Some(self_param.self_token()?.text_range().start())
64+
Some(self_param.name()?.syntax().text_range().start())
6565
} else {
6666
// otherwise, if there's a single reference parameter without a named liftime, use that
6767
let fn_params_without_lifetime: Vec<_> = param_list

crates/hir_ty/src/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use once_cell::race::OnceBool;
2626
use stdx::format_to;
2727
use syntax::{
2828
algo,
29-
ast::{self, AstNode},
29+
ast::{self, AstNode, NameOwner},
3030
SyntaxNode,
3131
};
3232
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
@@ -153,7 +153,7 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
153153
});
154154
for (node, ty) in &types {
155155
let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node.value.clone()) {
156-
(self_param.self_token().unwrap().text_range(), "self".to_string())
156+
(self_param.name().unwrap().syntax().text_range(), "self".to_string())
157157
} else {
158158
(node.value.text_range(), node.value.text().to_string().replace("\n", " "))
159159
};

crates/ide/src/display/navigation_target.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,15 +400,13 @@ 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, 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())),
403+
let (node, name) = match &src.value {
404+
Either::Left(bind_pat) => (bind_pat.syntax().clone(), bind_pat.name()),
405+
Either::Right(it) => (it.syntax().clone(), it.name()),
411406
};
407+
let focus_range =
408+
name.map(|it| src.with_value(&it.syntax().clone()).original_file_range(db).range);
409+
412410
let full_range = src.with_value(&node).original_file_range(db);
413411
let name = match self.name(db) {
414412
Some(it) => it.to_string().into(),

crates/ide/src/goto_definition.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ pub(crate) fn goto_definition(
5555
} else {
5656
reference_definition(&sema, Either::Left(&lt)).to_vec()
5757
},
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-
},
6358
_ => return None,
6459
}
6560
};

crates/ide/src/hover.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ 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)),
102101
_ => None,
103102
}
104103
};
@@ -3223,7 +3222,7 @@ impl Foo {
32233222
}
32243223
"#,
32253224
expect![[r#"
3226-
*&self*
3225+
*self*
32273226
32283227
```rust
32293228
&Foo
@@ -3243,7 +3242,7 @@ impl Foo {
32433242
}
32443243
"#,
32453244
expect![[r#"
3246-
*self: Arc<Foo>*
3245+
*self*
32473246
32483247
```rust
32493248
Arc<Foo>

crates/ide/src/references/rename.rs

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
//! FIXME: write short doc here
2-
use std::{
3-
convert::TryInto,
4-
fmt::{self, Display},
5-
};
2+
use std::fmt::{self, Display};
63

74
use hir::{Module, ModuleDef, ModuleSource, Semantics};
85
use ide_db::{
9-
base_db::{AnchoredPathBuf, FileId, FileRange, SourceDatabaseExt},
6+
base_db::{AnchoredPathBuf, FileId, FileRange},
107
defs::{Definition, NameClass, NameRefClass},
118
search::FileReference,
129
RootDatabase,
1310
};
1411
use syntax::{
1512
algo::find_node_at_offset,
1613
ast::{self, NameOwner},
17-
lex_single_syntax_kind, match_ast, AstNode, SyntaxKind, SyntaxNode, SyntaxToken, T,
14+
lex_single_syntax_kind, match_ast, AstNode, SyntaxKind, SyntaxNode, T,
1815
};
1916
use test_utils::mark;
2017
use text_edit::TextEdit;
2118

2219
use crate::{
2320
FilePosition, FileSystemEdit, RangeInfo, ReferenceKind, ReferenceSearchResult, SourceChange,
24-
TextRange, TextSize,
21+
TextRange,
2522
};
2623

2724
type RenameResult<T> = Result<T, RenameError>;
@@ -52,10 +49,6 @@ pub(crate) fn prepare_rename(
5249
let syntax = source_file.syntax();
5350
if let Some(module) = find_module_at_offset(&sema, position, syntax) {
5451
rename_mod(&sema, position, module, "dummy")
55-
} else if let Some(self_token) =
56-
syntax.token_at_offset(position.offset).find(|t| t.kind() == T![self])
57-
{
58-
rename_self_to_param(&sema, position, self_token, "dummy")
5952
} else {
6053
let RangeInfo { range, .. } = find_all_refs(&sema, position)?;
6154
Ok(RangeInfo::new(range, SourceChange::default()))
@@ -82,10 +75,6 @@ pub(crate) fn rename_with_semantics(
8275

8376
if let Some(module) = find_module_at_offset(&sema, position, syntax) {
8477
rename_mod(&sema, position, module, new_name)
85-
} else if let Some(self_token) =
86-
syntax.token_at_offset(position.offset).find(|t| t.kind() == T![self])
87-
{
88-
rename_self_to_param(&sema, position, self_token, new_name)
8978
} else {
9079
rename_reference(&sema, position, new_name)
9180
}
@@ -108,7 +97,7 @@ pub(crate) fn will_rename_file(
10897
Some(change)
10998
}
11099

111-
#[derive(Debug, PartialEq)]
100+
#[derive(Copy, Clone, Debug, PartialEq)]
112101
enum IdentifierKind {
113102
Ident,
114103
Lifetime,
@@ -375,53 +364,50 @@ fn text_edit_from_self_param(
375364
fn rename_self_to_param(
376365
sema: &Semantics<RootDatabase>,
377366
position: FilePosition,
378-
self_token: SyntaxToken,
379367
new_name: &str,
368+
ident_kind: IdentifierKind,
369+
range: TextRange,
370+
refs: ReferenceSearchResult,
380371
) -> Result<RangeInfo<SourceChange>, RenameError> {
381-
let ident_kind = check_identifier(new_name)?;
382372
match ident_kind {
383373
IdentifierKind::Lifetime => bail!("Invalid name `{}`: not an identifier", new_name),
384374
IdentifierKind::ToSelf => {
385375
// no-op
386376
mark::hit!(rename_self_to_self);
387-
return Ok(RangeInfo::new(self_token.text_range(), SourceChange::default()));
377+
return Ok(RangeInfo::new(range, SourceChange::default()));
388378
}
389379
_ => (),
390380
}
391381
let source_file = sema.parse(position.file_id);
392382
let syn = source_file.syntax();
393383

394-
let text = sema.db.file_text(position.file_id);
395384
let fn_def = find_node_at_offset::<ast::Fn>(syn, position.offset)
396385
.ok_or_else(|| format_err!("No surrounding method declaration found"))?;
397-
let search_range = fn_def.syntax().text_range();
398386

399387
let mut source_change = SourceChange::default();
400-
401-
for (idx, _) in text.match_indices("self") {
402-
let offset: TextSize = idx.try_into().unwrap();
403-
if !search_range.contains_inclusive(offset) {
404-
continue;
405-
}
406-
if let Some(ref usage) = syn.token_at_offset(offset).find(|t| t.kind() == T![self]) {
407-
let edit = if let Some(ref self_param) = ast::SelfParam::cast(usage.parent()) {
408-
text_edit_from_self_param(syn, self_param, new_name)
409-
.ok_or_else(|| format_err!("No target type found"))?
410-
} else {
411-
TextEdit::replace(usage.text_range(), String::from(new_name))
412-
};
388+
if let Some(self_param) = fn_def.param_list().and_then(|it| it.self_param()) {
389+
if self_param
390+
.syntax()
391+
.text_range()
392+
.contains_range(refs.declaration().nav.focus_or_full_range())
393+
{
394+
let edit = text_edit_from_self_param(syn, &self_param, new_name)
395+
.ok_or_else(|| format_err!("No target type found"))?;
413396
source_change.insert_source_edit(position.file_id, edit);
414-
}
415-
}
416397

417-
if source_change.source_file_edits.len() > 1 && ident_kind == IdentifierKind::Underscore {
418-
bail!("Cannot rename reference to `_` as it is being referenced multiple times");
419-
}
398+
source_change.extend(refs.references().iter().map(|(&file_id, references)| {
399+
source_edit_from_references(sema, file_id, &references, new_name)
400+
}));
420401

421-
let range = ast::SelfParam::cast(self_token.parent())
422-
.map_or(self_token.text_range(), |p| p.syntax().text_range());
402+
if source_change.source_file_edits.len() > 1 && ident_kind == IdentifierKind::Underscore
403+
{
404+
bail!("Cannot rename reference to `_` as it is being referenced multiple times");
405+
}
423406

424-
Ok(RangeInfo::new(range, source_change))
407+
return Ok(RangeInfo::new(range, source_change));
408+
}
409+
}
410+
Err(format_err!("Method has no self param"))
425411
}
426412

427413
fn rename_reference(
@@ -444,8 +430,9 @@ fn rename_reference(
444430
mark::hit!(rename_not_an_ident_ref);
445431
bail!("Invalid name `{}`: not an identifier", new_name)
446432
}
447-
(IdentifierKind::ToSelf, ReferenceKind::SelfParam) => {
448-
unreachable!("rename_self_to_param should've been called instead")
433+
(_, ReferenceKind::SelfParam) => {
434+
mark::hit!(rename_self_to_param);
435+
return rename_self_to_param(sema, position, new_name, ident_kind, range, refs);
449436
}
450437
(IdentifierKind::ToSelf, _) => {
451438
mark::hit!(rename_to_self);
@@ -1350,6 +1337,7 @@ impl Foo {
13501337

13511338
#[test]
13521339
fn test_owned_self_to_parameter() {
1340+
mark::check!(rename_self_to_param);
13531341
check(
13541342
"foo",
13551343
r#"

crates/ide/src/syntax_highlighting/highlight.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ pub(super) fn element(
6868
NAME_REF => {
6969
let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap();
7070
highlight_func_by_name_ref(sema, &name_ref).unwrap_or_else(|| {
71-
match NameRefClass::classify(sema, &name_ref) {
71+
let is_self = name_ref.self_token().is_some();
72+
let h = match NameRefClass::classify(sema, &name_ref) {
7273
Some(name_kind) => match name_kind {
7374
NameRefClass::ExternCrate(_) => HlTag::Symbol(SymbolKind::Module).into(),
7475
NameRefClass::Definition(def) => {
@@ -108,6 +109,11 @@ pub(super) fn element(
108109
highlight_name_ref_by_syntax(name_ref, sema)
109110
}
110111
None => HlTag::UnresolvedReference.into(),
112+
};
113+
if h.tag == HlTag::Symbol(SymbolKind::Module) && is_self {
114+
HlTag::Symbol(SymbolKind::SelfParam).into()
115+
} else {
116+
h
111117
}
112118
})
113119
}
@@ -225,18 +231,8 @@ pub(super) fn element(
225231
T![for] if !is_child_of_impl(&element) => h | HlMod::ControlFlow,
226232
T![unsafe] => h | HlMod::Unsafe,
227233
T![true] | T![false] => HlTag::BoolLiteral.into(),
228-
T![self] => {
229-
let self_param = element.parent().and_then(ast::SelfParam::cast);
230-
if let Some(NameClass::Definition(def)) = self_param
231-
.and_then(|self_param| NameClass::classify_self_param(sema, &self_param))
232-
{
233-
highlight_def(db, def) | HlMod::Definition
234-
} else if element.ancestors().any(|it| it.kind() == USE_TREE) {
235-
HlTag::Symbol(SymbolKind::SelfParam).into()
236-
} else {
237-
return None;
238-
}
239-
}
234+
// self is handled as either a Name or NameRef already
235+
T![self] => return None,
240236
T![ref] => element
241237
.parent()
242238
.and_then(ast::IdentPat::cast)

crates/ide_db/src/defs.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,6 @@ impl NameClass {
117117
}
118118
}
119119

120-
pub fn classify_self_param(
121-
sema: &Semantics<RootDatabase>,
122-
self_param: &ast::SelfParam,
123-
) -> Option<NameClass> {
124-
sema.to_def(self_param).map(Definition::Local).map(NameClass::Definition)
125-
}
126-
127120
pub fn classify(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option<NameClass> {
128121
let _p = profile::span("classify_name");
129122

@@ -186,6 +179,10 @@ impl NameClass {
186179

187180
Some(NameClass::Definition(Definition::Local(local)))
188181
},
182+
ast::SelfParam(it) => {
183+
let def = sema.to_def(&it)?;
184+
Some(NameClass::Definition(Definition::Local(def.into())))
185+
},
189186
ast::RecordField(it) => {
190187
let field: hir::Field = sema.to_def(&it)?;
191188
Some(NameClass::Definition(Definition::Field(field)))

crates/parser/src/grammar.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,25 @@ fn opt_visibility(p: &mut Parser) -> bool {
190190
// test crate_visibility
191191
// pub(crate) struct S;
192192
// pub(self) struct S;
193-
// pub(self) struct S;
194-
// pub(self) struct S;
193+
// pub(super) struct S;
195194

196195
// test pub_parens_typepath
197196
// struct B(pub (super::A));
198197
// struct B(pub (crate::A,));
199198
T![crate] | T![self] | T![super] if p.nth(2) != T![:] => {
200199
p.bump_any();
200+
let path_m = p.start();
201+
let path_segment_m = p.start();
202+
let name_ref_m = p.start();
201203
p.bump_any();
204+
name_ref_m.complete(p, NAME_REF);
205+
path_segment_m.complete(p, PATH_SEGMENT);
206+
path_m.complete(p, PATH);
202207
p.expect(T![')']);
203208
}
209+
// test crate_visibility_in
210+
// pub(in super::A) struct S;
211+
// pub(in crate) struct S;
204212
T![in] => {
205213
p.bump_any();
206214
p.bump_any();

0 commit comments

Comments
 (0)