aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_assists/src/add_explicit_type.rs14
-rw-r--r--docs/user/features.md32
2 files changed, 37 insertions, 9 deletions
diff --git a/crates/ra_assists/src/add_explicit_type.rs b/crates/ra_assists/src/add_explicit_type.rs
index dec4f68ee..1dc59bb87 100644
--- a/crates/ra_assists/src/add_explicit_type.rs
+++ b/crates/ra_assists/src/add_explicit_type.rs
@@ -18,9 +18,7 @@ pub(crate) fn add_explicit_type(mut ctx: AssistCtx<impl HirDatabase>) -> Option<
18 // Must be a binding 18 // Must be a binding
19 let pat = match pat.kind() { 19 let pat = match pat.kind() {
20 PatKind::BindPat(bind_pat) => bind_pat, 20 PatKind::BindPat(bind_pat) => bind_pat,
21 _ => { 21 _ => return None,
22 return None;
23 }
24 }; 22 };
25 let pat_range = pat.syntax().range(); 23 let pat_range = pat.syntax().range();
26 // The binding must have a name 24 // The binding must have a name
@@ -31,20 +29,20 @@ pub(crate) fn add_explicit_type(mut ctx: AssistCtx<impl HirDatabase>) -> Option<
31 return None; 29 return None;
32 } 30 }
33 // Infer type 31 // Infer type
34 let func = function_from_child_node(ctx.db, ctx.frange.file_id, pat.syntax())?; 32 let db = ctx.db;
35 let inference_res = func.infer(ctx.db); 33 let func = function_from_child_node(db, ctx.frange.file_id, pat.syntax())?;
36 let source_map = func.body_source_map(ctx.db); 34 let inference_res = func.infer(db);
35 let source_map = func.body_source_map(db);
37 let expr_id = source_map.node_expr(expr.into())?; 36 let expr_id = source_map.node_expr(expr.into())?;
38 let ty = inference_res[expr_id].clone(); 37 let ty = inference_res[expr_id].clone();
39 // Assist not applicable if the type is unknown 38 // Assist not applicable if the type is unknown
40 if is_unknown(&ty) { 39 if is_unknown(&ty) {
41 return None; 40 return None;
42 } 41 }
43 let ty_str = ty.display(ctx.db).to_string();
44 42
45 ctx.add_action(AssistId("add_explicit_type"), "add explicit type", |edit| { 43 ctx.add_action(AssistId("add_explicit_type"), "add explicit type", |edit| {
46 edit.target(pat_range); 44 edit.target(pat_range);
47 edit.insert(name_range.end(), format!(": {}", ty_str)); 45 edit.insert(name_range.end(), format!(": {}", ty.display(db)));
48 }); 46 });
49 ctx.build() 47 ctx.build()
50} 48}
diff --git a/docs/user/features.md b/docs/user/features.md
index 3ac99eef1..09a7f5a43 100644
--- a/docs/user/features.md
+++ b/docs/user/features.md
@@ -333,10 +333,40 @@ impl VariantData {
333```rust 333```rust
334// before: 334// before:
335use algo:<|>:visitor::{Visitor, visit}; 335use algo:<|>:visitor::{Visitor, visit};
336//after: 336// after:
337use algo::{<|>visitor::{Visitor, visit}}; 337use algo::{<|>visitor::{Visitor, visit}};
338``` 338```
339 339
340- Flip binary expression
341
342```rust
343// before:
344fn foo() {
345 if 1 <<|> 2 {
346 println!("Who would have thought?");
347 }
348}
349// after:
350fn foo() {
351 if 2 ><|> 1 {
352 println!("Who would have thought?");
353 }
354}
355```
356
357- Add explicit type
358
359```rust
360// before:
361fn foo() {
362 let t<|> = (&2, Some(1));
363}
364// after:
365fn foo() {
366 let t<|>: (&i32, Option<i32>) = (&2, Some(1));
367}
368```
369
340### Magic Completions 370### Magic Completions
341 371
342In addition to usual reference completion, rust-analyzer provides some ✨magic✨ 372In addition to usual reference completion, rust-analyzer provides some ✨magic✨