Skip to content

Commit b9d94d7

Browse files
committed
Use proper range for hover on macro arguments
1 parent 20252ef commit b9d94d7

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

crates/ra_ide/src/hover.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ra_syntax::{
1313

1414
use crate::{
1515
display::{macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel},
16-
expand::descend_into_macros,
16+
expand::{descend_into_macros, original_range},
1717
references::{classify_name, classify_name_ref, NameKind, NameKind::*},
1818
FilePosition, FileRange, RangeInfo,
1919
};
@@ -148,17 +148,18 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
148148
let mut res = HoverResult::new();
149149

150150
let mut sb = SourceBinder::new(db);
151-
if let Some((range, name_kind)) = match_ast! {
151+
if let Some((node, name_kind)) = match_ast! {
152152
match (token.value.parent()) {
153153
ast::NameRef(name_ref) => {
154-
classify_name_ref(&mut sb, token.with_value(&name_ref)).map(|d| (name_ref.syntax().text_range(), d.kind))
154+
classify_name_ref(&mut sb, token.with_value(&name_ref)).map(|d| (name_ref.syntax().clone(), d.kind))
155155
},
156156
ast::Name(name) => {
157-
classify_name(&mut sb, token.with_value(&name)).map(|d| (name.syntax().text_range(), d.kind))
157+
classify_name(&mut sb, token.with_value(&name)).map(|d| (name.syntax().clone(), d.kind))
158158
},
159159
_ => None,
160160
}
161161
} {
162+
let range = original_range(db, token.with_value(&node)).range;
162163
res.extend(hover_text_from_name_kind(db, name_kind));
163164

164165
if !res.is_empty() {
@@ -171,8 +172,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
171172
.ancestors()
172173
.find(|n| ast::Expr::cast(n.clone()).is_some() || ast::Pat::cast(n.clone()).is_some())?;
173174

174-
// The following logic will not work if token is coming from a macro
175-
let frange = FileRange { file_id: position.file_id, range: node.text_range() };
175+
let frange = original_range(db, token.with_value(&node));
176176
res.extend(type_of(db, frange).map(rust_code_markup));
177177
if res.is_empty() {
178178
return None;
@@ -220,6 +220,7 @@ mod tests {
220220
use crate::mock_analysis::{
221221
analysis_and_position, single_file_with_position, single_file_with_range,
222222
};
223+
use ra_db::FileLoader;
223224
use ra_syntax::TextRange;
224225

225226
fn trim_markup(s: &str) -> &str {
@@ -230,7 +231,7 @@ mod tests {
230231
s.map(trim_markup)
231232
}
232233

233-
fn check_hover_result(fixture: &str, expected: &[&str]) {
234+
fn check_hover_result(fixture: &str, expected: &[&str]) -> String {
234235
let (analysis, position) = analysis_and_position(fixture);
235236
let hover = analysis.hover(position).unwrap().unwrap();
236237
let mut results = Vec::from(hover.info.results());
@@ -243,6 +244,9 @@ mod tests {
243244
}
244245

245246
assert_eq!(hover.info.len(), expected.len());
247+
248+
let content = analysis.db.file_text(position.file_id);
249+
content[hover.range].to_string()
246250
}
247251

248252
#[test]
@@ -711,7 +715,7 @@ fn func(foo: i32) { if true { <|>foo; }; }
711715

712716
#[test]
713717
fn test_hover_through_macro() {
714-
check_hover_result(
718+
let hover_on = check_hover_result(
715719
"
716720
//- /lib.rs
717721
macro_rules! id {
@@ -726,11 +730,13 @@ fn func(foo: i32) { if true { <|>foo; }; }
726730
",
727731
&["fn foo()"],
728732
);
733+
734+
assert_eq!(hover_on, "foo")
729735
}
730736

731737
#[test]
732738
fn test_hover_through_expr_in_macro() {
733-
check_hover_result(
739+
let hover_on = check_hover_result(
734740
"
735741
//- /lib.rs
736742
macro_rules! id {
@@ -742,5 +748,7 @@ fn func(foo: i32) { if true { <|>foo; }; }
742748
",
743749
&["u32"],
744750
);
751+
752+
assert_eq!(hover_on, "bar")
745753
}
746754
}

0 commit comments

Comments
 (0)