From c5f8f3b1f423781e09bb5f63e33d772ee59fab77 Mon Sep 17 00:00:00 2001 From: Marco Groppo Date: Tue, 9 Apr 2019 21:12:54 +0200 Subject: Stylistic changes. Updated features.md with the new assists. --- crates/ra_assists/src/add_explicit_type.rs | 14 ++++++------- 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) -> Option< // Must be a binding let pat = match pat.kind() { PatKind::BindPat(bind_pat) => bind_pat, - _ => { - return None; - } + _ => return None, }; let pat_range = pat.syntax().range(); // The binding must have a name @@ -31,20 +29,20 @@ pub(crate) fn add_explicit_type(mut ctx: AssistCtx) -> Option< return None; } // Infer type - let func = function_from_child_node(ctx.db, ctx.frange.file_id, pat.syntax())?; - let inference_res = func.infer(ctx.db); - let source_map = func.body_source_map(ctx.db); + let db = ctx.db; + let func = function_from_child_node(db, ctx.frange.file_id, pat.syntax())?; + let inference_res = func.infer(db); + let source_map = func.body_source_map(db); let expr_id = source_map.node_expr(expr.into())?; let ty = inference_res[expr_id].clone(); // Assist not applicable if the type is unknown if is_unknown(&ty) { return None; } - let ty_str = ty.display(ctx.db).to_string(); ctx.add_action(AssistId("add_explicit_type"), "add explicit type", |edit| { edit.target(pat_range); - edit.insert(name_range.end(), format!(": {}", ty_str)); + edit.insert(name_range.end(), format!(": {}", ty.display(db))); }); ctx.build() } 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 { ```rust // before: use algo:<|>:visitor::{Visitor, visit}; -//after: +// after: use algo::{<|>visitor::{Visitor, visit}}; ``` +- Flip binary expression + +```rust +// before: +fn foo() { + if 1 <<|> 2 { + println!("Who would have thought?"); + } +} +// after: +fn foo() { + if 2 ><|> 1 { + println!("Who would have thought?"); + } +} +``` + +- Add explicit type + +```rust +// before: +fn foo() { + let t<|> = (&2, Some(1)); +} +// after: +fn foo() { + let t<|>: (&i32, Option) = (&2, Some(1)); +} +``` + ### Magic Completions In addition to usual reference completion, rust-analyzer provides some ✨magic✨ -- cgit v1.2.3