]> git.lizzy.rs Git - rust.git/blob - crates/ra_ide_api/src/completion.rs
b1867de4270284f48d88ac004173c716ea4871fb
[rust.git] / crates / ra_ide_api / src / completion.rs
1 mod completion_item;
2 mod completion_context;
3
4 mod complete_dot;
5 mod complete_fn_param;
6 mod complete_keyword;
7 mod complete_snippet;
8 mod complete_path;
9 mod complete_scope;
10 mod complete_postfix;
11
12 use ra_db::SourceDatabase;
13
14 use crate::{
15     db,
16     FilePosition,
17     completion::{
18         completion_item::{Completions, CompletionKind},
19         completion_context::CompletionContext,
20     },
21 };
22
23 pub use crate::completion::completion_item::{CompletionItem, CompletionItemKind, InsertTextFormat};
24
25 /// Main entry point for completion. We run completion as a two-phase process.
26 ///
27 /// First, we look at the position and collect a so-called `CompletionContext.
28 /// This is a somewhat messy process, because, during completion, syntax tree is
29 /// incomplete and can look really weird.
30 ///
31 /// Once the context is collected, we run a series of completion routines which
32 /// look at the context and produce completion items. One subtelty about this
33 /// phase is that completion engine should not filter by the substring which is
34 /// already present, it should give all possible variants for the identifier at
35 /// the caret. In other words, for
36 ///
37 /// ```no-run
38 /// fn f() {
39 ///     let foo = 92;
40 ///     let _ = bar<|>
41 /// }
42 /// ```
43 ///
44 /// `foo` *should* be present among the completion variants. Filtering by
45 /// identifier prefix/fuzzy match should be done higher in the stack, together
46 /// with ordering of completions (currently this is done by the client).
47 pub(crate) fn completions(db: &db::RootDatabase, position: FilePosition) -> Option<Completions> {
48     let original_file = db.parse(position.file_id);
49     let ctx = CompletionContext::new(db, &original_file, position)?;
50
51     let mut acc = Completions::default();
52
53     complete_fn_param::complete_fn_param(&mut acc, &ctx);
54     complete_keyword::complete_expr_keyword(&mut acc, &ctx);
55     complete_keyword::complete_use_tree_keyword(&mut acc, &ctx);
56     complete_snippet::complete_expr_snippet(&mut acc, &ctx);
57     complete_snippet::complete_item_snippet(&mut acc, &ctx);
58     complete_path::complete_path(&mut acc, &ctx);
59     complete_scope::complete_scope(&mut acc, &ctx);
60     complete_dot::complete_dot(&mut acc, &ctx);
61     complete_postfix::complete_postfix(&mut acc, &ctx);
62     Some(acc)
63 }