From 209eb32796c6fd56d1b378690c807cf73ce54235 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 28 Feb 2020 15:27:52 +0100 Subject: Classify name takes const patterns into account --- crates/ra_ide/src/goto_definition.rs | 13 +++---------- crates/ra_ide/src/hover.rs | 6 +++--- crates/ra_ide/src/references.rs | 2 +- crates/ra_ide/src/syntax_highlighting.rs | 16 +++++++++------- 4 files changed, 16 insertions(+), 21 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index 6053c1bb6..621ab982c 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -30,7 +30,9 @@ pub(crate) fn goto_definition( reference_definition(&sema, &name_ref).to_vec() }, ast::Name(name) => { - name_definition(&sema, &name)? + let def = classify_name(&sema, &name)?.definition(); + let nav = def.try_to_nav(sema.db)?; + vec![nav] }, _ => return None, } @@ -88,15 +90,6 @@ pub(crate) fn reference_definition( Approximate(navs) } -fn name_definition( - sema: &Semantics, - name: &ast::Name, -) -> Option> { - let def = classify_name(sema, name)?; - let nav = def.try_to_nav(sema.db)?; - Some(vec![nav]) -} - #[cfg(test)] mod tests { use test_utils::{assert_eq_text, covers}; diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index 7ba4bfcac..1e4fcdefb 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -156,7 +156,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option { - classify_name(&sema, &name).map(|d| (name.syntax().clone(), d)) + classify_name(&sema, &name).map(|d| (name.syntax().clone(), d.definition())) }, _ => None, } @@ -761,13 +761,13 @@ fn func(foo: i32) { if true { <|>foo; }; } fn test_hover_through_literal_string_in_builtin_macro() { check_hover_no_result( r#" - //- /lib.rs + //- /lib.rs #[rustc_builtin_macro] macro_rules! assert { ($cond:expr) => {{ /* compiler built-in */ }}; ($cond:expr,) => {{ /* compiler built-in */ }}; ($cond:expr, $($arg:tt)+) => {{ /* compiler built-in */ }}; - } + } fn foo() { assert!("hel<|>lo"); diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs index baa8a4d29..f763013ae 100644 --- a/crates/ra_ide/src/references.rs +++ b/crates/ra_ide/src/references.rs @@ -155,7 +155,7 @@ fn find_name( opt_name: Option, ) -> Option> { if let Some(name) = opt_name { - let def = classify_name(sema, &name)?; + let def = classify_name(sema, &name)?.definition(); let range = name.syntax().text_range(); return Some(RangeInfo::new(range, (name.text().to_string(), def))); } diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index c0f7c1c9f..6312bcb83 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -7,7 +7,7 @@ mod tests; use hir::{Name, Semantics}; use ra_ide_db::{ - defs::{classify_name, NameDefinition}, + defs::{classify_name, NameClass, NameDefinition}, RootDatabase, }; use ra_prof::profile; @@ -169,7 +169,7 @@ fn highlight_element( let name = element.into_node().and_then(ast::Name::cast).unwrap(); let name_kind = classify_name(sema, &name); - if let Some(NameDefinition::Local(local)) = &name_kind { + if let Some(NameClass::NameDefinition(NameDefinition::Local(local))) = &name_kind { if let Some(name) = local.name(db) { let shadow_count = bindings_shadow_count.entry(name.clone()).or_default(); *shadow_count += 1; @@ -177,11 +177,13 @@ fn highlight_element( } }; - let h = match name_kind { - Some(name_kind) => highlight_name(db, name_kind), - None => highlight_name_by_syntax(name), - }; - h | HighlightModifier::Definition + match name_kind { + Some(NameClass::NameDefinition(def)) => { + highlight_name(db, def) | HighlightModifier::Definition + } + Some(NameClass::ConstReference(def)) => highlight_name(db, def), + None => highlight_name_by_syntax(name) | HighlightModifier::Definition, + } } // Highlight references like the definitions they resolve to -- cgit v1.2.3