diff options
author | Aleksey Kladov <[email protected]> | 2020-07-10 16:41:43 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-07-10 16:41:43 +0100 |
commit | e4983daa5e595d52e4e5f1d6a06330f2be27c9ee (patch) | |
tree | 67994fd0735f3084df9597d72e6c679e9e1277da /crates/ra_ide | |
parent | 5a195001d79995a6336988e18a2b78052917a02e (diff) |
Better complete expression keywords
Diffstat (limited to 'crates/ra_ide')
-rw-r--r-- | crates/ra_ide/src/completion/complete_keyword.rs | 34 | ||||
-rw-r--r-- | crates/ra_ide/src/completion/completion_context.rs | 4 |
2 files changed, 27 insertions, 11 deletions
diff --git a/crates/ra_ide/src/completion/complete_keyword.rs b/crates/ra_ide/src/completion/complete_keyword.rs index 2dc401f57..921d610e6 100644 --- a/crates/ra_ide/src/completion/complete_keyword.rs +++ b/crates/ra_ide/src/completion/complete_keyword.rs | |||
@@ -92,20 +92,18 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte | |||
92 | add_keyword(ctx, acc, "union", "union $0 {}"); | 92 | add_keyword(ctx, acc, "union", "union $0 {}"); |
93 | } | 93 | } |
94 | 94 | ||
95 | if ctx.block_expr_parent || ctx.is_match_arm { | 95 | if ctx.can_be_expr { |
96 | add_keyword(ctx, acc, "match", "match $0 {}"); | 96 | add_keyword(ctx, acc, "match", "match $0 {}"); |
97 | add_keyword(ctx, acc, "loop", "loop {$0}"); | ||
98 | } | ||
99 | if ctx.block_expr_parent { | ||
100 | add_keyword(ctx, acc, "while", "while $0 {}"); | 97 | add_keyword(ctx, acc, "while", "while $0 {}"); |
98 | add_keyword(ctx, acc, "loop", "loop {$0}"); | ||
99 | add_keyword(ctx, acc, "if", "if "); | ||
100 | add_keyword(ctx, acc, "if let", "if let "); | ||
101 | } | 101 | } |
102 | |||
102 | if ctx.if_is_prev || ctx.block_expr_parent { | 103 | if ctx.if_is_prev || ctx.block_expr_parent { |
103 | add_keyword(ctx, acc, "let", "let "); | 104 | add_keyword(ctx, acc, "let", "let "); |
104 | } | 105 | } |
105 | if ctx.if_is_prev || ctx.block_expr_parent || ctx.is_match_arm { | 106 | |
106 | add_keyword(ctx, acc, "if", "if "); | ||
107 | add_keyword(ctx, acc, "if let", "if let "); | ||
108 | } | ||
109 | if ctx.after_if { | 107 | if ctx.after_if { |
110 | add_keyword(ctx, acc, "else", "else {$0}"); | 108 | add_keyword(ctx, acc, "else", "else {$0}"); |
111 | add_keyword(ctx, acc, "else if", "else if $0 {}"); | 109 | add_keyword(ctx, acc, "else if", "else if $0 {}"); |
@@ -343,9 +341,7 @@ mod tests { | |||
343 | check( | 341 | check( |
344 | r#" | 342 | r#" |
345 | fn quux() -> i32 { | 343 | fn quux() -> i32 { |
346 | match () { | 344 | match () { () => <|> } |
347 | () => <|> | ||
348 | } | ||
349 | } | 345 | } |
350 | "#, | 346 | "#, |
351 | expect![[r#" | 347 | expect![[r#" |
@@ -355,6 +351,7 @@ fn quux() -> i32 { | |||
355 | kw match | 351 | kw match |
356 | kw return | 352 | kw return |
357 | kw unsafe | 353 | kw unsafe |
354 | kw while | ||
358 | "#]], | 355 | "#]], |
359 | ); | 356 | ); |
360 | } | 357 | } |
@@ -525,4 +522,19 @@ pub mod future { | |||
525 | "#]], | 522 | "#]], |
526 | ) | 523 | ) |
527 | } | 524 | } |
525 | |||
526 | #[test] | ||
527 | fn after_let() { | ||
528 | check( | ||
529 | r#"fn main() { let _ = <|> }"#, | ||
530 | expect![[r#" | ||
531 | kw if | ||
532 | kw if let | ||
533 | kw loop | ||
534 | kw match | ||
535 | kw return | ||
536 | kw while | ||
537 | "#]], | ||
538 | ) | ||
539 | } | ||
528 | } | 540 | } |
diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index 7688a1483..8e1f6dd98 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs | |||
@@ -53,6 +53,8 @@ pub(crate) struct CompletionContext<'a> { | |||
53 | pub(super) after_if: bool, | 53 | pub(super) after_if: bool, |
54 | /// `true` if we are a statement or a last expr in the block. | 54 | /// `true` if we are a statement or a last expr in the block. |
55 | pub(super) can_be_stmt: bool, | 55 | pub(super) can_be_stmt: bool, |
56 | /// `true` if we expect an expression at the cursor position. | ||
57 | pub(super) can_be_expr: bool, | ||
56 | /// Something is typed at the "top" level, in module or impl/trait. | 58 | /// Something is typed at the "top" level, in module or impl/trait. |
57 | pub(super) is_new_item: bool, | 59 | pub(super) is_new_item: bool, |
58 | /// The receiver if this is a field or method access, i.e. writing something.<|> | 60 | /// The receiver if this is a field or method access, i.e. writing something.<|> |
@@ -127,6 +129,7 @@ impl<'a> CompletionContext<'a> { | |||
127 | path_prefix: None, | 129 | path_prefix: None, |
128 | after_if: false, | 130 | after_if: false, |
129 | can_be_stmt: false, | 131 | can_be_stmt: false, |
132 | can_be_expr: false, | ||
130 | is_new_item: false, | 133 | is_new_item: false, |
131 | dot_receiver: None, | 134 | dot_receiver: None, |
132 | is_call: false, | 135 | is_call: false, |
@@ -403,6 +406,7 @@ impl<'a> CompletionContext<'a> { | |||
403 | None | 406 | None |
404 | }) | 407 | }) |
405 | .unwrap_or(false); | 408 | .unwrap_or(false); |
409 | self.can_be_expr = path.syntax().parent().and_then(ast::PathExpr::cast).is_some(); | ||
406 | 410 | ||
407 | if let Some(off) = name_ref.syntax().text_range().start().checked_sub(2.into()) { | 411 | if let Some(off) = name_ref.syntax().text_range().start().checked_sub(2.into()) { |
408 | if let Some(if_expr) = | 412 | if let Some(if_expr) = |