1 //! Tests and test utilities for completions.
3 //! Most tests live in this module or its submodules. The tests in these submodules are "location"
4 //! oriented, that is they try to check completions for something like type position, param position
6 //! Tests that are more orientated towards specific completion types like visibility checks of path
7 //! completions or `check_edit` tests usually live in their respective completion modules instead.
8 //! This gives this test module and its submodules here the main purpose of giving the developer an
9 //! overview of whats being completed where, not how.
26 use hir::{db::DefDatabase, PrefixKind, Semantics};
28 base_db::{fixture::ChangeFixture, FileLoader, FilePosition},
29 imports::insert_use::{ImportGranularity, InsertUseConfig},
30 RootDatabase, SnippetCap,
32 use itertools::Itertools;
33 use stdx::{format_to, trim_indent};
34 use syntax::{AstNode, NodeOrToken, SyntaxElement};
35 use test_utils::assert_eq_text;
38 resolve_completion_edits, CallableSnippets, CompletionConfig, CompletionItem,
42 /// Lots of basic item definitions
43 const BASE_ITEMS_FIXTURE: &str = r#"
44 enum Enum { TupleV(u32), RecordV { field: u32 }, UnitV }
45 use self::Enum::TupleV;
49 static STATIC: Unit = Unit;
50 const CONST: Unit = Unit;
51 struct Record { field: u32 }
56 #[rustc_builtin_macro]
59 union Union { field: i32 }
62 pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
63 enable_postfix_completions: true,
64 enable_imports_on_the_fly: true,
65 enable_self_on_the_fly: true,
66 enable_private_editable: false,
67 callable: Some(CallableSnippets::FillArguments),
68 snippet_cap: SnippetCap::new(true),
70 insert_use: InsertUseConfig {
71 granularity: ImportGranularity::Crate,
72 prefix_kind: PrefixKind::Plain,
73 enforce_granularity: true,
75 skip_glob_imports: true,
80 pub(crate) fn completion_list(ra_fixture: &str) -> String {
81 completion_list_with_config(TEST_CONFIG, ra_fixture, true, None)
84 pub(crate) fn completion_list_no_kw(ra_fixture: &str) -> String {
85 completion_list_with_config(TEST_CONFIG, ra_fixture, false, None)
88 pub(crate) fn completion_list_no_kw_with_private_editable(ra_fixture: &str) -> String {
89 let mut config = TEST_CONFIG.clone();
90 config.enable_private_editable = true;
91 completion_list_with_config(config, ra_fixture, false, None)
94 pub(crate) fn completion_list_with_trigger_character(
96 trigger_character: Option<char>,
98 completion_list_with_config(TEST_CONFIG, ra_fixture, true, trigger_character)
101 fn completion_list_with_config(
102 config: CompletionConfig,
104 include_keywords: bool,
105 trigger_character: Option<char>,
107 // filter out all but one builtintype completion for smaller test outputs
108 let items = get_all_items(config, ra_fixture, trigger_character);
111 .filter(|it| it.kind() != CompletionItemKind::BuiltinType || it.label() == "u32")
112 .filter(|it| include_keywords || it.kind() != CompletionItemKind::Keyword)
113 .filter(|it| include_keywords || it.kind() != CompletionItemKind::Snippet)
114 .sorted_by_key(|it| (it.kind(), it.label().to_owned(), it.detail().map(ToOwned::to_owned)))
116 render_completion_list(items)
119 /// Creates analysis from a multi-file fixture, returns positions marked with $0.
120 pub(crate) fn position(ra_fixture: &str) -> (RootDatabase, FilePosition) {
121 let change_fixture = ChangeFixture::parse(ra_fixture);
122 let mut database = RootDatabase::default();
123 database.set_enable_proc_attr_macros(true);
124 database.apply_change(change_fixture.change);
125 let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
126 let offset = range_or_offset.expect_offset();
127 (database, FilePosition { file_id, offset })
130 pub(crate) fn do_completion(code: &str, kind: CompletionItemKind) -> Vec<CompletionItem> {
131 do_completion_with_config(TEST_CONFIG, code, kind)
134 pub(crate) fn do_completion_with_config(
135 config: CompletionConfig,
137 kind: CompletionItemKind,
138 ) -> Vec<CompletionItem> {
139 get_all_items(config, code, None)
141 .filter(|c| c.kind() == kind)
142 .sorted_by(|l, r| l.label().cmp(r.label()))
146 fn render_completion_list(completions: Vec<CompletionItem>) -> String {
147 fn monospace_width(s: &str) -> usize {
151 completions.iter().map(|it| monospace_width(it.label())).max().unwrap_or_default().min(22);
155 let tag = it.kind().tag();
156 let var_name = format!("{} {}", tag, it.label());
157 let mut buf = var_name;
158 if let Some(detail) = it.detail() {
159 let width = label_width.saturating_sub(monospace_width(it.label()));
160 format_to!(buf, "{:width$} {}", "", detail, width = width);
163 format_to!(buf, " DEPRECATED");
165 format_to!(buf, "\n");
172 pub(crate) fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
173 check_edit_with_config(TEST_CONFIG, what, ra_fixture_before, ra_fixture_after)
177 pub(crate) fn check_edit_with_config(
178 config: CompletionConfig,
180 ra_fixture_before: &str,
181 ra_fixture_after: &str,
183 let ra_fixture_after = trim_indent(ra_fixture_after);
184 let (db, position) = position(ra_fixture_before);
185 let completions: Vec<CompletionItem> =
186 crate::completions(&db, &config, position, None).unwrap().into();
187 let (completion,) = completions
189 .filter(|it| it.lookup() == what)
191 .unwrap_or_else(|| panic!("can't find {:?} completion in {:#?}", what, completions));
192 let mut actual = db.file_text(position.file_id).to_string();
194 let mut combined_edit = completion.text_edit().to_owned();
196 resolve_completion_edits(
200 completion.imports_to_add().iter().filter_map(|import_edit| {
201 let import_path = &import_edit.import_path;
202 let import_name = import_path.segments().last()?;
203 Some((import_path.to_string(), import_name.to_string()))
208 .for_each(|text_edit| {
209 combined_edit.union(text_edit).expect(
210 "Failed to apply completion resolve changes: change ranges overlap, but should not",
214 combined_edit.apply(&mut actual);
215 assert_eq_text!(&ra_fixture_after, &actual)
218 pub(crate) fn check_pattern_is_applicable(code: &str, check: impl FnOnce(SyntaxElement) -> bool) {
219 let (db, pos) = position(code);
221 let sema = Semantics::new(&db);
222 let original_file = sema.parse(pos.file_id);
223 let token = original_file.syntax().token_at_offset(pos.offset).left_biased().unwrap();
224 assert!(check(NodeOrToken::Token(token)));
227 pub(crate) fn get_all_items(
228 config: CompletionConfig,
230 trigger_character: Option<char>,
231 ) -> Vec<CompletionItem> {
232 let (db, position) = position(code);
233 let res = crate::completions(&db, &config, position, trigger_character)
234 .map_or_else(Vec::default, Into::into);
236 res.iter().for_each(|it| {
237 let sr = it.source_range();
239 sr.contains_inclusive(position.offset),
240 "source range {sr:?} does not contain the offset {:?} of the completion request: {it:?}",
248 fn test_no_completions_required() {
249 assert_eq!(completion_list(r#"fn foo() { for i i$0 }"#), String::new());
253 fn regression_10042() {
256 macro_rules! preset {
257 ($($x:ident)&&*) => {
259 let mut v = Vec::new();
276 fn no_completions_in_comments() {
281 let x = 2; // A comment$0
291 Some multi-line comment$0