tweak postfix completions

* better label
* add refm for &mut
This commit is contained in:
Aleksey Kladov 2019-02-14 20:18:49 +03:00
parent 434f1520df
commit 5f8ec8aa10
2 changed files with 60 additions and 13 deletions

View file

@ -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);
}
}