2019-09-30 08:58:53 +00:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2020-03-23 23:18:21 +00:00
|
|
|
use ra_syntax::{
|
|
|
|
ast::{self, AstNode},
|
|
|
|
TextRange, TextUnit,
|
|
|
|
};
|
2019-11-06 11:06:44 +00:00
|
|
|
use ra_text_edit::TextEdit;
|
|
|
|
|
2020-04-24 00:26:38 +00:00
|
|
|
use super::completion_config::SnippetCap;
|
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-11-06 11:06:44 +00:00
|
|
|
|
|
|
|
pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
2020-03-31 14:02:55 +00:00
|
|
|
if !ctx.config.enable_postfix_completions {
|
2019-11-12 03:55:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-06 11:06:44 +00:00
|
|
|
let dot_receiver = match &ctx.dot_receiver {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
|
2020-03-23 23:18:21 +00:00
|
|
|
let receiver_text =
|
|
|
|
get_receiver_text(dot_receiver, ctx.dot_receiver_is_ambiguous_float_literal);
|
2019-11-06 11:06:44 +00:00
|
|
|
|
2020-02-18 17:35:10 +00:00
|
|
|
let receiver_ty = match ctx.sema.type_of_expr(&dot_receiver) {
|
2019-11-26 11:02:57 +00:00
|
|
|
Some(it) => it,
|
|
|
|
None => return,
|
|
|
|
};
|
2019-11-06 11:06:44 +00:00
|
|
|
|
2020-04-24 00:26:38 +00:00
|
|
|
let cap = match ctx.config.snippet_cap {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
|
2019-11-26 11:02:57 +00:00
|
|
|
if receiver_ty.is_bool() || receiver_ty.is_unknown() {
|
2019-11-06 11:06:44 +00:00
|
|
|
postfix_snippet(
|
|
|
|
ctx,
|
2020-04-24 00:26:38 +00:00
|
|
|
cap,
|
2020-03-23 23:18:21 +00:00
|
|
|
&dot_receiver,
|
|
|
|
"if",
|
|
|
|
"if expr {}",
|
|
|
|
&format!("if {} {{$0}}", receiver_text),
|
|
|
|
)
|
|
|
|
.add_to(acc);
|
|
|
|
postfix_snippet(
|
|
|
|
ctx,
|
2020-04-24 00:26:38 +00:00
|
|
|
cap,
|
2020-03-23 23:18:21 +00:00
|
|
|
&dot_receiver,
|
2019-11-06 11:06:44 +00:00
|
|
|
"while",
|
|
|
|
"while expr {}",
|
|
|
|
&format!("while {} {{\n$0\n}}", receiver_text),
|
|
|
|
)
|
|
|
|
.add_to(acc);
|
|
|
|
}
|
|
|
|
|
2020-03-23 23:18:21 +00:00
|
|
|
// !&&&42 is a compiler error, ergo process it before considering the references
|
2020-04-24 00:26:38 +00:00
|
|
|
postfix_snippet(ctx, cap, &dot_receiver, "not", "!expr", &format!("!{}", receiver_text))
|
|
|
|
.add_to(acc);
|
2019-11-06 11:06:44 +00:00
|
|
|
|
2020-04-24 00:26:38 +00:00
|
|
|
postfix_snippet(ctx, cap, &dot_receiver, "ref", "&expr", &format!("&{}", receiver_text))
|
2020-03-23 23:18:21 +00:00
|
|
|
.add_to(acc);
|
2020-04-24 00:26:38 +00:00
|
|
|
postfix_snippet(
|
|
|
|
ctx,
|
|
|
|
cap,
|
|
|
|
&dot_receiver,
|
|
|
|
"refm",
|
|
|
|
"&mut expr",
|
|
|
|
&format!("&mut {}", receiver_text),
|
|
|
|
)
|
|
|
|
.add_to(acc);
|
2020-03-23 23:18:21 +00:00
|
|
|
|
|
|
|
// The rest of the postfix completions create an expression that moves an argument,
|
|
|
|
// so it's better to consider references now to avoid breaking the compilation
|
|
|
|
let dot_receiver = include_references(dot_receiver);
|
|
|
|
let receiver_text =
|
|
|
|
get_receiver_text(&dot_receiver, ctx.dot_receiver_is_ambiguous_float_literal);
|
2019-11-06 11:06:44 +00:00
|
|
|
|
|
|
|
postfix_snippet(
|
|
|
|
ctx,
|
2020-04-24 00:26:38 +00:00
|
|
|
cap,
|
2020-03-23 23:18:21 +00:00
|
|
|
&dot_receiver,
|
2019-11-06 11:06:44 +00:00
|
|
|
"match",
|
|
|
|
"match expr {}",
|
|
|
|
&format!("match {} {{\n ${{1:_}} => {{$0\\}},\n}}", receiver_text),
|
|
|
|
)
|
|
|
|
.add_to(acc);
|
|
|
|
|
2020-03-23 23:18:21 +00:00
|
|
|
postfix_snippet(
|
|
|
|
ctx,
|
2020-04-24 00:26:38 +00:00
|
|
|
cap,
|
2020-03-23 23:18:21 +00:00
|
|
|
&dot_receiver,
|
|
|
|
"box",
|
|
|
|
"Box::new(expr)",
|
|
|
|
&format!("Box::new({})", receiver_text),
|
|
|
|
)
|
|
|
|
.add_to(acc);
|
2019-11-06 11:06:44 +00:00
|
|
|
|
2020-04-24 00:26:38 +00:00
|
|
|
postfix_snippet(
|
|
|
|
ctx,
|
|
|
|
cap,
|
|
|
|
&dot_receiver,
|
|
|
|
"dbg",
|
|
|
|
"dbg!(expr)",
|
|
|
|
&format!("dbg!({})", receiver_text),
|
|
|
|
)
|
|
|
|
.add_to(acc);
|
2019-11-06 11:06:44 +00:00
|
|
|
}
|
2019-01-21 05:19:51 +00:00
|
|
|
|
2020-03-23 23:18:21 +00:00
|
|
|
fn get_receiver_text(receiver: &ast::Expr, receiver_is_ambiguous_float_literal: bool) -> String {
|
|
|
|
if receiver_is_ambiguous_float_literal {
|
|
|
|
let text = receiver.syntax().text();
|
|
|
|
let without_dot = ..text.len() - TextUnit::of_char('.');
|
|
|
|
text.slice(without_dot).to_string()
|
|
|
|
} else {
|
|
|
|
receiver.to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn include_references(initial_element: &ast::Expr) -> ast::Expr {
|
|
|
|
let mut resulting_element = initial_element.clone();
|
|
|
|
while let Some(parent_ref_element) =
|
|
|
|
resulting_element.syntax().parent().and_then(ast::RefExpr::cast)
|
|
|
|
{
|
|
|
|
resulting_element = ast::Expr::from(parent_ref_element);
|
|
|
|
}
|
|
|
|
resulting_element
|
|
|
|
}
|
|
|
|
|
|
|
|
fn postfix_snippet(
|
|
|
|
ctx: &CompletionContext,
|
2020-04-24 00:26:38 +00:00
|
|
|
cap: SnippetCap,
|
2020-03-23 23:18:21 +00:00
|
|
|
receiver: &ast::Expr,
|
|
|
|
label: &str,
|
|
|
|
detail: &str,
|
|
|
|
snippet: &str,
|
|
|
|
) -> Builder {
|
2019-02-18 09:05:16 +00:00
|
|
|
let edit = {
|
2020-03-23 23:18:21 +00:00
|
|
|
let receiver_syntax = receiver.syntax();
|
2020-03-07 15:44:51 +00:00
|
|
|
let receiver_range = ctx.sema.original_range(receiver_syntax).range;
|
2019-02-18 09:05:16 +00:00
|
|
|
let delete_range = TextRange::from_to(receiver_range.start(), ctx.source_range().end());
|
2019-10-26 17:07:24 +00:00
|
|
|
TextEdit::replace(delete_range, snippet.to_string())
|
2019-02-18 09:05:16 +00:00
|
|
|
};
|
|
|
|
CompletionItem::new(CompletionKind::Postfix, ctx.source_range(), label)
|
2019-02-14 17:18:49 +00:00
|
|
|
.detail(detail)
|
2020-04-24 00:26:38 +00:00
|
|
|
.snippet_edit(cap, edit)
|
2019-01-21 05:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-08-29 13:49:10 +00:00
|
|
|
use insta::assert_debug_snapshot;
|
2019-01-21 05:19:51 +00:00
|
|
|
|
2020-03-11 09:46:43 +00:00
|
|
|
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
|
2019-11-06 11:06:44 +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-08-29 13:49:10 +00:00
|
|
|
assert_debug_snapshot!(
|
2019-07-07 10:52:16 +00:00
|
|
|
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.<|>
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
),
|
2019-11-15 09:56:24 +00:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "box",
|
|
|
|
source_range: [89; 89),
|
|
|
|
delete: [85; 89),
|
|
|
|
insert: "Box::new(bar)",
|
|
|
|
detail: "Box::new(expr)",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "dbg",
|
|
|
|
source_range: [89; 89),
|
|
|
|
delete: [85; 89),
|
|
|
|
insert: "dbg!(bar)",
|
|
|
|
detail: "dbg!(expr)",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "if",
|
|
|
|
source_range: [89; 89),
|
|
|
|
delete: [85; 89),
|
|
|
|
insert: "if bar {$0}",
|
|
|
|
detail: "if expr {}",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "match",
|
|
|
|
source_range: [89; 89),
|
|
|
|
delete: [85; 89),
|
|
|
|
insert: "match bar {\n ${1:_} => {$0\\},\n}",
|
|
|
|
detail: "match expr {}",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "not",
|
|
|
|
source_range: [89; 89),
|
|
|
|
delete: [85; 89),
|
|
|
|
insert: "!bar",
|
|
|
|
detail: "!expr",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "ref",
|
|
|
|
source_range: [89; 89),
|
|
|
|
delete: [85; 89),
|
|
|
|
insert: "&bar",
|
|
|
|
detail: "&expr",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "refm",
|
|
|
|
source_range: [89; 89),
|
|
|
|
delete: [85; 89),
|
|
|
|
insert: "&mut bar",
|
|
|
|
detail: "&mut expr",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "while",
|
|
|
|
source_range: [89; 89),
|
|
|
|
delete: [85; 89),
|
|
|
|
insert: "while bar {\n$0\n}",
|
|
|
|
detail: "while expr {}",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-07-16 13:06:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn some_postfix_completions_ignored() {
|
2019-08-29 13:49:10 +00:00
|
|
|
assert_debug_snapshot!(
|
2019-07-16 13:06:15 +00:00
|
|
|
do_postfix_completion(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let bar: u8 = 12;
|
|
|
|
bar.<|>
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
),
|
2019-11-15 09:56:24 +00:00
|
|
|
@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-10-14 15:39:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn postfix_completion_works_for_ambiguous_float_literal() {
|
|
|
|
assert_debug_snapshot!(
|
|
|
|
do_postfix_completion(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
42.<|>
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
),
|
2019-11-15 09:56:24 +00:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "box",
|
|
|
|
source_range: [52; 52),
|
|
|
|
delete: [49; 52),
|
|
|
|
insert: "Box::new(42)",
|
|
|
|
detail: "Box::new(expr)",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "dbg",
|
|
|
|
source_range: [52; 52),
|
|
|
|
delete: [49; 52),
|
|
|
|
insert: "dbg!(42)",
|
|
|
|
detail: "dbg!(expr)",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "match",
|
|
|
|
source_range: [52; 52),
|
|
|
|
delete: [49; 52),
|
|
|
|
insert: "match 42 {\n ${1:_} => {$0\\},\n}",
|
|
|
|
detail: "match expr {}",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "not",
|
|
|
|
source_range: [52; 52),
|
|
|
|
delete: [49; 52),
|
|
|
|
insert: "!42",
|
|
|
|
detail: "!expr",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "ref",
|
|
|
|
source_range: [52; 52),
|
|
|
|
delete: [49; 52),
|
|
|
|
insert: "&42",
|
|
|
|
detail: "&expr",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "refm",
|
|
|
|
source_range: [52; 52),
|
|
|
|
delete: [49; 52),
|
|
|
|
insert: "&mut 42",
|
|
|
|
detail: "&mut expr",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-01-21 05:19:51 +00:00
|
|
|
);
|
|
|
|
}
|
2020-03-07 15:44:51 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn works_in_simple_macro() {
|
|
|
|
assert_debug_snapshot!(
|
|
|
|
do_postfix_completion(
|
|
|
|
r#"
|
|
|
|
macro_rules! m { ($e:expr) => { $e } }
|
|
|
|
fn main() {
|
|
|
|
let bar: u8 = 12;
|
|
|
|
m!(bar.b<|>)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
),
|
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "box",
|
|
|
|
source_range: [149; 150),
|
|
|
|
delete: [145; 150),
|
|
|
|
insert: "Box::new(bar)",
|
|
|
|
detail: "Box::new(expr)",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "dbg",
|
|
|
|
source_range: [149; 150),
|
|
|
|
delete: [145; 150),
|
|
|
|
insert: "dbg!(bar)",
|
|
|
|
detail: "dbg!(expr)",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "match",
|
|
|
|
source_range: [149; 150),
|
|
|
|
delete: [145; 150),
|
|
|
|
insert: "match bar {\n ${1:_} => {$0\\},\n}",
|
|
|
|
detail: "match expr {}",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "not",
|
|
|
|
source_range: [149; 150),
|
|
|
|
delete: [145; 150),
|
|
|
|
insert: "!bar",
|
|
|
|
detail: "!expr",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "ref",
|
|
|
|
source_range: [149; 150),
|
|
|
|
delete: [145; 150),
|
|
|
|
insert: "&bar",
|
|
|
|
detail: "&expr",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "refm",
|
|
|
|
source_range: [149; 150),
|
|
|
|
delete: [145; 150),
|
|
|
|
insert: "&mut bar",
|
|
|
|
detail: "&mut expr",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
|
|
|
);
|
|
|
|
}
|
2020-03-23 23:18:21 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn postfix_completion_for_references() {
|
|
|
|
assert_debug_snapshot!(
|
|
|
|
do_postfix_completion(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
&&&&42.<|>
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
),
|
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "box",
|
|
|
|
source_range: [56; 56),
|
|
|
|
delete: [49; 56),
|
|
|
|
insert: "Box::new(&&&&42)",
|
|
|
|
detail: "Box::new(expr)",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "dbg",
|
|
|
|
source_range: [56; 56),
|
|
|
|
delete: [49; 56),
|
|
|
|
insert: "dbg!(&&&&42)",
|
|
|
|
detail: "dbg!(expr)",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "match",
|
|
|
|
source_range: [56; 56),
|
|
|
|
delete: [49; 56),
|
|
|
|
insert: "match &&&&42 {\n ${1:_} => {$0\\},\n}",
|
|
|
|
detail: "match expr {}",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "not",
|
|
|
|
source_range: [56; 56),
|
|
|
|
delete: [53; 56),
|
|
|
|
insert: "!42",
|
|
|
|
detail: "!expr",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "ref",
|
|
|
|
source_range: [56; 56),
|
|
|
|
delete: [53; 56),
|
|
|
|
insert: "&42",
|
|
|
|
detail: "&expr",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "refm",
|
|
|
|
source_range: [56; 56),
|
|
|
|
delete: [53; 56),
|
|
|
|
insert: "&mut 42",
|
|
|
|
detail: "&mut expr",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
|
|
|
);
|
|
|
|
}
|
2019-01-21 05:19:51 +00:00
|
|
|
}
|