diff options
Diffstat (limited to 'crates/completion/src/completions')
-rw-r--r-- | crates/completion/src/completions/postfix.rs | 65 |
1 files changed, 54 insertions, 11 deletions
diff --git a/crates/completion/src/completions/postfix.rs b/crates/completion/src/completions/postfix.rs index b4fac74a0..1785794cc 100644 --- a/crates/completion/src/completions/postfix.rs +++ b/crates/completion/src/completions/postfix.rs | |||
@@ -5,6 +5,7 @@ mod format_like; | |||
5 | use ide_db::ty_filter::TryEnum; | 5 | use ide_db::ty_filter::TryEnum; |
6 | use syntax::{ | 6 | use syntax::{ |
7 | ast::{self, AstNode, AstToken}, | 7 | ast::{self, AstNode, AstToken}, |
8 | SyntaxKind::BLOCK_EXPR, | ||
8 | TextRange, TextSize, | 9 | TextRange, TextSize, |
9 | }; | 10 | }; |
10 | use text_edit::TextEdit; | 11 | use text_edit::TextEdit; |
@@ -220,17 +221,29 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { | |||
220 | ) | 221 | ) |
221 | .add_to(acc); | 222 | .add_to(acc); |
222 | 223 | ||
223 | postfix_snippet(ctx, cap, &dot_receiver, "let", "let", &format!("let $0 = {};", receiver_text)) | 224 | let parent_node = dot_receiver.syntax().parent().and_then(|p| p.parent()); |
224 | .add_to(acc); | 225 | if let Some(parent) = parent_node { |
225 | postfix_snippet( | 226 | if parent.kind() == BLOCK_EXPR { |
226 | ctx, | 227 | postfix_snippet( |
227 | cap, | 228 | ctx, |
228 | &dot_receiver, | 229 | cap, |
229 | "letm", | 230 | &dot_receiver, |
230 | "let mut", | 231 | "let", |
231 | &format!("let mut $0 = {};", receiver_text), | 232 | "let", |
232 | ) | 233 | &format!("let $0 = {};", receiver_text), |
233 | .add_to(acc); | 234 | ) |
235 | .add_to(acc); | ||
236 | postfix_snippet( | ||
237 | ctx, | ||
238 | cap, | ||
239 | &dot_receiver, | ||
240 | "letm", | ||
241 | "let mut", | ||
242 | &format!("let mut $0 = {};", receiver_text), | ||
243 | ) | ||
244 | .add_to(acc); | ||
245 | } | ||
246 | } | ||
234 | 247 | ||
235 | if let ast::Expr::Literal(literal) = dot_receiver.clone() { | 248 | if let ast::Expr::Literal(literal) = dot_receiver.clone() { |
236 | if let Some(literal_text) = ast::String::cast(literal.token()) { | 249 | if let Some(literal_text) = ast::String::cast(literal.token()) { |
@@ -322,6 +335,36 @@ fn main() { | |||
322 | } | 335 | } |
323 | 336 | ||
324 | #[test] | 337 | #[test] |
338 | fn postfix_completion_works_for_function_calln() { | ||
339 | check( | ||
340 | r#" | ||
341 | fn foo(elt: bool) -> bool { | ||
342 | !elt | ||
343 | } | ||
344 | |||
345 | fn main() { | ||
346 | let bar = true; | ||
347 | foo(bar.<|>) | ||
348 | } | ||
349 | "#, | ||
350 | expect![[r#" | ||
351 | sn box Box::new(expr) | ||
352 | sn call function(expr) | ||
353 | sn dbg dbg!(expr) | ||
354 | sn dbgr dbg!(&expr) | ||
355 | sn if if expr {} | ||
356 | sn match match expr {} | ||
357 | sn not !expr | ||
358 | sn ok Ok(expr) | ||
359 | sn ref &expr | ||
360 | sn refm &mut expr | ||
361 | sn some Some(expr) | ||
362 | sn while while expr {} | ||
363 | "#]], | ||
364 | ); | ||
365 | } | ||
366 | |||
367 | #[test] | ||
325 | fn postfix_type_filtering() { | 368 | fn postfix_type_filtering() { |
326 | check( | 369 | check( |
327 | r#" | 370 | r#" |