aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikhail Rakhmanov <[email protected]>2020-06-12 07:49:12 +0100
committerMikhail Rakhmanov <[email protected]>2020-06-12 07:49:12 +0100
commit396167eadbea168e0d9858b5f45b7db860873f8b (patch)
tree6e011295e5b153166cfbdc1825368b7dce8a419c
parenta2b4385f161134955fd729087f142d54c3a5e035 (diff)
New testing approach for keywords
-rw-r--r--crates/ra_ide/src/completion/complete_keyword.rs43
-rw-r--r--crates/ra_ide/src/completion/test_utils.rs19
2 files changed, 56 insertions, 6 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!(
diff --git a/crates/ra_ide/src/completion/test_utils.rs b/crates/ra_ide/src/completion/test_utils.rs
index e9e89104e..277d2904d 100644
--- a/crates/ra_ide/src/completion/test_utils.rs
+++ b/crates/ra_ide/src/completion/test_utils.rs
@@ -3,7 +3,7 @@
3use crate::{ 3use crate::{
4 completion::{completion_item::CompletionKind, CompletionConfig}, 4 completion::{completion_item::CompletionKind, CompletionConfig},
5 mock_analysis::{analysis_and_position, single_file_with_position}, 5 mock_analysis::{analysis_and_position, single_file_with_position},
6 CompletionItem, 6 CompletionItem, FilePosition,
7}; 7};
8use hir::Semantics; 8use hir::Semantics;
9use ra_syntax::{AstNode, NodeOrToken, SyntaxElement, SyntaxToken}; 9use ra_syntax::{AstNode, NodeOrToken, SyntaxElement, SyntaxToken};
@@ -12,11 +12,26 @@ pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionI
12 do_completion_with_options(code, kind, &CompletionConfig::default()) 12 do_completion_with_options(code, kind, &CompletionConfig::default())
13} 13}
14 14
15pub(crate) fn do_completion_with_position(
16 code: &str,
17 kind: CompletionKind,
18) -> (FilePosition, Vec<CompletionItem>) {
19 do_completion_with_options_and_position(code, kind, &CompletionConfig::default())
20}
21
15pub(crate) fn do_completion_with_options( 22pub(crate) fn do_completion_with_options(
16 code: &str, 23 code: &str,
17 kind: CompletionKind, 24 kind: CompletionKind,
18 options: &CompletionConfig, 25 options: &CompletionConfig,
19) -> Vec<CompletionItem> { 26) -> Vec<CompletionItem> {
27 do_completion_with_options_and_position(code, kind, options).1
28}
29
30pub(crate) fn do_completion_with_options_and_position(
31 code: &str,
32 kind: CompletionKind,
33 options: &CompletionConfig,
34) -> (FilePosition, Vec<CompletionItem>) {
20 let (analysis, position) = if code.contains("//-") { 35 let (analysis, position) = if code.contains("//-") {
21 analysis_and_position(code) 36 analysis_and_position(code)
22 } else { 37 } else {
@@ -27,7 +42,7 @@ pub(crate) fn do_completion_with_options(
27 let mut kind_completions: Vec<CompletionItem> = 42 let mut kind_completions: Vec<CompletionItem> =
28 completion_items.into_iter().filter(|c| c.completion_kind == kind).collect(); 43 completion_items.into_iter().filter(|c| c.completion_kind == kind).collect();
29 kind_completions.sort_by_key(|c| c.label().to_owned()); 44 kind_completions.sort_by_key(|c| c.label().to_owned());
30 kind_completions 45 (position, kind_completions)
31} 46}
32 47
33pub(crate) fn check_pattern_is_applicable(code: &str, check: fn(SyntaxElement) -> bool) { 48pub(crate) fn check_pattern_is_applicable(code: &str, check: fn(SyntaxElement) -> bool) {