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