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_semicolon_to_unit: bool,
29    pub snippet_cap: Option<SnippetCap>,
30    pub insert_use: InsertUseConfig,
31    pub prefer_no_std: bool,
32    pub prefer_prelude: bool,
33    pub prefer_absolute: bool,
34    pub snippets: Vec<Snippet>,
35    pub limit: Option<usize>,
36    pub fields_to_resolve: CompletionFieldsToResolve,
37    pub exclude_flyimport: Vec<(String, AutoImportExclusionType)>,
38    pub exclude_traits: &'a [String],
39    pub ra_fixture: RaFixtureConfig<'a>,
40}
41
42#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
43pub enum AutoImportExclusionType {
44    Always,
45    Methods,
46}
47
48#[derive(Clone, Debug, PartialEq, Eq)]
49pub enum CallableSnippets {
50    FillArguments,
51    AddParentheses,
52}
53
54impl CompletionConfig<'_> {
55    pub fn postfix_snippets(&self) -> impl Iterator<Item = (&str, &Snippet)> {
56        self.snippets
57            .iter()
58            .flat_map(|snip| snip.postfix_triggers.iter().map(move |trigger| (&**trigger, snip)))
59    }
60
61    pub fn prefix_snippets(&self) -> impl Iterator<Item = (&str, &Snippet)> {
62        self.snippets
63            .iter()
64            .flat_map(|snip| snip.prefix_triggers.iter().map(move |trigger| (&**trigger, snip)))
65    }
66
67    pub fn find_path_config(&self, allow_unstable: bool) -> FindPathConfig {
68        FindPathConfig {
69            prefer_no_std: self.prefer_no_std,
70            prefer_prelude: self.prefer_prelude,
71            prefer_absolute: self.prefer_absolute,
72            allow_unstable,
73        }
74    }
75
76    pub fn import_path_config(&self) -> ImportPathConfig {
77        ImportPathConfig {
78            prefer_no_std: self.prefer_no_std,
79            prefer_prelude: self.prefer_prelude,
80            prefer_absolute: self.prefer_absolute,
81        }
82    }
83}