|
1 | 1 | //! Completes keywords.
|
2 | 2 |
|
3 |
| -use syntax::{ast, SyntaxKind}; |
| 3 | +use syntax::SyntaxKind; |
4 | 4 | use test_utils::mark;
|
5 | 5 |
|
6 | 6 | use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions};
|
@@ -86,8 +86,8 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
|
86 | 86 | add_keyword(ctx, acc, "match", "match $0 {}");
|
87 | 87 | add_keyword(ctx, acc, "while", "while $0 {}");
|
88 | 88 | add_keyword(ctx, acc, "loop", "loop {$0}");
|
89 |
| - add_keyword(ctx, acc, "if", "if "); |
90 |
| - add_keyword(ctx, acc, "if let", "if let "); |
| 89 | + add_keyword(ctx, acc, "if", "if $0 {}"); |
| 90 | + add_keyword(ctx, acc, "if let", "if let $1 = $0 {}"); |
91 | 91 | }
|
92 | 92 |
|
93 | 93 | if ctx.if_is_prev || ctx.block_expr_parent {
|
@@ -143,47 +143,49 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
|
143 | 143 | Some(it) => it,
|
144 | 144 | None => return,
|
145 | 145 | };
|
146 |
| - acc.add_all(complete_return(ctx, &fn_def, ctx.can_be_stmt)); |
147 |
| -} |
148 |
| - |
149 |
| -fn keyword(ctx: &CompletionContext, kw: &str, snippet: &str) -> CompletionItem { |
150 |
| - let res = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), kw) |
151 |
| - .kind(CompletionItemKind::Keyword); |
152 | 146 |
|
153 |
| - match ctx.config.snippet_cap { |
154 |
| - Some(cap) => res.insert_snippet(cap, snippet), |
155 |
| - _ => res.insert_text(if snippet.contains('$') { kw } else { snippet }), |
156 |
| - } |
157 |
| - .build() |
| 147 | + add_keyword( |
| 148 | + ctx, |
| 149 | + acc, |
| 150 | + "return", |
| 151 | + match (ctx.can_be_stmt, fn_def.ret_type().is_some()) { |
| 152 | + (true, true) => "return $0;", |
| 153 | + (true, false) => "return;", |
| 154 | + (false, true) => "return $0", |
| 155 | + (false, false) => "return", |
| 156 | + }, |
| 157 | + ) |
158 | 158 | }
|
159 | 159 |
|
160 | 160 | fn add_keyword(ctx: &CompletionContext, acc: &mut Completions, kw: &str, snippet: &str) {
|
161 |
| - acc.add(keyword(ctx, kw, snippet)); |
162 |
| -} |
163 |
| - |
164 |
| -fn complete_return( |
165 |
| - ctx: &CompletionContext, |
166 |
| - fn_def: &ast::Fn, |
167 |
| - can_be_stmt: bool, |
168 |
| -) -> Option<CompletionItem> { |
169 |
| - let snip = match (can_be_stmt, fn_def.ret_type().is_some()) { |
170 |
| - (true, true) => "return $0;", |
171 |
| - (true, false) => "return;", |
172 |
| - (false, true) => "return $0", |
173 |
| - (false, false) => "return", |
| 161 | + let builder = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), kw) |
| 162 | + .kind(CompletionItemKind::Keyword); |
| 163 | + let builder = match ctx.config.snippet_cap { |
| 164 | + Some(cap) => { |
| 165 | + let tmp; |
| 166 | + let snippet = if snippet.ends_with('}') && ctx.incomplete_let { |
| 167 | + mark::hit!(let_semi); |
| 168 | + tmp = format!("{};", snippet); |
| 169 | + &tmp |
| 170 | + } else { |
| 171 | + snippet |
| 172 | + }; |
| 173 | + builder.insert_snippet(cap, snippet) |
| 174 | + } |
| 175 | + None => builder.insert_text(if snippet.contains('$') { kw } else { snippet }), |
174 | 176 | };
|
175 |
| - Some(keyword(ctx, "return", snip)) |
| 177 | + acc.add(builder.build()); |
176 | 178 | }
|
177 | 179 |
|
178 | 180 | #[cfg(test)]
|
179 | 181 | mod tests {
|
180 | 182 | use expect_test::{expect, Expect};
|
| 183 | + use test_utils::mark; |
181 | 184 |
|
182 | 185 | use crate::{
|
183 | 186 | test_utils::{check_edit, completion_list},
|
184 | 187 | CompletionKind,
|
185 | 188 | };
|
186 |
| - use test_utils::mark; |
187 | 189 |
|
188 | 190 | fn check(ra_fixture: &str, expect: Expect) {
|
189 | 191 | let actual = completion_list(ra_fixture, CompletionKind::Keyword);
|
@@ -609,4 +611,50 @@ fn foo() {
|
609 | 611 | "#]],
|
610 | 612 | );
|
611 | 613 | }
|
| 614 | + |
| 615 | + #[test] |
| 616 | + fn let_semi() { |
| 617 | + mark::check!(let_semi); |
| 618 | + check_edit( |
| 619 | + "match", |
| 620 | + r#" |
| 621 | +fn main() { let x = $0 } |
| 622 | +"#, |
| 623 | + r#" |
| 624 | +fn main() { let x = match $0 {}; } |
| 625 | +"#, |
| 626 | + ); |
| 627 | + |
| 628 | + check_edit( |
| 629 | + "if", |
| 630 | + r#" |
| 631 | +fn main() { |
| 632 | + let x = $0 |
| 633 | + let y = 92; |
| 634 | +} |
| 635 | +"#, |
| 636 | + r#" |
| 637 | +fn main() { |
| 638 | + let x = if $0 {}; |
| 639 | + let y = 92; |
| 640 | +} |
| 641 | +"#, |
| 642 | + ); |
| 643 | + |
| 644 | + check_edit( |
| 645 | + "loop", |
| 646 | + r#" |
| 647 | +fn main() { |
| 648 | + let x = $0 |
| 649 | + bar(); |
| 650 | +} |
| 651 | +"#, |
| 652 | + r#" |
| 653 | +fn main() { |
| 654 | + let x = loop {$0}; |
| 655 | + bar(); |
| 656 | +} |
| 657 | +"#, |
| 658 | + ); |
| 659 | + } |
612 | 660 | }
|
0 commit comments