aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/completion/complete_keyword.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/completion/complete_keyword.rs')
-rw-r--r--crates/ra_ide/src/completion/complete_keyword.rs43
1 files changed, 39 insertions, 4 deletions
diff --git a/crates/ra_ide/src/completion/complete_keyword.rs b/crates/ra_ide/src/completion/complete_keyword.rs
index 675991154..7eddf76b9 100644
--- a/crates/ra_ide/src/completion/complete_keyword.rs
+++ b/crates/ra_ide/src/completion/complete_keyword.rs
@@ -76,8 +76,6 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
76 add_keyword(ctx, acc, "else if", "else if $0 {}", ctx.after_if); 76 add_keyword(ctx, acc, "else if", "else if $0 {}", ctx.after_if);
77 add_keyword(ctx, acc, "mod", "mod $0 {}", ctx.is_new_item || ctx.block_expr_parent); 77 add_keyword(ctx, acc, "mod", "mod $0 {}", ctx.is_new_item || ctx.block_expr_parent);
78 add_keyword(ctx, acc, "mut", "mut ", ctx.bind_pat_parent || ctx.ref_pat_parent); 78 add_keyword(ctx, acc, "mut", "mut ", ctx.bind_pat_parent || ctx.ref_pat_parent);
79 add_keyword(ctx, acc, "true", "true", !ctx.is_new_item); // this should be defined properly
80 add_keyword(ctx, acc, "false", "false", !ctx.is_new_item); // this should be defined properly
81 add_keyword(ctx, acc, "const", "const ", ctx.is_new_item || ctx.block_expr_parent); 79 add_keyword(ctx, acc, "const", "const ", ctx.is_new_item || ctx.block_expr_parent);
82 add_keyword(ctx, acc, "type", "type ", ctx.is_new_item || ctx.block_expr_parent); 80 add_keyword(ctx, acc, "type", "type ", ctx.is_new_item || ctx.block_expr_parent);
83 add_keyword(ctx, acc, "static", "static ", ctx.is_new_item || ctx.block_expr_parent); 81 add_keyword(ctx, acc, "static", "static ", ctx.is_new_item || ctx.block_expr_parent);
@@ -89,7 +87,6 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
89 add_keyword(ctx, acc, "break", "break", ctx.in_loop_body && !ctx.can_be_stmt); 87 add_keyword(ctx, acc, "break", "break", ctx.in_loop_body && !ctx.can_be_stmt);
90 add_keyword(ctx, acc, "pub", "pub ", ctx.is_new_item && !ctx.inside_trait); 88 add_keyword(ctx, acc, "pub", "pub ", ctx.is_new_item && !ctx.inside_trait);
91 add_keyword(ctx, acc, "where", "where ", ctx.trait_as_prev_sibling || ctx.impl_as_prev_sibling); 89 add_keyword(ctx, acc, "where", "where ", ctx.trait_as_prev_sibling || ctx.impl_as_prev_sibling);
92 complete_use_tree_keyword(acc, ctx);
93 90
94 let fn_def = match &ctx.function_syntax { 91 let fn_def = match &ctx.function_syntax {
95 Some(it) => it, 92 Some(it) => it,
@@ -114,13 +111,51 @@ fn complete_return(
114 111
115#[cfg(test)] 112#[cfg(test)]
116mod tests { 113mod tests {
117 use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; 114 use crate::{
115 completion::{
116 test_utils::{do_completion, do_completion_with_position},
117 CompletionItem, CompletionKind,
118 },
119 CompletionItemKind,
120 };
118 use insta::assert_debug_snapshot; 121 use insta::assert_debug_snapshot;
122 use rustc_hash::FxHashSet;
119 123
120 fn do_keyword_completion(code: &str) -> Vec<CompletionItem> { 124 fn do_keyword_completion(code: &str) -> Vec<CompletionItem> {
121 do_completion(code, CompletionKind::Keyword) 125 do_completion(code, CompletionKind::Keyword)
122 } 126 }
123 127
128 fn assert_completion_keyword(code: &str, keywords: &[(&str, &str)]) {
129 let (position, completion_items) =
130 do_completion_with_position(code, CompletionKind::Keyword);
131 let mut set = FxHashSet::<(String, String)>::default();
132 for (key, value) in keywords {
133 set.insert(((*key).to_string(), (*value).to_string()));
134 }
135
136 for item in completion_items {
137 assert!(item.text_edit().len() == 1);
138 assert!(item.kind() == Some(CompletionItemKind::Keyword));
139 let atom = item.text_edit().iter().next().unwrap().clone();
140 assert!(atom.delete.start() == position.offset);
141 assert!(atom.delete.end() == position.offset);
142 let pair = (item.label().to_string(), atom.insert);
143 assert!(set.contains(&pair));
144 set.remove(&pair);
145 }
146 assert!(set.is_empty());
147 }
148
149 #[test]
150 fn completes_keywords_in_use_stmt_new_approach() {
151 assert_completion_keyword(
152 r"
153 use <|>
154 ",
155 &[("crate", "crate::"), ("self", "self"), ("super", "super::")],
156 );
157 }
158
124 #[test] 159 #[test]
125 fn completes_keywords_in_use_stmt() { 160 fn completes_keywords_in_use_stmt() {
126 assert_debug_snapshot!( 161 assert_debug_snapshot!(