2019-01-21 05:19:51 +00:00
|
|
|
use crate::{
|
|
|
|
completion::{
|
|
|
|
completion_context::CompletionContext,
|
2019-07-04 20:05:17 +00:00
|
|
|
completion_item::{Builder, CompletionKind, Completions},
|
2019-01-21 05:19:51 +00:00
|
|
|
},
|
2019-07-04 20:05:17 +00:00
|
|
|
CompletionItem,
|
2019-01-21 05:19:51 +00:00
|
|
|
};
|
2019-07-16 13:06:15 +00:00
|
|
|
use hir::{Ty, TypeCtor};
|
2019-07-04 20:05:17 +00:00
|
|
|
use ra_syntax::{ast::AstNode, TextRange};
|
2019-01-21 05:19:51 +00:00
|
|
|
use ra_text_edit::TextEditBuilder;
|
|
|
|
|
2019-02-14 17:18:49 +00:00
|
|
|
fn postfix_snippet(ctx: &CompletionContext, label: &str, detail: &str, snippet: &str) -> Builder {
|
2019-02-18 09:05:16 +00:00
|
|
|
let edit = {
|
2019-07-19 09:56:47 +00:00
|
|
|
let receiver_range =
|
2019-07-20 09:58:27 +00:00
|
|
|
ctx.dot_receiver.as_ref().expect("no receiver available").syntax().text_range();
|
2019-02-18 09:05:16 +00:00
|
|
|
let delete_range = TextRange::from_to(receiver_range.start(), ctx.source_range().end());
|
|
|
|
let mut builder = TextEditBuilder::default();
|
|
|
|
builder.replace(delete_range, snippet.to_string());
|
|
|
|
builder.finish()
|
|
|
|
};
|
|
|
|
CompletionItem::new(CompletionKind::Postfix, ctx.source_range(), label)
|
2019-02-14 17:18:49 +00:00
|
|
|
.detail(detail)
|
2019-02-18 09:05:16 +00:00
|
|
|
.snippet_edit(edit)
|
2019-01-21 05:19:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 13:06:15 +00:00
|
|
|
fn is_bool_or_unknown(ty: Option<Ty>) -> bool {
|
|
|
|
if let Some(ty) = ty {
|
|
|
|
match ty {
|
|
|
|
Ty::Apply(at) => match at.ctor {
|
|
|
|
TypeCtor::Bool => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
Ty::Unknown => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 05:19:51 +00:00
|
|
|
pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
2019-07-19 09:56:47 +00:00
|
|
|
if let Some(dot_receiver) = &ctx.dot_receiver {
|
2019-01-21 05:19:51 +00:00
|
|
|
let receiver_text = dot_receiver.syntax().text().to_string();
|
2019-07-19 09:56:47 +00:00
|
|
|
let receiver_ty = ctx.analyzer.type_of(ctx.db, &dot_receiver);
|
2019-07-16 13:06:15 +00:00
|
|
|
if is_bool_or_unknown(receiver_ty) {
|
|
|
|
postfix_snippet(ctx, "if", "if expr {}", &format!("if {} {{$0}}", receiver_text))
|
|
|
|
.add_to(acc);
|
|
|
|
postfix_snippet(
|
|
|
|
ctx,
|
|
|
|
"while",
|
|
|
|
"while expr {}",
|
|
|
|
&format!("while {} {{\n$0\n}}", receiver_text),
|
|
|
|
)
|
|
|
|
.add_to(acc);
|
|
|
|
}
|
2019-02-14 17:18:49 +00:00
|
|
|
postfix_snippet(ctx, "not", "!expr", &format!("!{}", receiver_text)).add_to(acc);
|
|
|
|
postfix_snippet(ctx, "ref", "&expr", &format!("&{}", receiver_text)).add_to(acc);
|
2019-02-14 18:28:23 +00:00
|
|
|
postfix_snippet(ctx, "refm", "&mut expr", &format!("&mut {}", receiver_text)).add_to(acc);
|
2019-01-21 05:19:51 +00:00
|
|
|
postfix_snippet(
|
|
|
|
ctx,
|
|
|
|
"match",
|
2019-02-14 17:18:49 +00:00
|
|
|
"match expr {}",
|
2019-04-22 07:38:39 +00:00
|
|
|
&format!("match {} {{\n ${{1:_}} => {{$0\\}},\n}}", receiver_text),
|
2019-01-21 05:19:51 +00:00
|
|
|
)
|
|
|
|
.add_to(acc);
|
2019-02-14 17:18:49 +00:00
|
|
|
postfix_snippet(ctx, "dbg", "dbg!(expr)", &format!("dbg!({})", receiver_text)).add_to(acc);
|
2019-06-23 01:41:43 +00:00
|
|
|
postfix_snippet(ctx, "box", "Box::new(expr)", &format!("Box::new({})", receiver_text))
|
|
|
|
.add_to(acc);
|
2019-01-21 05:19:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-07-07 10:52:16 +00:00
|
|
|
use crate::completion::{do_completion, CompletionItem, CompletionKind};
|
|
|
|
use insta::assert_debug_snapshot_matches;
|
2019-01-21 05:19:51 +00:00
|
|
|
|
2019-07-07 10:52:16 +00:00
|
|
|
fn do_postfix_completion(code: &str) -> Vec<CompletionItem> {
|
|
|
|
do_completion(code, CompletionKind::Postfix)
|
2019-01-21 05:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-02-03 13:12:57 +00:00
|
|
|
fn postfix_completion_works_for_trivial_path_expression() {
|
2019-07-07 10:52:16 +00:00
|
|
|
assert_debug_snapshot_matches!(
|
|
|
|
do_postfix_completion(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
2019-07-16 13:06:15 +00:00
|
|
|
let bar = true;
|
2019-07-07 10:52:16 +00:00
|
|
|
bar.<|>
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
),
|
|
|
|
@r###"[
|
|
|
|
CompletionItem {
|
|
|
|
label: "box",
|
2019-07-16 13:06:15 +00:00
|
|
|
source_range: [89; 89),
|
|
|
|
delete: [85; 89),
|
2019-07-07 10:52:16 +00:00
|
|
|
insert: "Box::new(bar)",
|
|
|
|
detail: "Box::new(expr)",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "dbg",
|
2019-07-16 13:06:15 +00:00
|
|
|
source_range: [89; 89),
|
|
|
|
delete: [85; 89),
|
2019-07-07 10:52:16 +00:00
|
|
|
insert: "dbg!(bar)",
|
|
|
|
detail: "dbg!(expr)",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "if",
|
2019-07-16 13:06:15 +00:00
|
|
|
source_range: [89; 89),
|
|
|
|
delete: [85; 89),
|
2019-07-07 10:52:16 +00:00
|
|
|
insert: "if bar {$0}",
|
|
|
|
detail: "if expr {}",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "match",
|
2019-07-16 13:06:15 +00:00
|
|
|
source_range: [89; 89),
|
|
|
|
delete: [85; 89),
|
2019-07-07 10:52:16 +00:00
|
|
|
insert: "match bar {\n ${1:_} => {$0\\},\n}",
|
|
|
|
detail: "match expr {}",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "not",
|
2019-07-16 13:06:15 +00:00
|
|
|
source_range: [89; 89),
|
|
|
|
delete: [85; 89),
|
2019-07-07 10:52:16 +00:00
|
|
|
insert: "!bar",
|
|
|
|
detail: "!expr",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "ref",
|
2019-07-16 13:06:15 +00:00
|
|
|
source_range: [89; 89),
|
|
|
|
delete: [85; 89),
|
2019-07-07 10:52:16 +00:00
|
|
|
insert: "&bar",
|
|
|
|
detail: "&expr",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "refm",
|
2019-07-16 13:06:15 +00:00
|
|
|
source_range: [89; 89),
|
|
|
|
delete: [85; 89),
|
2019-07-07 10:52:16 +00:00
|
|
|
insert: "&mut bar",
|
|
|
|
detail: "&mut expr",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "while",
|
2019-07-16 13:06:15 +00:00
|
|
|
source_range: [89; 89),
|
|
|
|
delete: [85; 89),
|
2019-07-07 10:52:16 +00:00
|
|
|
insert: "while bar {\n$0\n}",
|
|
|
|
detail: "while expr {}",
|
|
|
|
},
|
2019-07-16 13:06:15 +00:00
|
|
|
]"###
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn some_postfix_completions_ignored() {
|
|
|
|
assert_debug_snapshot_matches!(
|
|
|
|
do_postfix_completion(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let bar: u8 = 12;
|
|
|
|
bar.<|>
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
),
|
|
|
|
@r###"[
|
|
|
|
CompletionItem {
|
|
|
|
label: "box",
|
|
|
|
source_range: [91; 91),
|
|
|
|
delete: [87; 91),
|
|
|
|
insert: "Box::new(bar)",
|
|
|
|
detail: "Box::new(expr)",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "dbg",
|
|
|
|
source_range: [91; 91),
|
|
|
|
delete: [87; 91),
|
|
|
|
insert: "dbg!(bar)",
|
|
|
|
detail: "dbg!(expr)",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "match",
|
|
|
|
source_range: [91; 91),
|
|
|
|
delete: [87; 91),
|
|
|
|
insert: "match bar {\n ${1:_} => {$0\\},\n}",
|
|
|
|
detail: "match expr {}",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "not",
|
|
|
|
source_range: [91; 91),
|
|
|
|
delete: [87; 91),
|
|
|
|
insert: "!bar",
|
|
|
|
detail: "!expr",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "ref",
|
|
|
|
source_range: [91; 91),
|
|
|
|
delete: [87; 91),
|
|
|
|
insert: "&bar",
|
|
|
|
detail: "&expr",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "refm",
|
|
|
|
source_range: [91; 91),
|
|
|
|
delete: [87; 91),
|
|
|
|
insert: "&mut bar",
|
|
|
|
detail: "&mut expr",
|
|
|
|
},
|
2019-07-07 10:52:16 +00:00
|
|
|
]"###
|
2019-01-21 05:19:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|