diff options
Diffstat (limited to 'crates/completion/src/completions')
-rw-r--r-- | crates/completion/src/completions/postfix.rs | 86 | ||||
-rw-r--r-- | crates/completion/src/completions/qualified_path.rs | 40 | ||||
-rw-r--r-- | crates/completion/src/completions/trait_impl.rs | 2 |
3 files changed, 107 insertions, 21 deletions
diff --git a/crates/completion/src/completions/postfix.rs b/crates/completion/src/completions/postfix.rs index 7fbda7a6b..c8ba63cd3 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, EXPR_STMT}, | ||
8 | TextRange, TextSize, | 9 | TextRange, TextSize, |
9 | }; | 10 | }; |
10 | use text_edit::TextEdit; | 11 | use text_edit::TextEdit; |
@@ -220,6 +221,29 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { | |||
220 | ) | 221 | ) |
221 | .add_to(acc); | 222 | .add_to(acc); |
222 | 223 | ||
224 | if let Some(parent) = dot_receiver.syntax().parent().and_then(|p| p.parent()) { | ||
225 | if matches!(parent.kind(), BLOCK_EXPR | EXPR_STMT) { | ||
226 | postfix_snippet( | ||
227 | ctx, | ||
228 | cap, | ||
229 | &dot_receiver, | ||
230 | "let", | ||
231 | "let", | ||
232 | &format!("let $0 = {};", receiver_text), | ||
233 | ) | ||
234 | .add_to(acc); | ||
235 | postfix_snippet( | ||
236 | ctx, | ||
237 | cap, | ||
238 | &dot_receiver, | ||
239 | "letm", | ||
240 | "let mut", | ||
241 | &format!("let mut $0 = {};", receiver_text), | ||
242 | ) | ||
243 | .add_to(acc); | ||
244 | } | ||
245 | } | ||
246 | |||
223 | if let ast::Expr::Literal(literal) = dot_receiver.clone() { | 247 | if let ast::Expr::Literal(literal) = dot_receiver.clone() { |
224 | if let Some(literal_text) = ast::String::cast(literal.token()) { | 248 | if let Some(literal_text) = ast::String::cast(literal.token()) { |
225 | add_format_like_completions(acc, ctx, &dot_receiver, cap, &literal_text); | 249 | add_format_like_completions(acc, ctx, &dot_receiver, cap, &literal_text); |
@@ -296,6 +320,38 @@ fn main() { | |||
296 | sn dbg dbg!(expr) | 320 | sn dbg dbg!(expr) |
297 | sn dbgr dbg!(&expr) | 321 | sn dbgr dbg!(&expr) |
298 | sn if if expr {} | 322 | sn if if expr {} |
323 | sn let let | ||
324 | sn letm let mut | ||
325 | sn match match expr {} | ||
326 | sn not !expr | ||
327 | sn ok Ok(expr) | ||
328 | sn ref &expr | ||
329 | sn refm &mut expr | ||
330 | sn some Some(expr) | ||
331 | sn while while expr {} | ||
332 | "#]], | ||
333 | ); | ||
334 | } | ||
335 | |||
336 | #[test] | ||
337 | fn postfix_completion_works_for_function_calln() { | ||
338 | check( | ||
339 | r#" | ||
340 | fn foo(elt: bool) -> bool { | ||
341 | !elt | ||
342 | } | ||
343 | |||
344 | fn main() { | ||
345 | let bar = true; | ||
346 | foo(bar.<|>) | ||
347 | } | ||
348 | "#, | ||
349 | expect![[r#" | ||
350 | sn box Box::new(expr) | ||
351 | sn call function(expr) | ||
352 | sn dbg dbg!(expr) | ||
353 | sn dbgr dbg!(&expr) | ||
354 | sn if if expr {} | ||
299 | sn match match expr {} | 355 | sn match match expr {} |
300 | sn not !expr | 356 | sn not !expr |
301 | sn ok Ok(expr) | 357 | sn ok Ok(expr) |
@@ -321,6 +377,8 @@ fn main() { | |||
321 | sn call function(expr) | 377 | sn call function(expr) |
322 | sn dbg dbg!(expr) | 378 | sn dbg dbg!(expr) |
323 | sn dbgr dbg!(&expr) | 379 | sn dbgr dbg!(&expr) |
380 | sn let let | ||
381 | sn letm let mut | ||
324 | sn match match expr {} | 382 | sn match match expr {} |
325 | sn ok Ok(expr) | 383 | sn ok Ok(expr) |
326 | sn ref &expr | 384 | sn ref &expr |
@@ -331,6 +389,34 @@ fn main() { | |||
331 | } | 389 | } |
332 | 390 | ||
333 | #[test] | 391 | #[test] |
392 | fn let_middle_block() { | ||
393 | check( | ||
394 | r#" | ||
395 | fn main() { | ||
396 | baz.l<|> | ||
397 | res | ||
398 | } | ||
399 | "#, | ||
400 | expect![[r#" | ||
401 | sn box Box::new(expr) | ||
402 | sn call function(expr) | ||
403 | sn dbg dbg!(expr) | ||
404 | sn dbgr dbg!(&expr) | ||
405 | sn if if expr {} | ||
406 | sn let let | ||
407 | sn letm let mut | ||
408 | sn match match expr {} | ||
409 | sn not !expr | ||
410 | sn ok Ok(expr) | ||
411 | sn ref &expr | ||
412 | sn refm &mut expr | ||
413 | sn some Some(expr) | ||
414 | sn while while expr {} | ||
415 | "#]], | ||
416 | ); | ||
417 | } | ||
418 | |||
419 | #[test] | ||
334 | fn option_iflet() { | 420 | fn option_iflet() { |
335 | check_edit( | 421 | check_edit( |
336 | "ifl", | 422 | "ifl", |
diff --git a/crates/completion/src/completions/qualified_path.rs b/crates/completion/src/completions/qualified_path.rs index d9387054d..bc23bea3f 100644 --- a/crates/completion/src/completions/qualified_path.rs +++ b/crates/completion/src/completions/qualified_path.rs | |||
@@ -353,10 +353,10 @@ impl S { | |||
353 | fn foo() { let _ = S::<|> } | 353 | fn foo() { let _ = S::<|> } |
354 | "#, | 354 | "#, |
355 | expect![[r#" | 355 | expect![[r#" |
356 | ct C const C: i32 = 42; | 356 | ct C const C: i32 = 42; |
357 | ta T type T = i32; | 357 | ta T type T = i32; |
358 | fn a() fn a() | 358 | fn a() fn a() |
359 | me b() fn b(&self) | 359 | me b(…) fn b(&self) |
360 | "#]], | 360 | "#]], |
361 | ); | 361 | ); |
362 | } | 362 | } |
@@ -503,14 +503,14 @@ trait Sub: Super { | |||
503 | fn foo<T: Sub>() { T::<|> } | 503 | fn foo<T: Sub>() { T::<|> } |
504 | "#, | 504 | "#, |
505 | expect![[r#" | 505 | expect![[r#" |
506 | ct C2 const C2: (); | 506 | ct C2 const C2: (); |
507 | ct CONST const CONST: u8; | 507 | ct CONST const CONST: u8; |
508 | ta SubTy type SubTy; | 508 | ta SubTy type SubTy; |
509 | ta Ty type Ty; | 509 | ta Ty type Ty; |
510 | fn func() fn func() | 510 | fn func() fn func() |
511 | me method() fn method(&self) | 511 | me method(…) fn method(&self) |
512 | fn subfunc() fn subfunc() | 512 | fn subfunc() fn subfunc() |
513 | me submethod() fn submethod(&self) | 513 | me submethod(…) fn submethod(&self) |
514 | "#]], | 514 | "#]], |
515 | ); | 515 | ); |
516 | } | 516 | } |
@@ -543,14 +543,14 @@ impl<T> Sub for Wrap<T> { | |||
543 | } | 543 | } |
544 | "#, | 544 | "#, |
545 | expect![[r#" | 545 | expect![[r#" |
546 | ct C2 const C2: () = (); | 546 | ct C2 const C2: () = (); |
547 | ct CONST const CONST: u8 = 0; | 547 | ct CONST const CONST: u8 = 0; |
548 | ta SubTy type SubTy; | 548 | ta SubTy type SubTy; |
549 | ta Ty type Ty; | 549 | ta Ty type Ty; |
550 | fn func() fn func() | 550 | fn func() fn func() |
551 | me method() fn method(&self) | 551 | me method(…) fn method(&self) |
552 | fn subfunc() fn subfunc() | 552 | fn subfunc() fn subfunc() |
553 | me submethod() fn submethod(&self) | 553 | me submethod(…) fn submethod(&self) |
554 | "#]], | 554 | "#]], |
555 | ); | 555 | ); |
556 | } | 556 | } |
diff --git a/crates/completion/src/completions/trait_impl.rs b/crates/completion/src/completions/trait_impl.rs index a14be9c73..e2fe44aff 100644 --- a/crates/completion/src/completions/trait_impl.rs +++ b/crates/completion/src/completions/trait_impl.rs | |||
@@ -139,7 +139,7 @@ fn add_function_impl( | |||
139 | ) { | 139 | ) { |
140 | let fn_name = func.name(ctx.db).to_string(); | 140 | let fn_name = func.name(ctx.db).to_string(); |
141 | 141 | ||
142 | let label = if func.params(ctx.db).is_empty() { | 142 | let label = if func.assoc_fn_params(ctx.db).is_empty() { |
143 | format!("fn {}()", fn_name) | 143 | format!("fn {}()", fn_name) |
144 | } else { | 144 | } else { |
145 | format!("fn {}(..)", fn_name) | 145 | format!("fn {}(..)", fn_name) |