diff options
Diffstat (limited to 'crates/ide/src/hover.rs')
-rw-r--r-- | crates/ide/src/hover.rs | 71 |
1 files changed, 47 insertions, 24 deletions
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index a9454cfa3..ea45086ce 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs | |||
@@ -1,3 +1,4 @@ | |||
1 | use either::Either; | ||
1 | use hir::{ | 2 | use hir::{ |
2 | Adt, AsAssocItem, AssocItemContainer, FieldSource, GenericParam, HasAttrs, HasSource, | 3 | Adt, AsAssocItem, AssocItemContainer, FieldSource, GenericParam, HasAttrs, HasSource, |
3 | HirDisplay, Module, ModuleDef, ModuleSource, Semantics, | 4 | HirDisplay, Module, ModuleDef, ModuleSource, Semantics, |
@@ -11,7 +12,6 @@ use ide_db::{ | |||
11 | use itertools::Itertools; | 12 | use itertools::Itertools; |
12 | use stdx::format_to; | 13 | use stdx::format_to; |
13 | use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T}; | 14 | use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T}; |
14 | use test_utils::mark; | ||
15 | 15 | ||
16 | use crate::{ | 16 | use crate::{ |
17 | display::{macro_label, ShortLabel, TryToNav}, | 17 | display::{macro_label, ShortLabel, TryToNav}, |
@@ -193,8 +193,8 @@ fn runnable_action( | |||
193 | ModuleDef::Function(func) => { | 193 | ModuleDef::Function(func) => { |
194 | let src = func.source(sema.db)?; | 194 | let src = func.source(sema.db)?; |
195 | if src.file_id != file_id.into() { | 195 | if src.file_id != file_id.into() { |
196 | mark::hit!(hover_macro_generated_struct_fn_doc_comment); | 196 | cov_mark::hit!(hover_macro_generated_struct_fn_doc_comment); |
197 | mark::hit!(hover_macro_generated_struct_fn_doc_attr); | 197 | cov_mark::hit!(hover_macro_generated_struct_fn_doc_attr); |
198 | return None; | 198 | return None; |
199 | } | 199 | } |
200 | 200 | ||
@@ -367,7 +367,7 @@ fn hover_for_definition( | |||
367 | .and_then(|fd| hover_for_builtin(fd, it)) | 367 | .and_then(|fd| hover_for_builtin(fd, it)) |
368 | .or_else(|| Some(Markup::fenced_block(&it.name()))), | 368 | .or_else(|| Some(Markup::fenced_block(&it.name()))), |
369 | }, | 369 | }, |
370 | Definition::Local(it) => Some(Markup::fenced_block(&it.ty(db).display(db))), | 370 | Definition::Local(it) => hover_for_local(it, db), |
371 | Definition::SelfType(impl_def) => { | 371 | Definition::SelfType(impl_def) => { |
372 | impl_def.target_ty(db).as_adt().and_then(|adt| match adt { | 372 | impl_def.target_ty(db).as_adt().and_then(|adt| match adt { |
373 | Adt::Struct(it) => from_def_source(db, it, mod_path), | 373 | Adt::Struct(it) => from_def_source(db, it, mod_path), |
@@ -406,6 +406,29 @@ fn hover_for_definition( | |||
406 | } | 406 | } |
407 | } | 407 | } |
408 | 408 | ||
409 | fn hover_for_local(it: hir::Local, db: &RootDatabase) -> Option<Markup> { | ||
410 | let ty = it.ty(db); | ||
411 | let ty = ty.display(db); | ||
412 | let is_mut = if it.is_mut(db) { "mut " } else { "" }; | ||
413 | let desc = match it.source(db).value { | ||
414 | Either::Left(ident) => { | ||
415 | let name = it.name(db).unwrap(); | ||
416 | let let_kw = if ident | ||
417 | .syntax() | ||
418 | .parent() | ||
419 | .map_or(false, |p| p.kind() == LET_STMT || p.kind() == CONDITION) | ||
420 | { | ||
421 | "let " | ||
422 | } else { | ||
423 | "" | ||
424 | }; | ||
425 | format!("{}{}{}: {}", let_kw, is_mut, name, ty) | ||
426 | } | ||
427 | Either::Right(_) => format!("{}self: {}", is_mut, ty), | ||
428 | }; | ||
429 | hover_markup(None, Some(desc), None) | ||
430 | } | ||
431 | |||
409 | fn hover_for_keyword( | 432 | fn hover_for_keyword( |
410 | sema: &Semantics<RootDatabase>, | 433 | sema: &Semantics<RootDatabase>, |
411 | links_in_hover: bool, | 434 | links_in_hover: bool, |
@@ -575,7 +598,7 @@ fn main() { | |||
575 | *iter* | 598 | *iter* |
576 | 599 | ||
577 | ```rust | 600 | ```rust |
578 | Iter<Scan<OtherStruct<OtherStruct<i32>>, |&mut u32, &u32, &mut u32| -> Option<u32>, u32>> | 601 | let mut iter: Iter<Scan<OtherStruct<OtherStruct<i32>>, |&mut u32, &u32, &mut u32| -> Option<u32>, u32>> |
579 | ``` | 602 | ``` |
580 | "#]], | 603 | "#]], |
581 | ); | 604 | ); |
@@ -799,7 +822,7 @@ fn main() { | |||
799 | ``` | 822 | ``` |
800 | 823 | ||
801 | ```rust | 824 | ```rust |
802 | const foo: u32 = 123 | 825 | const foo: u32 |
803 | ``` | 826 | ``` |
804 | "#]], | 827 | "#]], |
805 | ); | 828 | ); |
@@ -832,7 +855,7 @@ fn main() { | |||
832 | *zz* | 855 | *zz* |
833 | 856 | ||
834 | ```rust | 857 | ```rust |
835 | Test<i32, u8> | 858 | let zz: Test<i32, u8> |
836 | ``` | 859 | ``` |
837 | "#]], | 860 | "#]], |
838 | ); | 861 | ); |
@@ -871,7 +894,7 @@ fn main() { let b$0ar = Some(12); } | |||
871 | *bar* | 894 | *bar* |
872 | 895 | ||
873 | ```rust | 896 | ```rust |
874 | Option<i32> | 897 | let bar: Option<i32> |
875 | ``` | 898 | ``` |
876 | "#]], | 899 | "#]], |
877 | ); | 900 | ); |
@@ -939,7 +962,7 @@ fn main() { | |||
939 | *foo* | 962 | *foo* |
940 | 963 | ||
941 | ```rust | 964 | ```rust |
942 | i32 | 965 | foo: i32 |
943 | ``` | 966 | ``` |
944 | "#]], | 967 | "#]], |
945 | ) | 968 | ) |
@@ -953,7 +976,7 @@ fn main() { | |||
953 | *foo* | 976 | *foo* |
954 | 977 | ||
955 | ```rust | 978 | ```rust |
956 | i32 | 979 | foo: i32 |
957 | ``` | 980 | ``` |
958 | "#]], | 981 | "#]], |
959 | ) | 982 | ) |
@@ -967,7 +990,7 @@ fn main() { | |||
967 | *foo* | 990 | *foo* |
968 | 991 | ||
969 | ```rust | 992 | ```rust |
970 | i32 | 993 | foo: i32 |
971 | ``` | 994 | ``` |
972 | "#]], | 995 | "#]], |
973 | ) | 996 | ) |
@@ -981,7 +1004,7 @@ fn main() { | |||
981 | *foo* | 1004 | *foo* |
982 | 1005 | ||
983 | ```rust | 1006 | ```rust |
984 | i32 | 1007 | foo: i32 |
985 | ``` | 1008 | ``` |
986 | "#]], | 1009 | "#]], |
987 | ) | 1010 | ) |
@@ -1001,7 +1024,7 @@ fn main() { | |||
1001 | *_x* | 1024 | *_x* |
1002 | 1025 | ||
1003 | ```rust | 1026 | ```rust |
1004 | impl Deref<Target = u8> + DerefMut<Target = u8> | 1027 | _x: impl Deref<Target = u8> + DerefMut<Target = u8> |
1005 | ``` | 1028 | ``` |
1006 | "#]], | 1029 | "#]], |
1007 | ) | 1030 | ) |
@@ -1023,7 +1046,7 @@ fn main() { let foo_$0test = Thing::new(); } | |||
1023 | *foo_test* | 1046 | *foo_test* |
1024 | 1047 | ||
1025 | ```rust | 1048 | ```rust |
1026 | Thing | 1049 | let foo_test: Thing |
1027 | ``` | 1050 | ``` |
1028 | "#]], | 1051 | "#]], |
1029 | ) | 1052 | ) |
@@ -1082,7 +1105,7 @@ fn main() { | |||
1082 | ``` | 1105 | ``` |
1083 | 1106 | ||
1084 | ```rust | 1107 | ```rust |
1085 | const C: u32 = 1 | 1108 | const C: u32 |
1086 | ``` | 1109 | ``` |
1087 | "#]], | 1110 | "#]], |
1088 | ) | 1111 | ) |
@@ -1183,7 +1206,7 @@ fn y() { | |||
1183 | *x* | 1206 | *x* |
1184 | 1207 | ||
1185 | ```rust | 1208 | ```rust |
1186 | i32 | 1209 | let x: i32 |
1187 | ``` | 1210 | ``` |
1188 | "#]], | 1211 | "#]], |
1189 | ) | 1212 | ) |
@@ -1260,7 +1283,7 @@ fn foo(bar:u32) { let a = id!(ba$0r); } | |||
1260 | *bar* | 1283 | *bar* |
1261 | 1284 | ||
1262 | ```rust | 1285 | ```rust |
1263 | u32 | 1286 | bar: u32 |
1264 | ``` | 1287 | ``` |
1265 | "#]], | 1288 | "#]], |
1266 | ); | 1289 | ); |
@@ -1278,7 +1301,7 @@ fn foo(bar:u32) { let a = id!(ba$0r); } | |||
1278 | *bar* | 1301 | *bar* |
1279 | 1302 | ||
1280 | ```rust | 1303 | ```rust |
1281 | u32 | 1304 | bar: u32 |
1282 | ``` | 1305 | ``` |
1283 | "#]], | 1306 | "#]], |
1284 | ); | 1307 | ); |
@@ -2101,7 +2124,7 @@ pub fn fo$0o() {} | |||
2101 | 2124 | ||
2102 | #[test] | 2125 | #[test] |
2103 | fn test_hover_macro_generated_struct_fn_doc_comment() { | 2126 | fn test_hover_macro_generated_struct_fn_doc_comment() { |
2104 | mark::check!(hover_macro_generated_struct_fn_doc_comment); | 2127 | cov_mark::check!(hover_macro_generated_struct_fn_doc_comment); |
2105 | 2128 | ||
2106 | check( | 2129 | check( |
2107 | r#" | 2130 | r#" |
@@ -2139,7 +2162,7 @@ fn foo() { let bar = Bar; bar.fo$0o(); } | |||
2139 | 2162 | ||
2140 | #[test] | 2163 | #[test] |
2141 | fn test_hover_macro_generated_struct_fn_doc_attr() { | 2164 | fn test_hover_macro_generated_struct_fn_doc_attr() { |
2142 | mark::check!(hover_macro_generated_struct_fn_doc_attr); | 2165 | cov_mark::check!(hover_macro_generated_struct_fn_doc_attr); |
2143 | 2166 | ||
2144 | check( | 2167 | check( |
2145 | r#" | 2168 | r#" |
@@ -3303,7 +3326,7 @@ fn main() { | |||
3303 | *f* | 3326 | *f* |
3304 | 3327 | ||
3305 | ```rust | 3328 | ```rust |
3306 | &i32 | 3329 | f: &i32 |
3307 | ``` | 3330 | ``` |
3308 | "#]], | 3331 | "#]], |
3309 | ); | 3332 | ); |
@@ -3322,7 +3345,7 @@ impl Foo { | |||
3322 | *self* | 3345 | *self* |
3323 | 3346 | ||
3324 | ```rust | 3347 | ```rust |
3325 | &Foo | 3348 | self: &Foo |
3326 | ``` | 3349 | ``` |
3327 | "#]], | 3350 | "#]], |
3328 | ); | 3351 | ); |
@@ -3342,7 +3365,7 @@ impl Foo { | |||
3342 | *self* | 3365 | *self* |
3343 | 3366 | ||
3344 | ```rust | 3367 | ```rust |
3345 | Arc<Foo> | 3368 | self: Arc<Foo> |
3346 | ``` | 3369 | ``` |
3347 | "#]], | 3370 | "#]], |
3348 | ); | 3371 | ); |
@@ -3538,7 +3561,7 @@ fn foo() { | |||
3538 | ``` | 3561 | ``` |
3539 | 3562 | ||
3540 | ```rust | 3563 | ```rust |
3541 | const FOO: usize = 3 | 3564 | const FOO: usize |
3542 | ``` | 3565 | ``` |
3543 | 3566 | ||
3544 | --- | 3567 | --- |