Skip to main content

nu_protocol/config/
completions.rs

1use super::{config_update_string_enum, prelude::*};
2use crate as nu_protocol;
3use crate::engine::Closure;
4
5#[derive(Clone, Copy, Debug, Default, IntoValue, PartialEq, Eq, Serialize, Deserialize)]
6pub enum CompletionAlgorithm {
7    #[default]
8    Prefix,
9    Substring,
10    Fuzzy,
11}
12
13impl FromStr for CompletionAlgorithm {
14    type Err = &'static str;
15
16    fn from_str(s: &str) -> Result<Self, Self::Err> {
17        match s.to_ascii_lowercase().as_str() {
18            "prefix" => Ok(Self::Prefix),
19            "substring" => Ok(Self::Substring),
20            "fuzzy" => Ok(Self::Fuzzy),
21            _ => Err("'prefix' or 'fuzzy' or 'substring'"),
22        }
23    }
24}
25
26impl UpdateFromValue for CompletionAlgorithm {
27    fn update(&mut self, value: &Value, path: &mut ConfigPath, errors: &mut ConfigErrors) {
28        config_update_string_enum(self, value, path, errors)
29    }
30}
31
32#[derive(Clone, Copy, Debug, Default, IntoValue, PartialEq, Eq, Serialize, Deserialize)]
33pub enum CompletionSort {
34    #[default]
35    Smart,
36    Alphabetical,
37}
38
39impl FromStr for CompletionSort {
40    type Err = &'static str;
41
42    fn from_str(s: &str) -> Result<Self, Self::Err> {
43        match s.to_ascii_lowercase().as_str() {
44            "smart" => Ok(Self::Smart),
45            "alphabetical" => Ok(Self::Alphabetical),
46            _ => Err("'smart' or 'alphabetical'"),
47        }
48    }
49}
50
51impl UpdateFromValue for CompletionSort {
52    fn update(&mut self, value: &Value, path: &mut ConfigPath, errors: &mut ConfigErrors) {
53        config_update_string_enum(self, value, path, errors)
54    }
55}
56
57#[derive(Clone, Debug, IntoValue, Serialize, Deserialize)]
58pub struct ExternalCompleterConfig {
59    pub enable: bool,
60    pub max_results: i64,
61    pub completer: Option<Closure>,
62}
63
64impl Default for ExternalCompleterConfig {
65    fn default() -> Self {
66        Self {
67            enable: true,
68            max_results: 100,
69            completer: None,
70        }
71    }
72}
73
74impl UpdateFromValue for ExternalCompleterConfig {
75    fn update<'a>(
76        &mut self,
77        value: &'a Value,
78        path: &mut ConfigPath<'a>,
79        errors: &mut ConfigErrors,
80    ) {
81        let Value::Record { val: record, .. } = value else {
82            errors.type_mismatch(path, Type::record(), value);
83            return;
84        };
85
86        for (col, val) in record.iter() {
87            let path = &mut path.push(col);
88            match col.as_str() {
89                "completer" => match val {
90                    Value::Nothing { .. } => self.completer = None,
91                    Value::Closure { val, .. } => self.completer = Some(val.as_ref().clone()),
92                    _ => errors.type_mismatch(path, Type::custom("closure or nothing"), val),
93                },
94                "max_results" => self.max_results.update(val, path, errors),
95                "enable" => self.enable.update(val, path, errors),
96                _ => errors.unknown_option(path, val),
97            }
98        }
99    }
100}
101
102#[derive(Clone, Debug, IntoValue, Serialize, Deserialize)]
103pub struct CompletionConfig {
104    pub sort: CompletionSort,
105    pub case_sensitive: bool,
106    pub quick: bool,
107    pub partial: bool,
108    pub algorithm: CompletionAlgorithm,
109    pub external: ExternalCompleterConfig,
110    pub use_ls_colors: bool,
111    /// Completion results the cache keeps before evicting the least recently used entry; `0` disables it.
112    pub cache_size: i64,
113}
114
115impl Default for CompletionConfig {
116    fn default() -> Self {
117        Self {
118            sort: CompletionSort::default(),
119            case_sensitive: false,
120            quick: true,
121            partial: true,
122            algorithm: CompletionAlgorithm::default(),
123            external: ExternalCompleterConfig::default(),
124            use_ls_colors: true,
125            cache_size: 100,
126        }
127    }
128}
129
130impl UpdateFromValue for CompletionConfig {
131    fn update<'a>(
132        &mut self,
133        value: &'a Value,
134        path: &mut ConfigPath<'a>,
135        errors: &mut ConfigErrors,
136    ) {
137        let Value::Record { val: record, .. } = value else {
138            errors.type_mismatch(path, Type::record(), value);
139            return;
140        };
141
142        for (col, val) in record.iter() {
143            let path = &mut path.push(col);
144            match col.as_str() {
145                "sort" => self.sort.update(val, path, errors),
146                "quick" => self.quick.update(val, path, errors),
147                "partial" => self.partial.update(val, path, errors),
148                "algorithm" => self.algorithm.update(val, path, errors),
149                "case_sensitive" => self.case_sensitive.update(val, path, errors),
150                "external" => self.external.update(val, path, errors),
151                "use_ls_colors" => self.use_ls_colors.update(val, path, errors),
152                "cache_size" => self.cache_size.update(val, path, errors),
153                _ => errors.unknown_option(path, val),
154            }
155        }
156    }
157}