From 983726a45cf3daafbf4b245e220773bd12897bb7 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 9 Mar 2021 16:33:23 +0100 Subject: [PATCH 1/2] Don't show const items initializer expressions on hover --- crates/ide/src/display/short_label.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crates/ide/src/display/short_label.rs b/crates/ide/src/display/short_label.rs index 84b8883de5f..2df9266b41c 100644 --- a/crates/ide/src/display/short_label.rs +++ b/crates/ide/src/display/short_label.rs @@ -71,11 +71,7 @@ fn short_label(&self) -> Option { impl ShortLabel for ast::Const { fn short_label(&self) -> Option { - let mut new_buf = short_label_from_ty(self, self.ty(), "const ")?; - if let Some(expr) = self.body() { - format_to!(new_buf, " = {}", expr.syntax()); - } - Some(new_buf) + short_label_from_ty(self, self.ty(), "const ") } } From 814d617d348c23507fd444e58515c9bff6b73105 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 9 Mar 2021 16:33:41 +0100 Subject: [PATCH 2/2] Show whether a binding is mutable or not on hover --- crates/hir/src/lib.rs | 8 +-- crates/ide/src/hover.rs | 62 +++++++++++++------ .../ide/src/syntax_highlighting/highlight.rs | 5 +- 3 files changed, 47 insertions(+), 28 deletions(-) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index d5a3d9034c1..caf4c3395c5 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1296,13 +1296,7 @@ pub fn is_self(self, db: &dyn HirDatabase) -> bool { pub fn is_mut(self, db: &dyn HirDatabase) -> bool { let body = db.body(self.parent.into()); - match &body[self.pat_id] { - Pat::Bind { mode, .. } => match mode { - BindingAnnotation::Mutable | BindingAnnotation::RefMut => true, - _ => false, - }, - _ => false, - } + matches!(&body[self.pat_id], Pat::Bind { mode: BindingAnnotation::Mutable, .. }) } pub fn parent(self, _db: &dyn HirDatabase) -> DefWithBody { diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 5d1cc20529a..ea45086ce8f 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -1,3 +1,4 @@ +use either::Either; use hir::{ Adt, AsAssocItem, AssocItemContainer, FieldSource, GenericParam, HasAttrs, HasSource, HirDisplay, Module, ModuleDef, ModuleSource, Semantics, @@ -366,7 +367,7 @@ fn hover_for_definition( .and_then(|fd| hover_for_builtin(fd, it)) .or_else(|| Some(Markup::fenced_block(&it.name()))), }, - Definition::Local(it) => Some(Markup::fenced_block(&it.ty(db).display(db))), + Definition::Local(it) => hover_for_local(it, db), Definition::SelfType(impl_def) => { impl_def.target_ty(db).as_adt().and_then(|adt| match adt { Adt::Struct(it) => from_def_source(db, it, mod_path), @@ -405,6 +406,29 @@ fn from_def_source_labeled( } } +fn hover_for_local(it: hir::Local, db: &RootDatabase) -> Option { + let ty = it.ty(db); + let ty = ty.display(db); + let is_mut = if it.is_mut(db) { "mut " } else { "" }; + let desc = match it.source(db).value { + Either::Left(ident) => { + let name = it.name(db).unwrap(); + let let_kw = if ident + .syntax() + .parent() + .map_or(false, |p| p.kind() == LET_STMT || p.kind() == CONDITION) + { + "let " + } else { + "" + }; + format!("{}{}{}: {}", let_kw, is_mut, name, ty) + } + Either::Right(_) => format!("{}self: {}", is_mut, ty), + }; + hover_markup(None, Some(desc), None) +} + fn hover_for_keyword( sema: &Semantics, links_in_hover: bool, @@ -574,7 +598,7 @@ fn main() { *iter* ```rust - Iter>, |&mut u32, &u32, &mut u32| -> Option, u32>> + let mut iter: Iter>, |&mut u32, &u32, &mut u32| -> Option, u32>> ``` "#]], ); @@ -798,7 +822,7 @@ fn hover_const_static() { ``` ```rust - const foo: u32 = 123 + const foo: u32 ``` "#]], ); @@ -831,7 +855,7 @@ fn main() { *zz* ```rust - Test + let zz: Test ``` "#]], ); @@ -870,7 +894,7 @@ enum Option { Some(T) } *bar* ```rust - Option + let bar: Option ``` "#]], ); @@ -938,7 +962,7 @@ fn hover_for_local_variable() { *foo* ```rust - i32 + foo: i32 ``` "#]], ) @@ -952,7 +976,7 @@ fn hover_for_local_variable_pat() { *foo* ```rust - i32 + foo: i32 ``` "#]], ) @@ -966,7 +990,7 @@ fn hover_local_var_edge() { *foo* ```rust - i32 + foo: i32 ``` "#]], ) @@ -980,7 +1004,7 @@ fn hover_for_param_edge() { *foo* ```rust - i32 + foo: i32 ``` "#]], ) @@ -1000,7 +1024,7 @@ fn f(_x$0: impl Deref + DerefMut) {}"#, *_x* ```rust - impl Deref + DerefMut + _x: impl Deref + DerefMut ``` "#]], ) @@ -1022,7 +1046,7 @@ fn new() -> Thing { Thing { x: 0 } } *foo_test* ```rust - Thing + let foo_test: Thing ``` "#]], ) @@ -1081,7 +1105,7 @@ fn main() { ``` ```rust - const C: u32 = 1 + const C: u32 ``` "#]], ) @@ -1182,7 +1206,7 @@ fn y() { *x* ```rust - i32 + let x: i32 ``` "#]], ) @@ -1259,7 +1283,7 @@ macro_rules! id { ($($tt:tt)*) => { $($tt)* } } *bar* ```rust - u32 + bar: u32 ``` "#]], ); @@ -1277,7 +1301,7 @@ macro_rules! id { ($($tt:tt)*) => { id_deep!($($tt)*) } } *bar* ```rust - u32 + bar: u32 ``` "#]], ); @@ -3302,7 +3326,7 @@ fn main() { *f* ```rust - &i32 + f: &i32 ``` "#]], ); @@ -3321,7 +3345,7 @@ fn bar(&sel$0f) {} *self* ```rust - &Foo + self: &Foo ``` "#]], ); @@ -3341,7 +3365,7 @@ fn bar(sel$0f: Arc) {} *self* ```rust - Arc + self: Arc ``` "#]], ); @@ -3537,7 +3561,7 @@ fn foo() { ``` ```rust - const FOO: usize = 3 + const FOO: usize ``` --- diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 24fcbb5845a..b0cfdd8b7dc 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 { HlTag::Symbol(SymbolKind::Local) }; let mut h = Highlight::new(tag); - if local.is_mut(db) || local.ty(db).is_mutable_reference() { + let ty = local.ty(db); + if local.is_mut(db) || ty.is_mutable_reference() { h |= HlMod::Mutable; } - if local.ty(db).as_callable(db).is_some() || local.ty(db).impls_fnonce(db) { + if ty.as_callable(db).is_some() || ty.impls_fnonce(db) { h |= HlMod::Callable; } return h;