From 5f8ec8aa10d2c012b321d9779037b6320c1f2efb Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 14 Feb 2019 20:18:49 +0300 Subject: tweak postfix completions * better label * add refm for &mut --- .../ra_ide_api/src/completion/complete_postfix.rs | 22 +++++++--- ...mpletion_works_for_trivial_path_expression.snap | 51 +++++++++++++++++++--- 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/crates/ra_ide_api/src/completion/complete_postfix.rs b/crates/ra_ide_api/src/completion/complete_postfix.rs index b7dc50c34..d1f6b9433 100644 --- a/crates/ra_ide_api/src/completion/complete_postfix.rs +++ b/crates/ra_ide_api/src/completion/complete_postfix.rs @@ -15,7 +15,7 @@ use ra_syntax::{ }; use ra_text_edit::TextEditBuilder; -fn postfix_snippet(ctx: &CompletionContext, label: &str, snippet: &str) -> Builder { +fn postfix_snippet(ctx: &CompletionContext, label: &str, detail: &str, snippet: &str) -> Builder { let replace_range = ctx.source_range(); let receiver_range = ctx.dot_receiver.expect("no receiver available").syntax().range(); let delete_range = TextRange::from_to(receiver_range.start(), replace_range.start()); @@ -23,23 +23,33 @@ fn postfix_snippet(ctx: &CompletionContext, label: &str, snippet: &str) -> Build builder.delete(delete_range); CompletionItem::new(CompletionKind::Postfix, replace_range, label) .snippet(snippet) + .detail(detail) .text_edit(builder.finish()) } pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { if let Some(dot_receiver) = ctx.dot_receiver { let receiver_text = dot_receiver.syntax().text().to_string(); - postfix_snippet(ctx, "not", &format!("!{}", receiver_text)).add_to(acc); - postfix_snippet(ctx, "ref", &format!("&{}", receiver_text)).add_to(acc); - postfix_snippet(ctx, "if", &format!("if {} {{$0}}", receiver_text)).add_to(acc); + postfix_snippet(ctx, "not", "!expr", &format!("!{}", receiver_text)).add_to(acc); + postfix_snippet(ctx, "ref", "&expr", &format!("&{}", receiver_text)).add_to(acc); + postfix_snippet(ctx, "mref", "&mut expr", &format!("&mut {}", receiver_text)).add_to(acc); + postfix_snippet(ctx, "if", "if expr {}", &format!("if {} {{$0}}", receiver_text)) + .add_to(acc); postfix_snippet( ctx, "match", + "match expr {}", &format!("match {} {{\n${{1:_}} => {{$0\\}},\n}}", receiver_text), ) .add_to(acc); - postfix_snippet(ctx, "while", &format!("while {} {{\n$0\n}}", receiver_text)).add_to(acc); - postfix_snippet(ctx, "dbg", &format!("dbg!({})", receiver_text)).add_to(acc); + postfix_snippet( + ctx, + "while", + "while expr {}", + &format!("while {} {{\n$0\n}}", receiver_text), + ) + .add_to(acc); + postfix_snippet(ctx, "dbg", "dbg!(expr)", &format!("dbg!({})", receiver_text)).add_to(acc); } } diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__postfix_completion_works_for_trivial_path_expression.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__postfix_completion_works_for_trivial_path_expression.snap index 3e16c8e79..6f4351445 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__postfix_completion_works_for_trivial_path_expression.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__postfix_completion_works_for_trivial_path_expression.snap @@ -1,5 +1,5 @@ --- -created: "2019-02-14T17:12:57.412523988Z" +created: "2019-02-14T17:38:20.322102467Z" creator: insta@0.6.2 source: crates/ra_ide_api/src/completion/completion_item.rs expression: kind_completions @@ -9,7 +9,9 @@ expression: kind_completions completion_kind: Postfix, label: "dbg", kind: None, - detail: None, + detail: Some( + "dbg!(expr)" + ), documentation: None, lookup: None, insert_text: Some( @@ -32,7 +34,9 @@ expression: kind_completions completion_kind: Postfix, label: "if", kind: None, - detail: None, + detail: Some( + "if expr {}" + ), documentation: None, lookup: None, insert_text: Some( @@ -55,7 +59,9 @@ expression: kind_completions completion_kind: Postfix, label: "match", kind: None, - detail: None, + detail: Some( + "match expr {}" + ), documentation: None, lookup: None, insert_text: Some( @@ -74,11 +80,38 @@ expression: kind_completions } ) }, + CompletionItem { + completion_kind: Postfix, + label: "mref", + kind: None, + detail: Some( + "&mut expr" + ), + documentation: None, + lookup: None, + insert_text: Some( + "&mut bar" + ), + insert_text_format: Snippet, + source_range: [76; 76), + text_edit: Some( + TextEdit { + atoms: [ + AtomTextEdit { + delete: [72; 76), + insert: "" + } + ] + } + ) + }, CompletionItem { completion_kind: Postfix, label: "not", kind: None, - detail: None, + detail: Some( + "!expr" + ), documentation: None, lookup: None, insert_text: Some( @@ -101,7 +134,9 @@ expression: kind_completions completion_kind: Postfix, label: "ref", kind: None, - detail: None, + detail: Some( + "&expr" + ), documentation: None, lookup: None, insert_text: Some( @@ -124,7 +159,9 @@ expression: kind_completions completion_kind: Postfix, label: "while", kind: None, - detail: None, + detail: Some( + "while expr {}" + ), documentation: None, lookup: None, insert_text: Some( -- cgit v1.2.3