aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-06-11 18:24:28 +0100
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-06-11 18:24:28 +0100
commit98020ef2f30cae2c1333389f43d9014f6b03981f (patch)
tree11dcb1b9c9a7fb12be8868925be62f0aec8d80d2
parent80aa9d5f9f55341d2a51176e385d8aa6d2d2cec8 (diff)
parentacafbd66f80f79a1b9f9ce181eb3bc22d0ebbf72 (diff)
Merge #1394
1394: Fix hover for pat that shadows items r=matklad a=sinkuu ```rust fn x() {} fn y() { let x = 0i32; x; // hover on `x` is expected to be `i32`, but the actual result was `fn x()` } ``` This was because: if [`res.is_empty()`](https://github.com/sinkuu/rust-analyzer/blob/656a0fa9f99298123d7dcee8c65a8a5ed7043bc4/crates/ra_ide_api/src/hover.rs#L205), it fallbacks to "index based approach" and adds `fn x()` to `res`, which makes [`res.extend(type_of)` below](https://github.com/sinkuu/rust-analyzer/blob/656a0fa9f99298123d7dcee8c65a8a5ed7043bc4/crates/ra_ide_api/src/hover.rs#L260-L266) not happen. Co-authored-by: Shotaro Yamada <[email protected]>
-rw-r--r--crates/ra_ide_api/src/hover.rs29
1 files changed, 23 insertions, 6 deletions
diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs
index ad00abe49..df877c324 100644
--- a/crates/ra_ide_api/src/hover.rs
+++ b/crates/ra_ide_api/src/hover.rs
@@ -95,6 +95,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
95 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset) { 95 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset) {
96 let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None); 96 let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None);
97 97
98 let mut no_fallback = false;
99
98 match classify_name_ref(db, &analyzer, name_ref) { 100 match classify_name_ref(db, &analyzer, name_ref) {
99 Some(Method(it)) => res.extend(from_def_source(db, it)), 101 Some(Method(it)) => res.extend(from_def_source(db, it)),
100 Some(Macro(it)) => { 102 Some(Macro(it)) => {
@@ -142,11 +144,9 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
142 }) 144 })
143 } 145 }
144 } 146 }
145 Some(Pat(_)) => { 147 Some(Pat(_)) | Some(SelfParam(_)) => {
146 res.extend(None); 148 // Hover for these shows type names
147 } 149 no_fallback = true;
148 Some(SelfParam(_)) => {
149 res.extend(None);
150 } 150 }
151 Some(GenericParam(_)) => { 151 Some(GenericParam(_)) => {
152 // FIXME: Hover for generic param 152 // FIXME: Hover for generic param
@@ -154,7 +154,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
154 None => {} 154 None => {}
155 } 155 }
156 156
157 if res.is_empty() { 157 if res.is_empty() && !no_fallback {
158 // Fallback index based approach: 158 // Fallback index based approach:
159 let symbols = crate::symbol_index::index_resolve(db, name_ref); 159 let symbols = crate::symbol_index::index_resolve(db, name_ref);
160 for sym in symbols { 160 for sym in symbols {
@@ -675,4 +675,21 @@ fn func(foo: i32) { if true { <|>foo; }; }
675 assert_eq!(trim_markup_opt(hover.info.first()), Some("enum Thing")); 675 assert_eq!(trim_markup_opt(hover.info.first()), Some("enum Thing"));
676 assert_eq!(hover.info.is_exact(), true); 676 assert_eq!(hover.info.is_exact(), true);
677 } 677 }
678
679 #[test]
680 fn test_hover_shadowing_pat() {
681 let (analysis, position) = single_file_with_position(
682 "
683 fn x() {}
684
685 fn y() {
686 let x = 0i32;
687 x<|>;
688 }
689 ",
690 );
691 let hover = analysis.hover(position).unwrap().unwrap();
692 assert_eq!(trim_markup_opt(hover.info.first()), Some("i32"));
693 assert_eq!(hover.info.is_exact(), true);
694 }
678} 695}