diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-03-09 16:23:51 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-03-09 16:23:51 +0000 |
commit | c45ac6effe37562daa97e6675c54963252f65664 (patch) | |
tree | 9f00979cde7fa508ab0b583fa0deeaa79236b7db /crates/ide/src | |
parent | c2359608c926d4828f2b112e7069978449851d58 (diff) | |
parent | 814d617d348c23507fd444e58515c9bff6b73105 (diff) |
Merge #7942
7942: Show whether a binding is mutable or not on hover r=Veykril a=Veykril
bors r+
Co-authored-by: Lukas Wirth <[email protected]>
Diffstat (limited to 'crates/ide/src')
-rw-r--r-- | crates/ide/src/display/short_label.rs | 6 | ||||
-rw-r--r-- | crates/ide/src/hover.rs | 62 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/highlight.rs | 5 |
3 files changed, 47 insertions, 26 deletions
diff --git a/crates/ide/src/display/short_label.rs b/crates/ide/src/display/short_label.rs index 84b8883de..2df9266b4 100644 --- a/crates/ide/src/display/short_label.rs +++ b/crates/ide/src/display/short_label.rs | |||
@@ -71,11 +71,7 @@ impl ShortLabel for ast::TypeAlias { | |||
71 | 71 | ||
72 | impl ShortLabel for ast::Const { | 72 | impl ShortLabel for ast::Const { |
73 | fn short_label(&self) -> Option<String> { | 73 | fn short_label(&self) -> Option<String> { |
74 | let mut new_buf = short_label_from_ty(self, self.ty(), "const ")?; | 74 | short_label_from_ty(self, self.ty(), "const ") |
75 | if let Some(expr) = self.body() { | ||
76 | format_to!(new_buf, " = {}", expr.syntax()); | ||
77 | } | ||
78 | Some(new_buf) | ||
79 | } | 75 | } |
80 | } | 76 | } |
81 | 77 | ||
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 5d1cc2052..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, |
@@ -366,7 +367,7 @@ fn hover_for_definition( | |||
366 | .and_then(|fd| hover_for_builtin(fd, it)) | 367 | .and_then(|fd| hover_for_builtin(fd, it)) |
367 | .or_else(|| Some(Markup::fenced_block(&it.name()))), | 368 | .or_else(|| Some(Markup::fenced_block(&it.name()))), |
368 | }, | 369 | }, |
369 | Definition::Local(it) => Some(Markup::fenced_block(&it.ty(db).display(db))), | 370 | Definition::Local(it) => hover_for_local(it, db), |
370 | Definition::SelfType(impl_def) => { | 371 | Definition::SelfType(impl_def) => { |
371 | 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 { |
372 | Adt::Struct(it) => from_def_source(db, it, mod_path), | 373 | Adt::Struct(it) => from_def_source(db, it, mod_path), |
@@ -405,6 +406,29 @@ fn hover_for_definition( | |||
405 | } | 406 | } |
406 | } | 407 | } |
407 | 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 | |||
408 | fn hover_for_keyword( | 432 | fn hover_for_keyword( |
409 | sema: &Semantics<RootDatabase>, | 433 | sema: &Semantics<RootDatabase>, |
410 | links_in_hover: bool, | 434 | links_in_hover: bool, |
@@ -574,7 +598,7 @@ fn main() { | |||
574 | *iter* | 598 | *iter* |
575 | 599 | ||
576 | ```rust | 600 | ```rust |
577 | 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>> |
578 | ``` | 602 | ``` |
579 | "#]], | 603 | "#]], |
580 | ); | 604 | ); |
@@ -798,7 +822,7 @@ fn main() { | |||
798 | ``` | 822 | ``` |
799 | 823 | ||
800 | ```rust | 824 | ```rust |
801 | const foo: u32 = 123 | 825 | const foo: u32 |
802 | ``` | 826 | ``` |
803 | "#]], | 827 | "#]], |
804 | ); | 828 | ); |
@@ -831,7 +855,7 @@ fn main() { | |||
831 | *zz* | 855 | *zz* |
832 | 856 | ||
833 | ```rust | 857 | ```rust |
834 | Test<i32, u8> | 858 | let zz: Test<i32, u8> |
835 | ``` | 859 | ``` |
836 | "#]], | 860 | "#]], |
837 | ); | 861 | ); |
@@ -870,7 +894,7 @@ fn main() { let b$0ar = Some(12); } | |||
870 | *bar* | 894 | *bar* |
871 | 895 | ||
872 | ```rust | 896 | ```rust |
873 | Option<i32> | 897 | let bar: Option<i32> |
874 | ``` | 898 | ``` |
875 | "#]], | 899 | "#]], |
876 | ); | 900 | ); |
@@ -938,7 +962,7 @@ fn main() { | |||
938 | *foo* | 962 | *foo* |
939 | 963 | ||
940 | ```rust | 964 | ```rust |
941 | i32 | 965 | foo: i32 |
942 | ``` | 966 | ``` |
943 | "#]], | 967 | "#]], |
944 | ) | 968 | ) |
@@ -952,7 +976,7 @@ fn main() { | |||
952 | *foo* | 976 | *foo* |
953 | 977 | ||
954 | ```rust | 978 | ```rust |
955 | i32 | 979 | foo: i32 |
956 | ``` | 980 | ``` |
957 | "#]], | 981 | "#]], |
958 | ) | 982 | ) |
@@ -966,7 +990,7 @@ fn main() { | |||
966 | *foo* | 990 | *foo* |
967 | 991 | ||
968 | ```rust | 992 | ```rust |
969 | i32 | 993 | foo: i32 |
970 | ``` | 994 | ``` |
971 | "#]], | 995 | "#]], |
972 | ) | 996 | ) |
@@ -980,7 +1004,7 @@ fn main() { | |||
980 | *foo* | 1004 | *foo* |
981 | 1005 | ||
982 | ```rust | 1006 | ```rust |
983 | i32 | 1007 | foo: i32 |
984 | ``` | 1008 | ``` |
985 | "#]], | 1009 | "#]], |
986 | ) | 1010 | ) |
@@ -1000,7 +1024,7 @@ fn main() { | |||
1000 | *_x* | 1024 | *_x* |
1001 | 1025 | ||
1002 | ```rust | 1026 | ```rust |
1003 | impl Deref<Target = u8> + DerefMut<Target = u8> | 1027 | _x: impl Deref<Target = u8> + DerefMut<Target = u8> |
1004 | ``` | 1028 | ``` |
1005 | "#]], | 1029 | "#]], |
1006 | ) | 1030 | ) |
@@ -1022,7 +1046,7 @@ fn main() { let foo_$0test = Thing::new(); } | |||
1022 | *foo_test* | 1046 | *foo_test* |
1023 | 1047 | ||
1024 | ```rust | 1048 | ```rust |
1025 | Thing | 1049 | let foo_test: Thing |
1026 | ``` | 1050 | ``` |
1027 | "#]], | 1051 | "#]], |
1028 | ) | 1052 | ) |
@@ -1081,7 +1105,7 @@ fn main() { | |||
1081 | ``` | 1105 | ``` |
1082 | 1106 | ||
1083 | ```rust | 1107 | ```rust |
1084 | const C: u32 = 1 | 1108 | const C: u32 |
1085 | ``` | 1109 | ``` |
1086 | "#]], | 1110 | "#]], |
1087 | ) | 1111 | ) |
@@ -1182,7 +1206,7 @@ fn y() { | |||
1182 | *x* | 1206 | *x* |
1183 | 1207 | ||
1184 | ```rust | 1208 | ```rust |
1185 | i32 | 1209 | let x: i32 |
1186 | ``` | 1210 | ``` |
1187 | "#]], | 1211 | "#]], |
1188 | ) | 1212 | ) |
@@ -1259,7 +1283,7 @@ fn foo(bar:u32) { let a = id!(ba$0r); } | |||
1259 | *bar* | 1283 | *bar* |
1260 | 1284 | ||
1261 | ```rust | 1285 | ```rust |
1262 | u32 | 1286 | bar: u32 |
1263 | ``` | 1287 | ``` |
1264 | "#]], | 1288 | "#]], |
1265 | ); | 1289 | ); |
@@ -1277,7 +1301,7 @@ fn foo(bar:u32) { let a = id!(ba$0r); } | |||
1277 | *bar* | 1301 | *bar* |
1278 | 1302 | ||
1279 | ```rust | 1303 | ```rust |
1280 | u32 | 1304 | bar: u32 |
1281 | ``` | 1305 | ``` |
1282 | "#]], | 1306 | "#]], |
1283 | ); | 1307 | ); |
@@ -3302,7 +3326,7 @@ fn main() { | |||
3302 | *f* | 3326 | *f* |
3303 | 3327 | ||
3304 | ```rust | 3328 | ```rust |
3305 | &i32 | 3329 | f: &i32 |
3306 | ``` | 3330 | ``` |
3307 | "#]], | 3331 | "#]], |
3308 | ); | 3332 | ); |
@@ -3321,7 +3345,7 @@ impl Foo { | |||
3321 | *self* | 3345 | *self* |
3322 | 3346 | ||
3323 | ```rust | 3347 | ```rust |
3324 | &Foo | 3348 | self: &Foo |
3325 | ``` | 3349 | ``` |
3326 | "#]], | 3350 | "#]], |
3327 | ); | 3351 | ); |
@@ -3341,7 +3365,7 @@ impl Foo { | |||
3341 | *self* | 3365 | *self* |
3342 | 3366 | ||
3343 | ```rust | 3367 | ```rust |
3344 | Arc<Foo> | 3368 | self: Arc<Foo> |
3345 | ``` | 3369 | ``` |
3346 | "#]], | 3370 | "#]], |
3347 | ); | 3371 | ); |
@@ -3537,7 +3561,7 @@ fn foo() { | |||
3537 | ``` | 3561 | ``` |
3538 | 3562 | ||
3539 | ```rust | 3563 | ```rust |
3540 | const FOO: usize = 3 | 3564 | const FOO: usize |
3541 | ``` | 3565 | ``` |
3542 | 3566 | ||
3543 | --- | 3567 | --- |
diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 24fcbb584..b0cfdd8b7 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs | |||
@@ -330,10 +330,11 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { | |||
330 | HlTag::Symbol(SymbolKind::Local) | 330 | HlTag::Symbol(SymbolKind::Local) |
331 | }; | 331 | }; |
332 | let mut h = Highlight::new(tag); | 332 | let mut h = Highlight::new(tag); |
333 | if local.is_mut(db) || local.ty(db).is_mutable_reference() { | 333 | let ty = local.ty(db); |
334 | if local.is_mut(db) || ty.is_mutable_reference() { | ||
334 | h |= HlMod::Mutable; | 335 | h |= HlMod::Mutable; |
335 | } | 336 | } |
336 | if local.ty(db).as_callable(db).is_some() || local.ty(db).impls_fnonce(db) { | 337 | if ty.as_callable(db).is_some() || ty.impls_fnonce(db) { |
337 | h |= HlMod::Callable; | 338 | h |= HlMod::Callable; |
338 | } | 339 | } |
339 | return h; | 340 | return h; |