Acquire original text range when searching for references

This commit is contained in:
Lukas Wirth 2020-12-21 17:18:26 +01:00
parent 04525253a0
commit a4f34e2c8c

View file

@ -147,20 +147,20 @@ fn find_name(
) -> Option<RangeInfo<Definition>> { ) -> Option<RangeInfo<Definition>> {
if let Some(name) = opt_name { if let Some(name) = opt_name {
let def = NameClass::classify(sema, &name)?.referenced_or_defined(sema.db); 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)); 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::<ast::Lifetime>(&syntax, position.offset) sema.find_node_at_offset_with_descend::<ast::Lifetime>(&syntax, position.offset)
{ {
if let Some(def) = NameRefClass::classify_lifetime(sema, &lifetime) if let Some(def) = NameRefClass::classify_lifetime(sema, &lifetime)
.map(|class| NameRefClass::referenced(class, sema.db)) .map(|class| NameRefClass::referenced(class, sema.db))
{ {
(lifetime.syntax().text_range(), def) (sema.original_range(lifetime.syntax()), def)
} else { } else {
( (
lifetime.syntax().text_range(), sema.original_range(lifetime.syntax()),
NameClass::classify_lifetime(sema, &lifetime)?.referenced_or_defined(sema.db), NameClass::classify_lifetime(sema, &lifetime)?.referenced_or_defined(sema.db),
) )
} }
@ -168,11 +168,11 @@ fn find_name(
let name_ref = let name_ref =
sema.find_node_at_offset_with_descend::<ast::NameRef>(&syntax, position.offset)?; sema.find_node_at_offset_with_descend::<ast::NameRef>(&syntax, position.offset)?;
( (
name_ref.syntax().text_range(), sema.original_range(name_ref.syntax()),
NameRefClass::classify(sema, &name_ref)?.referenced(sema.db), 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<ReferenceAccess> { fn decl_access(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> Option<ReferenceAccess> {
@ -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
"#]],
);
}
} }