From a4f34e2c8ce1551966a202c107c81195b57060a2 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 21 Dec 2020 17:18:26 +0100 Subject: Acquire original text range when searching for references --- crates/ide/src/references.rs | 48 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs index 18ea19305..33b7358f7 100644 --- a/crates/ide/src/references.rs +++ b/crates/ide/src/references.rs @@ -147,20 +147,20 @@ fn find_name( ) -> Option> { if let Some(name) = opt_name { let def = NameClass::classify(sema, &name)?.referenced_or_defined(sema.db); - let range = name.syntax().text_range(); + let FileRange { range, .. } = sema.original_range(name.syntax()); return Some(RangeInfo::new(range, def)); } - let (text_range, def) = if let Some(lifetime) = + let (FileRange { range, .. }, def) = if let Some(lifetime) = sema.find_node_at_offset_with_descend::(&syntax, position.offset) { if let Some(def) = NameRefClass::classify_lifetime(sema, &lifetime) .map(|class| NameRefClass::referenced(class, sema.db)) { - (lifetime.syntax().text_range(), def) + (sema.original_range(lifetime.syntax()), def) } else { ( - lifetime.syntax().text_range(), + sema.original_range(lifetime.syntax()), NameClass::classify_lifetime(sema, &lifetime)?.referenced_or_defined(sema.db), ) } @@ -168,11 +168,11 @@ fn find_name( let name_ref = sema.find_node_at_offset_with_descend::(&syntax, position.offset)?; ( - name_ref.syntax().text_range(), + sema.original_range(name_ref.syntax()), NameRefClass::classify(sema, &name_ref)?.referenced(sema.db), ) }; - Some(RangeInfo::new(text_range, def)) + Some(RangeInfo::new(range, def)) } fn decl_access(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> Option { @@ -1086,4 +1086,40 @@ impl<'a> Foo<'a> for &'a () { "#]], ); } + + #[test] + fn test_map_range_to_original() { + check( + r#" +macro_rules! foo {($i:ident) => {$i} } +fn main() { + let a<|> = "test"; + foo!(a); +} +"#, + expect![[r#" + a Local FileId(0) 59..60 Other + + FileId(0) 80..81 Other Read + "#]], + ); + } + + #[test] + fn test_map_range_to_original_ref() { + check( + r#" +macro_rules! foo {($i:ident) => {$i} } +fn main() { + let a = "test"; + foo!(a<|>); +} +"#, + expect![[r#" + a Local FileId(0) 59..60 Other + + FileId(0) 80..81 Other Read + "#]], + ); + } } -- cgit v1.2.3