Skip to content

Commit 814d617

Browse files
committed
Show whether a binding is mutable or not on hover
1 parent 983726a commit 814d617

File tree

3 files changed

+47
-28
lines changed

3 files changed

+47
-28
lines changed

crates/hir/src/lib.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,13 +1296,7 @@ impl Local {
12961296

12971297
pub fn is_mut(self, db: &dyn HirDatabase) -> bool {
12981298
let body = db.body(self.parent.into());
1299-
match &body[self.pat_id] {
1300-
Pat::Bind { mode, .. } => match mode {
1301-
BindingAnnotation::Mutable | BindingAnnotation::RefMut => true,
1302-
_ => false,
1303-
},
1304-
_ => false,
1305-
}
1299+
matches!(&body[self.pat_id], Pat::Bind { mode: BindingAnnotation::Mutable, .. })
13061300
}
13071301

13081302
pub fn parent(self, _db: &dyn HirDatabase) -> DefWithBody {

crates/ide/src/hover.rs

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use either::Either;
12
use hir::{
23
Adt, AsAssocItem, AssocItemContainer, FieldSource, GenericParam, HasAttrs, HasSource,
34
HirDisplay, Module, ModuleDef, ModuleSource, Semantics,
@@ -366,7 +367,7 @@ fn hover_for_definition(
366367
.and_then(|fd| hover_for_builtin(fd, it))
367368
.or_else(|| Some(Markup::fenced_block(&it.name()))),
368369
},
369-
Definition::Local(it) => Some(Markup::fenced_block(&it.ty(db).display(db))),
370+
Definition::Local(it) => hover_for_local(it, db),
370371
Definition::SelfType(impl_def) => {
371372
impl_def.target_ty(db).as_adt().and_then(|adt| match adt {
372373
Adt::Struct(it) => from_def_source(db, it, mod_path),
@@ -405,6 +406,29 @@ fn hover_for_definition(
405406
}
406407
}
407408

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+
408432
fn hover_for_keyword(
409433
sema: &Semantics<RootDatabase>,
410434
links_in_hover: bool,
@@ -574,7 +598,7 @@ fn main() {
574598
*iter*
575599
576600
```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>>
578602
```
579603
"#]],
580604
);
@@ -798,7 +822,7 @@ fn main() {
798822
```
799823
800824
```rust
801-
const foo: u32 = 123
825+
const foo: u32
802826
```
803827
"#]],
804828
);
@@ -831,7 +855,7 @@ fn main() {
831855
*zz*
832856
833857
```rust
834-
Test<i32, u8>
858+
let zz: Test<i32, u8>
835859
```
836860
"#]],
837861
);
@@ -870,7 +894,7 @@ fn main() { let b$0ar = Some(12); }
870894
*bar*
871895
872896
```rust
873-
Option<i32>
897+
let bar: Option<i32>
874898
```
875899
"#]],
876900
);
@@ -938,7 +962,7 @@ fn main() {
938962
*foo*
939963
940964
```rust
941-
i32
965+
foo: i32
942966
```
943967
"#]],
944968
)
@@ -952,7 +976,7 @@ fn main() {
952976
*foo*
953977
954978
```rust
955-
i32
979+
foo: i32
956980
```
957981
"#]],
958982
)
@@ -966,7 +990,7 @@ fn main() {
966990
*foo*
967991
968992
```rust
969-
i32
993+
foo: i32
970994
```
971995
"#]],
972996
)
@@ -980,7 +1004,7 @@ fn main() {
9801004
*foo*
9811005
9821006
```rust
983-
i32
1007+
foo: i32
9841008
```
9851009
"#]],
9861010
)
@@ -1000,7 +1024,7 @@ fn main() {
10001024
*_x*
10011025
10021026
```rust
1003-
impl Deref<Target = u8> + DerefMut<Target = u8>
1027+
_x: impl Deref<Target = u8> + DerefMut<Target = u8>
10041028
```
10051029
"#]],
10061030
)
@@ -1022,7 +1046,7 @@ fn main() { let foo_$0test = Thing::new(); }
10221046
*foo_test*
10231047
10241048
```rust
1025-
Thing
1049+
let foo_test: Thing
10261050
```
10271051
"#]],
10281052
)
@@ -1081,7 +1105,7 @@ fn main() {
10811105
```
10821106
10831107
```rust
1084-
const C: u32 = 1
1108+
const C: u32
10851109
```
10861110
"#]],
10871111
)
@@ -1182,7 +1206,7 @@ fn y() {
11821206
*x*
11831207
11841208
```rust
1185-
i32
1209+
let x: i32
11861210
```
11871211
"#]],
11881212
)
@@ -1259,7 +1283,7 @@ fn foo(bar:u32) { let a = id!(ba$0r); }
12591283
*bar*
12601284
12611285
```rust
1262-
u32
1286+
bar: u32
12631287
```
12641288
"#]],
12651289
);
@@ -1277,7 +1301,7 @@ fn foo(bar:u32) { let a = id!(ba$0r); }
12771301
*bar*
12781302
12791303
```rust
1280-
u32
1304+
bar: u32
12811305
```
12821306
"#]],
12831307
);
@@ -3302,7 +3326,7 @@ fn main() {
33023326
*f*
33033327
33043328
```rust
3305-
&i32
3329+
f: &i32
33063330
```
33073331
"#]],
33083332
);
@@ -3321,7 +3345,7 @@ impl Foo {
33213345
*self*
33223346
33233347
```rust
3324-
&Foo
3348+
self: &Foo
33253349
```
33263350
"#]],
33273351
);
@@ -3341,7 +3365,7 @@ impl Foo {
33413365
*self*
33423366
33433367
```rust
3344-
Arc<Foo>
3368+
self: Arc<Foo>
33453369
```
33463370
"#]],
33473371
);
@@ -3537,7 +3561,7 @@ fn foo() {
35373561
```
35383562
35393563
```rust
3540-
const FOO: usize = 3
3564+
const FOO: usize
35413565
```
35423566
35433567
---

crates/ide/src/syntax_highlighting/highlight.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,11 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
330330
HlTag::Symbol(SymbolKind::Local)
331331
};
332332
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() {
334335
h |= HlMod::Mutable;
335336
}
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) {
337338
h |= HlMod::Callable;
338339
}
339340
return h;

0 commit comments

Comments
 (0)