From b9d94d73a2682be4418cb5de4f77cbd8db7b6901 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 19 Feb 2020 11:54:23 +0800 Subject: Use proper range for hover on macro arguments --- crates/ra_ide/src/hover.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'crates/ra_ide/src/hover.rs') diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index 3f88bb260..0bbaa0855 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -13,7 +13,7 @@ use ra_syntax::{ use crate::{ display::{macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel}, - expand::descend_into_macros, + expand::{descend_into_macros, original_range}, references::{classify_name, classify_name_ref, NameKind, NameKind::*}, FilePosition, FileRange, RangeInfo, }; @@ -148,17 +148,18 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option { - classify_name_ref(&mut sb, token.with_value(&name_ref)).map(|d| (name_ref.syntax().text_range(), d.kind)) + classify_name_ref(&mut sb, token.with_value(&name_ref)).map(|d| (name_ref.syntax().clone(), d.kind)) }, ast::Name(name) => { - classify_name(&mut sb, token.with_value(&name)).map(|d| (name.syntax().text_range(), d.kind)) + classify_name(&mut sb, token.with_value(&name)).map(|d| (name.syntax().clone(), d.kind)) }, _ => None, } } { + let range = original_range(db, token.with_value(&node)).range; res.extend(hover_text_from_name_kind(db, name_kind)); if !res.is_empty() { @@ -171,8 +172,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option &str { @@ -230,7 +231,7 @@ mod tests { s.map(trim_markup) } - fn check_hover_result(fixture: &str, expected: &[&str]) { + fn check_hover_result(fixture: &str, expected: &[&str]) -> String { let (analysis, position) = analysis_and_position(fixture); let hover = analysis.hover(position).unwrap().unwrap(); let mut results = Vec::from(hover.info.results()); @@ -243,6 +244,9 @@ mod tests { } assert_eq!(hover.info.len(), expected.len()); + + let content = analysis.db.file_text(position.file_id); + content[hover.range].to_string() } #[test] @@ -711,7 +715,7 @@ fn func(foo: i32) { if true { <|>foo; }; } #[test] fn test_hover_through_macro() { - check_hover_result( + let hover_on = check_hover_result( " //- /lib.rs macro_rules! id { @@ -726,11 +730,13 @@ fn func(foo: i32) { if true { <|>foo; }; } ", &["fn foo()"], ); + + assert_eq!(hover_on, "foo") } #[test] fn test_hover_through_expr_in_macro() { - check_hover_result( + let hover_on = check_hover_result( " //- /lib.rs macro_rules! id { @@ -742,5 +748,7 @@ fn func(foo: i32) { if true { <|>foo; }; } ", &["u32"], ); + + assert_eq!(hover_on, "bar") } } -- cgit v1.2.3