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