aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src/tests.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_completion/src/tests.rs')
-rw-r--r--crates/ide_completion/src/tests.rs176
1 files changed, 176 insertions, 0 deletions
diff --git a/crates/ide_completion/src/tests.rs b/crates/ide_completion/src/tests.rs
new file mode 100644
index 000000000..1495924ea
--- /dev/null
+++ b/crates/ide_completion/src/tests.rs
@@ -0,0 +1,176 @@
1mod item_list;
2
3use hir::{PrefixKind, Semantics};
4use ide_db::{
5 base_db::{fixture::ChangeFixture, FileLoader, FilePosition},
6 helpers::{
7 insert_use::{ImportGranularity, InsertUseConfig},
8 SnippetCap,
9 },
10 RootDatabase,
11};
12use itertools::Itertools;
13use stdx::{format_to, trim_indent};
14use syntax::{AstNode, NodeOrToken, SyntaxElement};
15use test_utils::assert_eq_text;
16
17use crate::{item::CompletionKind, CompletionConfig, CompletionItem};
18
19pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
20 enable_postfix_completions: true,
21 enable_imports_on_the_fly: true,
22 enable_self_on_the_fly: true,
23 add_call_parenthesis: true,
24 add_call_argument_snippets: true,
25 snippet_cap: SnippetCap::new(true),
26 insert_use: InsertUseConfig {
27 granularity: ImportGranularity::Crate,
28 prefix_kind: PrefixKind::Plain,
29 enforce_granularity: true,
30 group: true,
31 },
32};
33
34fn completion_list(code: &str) -> String {
35 completion_list_with_config(TEST_CONFIG, code)
36}
37
38fn completion_list_with_config(config: CompletionConfig, code: &str) -> String {
39 render_completion_list(get_all_items(config, code))
40}
41
42/// Creates analysis from a multi-file fixture, returns positions marked with $0.
43pub(crate) fn position(ra_fixture: &str) -> (RootDatabase, FilePosition) {
44 let change_fixture = ChangeFixture::parse(ra_fixture);
45 let mut database = RootDatabase::default();
46 database.apply_change(change_fixture.change);
47 let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
48 let offset = range_or_offset.expect_offset();
49 (database, FilePosition { file_id, offset })
50}
51
52pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> {
53 do_completion_with_config(TEST_CONFIG, code, kind)
54}
55
56pub(crate) fn do_completion_with_config(
57 config: CompletionConfig,
58 code: &str,
59 kind: CompletionKind,
60) -> Vec<CompletionItem> {
61 get_all_items(config, code)
62 .into_iter()
63 .filter(|c| c.completion_kind == kind)
64 .sorted_by(|l, r| l.label().cmp(r.label()))
65 .collect()
66}
67
68pub(crate) fn filtered_completion_list(code: &str, kind: CompletionKind) -> String {
69 filtered_completion_list_with_config(TEST_CONFIG, code, kind)
70}
71
72pub(crate) fn filtered_completion_list_with_config(
73 config: CompletionConfig,
74 code: &str,
75 kind: CompletionKind,
76) -> String {
77 let kind_completions: Vec<CompletionItem> =
78 get_all_items(config, code).into_iter().filter(|c| c.completion_kind == kind).collect();
79 render_completion_list(kind_completions)
80}
81
82fn render_completion_list(completions: Vec<CompletionItem>) -> String {
83 fn monospace_width(s: &str) -> usize {
84 s.chars().count()
85 }
86 let label_width =
87 completions.iter().map(|it| monospace_width(it.label())).max().unwrap_or_default().min(16);
88 completions
89 .into_iter()
90 .map(|it| {
91 let tag = it.kind().unwrap().tag();
92 let var_name = format!("{} {}", tag, it.label());
93 let mut buf = var_name;
94 if let Some(detail) = it.detail() {
95 let width = label_width.saturating_sub(monospace_width(it.label()));
96 format_to!(buf, "{:width$} {}", "", detail, width = width);
97 }
98 if it.deprecated() {
99 format_to!(buf, " DEPRECATED");
100 }
101 format_to!(buf, "\n");
102 buf
103 })
104 .collect()
105}
106
107pub(crate) fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
108 check_edit_with_config(TEST_CONFIG, what, ra_fixture_before, ra_fixture_after)
109}
110
111pub(crate) fn check_edit_with_config(
112 config: CompletionConfig,
113 what: &str,
114 ra_fixture_before: &str,
115 ra_fixture_after: &str,
116) {
117 let ra_fixture_after = trim_indent(ra_fixture_after);
118 let (db, position) = position(ra_fixture_before);
119 let completions: Vec<CompletionItem> =
120 crate::completions(&db, &config, position).unwrap().into();
121 let (completion,) = completions
122 .iter()
123 .filter(|it| it.lookup() == what)
124 .collect_tuple()
125 .unwrap_or_else(|| panic!("can't find {:?} completion in {:#?}", what, completions));
126 let mut actual = db.file_text(position.file_id).to_string();
127
128 let mut combined_edit = completion.text_edit().to_owned();
129 if let Some(import_text_edit) =
130 completion.import_to_add().and_then(|edit| edit.to_text_edit(config.insert_use))
131 {
132 combined_edit.union(import_text_edit).expect(
133 "Failed to apply completion resolve changes: change ranges overlap, but should not",
134 )
135 }
136
137 combined_edit.apply(&mut actual);
138 assert_eq_text!(&ra_fixture_after, &actual)
139}
140
141pub(crate) fn check_pattern_is_applicable(code: &str, check: impl FnOnce(SyntaxElement) -> bool) {
142 let (db, pos) = position(code);
143
144 let sema = Semantics::new(&db);
145 let original_file = sema.parse(pos.file_id);
146 let token = original_file.syntax().token_at_offset(pos.offset).left_biased().unwrap();
147 assert!(check(NodeOrToken::Token(token)));
148}
149
150pub(crate) fn check_pattern_is_not_applicable(code: &str, check: fn(SyntaxElement) -> bool) {
151 let (db, pos) = position(code);
152 let sema = Semantics::new(&db);
153 let original_file = sema.parse(pos.file_id);
154 let token = original_file.syntax().token_at_offset(pos.offset).left_biased().unwrap();
155 assert!(!check(NodeOrToken::Token(token)));
156}
157
158pub(crate) fn get_all_items(config: CompletionConfig, code: &str) -> Vec<CompletionItem> {
159 let (db, position) = position(code);
160 crate::completions(&db, &config, position).unwrap().into()
161}
162
163fn check_no_completion(ra_fixture: &str) {
164 let (db, position) = position(ra_fixture);
165
166 assert!(
167 crate::completions(&db, &TEST_CONFIG, position).is_none(),
168 "Completions were generated, but weren't expected"
169 );
170}
171
172#[test]
173fn test_no_completions_required() {
174 cov_mark::check!(no_completion_required);
175 check_no_completion(r#"fn foo() { for i i$0 }"#);
176}