aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/references
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-01-18 16:13:06 +0000
committerGitHub <[email protected]>2021-01-18 16:13:06 +0000
commit9daba961f236750c3a5d831c9775606271b37eff (patch)
tree6d8c037ea915434701e573bf5eb2eb1f696eef07 /crates/ide/src/references
parentcc58459ee39db804c0b4a660d1f0bb5c9c0bb648 (diff)
parent98718e0544f42e55642d2838b00d6a7bef1e2414 (diff)
Merge #7291
7291: Wrap remaining self/super/crate in Name{Ref} r=matklad a=Veykril That should be the remaining special casing for `self` 🎉 Co-authored-by: Lukas Wirth <[email protected]>
Diffstat (limited to 'crates/ide/src/references')
-rw-r--r--crates/ide/src/references/rename.rs76
1 files changed, 32 insertions, 44 deletions
diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs
index 9ac4af026..4df189c98 100644
--- a/crates/ide/src/references/rename.rs
+++ b/crates/ide/src/references/rename.rs
@@ -1,12 +1,9 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2use std::{ 2use std::fmt::{self, Display};
3 convert::TryInto,
4 fmt::{self, Display},
5};
6 3
7use hir::{Module, ModuleDef, ModuleSource, Semantics}; 4use hir::{Module, ModuleDef, ModuleSource, Semantics};
8use ide_db::{ 5use ide_db::{
9 base_db::{AnchoredPathBuf, FileId, FileRange, SourceDatabaseExt}, 6 base_db::{AnchoredPathBuf, FileId, FileRange},
10 defs::{Definition, NameClass, NameRefClass}, 7 defs::{Definition, NameClass, NameRefClass},
11 search::FileReference, 8 search::FileReference,
12 RootDatabase, 9 RootDatabase,
@@ -14,14 +11,14 @@ use ide_db::{
14use syntax::{ 11use syntax::{
15 algo::find_node_at_offset, 12 algo::find_node_at_offset,
16 ast::{self, NameOwner}, 13 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,
18}; 15};
19use test_utils::mark; 16use test_utils::mark;
20use text_edit::TextEdit; 17use text_edit::TextEdit;
21 18
22use crate::{ 19use crate::{
23 FilePosition, FileSystemEdit, RangeInfo, ReferenceKind, ReferenceSearchResult, SourceChange, 20 FilePosition, FileSystemEdit, RangeInfo, ReferenceKind, ReferenceSearchResult, SourceChange,
24 TextRange, TextSize, 21 TextRange,
25}; 22};
26 23
27type RenameResult<T> = Result<T, RenameError>; 24type RenameResult<T> = Result<T, RenameError>;
@@ -52,10 +49,6 @@ pub(crate) fn prepare_rename(
52 let syntax = source_file.syntax(); 49 let syntax = source_file.syntax();
53 if let Some(module) = find_module_at_offset(&sema, position, syntax) { 50 if let Some(module) = find_module_at_offset(&sema, position, syntax) {
54 rename_mod(&sema, position, module, "dummy") 51 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")
59 } else { 52 } else {
60 let RangeInfo { range, .. } = find_all_refs(&sema, position)?; 53 let RangeInfo { range, .. } = find_all_refs(&sema, position)?;
61 Ok(RangeInfo::new(range, SourceChange::default())) 54 Ok(RangeInfo::new(range, SourceChange::default()))
@@ -82,10 +75,6 @@ pub(crate) fn rename_with_semantics(
82 75
83 if let Some(module) = find_module_at_offset(&sema, position, syntax) { 76 if let Some(module) = find_module_at_offset(&sema, position, syntax) {
84 rename_mod(&sema, position, module, new_name) 77 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)
89 } else { 78 } else {
90 rename_reference(&sema, position, new_name) 79 rename_reference(&sema, position, new_name)
91 } 80 }
@@ -108,7 +97,7 @@ pub(crate) fn will_rename_file(
108 Some(change) 97 Some(change)
109} 98}
110 99
111#[derive(Debug, PartialEq)] 100#[derive(Copy, Clone, Debug, PartialEq)]
112enum IdentifierKind { 101enum IdentifierKind {
113 Ident, 102 Ident,
114 Lifetime, 103 Lifetime,
@@ -375,53 +364,50 @@ fn text_edit_from_self_param(
375fn rename_self_to_param( 364fn rename_self_to_param(
376 sema: &Semantics<RootDatabase>, 365 sema: &Semantics<RootDatabase>,
377 position: FilePosition, 366 position: FilePosition,
378 self_token: SyntaxToken,
379 new_name: &str, 367 new_name: &str,
368 ident_kind: IdentifierKind,
369 range: TextRange,
370 refs: ReferenceSearchResult,
380) -> Result<RangeInfo<SourceChange>, RenameError> { 371) -> Result<RangeInfo<SourceChange>, RenameError> {
381 let ident_kind = check_identifier(new_name)?;
382 match ident_kind { 372 match ident_kind {
383 IdentifierKind::Lifetime => bail!("Invalid name `{}`: not an identifier", new_name), 373 IdentifierKind::Lifetime => bail!("Invalid name `{}`: not an identifier", new_name),
384 IdentifierKind::ToSelf => { 374 IdentifierKind::ToSelf => {
385 // no-op 375 // no-op
386 mark::hit!(rename_self_to_self); 376 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()));
388 } 378 }
389 _ => (), 379 _ => (),
390 } 380 }
391 let source_file = sema.parse(position.file_id); 381 let source_file = sema.parse(position.file_id);
392 let syn = source_file.syntax(); 382 let syn = source_file.syntax();
393 383
394 let text = sema.db.file_text(position.file_id);
395 let fn_def = find_node_at_offset::<ast::Fn>(syn, position.offset) 384 let fn_def = find_node_at_offset::<ast::Fn>(syn, position.offset)
396 .ok_or_else(|| format_err!("No surrounding method declaration found"))?; 385 .ok_or_else(|| format_err!("No surrounding method declaration found"))?;
397 let search_range = fn_def.syntax().text_range();
398 386
399 let mut source_change = SourceChange::default(); 387 let mut source_change = SourceChange::default();
400 388 if let Some(self_param) = fn_def.param_list().and_then(|it| it.self_param()) {
401 for (idx, _) in text.match_indices("self") { 389 if self_param
402 let offset: TextSize = idx.try_into().unwrap(); 390 .syntax()
403 if !search_range.contains_inclusive(offset) { 391 .text_range()
404 continue; 392 .contains_range(refs.declaration().nav.focus_or_full_range())
405 } 393 {
406 if let Some(ref usage) = syn.token_at_offset(offset).find(|t| t.kind() == T![self]) { 394 let edit = text_edit_from_self_param(syn, &self_param, new_name)
407 let edit = if let Some(ref self_param) = ast::SelfParam::cast(usage.parent()) { 395 .ok_or_else(|| format_err!("No target type found"))?;
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 };
413 source_change.insert_source_edit(position.file_id, edit); 396 source_change.insert_source_edit(position.file_id, edit);
414 }
415 }
416 397
417 if source_change.source_file_edits.len() > 1 && ident_kind == IdentifierKind::Underscore { 398 source_change.extend(refs.references().iter().map(|(&file_id, references)| {
418 bail!("Cannot rename reference to `_` as it is being referenced multiple times"); 399 source_edit_from_references(sema, file_id, &references, new_name)
419 } 400 }));
420 401
421 let range = ast::SelfParam::cast(self_token.parent()) 402 if source_change.source_file_edits.len() > 1 && ident_kind == IdentifierKind::Underscore
422 .map_or(self_token.text_range(), |p| p.syntax().text_range()); 403 {
404 bail!("Cannot rename reference to `_` as it is being referenced multiple times");
405 }
423 406
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"))
425} 411}
426 412
427fn rename_reference( 413fn rename_reference(
@@ -444,8 +430,9 @@ fn rename_reference(
444 mark::hit!(rename_not_an_ident_ref); 430 mark::hit!(rename_not_an_ident_ref);
445 bail!("Invalid name `{}`: not an identifier", new_name) 431 bail!("Invalid name `{}`: not an identifier", new_name)
446 } 432 }
447 (IdentifierKind::ToSelf, ReferenceKind::SelfParam) => { 433 (_, ReferenceKind::SelfParam) => {
448 unreachable!("rename_self_to_param should've been called instead") 434 mark::hit!(rename_self_to_param);
435 return rename_self_to_param(sema, position, new_name, ident_kind, range, refs);
449 } 436 }
450 (IdentifierKind::ToSelf, _) => { 437 (IdentifierKind::ToSelf, _) => {
451 mark::hit!(rename_to_self); 438 mark::hit!(rename_to_self);
@@ -1350,6 +1337,7 @@ impl Foo {
1350 1337
1351 #[test] 1338 #[test]
1352 fn test_owned_self_to_parameter() { 1339 fn test_owned_self_to_parameter() {
1340 mark::check!(rename_self_to_param);
1353 check( 1341 check(
1354 "foo", 1342 "foo",
1355 r#" 1343 r#"