ra_ap_ide_completion/
config.rs

1//! Settings for tweaking completion.
2//!
3//! The fun thing here is `SnippetCap` -- this type can only be created in this
4//! module, and we use to statically check that we only produce snippet
5//! completions if we are allowed to.
6
7use hir::ImportPathConfig;
8use ide_db::{SnippetCap, imports::insert_use::InsertUseConfig};
9
10use crate::{CompletionFieldsToResolve, snippet::Snippet};
11
12#[derive(Clone, Debug, PartialEq, Eq)]
13pub struct CompletionConfig<'a> {
14    pub enable_postfix_completions: bool,
15    pub enable_imports_on_the_fly: bool,
16    pub enable_self_on_the_fly: bool,
17    pub enable_auto_iter: bool,
18    pub enable_auto_await: bool,
19    pub enable_private_editable: bool,
20    pub enable_term_search: bool,
21    pub term_search_fuel: u64,
22    pub full_function_signatures: bool,
23    pub callable: Option<CallableSnippets>,
24    pub add_semicolon_to_unit: bool,
25    pub snippet_cap: Option<SnippetCap>,
26    pub insert_use: InsertUseConfig,
27    pub prefer_no_std: bool,
28    pub prefer_prelude: bool,
29    pub prefer_absolute: bool,
30    pub snippets: Vec<Snippet>,
31    pub limit: Option<usize>,
32    pub fields_to_resolve: CompletionFieldsToResolve,
33    pub exclude_flyimport: Vec<(String, AutoImportExclusionType)>,
34    pub exclude_traits: &'a [String],
35}
36
37#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
38pub enum AutoImportExclusionType {
39    Always,
40    Methods,
41}
42
43#[derive(Clone, Debug, PartialEq, Eq)]
44pub enum CallableSnippets {
45    FillArguments,
46    AddParentheses,
47}
48
49impl CompletionConfig<'_> {
50    pub fn postfix_snippets(&self) -> impl Iterator<Item = (&str, &Snippet)> {
51        self.snippets
52            .iter()
53            .flat_map(|snip| snip.postfix_triggers.iter().map(move |trigger| (&**trigger, snip)))
54    }
55
56    pub fn prefix_snippets(&self) -> impl Iterator<Item = (&str, &Snippet)> {
57        self.snippets
58            .iter()
59            .flat_map(|snip| snip.prefix_triggers.iter().map(move |trigger| (&**trigger, snip)))
60    }
61
62    pub fn import_path_config(&self, allow_unstable: bool) -> ImportPathConfig {
63        ImportPathConfig {
64            prefer_no_std: self.prefer_no_std,
65            prefer_prelude: self.prefer_prelude,
66            prefer_absolute: self.prefer_absolute,
67            allow_unstable,
68        }
69    }
70}