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    pub hide_mouse_while_typing: Option<bool>,
 41}
 42
 43#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
 44#[serde(rename_all = "snake_case")]
 45pub enum CurrentLineHighlight {
 46    // Don't highlight the current line.
 47    None,
 48    // Highlight the gutter area.
 49    Gutter,
 50    // Highlight the editor area.
 51    Line,
 52    // Highlight the full line.
 53    All,
 54}
 55
 56/// When to populate a new search's query based on the text under the cursor.
 57#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
 58#[serde(rename_all = "snake_case")]
 59pub enum SeedQuerySetting {
 60    /// Always populate the search query with the word under the cursor.
 61    Always,
 62    /// Only populate the search query when there is text selected.
 63    Selection,
 64    /// Never populate the search query
 65    Never,
 66}
 67
 68/// What to do when multibuffer is double clicked in some of its excerpts (parts of singleton buffers).
 69#[derive(Default, Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
 70#[serde(rename_all = "snake_case")]
 71pub enum DoubleClickInMultibuffer {
 72    /// Behave as a regular buffer and select the whole word.
 73    #[default]
 74    Select,
 75    /// Open the excerpt clicked as a new buffer in the new tab, if no `alt` modifier was pressed during double click.
 76    /// Otherwise, behave as a regular buffer and select the whole word.
 77    Open,
 78}
 79
 80#[derive(Debug, Clone, Deserialize)]
 81pub struct Jupyter {
 82    /// Whether the Jupyter feature is enabled.
 83    ///
 84    /// Default: true
 85    pub enabled: bool,
 86}
 87
 88#[derive(Default, Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
 89#[serde(rename_all = "snake_case")]
 90pub struct JupyterContent {
 91    /// Whether the Jupyter feature is enabled.
 92    ///
 93    /// Default: true
 94    pub enabled: Option<bool>,
 95}
 96
 97#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
 98pub struct Toolbar {
 99    pub breadcrumbs: bool,
100    pub quick_actions: bool,
101    pub selections_menu: bool,
102}
103
104#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
105pub struct Scrollbar {
106    pub show: ShowScrollbar,
107    pub git_diff: bool,
108    pub selected_text: bool,
109    pub selected_symbol: bool,
110    pub search_results: bool,
111    pub diagnostics: ScrollbarDiagnostics,
112    pub cursors: bool,
113    pub axes: ScrollbarAxes,
114}
115
116#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
117pub struct Gutter {
118    pub line_numbers: bool,
119    pub code_actions: bool,
120    pub runnables: 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<'de> serde::de::Visitor<'de> 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    /// Determines whether the mouse cursor should be hidden while typing in an editor or input box.
275    ///
276    /// Default: true
277    pub hide_mouse_while_typing: Option<bool>,
278    /// How to highlight the current line in the editor.
279    ///
280    /// Default: all
281    pub current_line_highlight: Option<CurrentLineHighlight>,
282    /// Whether to highlight all occurrences of the selected text in an editor.
283    ///
284    /// Default: true
285    pub selection_highlight: Option<bool>,
286    /// The debounce delay before querying highlights based on the selected text.
287    ///
288    /// Default: 75
289    pub selection_highlight_debounce: Option<u64>,
290    /// The debounce delay before querying highlights from the language
291    /// server based on the current cursor location.
292    ///
293    /// Default: 75
294    pub lsp_highlight_debounce: Option<u64>,
295    /// Whether to show the informational hover box when moving the mouse
296    /// over symbols in the editor.
297    ///
298    /// Default: true
299    pub hover_popover_enabled: Option<bool>,
300    /// Time to wait before showing the informational hover box
301    ///
302    /// Default: 350
303    pub hover_popover_delay: Option<u64>,
304    /// Toolbar related settings
305    pub toolbar: Option<ToolbarContent>,
306    /// Scrollbar related settings
307    pub scrollbar: Option<ScrollbarContent>,
308    /// Gutter related settings
309    pub gutter: Option<GutterContent>,
310    /// Whether the editor will scroll beyond the last line.
311    ///
312    /// Default: one_page
313    pub scroll_beyond_last_line: Option<ScrollBeyondLastLine>,
314    /// The number of lines to keep above/below the cursor when auto-scrolling.
315    ///
316    /// Default: 3.
317    pub vertical_scroll_margin: Option<f32>,
318    /// Whether to scroll when clicking near the edge of the visible text area.
319    ///
320    /// Default: false
321    pub autoscroll_on_clicks: Option<bool>,
322    /// The number of characters to keep on either side when scrolling with the mouse.
323    ///
324    /// Default: 5.
325    pub horizontal_scroll_margin: Option<f32>,
326    /// Scroll sensitivity multiplier. This multiplier is applied
327    /// to both the horizontal and vertical delta values while scrolling.
328    ///
329    /// Default: 1.0
330    pub scroll_sensitivity: Option<f32>,
331    /// Whether the line numbers on editors gutter are relative or not.
332    ///
333    /// Default: false
334    pub relative_line_numbers: Option<bool>,
335    /// When to populate a new search's query based on the text under the cursor.
336    ///
337    /// Default: always
338    pub seed_search_query_from_cursor: Option<SeedQuerySetting>,
339    pub use_smartcase_search: Option<bool>,
340    /// The key to use for adding multiple cursors
341    ///
342    /// Default: alt
343    pub multi_cursor_modifier: Option<MultiCursorModifier>,
344    /// Hide the values of variables in `private` files, as defined by the
345    /// private_files setting. This only changes the visual representation,
346    /// the values are still present in the file and can be selected / copied / pasted
347    ///
348    /// Default: false
349    pub redact_private_values: Option<bool>,
350
351    /// How many lines to expand the multibuffer excerpts by default
352    ///
353    /// Default: 3
354    pub expand_excerpt_lines: Option<u32>,
355
356    /// Whether to enable middle-click paste on Linux
357    ///
358    /// Default: true
359    pub middle_click_paste: Option<bool>,
360
361    /// What to do when multibuffer is double clicked in some of its excerpts
362    /// (parts of singleton buffers).
363    ///
364    /// Default: select
365    pub double_click_in_multibuffer: Option<DoubleClickInMultibuffer>,
366    /// Whether the editor search results will loop
367    ///
368    /// Default: true
369    pub search_wrap: Option<bool>,
370
371    /// Defaults to use when opening a new buffer and project search items.
372    ///
373    /// Default: nothing is enabled
374    pub search: Option<SearchSettings>,
375
376    /// Whether to automatically show a signature help pop-up or not.
377    ///
378    /// Default: false
379    pub auto_signature_help: Option<bool>,
380
381    /// Whether to show the signature help pop-up after completions or bracket pairs inserted.
382    ///
383    /// Default: false
384    pub show_signature_help_after_edits: Option<bool>,
385
386    /// Jupyter REPL settings.
387    pub jupyter: Option<JupyterContent>,
388}
389
390// Toolbar related settings
391#[derive(Clone, Default, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
392pub struct ToolbarContent {
393    /// Whether to display breadcrumbs in the editor toolbar.
394    ///
395    /// Default: true
396    pub breadcrumbs: Option<bool>,
397    /// Whether to display quick action buttons in the editor toolbar.
398    ///
399    /// Default: true
400    pub quick_actions: Option<bool>,
401
402    /// Whether to show the selections menu in the editor toolbar
403    ///
404    /// Default: true
405    pub selections_menu: Option<bool>,
406}
407
408/// Scrollbar related settings
409#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)]
410pub struct ScrollbarContent {
411    /// When to show the scrollbar in the editor.
412    ///
413    /// Default: auto
414    pub show: Option<ShowScrollbar>,
415    /// Whether to show git diff indicators in the scrollbar.
416    ///
417    /// Default: true
418    pub git_diff: Option<bool>,
419    /// Whether to show buffer search result indicators in the scrollbar.
420    ///
421    /// Default: true
422    pub search_results: Option<bool>,
423    /// Whether to show selected text occurrences in the scrollbar.
424    ///
425    /// Default: true
426    pub selected_text: Option<bool>,
427    /// Whether to show selected symbol occurrences in the scrollbar.
428    ///
429    /// Default: true
430    pub selected_symbol: Option<bool>,
431    /// Which diagnostic indicators to show in the scrollbar:
432    ///
433    /// Default: all
434    pub diagnostics: Option<ScrollbarDiagnostics>,
435    /// Whether to show cursor positions in the scrollbar.
436    ///
437    /// Default: true
438    pub cursors: Option<bool>,
439    /// Forcefully enable or disable the scrollbar for each axis
440    pub axes: Option<ScrollbarAxesContent>,
441}
442
443/// Forcefully enable or disable the scrollbar for each axis
444#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)]
445pub struct ScrollbarAxesContent {
446    /// When false, forcefully disables the horizontal scrollbar. Otherwise, obey other settings.
447    ///
448    /// Default: true
449    horizontal: Option<bool>,
450
451    /// When false, forcefully disables the vertical scrollbar. Otherwise, obey other settings.
452    ///
453    /// Default: true
454    vertical: Option<bool>,
455}
456
457/// Gutter related settings
458#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
459pub struct GutterContent {
460    /// Whether to show line numbers in the gutter.
461    ///
462    /// Default: true
463    pub line_numbers: Option<bool>,
464    /// Whether to show code action buttons in the gutter.
465    ///
466    /// Default: true
467    pub code_actions: Option<bool>,
468    /// Whether to show runnable buttons in the gutter.
469    ///
470    /// Default: true
471    pub runnables: 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}