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