aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/completion.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/completion.rs')
-rw-r--r--crates/ide/src/completion.rs211
1 files changed, 0 insertions, 211 deletions
diff --git a/crates/ide/src/completion.rs b/crates/ide/src/completion.rs
deleted file mode 100644
index b0e35b2bd..000000000
--- a/crates/ide/src/completion.rs
+++ /dev/null
@@ -1,211 +0,0 @@
1mod completion_config;
2mod completion_item;
3mod completion_context;
4mod presentation;
5mod patterns;
6mod generated_features;
7#[cfg(test)]
8mod test_utils;
9
10mod complete_attribute;
11mod complete_dot;
12mod complete_record;
13mod complete_pattern;
14mod complete_fn_param;
15mod complete_keyword;
16mod complete_snippet;
17mod complete_qualified_path;
18mod complete_unqualified_path;
19mod complete_postfix;
20mod complete_macro_in_item_position;
21mod complete_trait_impl;
22mod complete_mod;
23
24use ide_db::RootDatabase;
25
26use crate::{
27 completion::{
28 completion_context::CompletionContext,
29 completion_item::{CompletionKind, Completions},
30 },
31 FilePosition,
32};
33
34pub use crate::completion::{
35 completion_config::CompletionConfig,
36 completion_item::{CompletionItem, CompletionItemKind, CompletionScore, InsertTextFormat},
37};
38
39//FIXME: split the following feature into fine-grained features.
40
41// Feature: Magic Completions
42//
43// In addition to usual reference completion, rust-analyzer provides some ✨magic✨
44// completions as well:
45//
46// Keywords like `if`, `else` `while`, `loop` are completed with braces, and cursor
47// is placed at the appropriate position. Even though `if` is easy to type, you
48// still want to complete it, to get ` { }` for free! `return` is inserted with a
49// space or `;` depending on the return type of the function.
50//
51// When completing a function call, `()` are automatically inserted. If a function
52// takes arguments, the cursor is positioned inside the parenthesis.
53//
54// There are postfix completions, which can be triggered by typing something like
55// `foo().if`. The word after `.` determines postfix completion. Possible variants are:
56//
57// - `expr.if` -> `if expr {}` or `if let ... {}` for `Option` or `Result`
58// - `expr.match` -> `match expr {}`
59// - `expr.while` -> `while expr {}` or `while let ... {}` for `Option` or `Result`
60// - `expr.ref` -> `&expr`
61// - `expr.refm` -> `&mut expr`
62// - `expr.not` -> `!expr`
63// - `expr.dbg` -> `dbg!(expr)`
64// - `expr.dbgr` -> `dbg!(&expr)`
65// - `expr.call` -> `(expr)`
66//
67// There also snippet completions:
68//
69// .Expressions
70// - `pd` -> `eprintln!(" = {:?}", );`
71// - `ppd` -> `eprintln!(" = {:#?}", );`
72//
73// .Items
74// - `tfn` -> `#[test] fn feature(){}`
75// - `tmod` ->
76// ```rust
77// #[cfg(test)]
78// mod tests {
79// use super::*;
80//
81// #[test]
82// fn test_name() {}
83// }
84// ```
85
86/// Main entry point for completion. We run completion as a two-phase process.
87///
88/// First, we look at the position and collect a so-called `CompletionContext.
89/// This is a somewhat messy process, because, during completion, syntax tree is
90/// incomplete and can look really weird.
91///
92/// Once the context is collected, we run a series of completion routines which
93/// look at the context and produce completion items. One subtlety about this
94/// phase is that completion engine should not filter by the substring which is
95/// already present, it should give all possible variants for the identifier at
96/// the caret. In other words, for
97///
98/// ```no_run
99/// fn f() {
100/// let foo = 92;
101/// let _ = bar<|>
102/// }
103/// ```
104///
105/// `foo` *should* be present among the completion variants. Filtering by
106/// identifier prefix/fuzzy match should be done higher in the stack, together
107/// with ordering of completions (currently this is done by the client).
108pub(crate) fn completions(
109 db: &RootDatabase,
110 config: &CompletionConfig,
111 position: FilePosition,
112) -> Option<Completions> {
113 let ctx = CompletionContext::new(db, position, config)?;
114
115 let mut acc = Completions::default();
116 complete_attribute::complete_attribute(&mut acc, &ctx);
117 complete_fn_param::complete_fn_param(&mut acc, &ctx);
118 complete_keyword::complete_expr_keyword(&mut acc, &ctx);
119 complete_keyword::complete_use_tree_keyword(&mut acc, &ctx);
120 complete_snippet::complete_expr_snippet(&mut acc, &ctx);
121 complete_snippet::complete_item_snippet(&mut acc, &ctx);
122 complete_qualified_path::complete_qualified_path(&mut acc, &ctx);
123 complete_unqualified_path::complete_unqualified_path(&mut acc, &ctx);
124 complete_dot::complete_dot(&mut acc, &ctx);
125 complete_record::complete_record(&mut acc, &ctx);
126 complete_pattern::complete_pattern(&mut acc, &ctx);
127 complete_postfix::complete_postfix(&mut acc, &ctx);
128 complete_macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx);
129 complete_trait_impl::complete_trait_impl(&mut acc, &ctx);
130 complete_mod::complete_mod(&mut acc, &ctx);
131
132 Some(acc)
133}
134
135#[cfg(test)]
136mod tests {
137 use crate::completion::completion_config::CompletionConfig;
138 use crate::fixture;
139
140 struct DetailAndDocumentation<'a> {
141 detail: &'a str,
142 documentation: &'a str,
143 }
144
145 fn check_detail_and_documentation(ra_fixture: &str, expected: DetailAndDocumentation) {
146 let (analysis, position) = fixture::position(ra_fixture);
147 let config = CompletionConfig::default();
148 let completions = analysis.completions(&config, position).unwrap().unwrap();
149 for item in completions {
150 if item.detail() == Some(expected.detail) {
151 let opt = item.documentation();
152 let doc = opt.as_ref().map(|it| it.as_str());
153 assert_eq!(doc, Some(expected.documentation));
154 return;
155 }
156 }
157 panic!("completion detail not found: {}", expected.detail)
158 }
159
160 #[test]
161 fn test_completion_detail_from_macro_generated_struct_fn_doc_attr() {
162 check_detail_and_documentation(
163 r#"
164 //- /lib.rs
165 macro_rules! bar {
166 () => {
167 struct Bar;
168 impl Bar {
169 #[doc = "Do the foo"]
170 fn foo(&self) {}
171 }
172 }
173 }
174
175 bar!();
176
177 fn foo() {
178 let bar = Bar;
179 bar.fo<|>;
180 }
181 "#,
182 DetailAndDocumentation { detail: "fn foo(&self)", documentation: "Do the foo" },
183 );
184 }
185
186 #[test]
187 fn test_completion_detail_from_macro_generated_struct_fn_doc_comment() {
188 check_detail_and_documentation(
189 r#"
190 //- /lib.rs
191 macro_rules! bar {
192 () => {
193 struct Bar;
194 impl Bar {
195 /// Do the foo
196 fn foo(&self) {}
197 }
198 }
199 }
200
201 bar!();
202
203 fn foo() {
204 let bar = Bar;
205 bar.fo<|>;
206 }
207 "#,
208 DetailAndDocumentation { detail: "fn foo(&self)", documentation: " Do the foo" },
209 );
210 }
211}