3 mod completion_context;
9 mod complete_attribute;
13 mod complete_fn_param;
16 mod complete_qualified_path;
17 mod complete_unqualified_path;
19 mod complete_macro_in_item_position;
20 mod complete_trait_impl;
21 mod unstable_feature_descriptor;
23 use ide_db::RootDatabase;
27 completion_context::CompletionContext,
28 completion_item::{CompletionKind, Completions},
33 //FIXME: cyclic imports caused by xtask generation, this should be better
34 use crate::completion::{
35 complete_attribute::LintCompletion, unstable_feature_descriptor::UNSTABLE_FEATURE_DESCRIPTOR,
38 pub use crate::completion::{
39 completion_config::CompletionConfig,
40 completion_item::{CompletionItem, CompletionItemKind, CompletionScore, InsertTextFormat},
43 //FIXME: split the following feature into fine-grained features.
45 // Feature: Magic Completions
47 // In addition to usual reference completion, rust-analyzer provides some ✨magic✨
48 // completions as well:
50 // Keywords like `if`, `else` `while`, `loop` are completed with braces, and cursor
51 // is placed at the appropriate position. Even though `if` is easy to type, you
52 // still want to complete it, to get ` { }` for free! `return` is inserted with a
53 // space or `;` depending on the return type of the function.
55 // When completing a function call, `()` are automatically inserted. If a function
56 // takes arguments, the cursor is positioned inside the parenthesis.
58 // There are postfix completions, which can be triggered by typing something like
59 // `foo().if`. The word after `.` determines postfix completion. Possible variants are:
61 // - `expr.if` -> `if expr {}` or `if let ... {}` for `Option` or `Result`
62 // - `expr.match` -> `match expr {}`
63 // - `expr.while` -> `while expr {}` or `while let ... {}` for `Option` or `Result`
64 // - `expr.ref` -> `&expr`
65 // - `expr.refm` -> `&mut expr`
66 // - `expr.not` -> `!expr`
67 // - `expr.dbg` -> `dbg!(expr)`
69 // There also snippet completions:
72 // - `pd` -> `eprintln!(" = {:?}", );`
73 // - `ppd` -> `eprintln!(" = {:#?}", );`
76 // - `tfn` -> `#[test] fn feature(){}`
88 /// Main entry point for completion. We run completion as a two-phase process.
90 /// First, we look at the position and collect a so-called `CompletionContext.
91 /// This is a somewhat messy process, because, during completion, syntax tree is
92 /// incomplete and can look really weird.
94 /// Once the context is collected, we run a series of completion routines which
95 /// look at the context and produce completion items. One subtlety about this
96 /// phase is that completion engine should not filter by the substring which is
97 /// already present, it should give all possible variants for the identifier at
98 /// the caret. In other words, for
107 /// `foo` *should* be present among the completion variants. Filtering by
108 /// identifier prefix/fuzzy match should be done higher in the stack, together
109 /// with ordering of completions (currently this is done by the client).
110 pub(crate) fn completions(
112 config: &CompletionConfig,
113 position: FilePosition,
114 ) -> Option<Completions> {
115 let ctx = CompletionContext::new(db, position, config)?;
117 let mut acc = Completions::default();
118 complete_attribute::complete_attribute(&mut acc, &ctx);
119 complete_fn_param::complete_fn_param(&mut acc, &ctx);
120 complete_keyword::complete_expr_keyword(&mut acc, &ctx);
121 complete_keyword::complete_use_tree_keyword(&mut acc, &ctx);
122 complete_snippet::complete_expr_snippet(&mut acc, &ctx);
123 complete_snippet::complete_item_snippet(&mut acc, &ctx);
124 complete_qualified_path::complete_qualified_path(&mut acc, &ctx);
125 complete_unqualified_path::complete_unqualified_path(&mut acc, &ctx);
126 complete_dot::complete_dot(&mut acc, &ctx);
127 complete_record::complete_record(&mut acc, &ctx);
128 complete_pattern::complete_pattern(&mut acc, &ctx);
129 complete_postfix::complete_postfix(&mut acc, &ctx);
130 complete_macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx);
131 complete_trait_impl::complete_trait_impl(&mut acc, &ctx);
138 use crate::completion::completion_config::CompletionConfig;
139 use crate::mock_analysis::analysis_and_position;
141 struct DetailAndDocumentation<'a> {
143 documentation: &'a str,
146 fn check_detail_and_documentation(ra_fixture: &str, expected: DetailAndDocumentation) {
147 let (analysis, position) = analysis_and_position(ra_fixture);
148 let config = CompletionConfig::default();
149 let completions = analysis.completions(&config, position).unwrap().unwrap();
150 for item in completions {
151 if item.detail() == Some(expected.detail) {
152 let opt = item.documentation();
153 let doc = opt.as_ref().map(|it| it.as_str());
154 assert_eq!(doc, Some(expected.documentation));
158 panic!("completion detail not found: {}", expected.detail)
162 fn test_completion_detail_from_macro_generated_struct_fn_doc_attr() {
163 check_detail_and_documentation(
170 #[doc = "Do the foo"]
183 DetailAndDocumentation { detail: "fn foo(&self)", documentation: "Do the foo" },
188 fn test_completion_detail_from_macro_generated_struct_fn_doc_comment() {
189 check_detail_and_documentation(
209 DetailAndDocumentation { detail: "fn foo(&self)", documentation: " Do the foo" },