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    SubItems,
48}
49
50#[derive(Clone, Debug, PartialEq, Eq)]
51pub enum CallableSnippets {
52    FillArguments,
53    AddParentheses,
54}
55
56impl CompletionConfig<'_> {
57    pub fn postfix_snippets(&self) -> impl Iterator<Item = (&str, &Snippet)> {
58        self.snippets
59            .iter()
60            .flat_map(|snip| snip.postfix_triggers.iter().map(move |trigger| (&**trigger, snip)))
61    }
62
63    pub fn prefix_snippets(&self) -> impl Iterator<Item = (&str, &Snippet)> {
64        self.snippets
65            .iter()
66            .flat_map(|snip| snip.prefix_triggers.iter().map(move |trigger| (&**trigger, snip)))
67    }
68
69    pub fn find_path_config(&self, allow_unstable: bool) -> FindPathConfig {
70        FindPathConfig {
71            prefer_no_std: self.prefer_no_std,
72            prefer_prelude: self.prefer_prelude,
73            prefer_absolute: self.prefer_absolute,
74            allow_unstable,
75        }
76    }
77
78    pub fn import_path_config(&self) -> ImportPathConfig {
79        ImportPathConfig {
80            prefer_no_std: self.prefer_no_std,
81            prefer_prelude: self.prefer_prelude,
82            prefer_absolute: self.prefer_absolute,
83        }
84    }
85}