aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src/completions
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_completion/src/completions')
-rw-r--r--crates/ide_completion/src/completions/keyword.rs56
-rw-r--r--crates/ide_completion/src/completions/pattern.rs13
-rw-r--r--crates/ide_completion/src/completions/unqualified_path.rs6
3 files changed, 43 insertions, 32 deletions
diff --git a/crates/ide_completion/src/completions/keyword.rs b/crates/ide_completion/src/completions/keyword.rs
index 61b667104..fa6bcc955 100644
--- a/crates/ide_completion/src/completions/keyword.rs
+++ b/crates/ide_completion/src/completions/keyword.rs
@@ -2,7 +2,7 @@
2 2
3use std::iter; 3use std::iter;
4 4
5use syntax::SyntaxKind; 5use syntax::{SyntaxKind, T};
6 6
7use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions}; 7use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions};
8 8
@@ -54,51 +54,51 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
54 add_keyword(ctx, acc, "where", "where "); 54 add_keyword(ctx, acc, "where", "where ");
55 return; 55 return;
56 } 56 }
57 if ctx.unsafe_is_prev { 57 if ctx.previous_token_is(T![unsafe]) {
58 if ctx.has_item_list_or_source_file_parent || ctx.block_expr_parent { 58 if ctx.has_item_list_or_source_file_parent || ctx.block_expr_parent {
59 add_keyword(ctx, acc, "fn", "fn $0() {}") 59 add_keyword(ctx, acc, "fn", "fn $1($2) {\n $0\n}")
60 } 60 }
61 61
62 if (ctx.has_item_list_or_source_file_parent) || ctx.block_expr_parent { 62 if (ctx.has_item_list_or_source_file_parent) || ctx.block_expr_parent {
63 add_keyword(ctx, acc, "trait", "trait $0 {}"); 63 add_keyword(ctx, acc, "trait", "trait $1 {\n $0\n}");
64 add_keyword(ctx, acc, "impl", "impl $0 {}"); 64 add_keyword(ctx, acc, "impl", "impl $1 {\n $0\n}");
65 } 65 }
66 66
67 return; 67 return;
68 } 68 }
69 if ctx.has_item_list_or_source_file_parent || has_trait_or_impl_parent || ctx.block_expr_parent 69 if ctx.has_item_list_or_source_file_parent || has_trait_or_impl_parent || ctx.block_expr_parent
70 { 70 {
71 add_keyword(ctx, acc, "fn", "fn $0() {}"); 71 add_keyword(ctx, acc, "fn", "fn $1($2) {\n $0\n}");
72 } 72 }
73 if (ctx.has_item_list_or_source_file_parent) || ctx.block_expr_parent { 73 if (ctx.has_item_list_or_source_file_parent) || ctx.block_expr_parent {
74 add_keyword(ctx, acc, "use", "use "); 74 add_keyword(ctx, acc, "use", "use ");
75 add_keyword(ctx, acc, "impl", "impl $0 {}"); 75 add_keyword(ctx, acc, "impl", "impl $1 {\n $0\n}");
76 add_keyword(ctx, acc, "trait", "trait $0 {}"); 76 add_keyword(ctx, acc, "trait", "trait $1 {\n $0\n}");
77 } 77 }
78 78
79 if ctx.has_item_list_or_source_file_parent { 79 if ctx.has_item_list_or_source_file_parent {
80 add_keyword(ctx, acc, "enum", "enum $0 {}"); 80 add_keyword(ctx, acc, "enum", "enum $1 {\n $0\n}");
81 add_keyword(ctx, acc, "struct", "struct $0"); 81 add_keyword(ctx, acc, "struct", "struct $0");
82 add_keyword(ctx, acc, "union", "union $0 {}"); 82 add_keyword(ctx, acc, "union", "union $1 {\n $0\n}");
83 } 83 }
84 84
85 if ctx.is_expr { 85 if ctx.is_expr {
86 add_keyword(ctx, acc, "match", "match $0 {}"); 86 add_keyword(ctx, acc, "match", "match $1 {\n $0\n}");
87 add_keyword(ctx, acc, "while", "while $0 {}"); 87 add_keyword(ctx, acc, "while", "while $1 {\n $0\n}");
88 add_keyword(ctx, acc, "while let", "while let $1 = $0 {}"); 88 add_keyword(ctx, acc, "while let", "while let $1 = $2 {\n $0\n}");
89 add_keyword(ctx, acc, "loop", "loop {$0}"); 89 add_keyword(ctx, acc, "loop", "loop {\n $0\n}");
90 add_keyword(ctx, acc, "if", "if $0 {}"); 90 add_keyword(ctx, acc, "if", "if $1 {\n $0\n}");
91 add_keyword(ctx, acc, "if let", "if let $1 = $0 {}"); 91 add_keyword(ctx, acc, "if let", "if let $1 = $2 {\n $0\n}");
92 add_keyword(ctx, acc, "for", "for $1 in $0 {}"); 92 add_keyword(ctx, acc, "for", "for $1 in $2 {\n $0\n}");
93 } 93 }
94 94
95 if ctx.if_is_prev || ctx.block_expr_parent { 95 if ctx.previous_token_is(T![if]) || ctx.previous_token_is(T![while]) || ctx.block_expr_parent {
96 add_keyword(ctx, acc, "let", "let "); 96 add_keyword(ctx, acc, "let", "let ");
97 } 97 }
98 98
99 if ctx.after_if { 99 if ctx.after_if {
100 add_keyword(ctx, acc, "else", "else {$0}"); 100 add_keyword(ctx, acc, "else", "else {\n $0\n}");
101 add_keyword(ctx, acc, "else if", "else if $0 {}"); 101 add_keyword(ctx, acc, "else if", "else if $1 {\n $0\n}");
102 } 102 }
103 if (ctx.has_item_list_or_source_file_parent) || ctx.block_expr_parent { 103 if (ctx.has_item_list_or_source_file_parent) || ctx.block_expr_parent {
104 add_keyword(ctx, acc, "mod", "mod $0"); 104 add_keyword(ctx, acc, "mod", "mod $0");
@@ -342,7 +342,9 @@ mod tests {
342 check_edit( 342 check_edit(
343 "else", 343 "else",
344 r#"fn quux() { if true { () } $0 }"#, 344 r#"fn quux() { if true { () } $0 }"#,
345 r#"fn quux() { if true { () } else {$0} }"#, 345 r#"fn quux() { if true { () } else {
346 $0
347} }"#,
346 ); 348 );
347 } 349 }
348 350
@@ -646,7 +648,9 @@ fn foo() {
646fn main() { let x = $0 } 648fn main() { let x = $0 }
647"#, 649"#,
648 r#" 650 r#"
649fn main() { let x = match $0 {}; } 651fn main() { let x = match $1 {
652 $0
653}; }
650"#, 654"#,
651 ); 655 );
652 656
@@ -660,7 +664,9 @@ fn main() {
660"#, 664"#,
661 r#" 665 r#"
662fn main() { 666fn main() {
663 let x = if $0 {}; 667 let x = if $1 {
668 $0
669};
664 let y = 92; 670 let y = 92;
665} 671}
666"#, 672"#,
@@ -676,7 +682,9 @@ fn main() {
676"#, 682"#,
677 r#" 683 r#"
678fn main() { 684fn main() {
679 let x = loop {$0}; 685 let x = loop {
686 $0
687};
680 bar(); 688 bar();
681} 689}
682"#, 690"#,
diff --git a/crates/ide_completion/src/completions/pattern.rs b/crates/ide_completion/src/completions/pattern.rs
index 8dc9ab73c..3329a4844 100644
--- a/crates/ide_completion/src/completions/pattern.rs
+++ b/crates/ide_completion/src/completions/pattern.rs
@@ -1,17 +1,18 @@
1//! Completes constants and paths in patterns. 1//! Completes constants and paths in patterns.
2 2
3use crate::{CompletionContext, Completions}; 3use crate::{context::IsPatOrConst, CompletionContext, Completions};
4 4
5/// Completes constants and paths in patterns. 5/// Completes constants and paths in patterns.
6pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) { 6pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
7 if !(ctx.is_pat_binding_or_const || ctx.is_irrefutable_pat_binding) { 7 if ctx.is_pat_or_const == IsPatOrConst::No {
8 return; 8 return;
9 } 9 }
10 if ctx.record_pat_syntax.is_some() { 10 if ctx.record_pat_syntax.is_some() {
11 return; 11 return;
12 } 12 }
13 13
14 if !ctx.is_irrefutable_pat_binding { 14 let refutable = ctx.is_pat_or_const == IsPatOrConst::Refutable;
15 if refutable {
15 if let Some(hir::Adt::Enum(e)) = 16 if let Some(hir::Adt::Enum(e)) =
16 ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt()) 17 ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
17 { 18 {
@@ -31,14 +32,14 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
31 acc.add_struct_pat(ctx, *strukt, Some(name.clone())); 32 acc.add_struct_pat(ctx, *strukt, Some(name.clone()));
32 true 33 true
33 } 34 }
34 hir::ModuleDef::Variant(variant) if !ctx.is_irrefutable_pat_binding => { 35 hir::ModuleDef::Variant(variant) if refutable => {
35 acc.add_variant_pat(ctx, *variant, Some(name.clone())); 36 acc.add_variant_pat(ctx, *variant, Some(name.clone()));
36 true 37 true
37 } 38 }
38 hir::ModuleDef::Adt(hir::Adt::Enum(..)) 39 hir::ModuleDef::Adt(hir::Adt::Enum(..))
39 | hir::ModuleDef::Variant(..) 40 | hir::ModuleDef::Variant(..)
40 | hir::ModuleDef::Const(..) 41 | hir::ModuleDef::Const(..)
41 | hir::ModuleDef::Module(..) => !ctx.is_irrefutable_pat_binding, 42 | hir::ModuleDef::Module(..) => refutable,
42 _ => false, 43 _ => false,
43 }, 44 },
44 hir::ScopeDef::MacroDef(_) => true, 45 hir::ScopeDef::MacroDef(_) => true,
@@ -47,7 +48,7 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
47 acc.add_struct_pat(ctx, strukt, Some(name.clone())); 48 acc.add_struct_pat(ctx, strukt, Some(name.clone()));
48 true 49 true
49 } 50 }
50 Some(hir::Adt::Enum(_)) => !ctx.is_irrefutable_pat_binding, 51 Some(hir::Adt::Enum(_)) => refutable,
51 _ => true, 52 _ => true,
52 }, 53 },
53 _ => false, 54 _ => false,
diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs
index 7875500c1..b8f8ef25f 100644
--- a/crates/ide_completion/src/completions/unqualified_path.rs
+++ b/crates/ide_completion/src/completions/unqualified_path.rs
@@ -13,6 +13,8 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
13 || ctx.record_pat_syntax.is_some() 13 || ctx.record_pat_syntax.is_some()
14 || ctx.attribute_under_caret.is_some() 14 || ctx.attribute_under_caret.is_some()
15 || ctx.mod_declaration_under_caret.is_some() 15 || ctx.mod_declaration_under_caret.is_some()
16 || ctx.has_impl_parent
17 || ctx.has_trait_parent
16 { 18 {
17 return; 19 return;
18 } 20 }
@@ -86,7 +88,7 @@ fn quux(x: Option<Enum>) {
86 } 88 }
87} 89}
88"#, 90"#,
89 expect![[""]], 91 expect![[r#""#]],
90 ); 92 );
91 } 93 }
92 94
@@ -102,7 +104,7 @@ fn quux(x: Option<Enum>) {
102 } 104 }
103} 105}
104"#, 106"#,
105 expect![[""]], 107 expect![[r#""#]],
106 ); 108 );
107 } 109 }
108 110