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