diff options
author | Marco Groppo <[email protected]> | 2019-04-09 20:12:54 +0100 |
---|---|---|
committer | Marco Groppo <[email protected]> | 2019-04-09 20:12:54 +0100 |
commit | c5f8f3b1f423781e09bb5f63e33d772ee59fab77 (patch) | |
tree | c9cab7d79be607f2c741a91ee032f3a3a964f31b | |
parent | a4ba3841b4cbf2dd3536183464281dfdd2a22409 (diff) |
Stylistic changes. Updated features.md with the new assists.
-rw-r--r-- | crates/ra_assists/src/add_explicit_type.rs | 14 | ||||
-rw-r--r-- | docs/user/features.md | 32 |
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: |
335 | use algo:<|>:visitor::{Visitor, visit}; | 335 | use algo:<|>:visitor::{Visitor, visit}; |
336 | //after: | 336 | // after: |
337 | use algo::{<|>visitor::{Visitor, visit}}; | 337 | use algo::{<|>visitor::{Visitor, visit}}; |
338 | ``` | 338 | ``` |
339 | 339 | ||
340 | - Flip binary expression | ||
341 | |||
342 | ```rust | ||
343 | // before: | ||
344 | fn foo() { | ||
345 | if 1 <<|> 2 { | ||
346 | println!("Who would have thought?"); | ||
347 | } | ||
348 | } | ||
349 | // after: | ||
350 | fn foo() { | ||
351 | if 2 ><|> 1 { | ||
352 | println!("Who would have thought?"); | ||
353 | } | ||
354 | } | ||
355 | ``` | ||
356 | |||
357 | - Add explicit type | ||
358 | |||
359 | ```rust | ||
360 | // before: | ||
361 | fn foo() { | ||
362 | let t<|> = (&2, Some(1)); | ||
363 | } | ||
364 | // after: | ||
365 | fn foo() { | ||
366 | let t<|>: (&i32, Option<i32>) = (&2, Some(1)); | ||
367 | } | ||
368 | ``` | ||
369 | |||
340 | ### Magic Completions | 370 | ### Magic Completions |
341 | 371 | ||
342 | In addition to usual reference completion, rust-analyzer provides some ✨magic✨ | 372 | In addition to usual reference completion, rust-analyzer provides some ✨magic✨ |