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