1- //! FIXME: write short doc here
1+ //! Logic for computing info that is displayed when the user hovers over any
2+ //! source code items (e.g. function call, struct field, variable symbol...)
23
34use hir:: {
45 Adt , AsAssocItem , AssocItemContainer , FieldSource , HasSource , HirDisplay , ModuleDef ,
@@ -24,35 +25,20 @@ use itertools::Itertools;
2425use std:: iter:: once;
2526
2627/// Contains the results when hovering over an item
27- #[ derive( Debug , Clone ) ]
28+ #[ derive( Debug , Default ) ]
2829pub struct HoverResult {
2930 results : Vec < String > ,
30- exact : bool ,
31- }
32-
33- impl Default for HoverResult {
34- fn default ( ) -> Self {
35- HoverResult :: new ( )
36- }
3731}
3832
3933impl HoverResult {
4034 pub fn new ( ) -> HoverResult {
41- HoverResult {
42- results : Vec :: new ( ) ,
43- // We assume exact by default
44- exact : true ,
45- }
35+ Self :: default ( )
4636 }
4737
4838 pub fn extend ( & mut self , item : Option < String > ) {
4939 self . results . extend ( item) ;
5040 }
5141
52- pub fn is_exact ( & self ) -> bool {
53- self . exact
54- }
55-
5642 pub fn is_empty ( & self ) -> bool {
5743 self . results . is_empty ( )
5844 }
@@ -72,20 +58,7 @@ impl HoverResult {
7258 /// Returns the results converted into markup
7359 /// for displaying in a UI
7460 pub fn to_markup ( & self ) -> String {
75- let mut markup = if !self . exact {
76- let mut msg = String :: from ( "Failed to exactly resolve the symbol. This is probably because rust_analyzer does not yet support traits." ) ;
77- if !self . results . is_empty ( ) {
78- msg. push_str ( " \n These items were found instead:" ) ;
79- }
80- msg. push_str ( "\n \n ---\n " ) ;
81- msg
82- } else {
83- String :: new ( )
84- } ;
85-
86- markup. push_str ( & self . results . join ( "\n \n ---\n " ) ) ;
87-
88- markup
61+ self . results . join ( "\n \n ---\n " )
8962 }
9063}
9164
@@ -94,10 +67,10 @@ fn hover_text(
9467 desc : Option < String > ,
9568 mod_path : Option < String > ,
9669) -> Option < String > {
97- match ( desc, docs , mod_path ) {
98- ( Some ( desc ) , docs , mod_path ) => Some ( rust_code_markup_with_doc ( desc, docs, mod_path) ) ,
99- ( None , Some ( docs ) , _ ) => Some ( docs ) ,
100- _ => None ,
70+ if let Some ( desc) = desc {
71+ Some ( rust_code_markup_with_doc ( & desc, docs. as_deref ( ) , mod_path. as_deref ( ) ) )
72+ } else {
73+ docs
10174 }
10275}
10376
@@ -133,7 +106,7 @@ fn determine_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
133106 . flatten ( )
134107 . join ( "::" )
135108 } ) ;
136- mod_path
109+ mod_path // FIXME: replace dashes with underscores in crate display name
137110}
138111
139112fn hover_text_from_name_kind ( db : & RootDatabase , def : Definition ) -> Option < String > {
@@ -170,9 +143,7 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin
170143 ModuleDef :: TypeAlias ( it) => from_def_source ( db, it, mod_path) ,
171144 ModuleDef :: BuiltinType ( it) => Some ( it. to_string ( ) ) ,
172145 } ,
173- Definition :: Local ( it) => {
174- Some ( rust_code_markup ( it. ty ( db) . display_truncated ( db, None ) . to_string ( ) ) )
175- }
146+ Definition :: Local ( it) => Some ( rust_code_markup ( & it. ty ( db) . display_truncated ( db, None ) ) ) ,
176147 Definition :: TypeParam ( _) | Definition :: SelfType ( _) => {
177148 // FIXME: Hover for generic param
178149 None
@@ -237,7 +208,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
237208 }
238209 } ?;
239210
240- res. extend ( Some ( rust_code_markup ( ty. display_truncated ( db, None ) . to_string ( ) ) ) ) ;
211+ res. extend ( Some ( rust_code_markup ( & ty. display_truncated ( db, None ) ) ) ) ;
241212 let range = sema. original_range ( & node) . range ;
242213 Some ( RangeInfo :: new ( range, res) )
243214}
@@ -595,7 +566,6 @@ fn func(foo: i32) { if true { <|>foo; }; }
595566 ) ;
596567 let hover = analysis. hover ( position) . unwrap ( ) . unwrap ( ) ;
597568 assert_eq ! ( trim_markup_opt( hover. info. first( ) ) , Some ( "wrapper::Thing\n fn new() -> Thing" ) ) ;
598- assert_eq ! ( hover. info. is_exact( ) , true ) ;
599569 }
600570
601571 #[ test]
@@ -618,7 +588,6 @@ fn func(foo: i32) { if true { <|>foo; }; }
618588 ) ;
619589 let hover = analysis. hover ( position) . unwrap ( ) . unwrap ( ) ;
620590 assert_eq ! ( trim_markup_opt( hover. info. first( ) ) , Some ( "const C: u32" ) ) ;
621- assert_eq ! ( hover. info. is_exact( ) , true ) ;
622591 }
623592
624593 #[ test]
@@ -635,7 +604,6 @@ fn func(foo: i32) { if true { <|>foo; }; }
635604 ) ;
636605 let hover = analysis. hover ( position) . unwrap ( ) . unwrap ( ) ;
637606 assert_eq ! ( trim_markup_opt( hover. info. first( ) ) , Some ( "Thing" ) ) ;
638- assert_eq ! ( hover. info. is_exact( ) , true ) ;
639607
640608 /* FIXME: revive these tests
641609 let (analysis, position) = single_file_with_position(
@@ -651,7 +619,6 @@ fn func(foo: i32) { if true { <|>foo; }; }
651619
652620 let hover = analysis.hover(position).unwrap().unwrap();
653621 assert_eq!(trim_markup_opt(hover.info.first()), Some("Thing"));
654- assert_eq!(hover.info.is_exact(), true);
655622
656623 let (analysis, position) = single_file_with_position(
657624 "
@@ -665,7 +632,6 @@ fn func(foo: i32) { if true { <|>foo; }; }
665632 );
666633 let hover = analysis.hover(position).unwrap().unwrap();
667634 assert_eq!(trim_markup_opt(hover.info.first()), Some("enum Thing"));
668- assert_eq!(hover.info.is_exact(), true);
669635
670636 let (analysis, position) = single_file_with_position(
671637 "
@@ -678,7 +644,6 @@ fn func(foo: i32) { if true { <|>foo; }; }
678644 );
679645 let hover = analysis.hover(position).unwrap().unwrap();
680646 assert_eq!(trim_markup_opt(hover.info.first()), Some("enum Thing"));
681- assert_eq!(hover.info.is_exact(), true);
682647 */
683648 }
684649
@@ -696,7 +661,6 @@ fn func(foo: i32) { if true { <|>foo; }; }
696661 ) ;
697662 let hover = analysis. hover ( position) . unwrap ( ) . unwrap ( ) ;
698663 assert_eq ! ( trim_markup_opt( hover. info. first( ) ) , Some ( "i32" ) ) ;
699- assert_eq ! ( hover. info. is_exact( ) , true ) ;
700664 }
701665
702666 #[ test]
@@ -714,7 +678,6 @@ fn func(foo: i32) { if true { <|>foo; }; }
714678 ) ;
715679 let hover = analysis. hover ( position) . unwrap ( ) . unwrap ( ) ;
716680 assert_eq ! ( trim_markup_opt( hover. info. first( ) ) , Some ( "macro_rules! foo" ) ) ;
717- assert_eq ! ( hover. info. is_exact( ) , true ) ;
718681 }
719682
720683 #[ test]
@@ -726,7 +689,6 @@ fn func(foo: i32) { if true { <|>foo; }; }
726689 ) ;
727690 let hover = analysis. hover ( position) . unwrap ( ) . unwrap ( ) ;
728691 assert_eq ! ( trim_markup_opt( hover. info. first( ) ) , Some ( "i32" ) ) ;
729- assert_eq ! ( hover. info. is_exact( ) , true ) ;
730692 }
731693
732694 #[ test]
0 commit comments