editor_settings.rs

  1use gpui::App;
  2use language::CursorShape;
  3use schemars::JsonSchema;
  4use serde::{Deserialize, Serialize};
  5use settings::{Settings, SettingsSources};
  6
  7#[derive(Deserialize, Clone)]
  8pub struct EditorSettings {
  9    pub cursor_blink: bool,
 10    pub cursor_shape: Option<CursorShape>,
 11    pub current_line_highlight: CurrentLineHighlight,
 12    pub selection_highlight: bool,
 13    pub selection_highlight_debounce: u64,
 14    pub lsp_highlight_debounce: u64,
 15    pub hover_popover_enabled: bool,
 16    pub hover_popover_delay: u64,
 17    pub toolbar: Toolbar,
 18    pub scrollbar: Scrollbar,
 19    pub gutter: Gutter,
 20    pub scroll_beyond_last_line: ScrollBeyondLastLine,
 21    pub vertical_scroll_margin: f32,
 22    pub autoscroll_on_clicks: bool,
 23    pub horizontal_scroll_margin: f32,
 24    pub scroll_sensitivity: f32,
 25    pub relative_line_numbers: bool,
 26    pub seed_search_query_from_cursor: SeedQuerySetting,
 27    pub use_smartcase_search: bool,
 28    pub multi_cursor_modifier: MultiCursorModifier,
 29    pub redact_private_values: bool,
 30    pub expand_excerpt_lines: u32,
 31    pub middle_click_paste: bool,
 32    #[serde(default)]
 33    pub double_click_in_multibuffer: DoubleClickInMultibuffer,
 34    pub search_wrap: bool,
 35    #[serde(default)]
 36    pub search: SearchSettings,
 37    pub auto_signature_help: bool,
 38    pub show_signature_help_after_edits: bool,
 39    pub jupyter: Jupyter,
 40}
 41
 42#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
 43#[serde(rename_all = "snake_case")]
 44pub enum CurrentLineHighlight {
 45    // Don't highlight the current line.
 46    None,
 47    // Highlight the gutter area.
 48    Gutter,
 49    // Highlight the editor area.
 50    Line,
 51    // Highlight the full line.
 52    All,
 53}
 54
 55/// When to populate a new search's query based on the text under the cursor.
 56#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
 57#[serde(rename_all = "snake_case")]
 58pub enum SeedQuerySetting {
 59    /// Always populate the search query with the word under the cursor.
 60    Always,
 61    /// Only populate the search query when there is text selected.
 62    Selection,
 63    /// Never populate the search query
 64    Never,
 65}
 66
 67/// What to do when multibuffer is double clicked in some of its excerpts (parts of singleton buffers).
 68#[derive(Default, Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
 69#[serde(rename_all = "snake_case")]
 70pub enum DoubleClickInMultibuffer {
 71    /// Behave as a regular buffer and select the whole word.
 72    #[default]
 73    Select,
 74    /// Open the excerpt clicked as a new buffer in the new tab, if no `alt` modifier was pressed during double click.
 75    /// Otherwise, behave as a regular buffer and select the whole word.
 76    Open,
 77}
 78
 79#[derive(Debug, Clone, Deserialize)]
 80pub struct Jupyter {
 81    /// Whether the Jupyter feature is enabled.
 82    ///
 83    /// Default: true
 84    pub enabled: bool,
 85}
 86
 87#[derive(Default, Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
 88#[serde(rename_all = "snake_case")]
 89pub struct JupyterContent {
 90    /// Whether the Jupyter feature is enabled.
 91    ///
 92    /// Default: true
 93    pub enabled: Option<bool>,
 94}
 95
 96#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
 97pub struct Toolbar {
 98    pub breadcrumbs: bool,
 99    pub quick_actions: bool,
100    pub selections_menu: bool,
101}
102
103#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
104pub struct Scrollbar {
105    pub show: ShowScrollbar,
106    pub git_diff: bool,
107    pub selected_text: bool,
108    pub selected_symbol: bool,
109    pub search_results: bool,
110    pub diagnostics: ScrollbarDiagnostics,
111    pub cursors: bool,
112    pub axes: ScrollbarAxes,
113}
114
115#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
116pub struct Gutter {
117    pub line_numbers: bool,
118    pub code_actions: bool,
119    pub runnables: bool,
120    pub breakpoints: bool,
121    pub folds: bool,
122}
123
124/// When to show the scrollbar in the editor.
125///
126/// Default: auto
127#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
128#[serde(rename_all = "snake_case")]
129pub enum ShowScrollbar {
130    /// Show the scrollbar if there's important information or
131    /// follow the system's configured behavior.
132    Auto,
133    /// Match the system's configured behavior.
134    System,
135    /// Always show the scrollbar.
136    Always,
137    /// Never show the scrollbar.
138    Never,
139}
140
141/// Forcefully enable or disable the scrollbar for each axis
142#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
143#[serde(rename_all = "lowercase")]
144pub struct ScrollbarAxes {
145    /// When false, forcefully disables the horizontal scrollbar. Otherwise, obey other settings.
146    ///
147    /// Default: true
148    pub horizontal: bool,
149
150    /// When false, forcefully disables the vertical scrollbar. Otherwise, obey other settings.
151    ///
152    /// Default: true
153    pub vertical: bool,
154}
155
156/// Which diagnostic indicators to show in the scrollbar.
157///
158/// Default: all
159#[derive(Copy, Clone, Debug, Serialize, JsonSchema, PartialEq, Eq)]
160#[serde(rename_all = "lowercase")]
161pub enum ScrollbarDiagnostics {
162    /// Show all diagnostic levels: hint, information, warnings, error.
163    All,
164    /// Show only the following diagnostic levels: information, warning, error.
165    Information,
166    /// Show only the following diagnostic levels: warning, error.
167    Warning,
168    /// Show only the following diagnostic level: error.
169    Error,
170    /// Do not show diagnostics.
171    None,
172}
173
174impl<'de> Deserialize<'de> for ScrollbarDiagnostics {
175    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
176    where
177        D: serde::Deserializer<'de>,
178    {
179        struct Visitor;
180
181        impl serde::de::Visitor<'_> for Visitor {
182            type Value = ScrollbarDiagnostics;
183
184            fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
185                write!(
186                    f,
187                    r#"a boolean or one of "all", "information", "warning", "error", "none""#
188                )
189            }
190
191            fn visit_bool<E>(self, b: bool) -> Result<Self::Value, E>
192            where
193                E: serde::de::Error,
194            {
195                match b {
196                    false => Ok(ScrollbarDiagnostics::None),
197                    true => Ok(ScrollbarDiagnostics::All),
198                }
199            }
200
201            fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
202            where
203                E: serde::de::Error,
204            {
205                match s {
206                    "all" => Ok(ScrollbarDiagnostics::All),
207                    "information" => Ok(ScrollbarDiagnostics::Information),
208                    "warning" => Ok(ScrollbarDiagnostics::Warning),
209                    "error" => Ok(ScrollbarDiagnostics::Error),
210                    "none" => Ok(ScrollbarDiagnostics::None),
211                    _ => Err(E::unknown_variant(
212                        s,
213                        &["all", "information", "warning", "error", "none"],
214                    )),
215                }
216            }
217        }
218
219        deserializer.deserialize_any(Visitor)
220    }
221}
222
223/// The key to use for adding multiple cursors
224///
225/// Default: alt
226#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
227#[serde(rename_all = "snake_case")]
228pub enum MultiCursorModifier {
229    Alt,
230    #[serde(alias = "cmd", alias = "ctrl")]
231    CmdOrCtrl,
232}
233
234/// Whether the editor will scroll beyond the last line.
235///
236/// Default: one_page
237#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
238#[serde(rename_all = "snake_case")]
239pub enum ScrollBeyondLastLine {
240    /// The editor will not scroll beyond the last line.
241    Off,
242
243    /// The editor will scroll beyond the last line by one page.
244    OnePage,
245
246    /// The editor will scroll beyond the last line by the same number of lines as vertical_scroll_margin.
247    VerticalScrollMargin,
248}
249
250/// Default options for buffer and project search items.
251#[derive(Copy, Clone, Default, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
252pub struct SearchSettings {
253    #[serde(default)]
254    pub whole_word: bool,
255    #[serde(default)]
256    pub case_sensitive: bool,
257    #[serde(default)]
258    pub include_ignored: bool,
259    #[serde(default)]
260    pub regex: bool,
261}
262
263#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
264pub struct EditorSettingsContent {
265    /// Whether the cursor blinks in the editor.
266    ///
267    /// Default: true
268    pub cursor_blink: Option<bool>,
269    /// Cursor shape for the default editor.
270    /// Can be "bar", "block", "underline", or "hollow".
271    ///
272    /// Default: None
273    pub cursor_shape: Option<CursorShape>,
274    /// How to highlight the current line in the editor.
275    ///
276    /// Default: all
277    pub current_line_highlight: Option<CurrentLineHighlight>,
278    /// Whether to highlight all occurrences of the selected text in an editor.
279    ///
280    /// Default: true
281    pub selection_highlight: Option<bool>,
282    /// The debounce delay before querying highlights based on the selected text.
283    ///
284    /// Default: 75
285    pub selection_highlight_debounce: Option<u64>,
286    /// The debounce delay before querying highlights from the language
287    /// server based on the current cursor location.
288    ///
289    /// Default: 75
290    pub lsp_highlight_debounce: Option<u64>,
291    /// Whether to show the informational hover box when moving the mouse
292    /// over symbols in the editor.
293    ///
294    /// Default: true
295    pub hover_popover_enabled: Option<bool>,
296    /// Time to wait before showing the informational hover box
297    ///
298    /// Default: 350
299    pub hover_popover_delay: Option<u64>,
300    /// Toolbar related settings
301    pub toolbar: Option<ToolbarContent>,
302    /// Scrollbar related settings
303    pub scrollbar: Option<ScrollbarContent>,
304    /// Gutter related settings
305    pub gutter: Option<GutterContent>,
306    /// Whether the editor will scroll beyond the last line.
307    ///
308    /// Default: one_page
309    pub scroll_beyond_last_line: Option<ScrollBeyondLastLine>,
310    /// The number of lines to keep above/below the cursor when auto-scrolling.
311    ///
312    /// Default: 3.
313    pub vertical_scroll_margin: Option<f32>,
314    /// Whether to scroll when clicking near the edge of the visible text area.
315    ///
316    /// Default: false
317    pub autoscroll_on_clicks: Option<bool>,
318    /// The number of characters to keep on either side when scrolling with the mouse.
319    ///
320    /// Default: 5.
321    pub horizontal_scroll_margin: Option<f32>,
322    /// Scroll sensitivity multiplier. This multiplier is applied
323    /// to both the horizontal and vertical delta values while scrolling.
324    ///
325    /// Default: 1.0
326    pub scroll_sensitivity: Option<f32>,
327    /// Whether the line numbers on editors gutter are relative or not.
328    ///
329    /// Default: false
330    pub relative_line_numbers: Option<bool>,
331    /// When to populate a new search's query based on the text under the cursor.
332    ///
333    /// Default: always
334    pub seed_search_query_from_cursor: Option<SeedQuerySetting>,
335    pub use_smartcase_search: Option<bool>,
336    /// The key to use for adding multiple cursors
337    ///
338    /// Default: alt
339    pub multi_cursor_modifier: Option<MultiCursorModifier>,
340    /// Hide the values of variables in `private` files, as defined by the
341    /// private_files setting. This only changes the visual representation,
342    /// the values are still present in the file and can be selected / copied / pasted
343    ///
344    /// Default: false
345    pub redact_private_values: Option<bool>,
346
347    /// How many lines to expand the multibuffer excerpts by default
348    ///
349    /// Default: 3
350    pub expand_excerpt_lines: Option<u32>,
351
352    /// Whether to enable middle-click paste on Linux
353    ///
354    /// Default: true
355    pub middle_click_paste: Option<bool>,
356
357    /// What to do when multibuffer is double clicked in some of its excerpts
358    /// (parts of singleton buffers).
359    ///
360    /// Default: select
361    pub double_click_in_multibuffer: Option<DoubleClickInMultibuffer>,
362    /// Whether the editor search results will loop
363    ///
364    /// Default: true
365    pub search_wrap: Option<bool>,
366
367    /// Defaults to use when opening a new buffer and project search items.
368    ///
369    /// Default: nothing is enabled
370    pub search: Option<SearchSettings>,
371
372    /// Whether to automatically show a signature help pop-up or not.
373    ///
374    /// Default: false
375    pub auto_signature_help: Option<bool>,
376
377    /// Whether to show the signature help pop-up after completions or bracket pairs inserted.
378    ///
379    /// Default: false
380    pub show_signature_help_after_edits: Option<bool>,
381
382    /// Jupyter REPL settings.
383    pub jupyter: Option<JupyterContent>,
384}
385
386// Toolbar related settings
387#[derive(Clone, Default, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
388pub struct ToolbarContent {
389    /// Whether to display breadcrumbs in the editor toolbar.
390    ///
391    /// Default: true
392    pub breadcrumbs: Option<bool>,
393    /// Whether to display quick action buttons in the editor toolbar.
394    ///
395    /// Default: true
396    pub quick_actions: Option<bool>,
397
398    /// Whether to show the selections menu in the editor toolbar
399    ///
400    /// Default: true
401    pub selections_menu: Option<bool>,
402}
403
404/// Scrollbar related settings
405#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)]
406pub struct ScrollbarContent {
407    /// When to show the scrollbar in the editor.
408    ///
409    /// Default: auto
410    pub show: Option<ShowScrollbar>,
411    /// Whether to show git diff indicators in the scrollbar.
412    ///
413    /// Default: true
414    pub git_diff: Option<bool>,
415    /// Whether to show buffer search result indicators in the scrollbar.
416    ///
417    /// Default: true
418    pub search_results: Option<bool>,
419    /// Whether to show selected text occurrences in the scrollbar.
420    ///
421    /// Default: true
422    pub selected_text: Option<bool>,
423    /// Whether to show selected symbol occurrences in the scrollbar.
424    ///
425    /// Default: true
426    pub selected_symbol: Option<bool>,
427    /// Which diagnostic indicators to show in the scrollbar:
428    ///
429    /// Default: all
430    pub diagnostics: Option<ScrollbarDiagnostics>,
431    /// Whether to show cursor positions in the scrollbar.
432    ///
433    /// Default: true
434    pub cursors: Option<bool>,
435    /// Forcefully enable or disable the scrollbar for each axis
436    pub axes: Option<ScrollbarAxesContent>,
437}
438
439/// Forcefully enable or disable the scrollbar for each axis
440#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)]
441pub struct ScrollbarAxesContent {
442    /// When false, forcefully disables the horizontal scrollbar. Otherwise, obey other settings.
443    ///
444    /// Default: true
445    horizontal: Option<bool>,
446
447    /// When false, forcefully disables the vertical scrollbar. Otherwise, obey other settings.
448    ///
449    /// Default: true
450    vertical: Option<bool>,
451}
452
453/// Gutter related settings
454#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
455pub struct GutterContent {
456    /// Whether to show line numbers in the gutter.
457    ///
458    /// Default: true
459    pub line_numbers: Option<bool>,
460    /// Whether to show code action buttons in the gutter.
461    ///
462    /// Default: true
463    pub code_actions: Option<bool>,
464    /// Whether to show runnable buttons in the gutter.
465    ///
466    /// Default: true
467    pub runnables: Option<bool>,
468    /// Whether to show breakpoints in the gutter.
469    ///
470    /// Default: true
471    pub breakpoints: Option<bool>,
472    /// Whether to show fold buttons in the gutter.
473    ///
474    /// Default: true
475    pub folds: Option<bool>,
476}
477
478impl EditorSettings {
479    pub fn jupyter_enabled(cx: &App) -> bool {
480        EditorSettings::get_global(cx).jupyter.enabled
481    }
482}
483
484impl Settings for EditorSettings {
485    const KEY: Option<&'static str> = None;
486
487    type FileContent = EditorSettingsContent;
488
489    fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> anyhow::Result<Self> {
490        sources.json_merge()
491    }
492}