Skip to main content

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::FindPathConfig;
8use ide_db::{
9    SnippetCap,
10    imports::{import_assets::ImportPathConfig, insert_use::InsertUseConfig},
11    ra_fixture::RaFixtureConfig,
12};
13
14use crate::{CompletionFieldsToResolve, snippet::Snippet};
15
16#[derive(Clone, Debug)]
17pub struct CompletionConfig<'a> {
18    pub enable_postfix_completions: bool,
19    pub enable_imports_on_the_fly: bool,
20    pub enable_self_on_the_fly: bool,
21    pub enable_auto_iter: bool,
22    pub enable_auto_await: bool,
23    pub enable_private_editable: bool,
24    pub enable_term_search: bool,
25    pub term_search_fuel: u64,
26    pub full_function_signatures: bool,
27    pub callable: Option<CallableSnippets>,
28    pub add_colons_to_module: bool,
29    pub add_semicolon_to_unit: bool,
30    pub snippet_cap: Option<SnippetCap>,
31    pub insert_use: InsertUseConfig,
32    pub prefer_no_std: bool,
33    pub prefer_prelude: bool,
34    pub prefer_absolute: bool,
35    pub snippets: Vec<Snippet>,
36    pub limit: Option<usize>,
37    pub fields_to_resolve: CompletionFieldsToResolve,
38    pub exclude_flyimport: Vec<(String, AutoImportExclusionType)>,
39    pub exclude_traits: &'a [String],
40    pub ra_fixture: RaFixtureConfig<'a>,
41}
42
43#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
44pub enum AutoImportExclusionType {
45    Always,
46    Methods,
47}
48
49#[derive(Clone, Debug, PartialEq, Eq)]
50pub enum CallableSnippets {
51    FillArguments,
52    AddParentheses,
53}
54
55impl CompletionConfig<'_> {
56    pub fn postfix_snippets(&self) -> impl Iterator<Item = (&str, &Snippet)> {
57        self.snippets
58            .iter()
59            .flat_map(|snip| snip.postfix_triggers.iter().map(move |trigger| (&**trigger, snip)))
60    }
61
62    pub fn prefix_snippets(&self) -> impl Iterator<Item = (&str, &Snippet)> {
63        self.snippets
64            .iter()
65            .flat_map(|snip| snip.prefix_triggers.iter().map(move |trigger| (&**trigger, snip)))
66    }
67
68    pub fn find_path_config(&self, allow_unstable: bool) -> FindPathConfig {
69        FindPathConfig {
70            prefer_no_std: self.prefer_no_std,
71            prefer_prelude: self.prefer_prelude,
72            prefer_absolute: self.prefer_absolute,
73            allow_unstable,
74        }
75    }
76
77    pub fn import_path_config(&self) -> ImportPathConfig {
78        ImportPathConfig {
79            prefer_no_std: self.prefer_no_std,
80            prefer_prelude: self.prefer_prelude,
81            prefer_absolute: self.prefer_absolute,
82        }
83    }
84}