diff options
Diffstat (limited to 'crates/ra_ide/src/completion/test_utils.rs')
-rw-r--r-- | crates/ra_ide/src/completion/test_utils.rs | 72 |
1 files changed, 61 insertions, 11 deletions
diff --git a/crates/ra_ide/src/completion/test_utils.rs b/crates/ra_ide/src/completion/test_utils.rs index 5c01654cc..919177745 100644 --- a/crates/ra_ide/src/completion/test_utils.rs +++ b/crates/ra_ide/src/completion/test_utils.rs | |||
@@ -1,7 +1,10 @@ | |||
1 | //! Runs completion for testing purposes. | 1 | //! Runs completion for testing purposes. |
2 | 2 | ||
3 | use hir::Semantics; | 3 | use hir::Semantics; |
4 | use itertools::Itertools; | ||
4 | use ra_syntax::{AstNode, NodeOrToken, SyntaxElement}; | 5 | use ra_syntax::{AstNode, NodeOrToken, SyntaxElement}; |
6 | use stdx::{format_to, trim_indent}; | ||
7 | use test_utils::assert_eq_text; | ||
5 | 8 | ||
6 | use crate::{ | 9 | use crate::{ |
7 | completion::{completion_item::CompletionKind, CompletionConfig}, | 10 | completion::{completion_item::CompletionKind, CompletionConfig}, |
@@ -10,15 +13,15 @@ use crate::{ | |||
10 | }; | 13 | }; |
11 | 14 | ||
12 | pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> { | 15 | pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> { |
13 | do_completion_with_options(code, kind, &CompletionConfig::default()) | 16 | do_completion_with_config(CompletionConfig::default(), code, kind) |
14 | } | 17 | } |
15 | 18 | ||
16 | pub(crate) fn do_completion_with_options( | 19 | pub(crate) fn do_completion_with_config( |
20 | config: CompletionConfig, | ||
17 | code: &str, | 21 | code: &str, |
18 | kind: CompletionKind, | 22 | kind: CompletionKind, |
19 | options: &CompletionConfig, | ||
20 | ) -> Vec<CompletionItem> { | 23 | ) -> Vec<CompletionItem> { |
21 | let mut kind_completions: Vec<CompletionItem> = get_all_completion_items(code, options) | 24 | let mut kind_completions: Vec<CompletionItem> = get_all_completion_items(config, code) |
22 | .into_iter() | 25 | .into_iter() |
23 | .filter(|c| c.completion_kind == kind) | 26 | .filter(|c| c.completion_kind == kind) |
24 | .collect(); | 27 | .collect(); |
@@ -27,25 +30,69 @@ pub(crate) fn do_completion_with_options( | |||
27 | } | 30 | } |
28 | 31 | ||
29 | pub(crate) fn completion_list(code: &str, kind: CompletionKind) -> String { | 32 | pub(crate) fn completion_list(code: &str, kind: CompletionKind) -> String { |
30 | completion_list_with_options(code, kind, &CompletionConfig::default()) | 33 | completion_list_with_config(CompletionConfig::default(), code, kind) |
31 | } | 34 | } |
32 | 35 | ||
33 | pub(crate) fn completion_list_with_options( | 36 | pub(crate) fn completion_list_with_config( |
37 | config: CompletionConfig, | ||
34 | code: &str, | 38 | code: &str, |
35 | kind: CompletionKind, | 39 | kind: CompletionKind, |
36 | options: &CompletionConfig, | ||
37 | ) -> String { | 40 | ) -> String { |
38 | let mut kind_completions: Vec<CompletionItem> = get_all_completion_items(code, options) | 41 | let mut kind_completions: Vec<CompletionItem> = get_all_completion_items(config, code) |
39 | .into_iter() | 42 | .into_iter() |
40 | .filter(|c| c.completion_kind == kind) | 43 | .filter(|c| c.completion_kind == kind) |
41 | .collect(); | 44 | .collect(); |
42 | kind_completions.sort_by_key(|c| c.label().to_owned()); | 45 | kind_completions.sort_by_key(|c| c.label().to_owned()); |
46 | let label_width = kind_completions | ||
47 | .iter() | ||
48 | .map(|it| monospace_width(it.label())) | ||
49 | .max() | ||
50 | .unwrap_or_default() | ||
51 | .min(16); | ||
43 | kind_completions | 52 | kind_completions |
44 | .into_iter() | 53 | .into_iter() |
45 | .map(|it| format!("{} {}\n", it.kind().unwrap().tag(), it.label())) | 54 | .map(|it| { |
55 | let tag = it.kind().unwrap().tag(); | ||
56 | let var_name = format!("{} {}", tag, it.label()); | ||
57 | let mut buf = var_name; | ||
58 | if let Some(detail) = it.detail() { | ||
59 | let width = label_width.saturating_sub(monospace_width(it.label())); | ||
60 | format_to!(buf, "{:width$} {}", "", detail, width = width); | ||
61 | } | ||
62 | format_to!(buf, "\n"); | ||
63 | buf | ||
64 | }) | ||
46 | .collect() | 65 | .collect() |
47 | } | 66 | } |
48 | 67 | ||
68 | fn monospace_width(s: &str) -> usize { | ||
69 | s.chars().count() | ||
70 | } | ||
71 | |||
72 | pub(crate) fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) { | ||
73 | check_edit_with_config(CompletionConfig::default(), what, ra_fixture_before, ra_fixture_after) | ||
74 | } | ||
75 | |||
76 | pub(crate) fn check_edit_with_config( | ||
77 | config: CompletionConfig, | ||
78 | what: &str, | ||
79 | ra_fixture_before: &str, | ||
80 | ra_fixture_after: &str, | ||
81 | ) { | ||
82 | let ra_fixture_after = trim_indent(ra_fixture_after); | ||
83 | let (analysis, position) = analysis_and_position(ra_fixture_before); | ||
84 | let completions: Vec<CompletionItem> = | ||
85 | analysis.completions(&config, position).unwrap().unwrap().into(); | ||
86 | let (completion,) = completions | ||
87 | .iter() | ||
88 | .filter(|it| it.lookup() == what) | ||
89 | .collect_tuple() | ||
90 | .unwrap_or_else(|| panic!("can't find {:?} completion in {:#?}", what, completions)); | ||
91 | let mut actual = analysis.file_text(position.file_id).unwrap().to_string(); | ||
92 | completion.text_edit().apply(&mut actual); | ||
93 | assert_eq_text!(&ra_fixture_after, &actual) | ||
94 | } | ||
95 | |||
49 | pub(crate) fn check_pattern_is_applicable(code: &str, check: fn(SyntaxElement) -> bool) { | 96 | pub(crate) fn check_pattern_is_applicable(code: &str, check: fn(SyntaxElement) -> bool) { |
50 | let (analysis, pos) = analysis_and_position(code); | 97 | let (analysis, pos) = analysis_and_position(code); |
51 | analysis | 98 | analysis |
@@ -58,7 +105,10 @@ pub(crate) fn check_pattern_is_applicable(code: &str, check: fn(SyntaxElement) - | |||
58 | .unwrap(); | 105 | .unwrap(); |
59 | } | 106 | } |
60 | 107 | ||
61 | fn get_all_completion_items(code: &str, options: &CompletionConfig) -> Vec<CompletionItem> { | 108 | pub(crate) fn get_all_completion_items( |
109 | config: CompletionConfig, | ||
110 | code: &str, | ||
111 | ) -> Vec<CompletionItem> { | ||
62 | let (analysis, position) = analysis_and_position(code); | 112 | let (analysis, position) = analysis_and_position(code); |
63 | analysis.completions(options, position).unwrap().unwrap().into() | 113 | analysis.completions(&config, position).unwrap().unwrap().into() |
64 | } | 114 | } |