editor_settings.rs

  1use gpui::App;
  2use language::CursorShape;
  3use schemars::JsonSchema;
  4use serde::{Deserialize, Serialize};
  5use settings::{Settings, SettingsSources, VsCodeSettings};
  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 lsp_highlight_debounce: u64,
 14    pub hover_popover_enabled: bool,
 15    pub hover_popover_delay: u64,
 16    pub toolbar: Toolbar,
 17    pub scrollbar: Scrollbar,
 18    pub gutter: Gutter,
 19    pub scroll_beyond_last_line: ScrollBeyondLastLine,
 20    pub vertical_scroll_margin: f32,
 21    pub autoscroll_on_clicks: bool,
 22    pub horizontal_scroll_margin: f32,
 23    pub scroll_sensitivity: f32,
 24    pub relative_line_numbers: bool,
 25    pub seed_search_query_from_cursor: SeedQuerySetting,
 26    pub use_smartcase_search: bool,
 27    pub multi_cursor_modifier: MultiCursorModifier,
 28    pub redact_private_values: bool,
 29    pub expand_excerpt_lines: u32,
 30    pub middle_click_paste: bool,
 31    #[serde(default)]
 32    pub double_click_in_multibuffer: DoubleClickInMultibuffer,
 33    pub search_wrap: bool,
 34    #[serde(default)]
 35    pub search: SearchSettings,
 36    pub auto_signature_help: bool,
 37    pub show_signature_help_after_edits: bool,
 38    #[serde(default)]
 39    pub go_to_definition_fallback: GoToDefinitionFallback,
 40    pub jupyter: Jupyter,
 41    pub hide_mouse: Option<HideMouseMode>,
 42    pub snippet_sort_order: SnippetSortOrder,
 43}
 44
 45#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
 46#[serde(rename_all = "snake_case")]
 47pub enum CurrentLineHighlight {
 48    // Don't highlight the current line.
 49    None,
 50    // Highlight the gutter area.
 51    Gutter,
 52    // Highlight the editor area.
 53    Line,
 54    // Highlight the full line.
 55    All,
 56}
 57
 58/// When to populate a new search's query based on the text under the cursor.
 59#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
 60#[serde(rename_all = "snake_case")]
 61pub enum SeedQuerySetting {
 62    /// Always populate the search query with the word under the cursor.
 63    Always,
 64    /// Only populate the search query when there is text selected.
 65    Selection,
 66    /// Never populate the search query
 67    Never,
 68}
 69
 70/// What to do when multibuffer is double clicked in some of its excerpts (parts of singleton buffers).
 71#[derive(Default, Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
 72#[serde(rename_all = "snake_case")]
 73pub enum DoubleClickInMultibuffer {
 74    /// Behave as a regular buffer and select the whole word.
 75    #[default]
 76    Select,
 77    /// Open the excerpt clicked as a new buffer in the new tab, if no `alt` modifier was pressed during double click.
 78    /// Otherwise, behave as a regular buffer and select the whole word.
 79    Open,
 80}
 81
 82#[derive(Debug, Clone, Deserialize)]
 83pub struct Jupyter {
 84    /// Whether the Jupyter feature is enabled.
 85    ///
 86    /// Default: true
 87    pub enabled: bool,
 88}
 89
 90#[derive(Default, Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
 91#[serde(rename_all = "snake_case")]
 92pub struct JupyterContent {
 93    /// Whether the Jupyter feature is enabled.
 94    ///
 95    /// Default: true
 96    pub enabled: Option<bool>,
 97}
 98
 99#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
100pub struct Toolbar {
101    pub breadcrumbs: bool,
102    pub quick_actions: bool,
103    pub selections_menu: bool,
104    pub agent_review: bool,
105}
106
107#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
108pub struct Scrollbar {
109    pub show: ShowScrollbar,
110    pub git_diff: bool,
111    pub selected_text: bool,
112    pub selected_symbol: bool,
113    pub search_results: bool,
114    pub diagnostics: ScrollbarDiagnostics,
115    pub cursors: bool,
116    pub axes: ScrollbarAxes,
117}
118
119#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
120pub struct Gutter {
121    pub line_numbers: bool,
122    pub runnables: bool,
123    pub breakpoints: bool,
124    pub folds: bool,
125}
126
127/// When to show the scrollbar in the editor.
128///
129/// Default: auto
130#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
131#[serde(rename_all = "snake_case")]
132pub enum ShowScrollbar {
133    /// Show the scrollbar if there's important information or
134    /// follow the system's configured behavior.
135    Auto,
136    /// Match the system's configured behavior.
137    System,
138    /// Always show the scrollbar.
139    Always,
140    /// Never show the scrollbar.
141    Never,
142}
143
144/// Forcefully enable or disable the scrollbar for each axis
145#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
146#[serde(rename_all = "lowercase")]
147pub struct ScrollbarAxes {
148    /// When false, forcefully disables the horizontal scrollbar. Otherwise, obey other settings.
149    ///
150    /// Default: true
151    pub horizontal: bool,
152
153    /// When false, forcefully disables the vertical scrollbar. Otherwise, obey other settings.
154    ///
155    /// Default: true
156    pub vertical: bool,
157}
158
159/// Which diagnostic indicators to show in the scrollbar.
160///
161/// Default: all
162#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
163#[serde(rename_all = "lowercase")]
164pub enum ScrollbarDiagnostics {
165    /// Show all diagnostic levels: hint, information, warnings, error.
166    All,
167    /// Show only the following diagnostic levels: information, warning, error.
168    Information,
169    /// Show only the following diagnostic levels: warning, error.
170    Warning,
171    /// Show only the following diagnostic level: error.
172    Error,
173    /// Do not show diagnostics.
174    None,
175}
176
177/// The key to use for adding multiple cursors
178///
179/// Default: alt
180#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
181#[serde(rename_all = "snake_case")]
182pub enum MultiCursorModifier {
183    Alt,
184    #[serde(alias = "cmd", alias = "ctrl")]
185    CmdOrCtrl,
186}
187
188/// Whether the editor will scroll beyond the last line.
189///
190/// Default: one_page
191#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
192#[serde(rename_all = "snake_case")]
193pub enum ScrollBeyondLastLine {
194    /// The editor will not scroll beyond the last line.
195    Off,
196
197    /// The editor will scroll beyond the last line by one page.
198    OnePage,
199
200    /// The editor will scroll beyond the last line by the same number of lines as vertical_scroll_margin.
201    VerticalScrollMargin,
202}
203
204/// Default options for buffer and project search items.
205#[derive(Copy, Clone, Default, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
206pub struct SearchSettings {
207    #[serde(default)]
208    pub whole_word: bool,
209    #[serde(default)]
210    pub case_sensitive: bool,
211    #[serde(default)]
212    pub include_ignored: bool,
213    #[serde(default)]
214    pub regex: bool,
215}
216
217/// What to do when go to definition yields no results.
218#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
219#[serde(rename_all = "snake_case")]
220pub enum GoToDefinitionFallback {
221    /// Disables the fallback.
222    None,
223    /// Looks up references of the same symbol instead.
224    #[default]
225    FindAllReferences,
226}
227
228/// Determines when the mouse cursor should be hidden in an editor or input box.
229///
230/// Default: on_typing_and_movement
231#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
232#[serde(rename_all = "snake_case")]
233pub enum HideMouseMode {
234    /// Never hide the mouse cursor
235    Never,
236    /// Hide only when typing
237    OnTyping,
238    /// Hide on both typing and cursor movement
239    #[default]
240    OnTypingAndMovement,
241}
242
243/// Determines how snippets are sorted relative to other completion items.
244///
245/// Default: inline
246#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
247#[serde(rename_all = "snake_case")]
248pub enum SnippetSortOrder {
249    /// Place snippets at the top of the completion list
250    Top,
251    /// Sort snippets normally using the default comparison logic
252    #[default]
253    Inline,
254    /// Place snippets at the bottom of the completion list
255    Bottom,
256}
257
258#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
259pub struct EditorSettingsContent {
260    /// Whether the cursor blinks in the editor.
261    ///
262    /// Default: true
263    pub cursor_blink: Option<bool>,
264    /// Cursor shape for the default editor.
265    /// Can be "bar", "block", "underline", or "hollow".
266    ///
267    /// Default: None
268    pub cursor_shape: Option<CursorShape>,
269    /// Determines when the mouse cursor should be hidden in an editor or input box.
270    ///
271    /// Default: on_typing_and_movement
272    pub hide_mouse: Option<HideMouseMode>,
273    /// Determines how snippets are sorted relative to other completion items.
274    ///
275    /// Default: inline
276    pub snippet_sort_order: Option<SnippetSortOrder>,
277    /// How to highlight the current line in the editor.
278    ///
279    /// Default: all
280    pub current_line_highlight: Option<CurrentLineHighlight>,
281    /// Whether to highlight all occurrences of the selected text in an editor.
282    ///
283    /// Default: true
284    pub selection_highlight: Option<bool>,
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    /// Whether to follow-up empty go to definition responses from the language server or not.
382    /// `FindAllReferences` allows to look up references of the same symbol instead.
383    /// `None` disables the fallback.
384    ///
385    /// Default: FindAllReferences
386    pub go_to_definition_fallback: Option<GoToDefinitionFallback>,
387
388    /// Jupyter REPL settings.
389    pub jupyter: Option<JupyterContent>,
390}
391
392// Toolbar related settings
393#[derive(Clone, Default, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
394pub struct ToolbarContent {
395    /// Whether to display breadcrumbs in the editor toolbar.
396    ///
397    /// Default: true
398    pub breadcrumbs: Option<bool>,
399    /// Whether to display quick action buttons in the editor toolbar.
400    ///
401    /// Default: true
402    pub quick_actions: Option<bool>,
403    /// Whether to show the selections menu in the editor toolbar.
404    ///
405    /// Default: true
406    pub selections_menu: Option<bool>,
407    /// Whether to display Agent review buttons in the editor toolbar.
408    /// Only applicable while reviewing a file edited by the Agent.
409    ///
410    /// Default: true
411    pub agent_review: Option<bool>,
412}
413
414/// Scrollbar related settings
415#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Default)]
416pub struct ScrollbarContent {
417    /// When to show the scrollbar in the editor.
418    ///
419    /// Default: auto
420    pub show: Option<ShowScrollbar>,
421    /// Whether to show git diff indicators in the scrollbar.
422    ///
423    /// Default: true
424    pub git_diff: Option<bool>,
425    /// Whether to show buffer search result indicators in the scrollbar.
426    ///
427    /// Default: true
428    pub search_results: Option<bool>,
429    /// Whether to show selected text occurrences in the scrollbar.
430    ///
431    /// Default: true
432    pub selected_text: Option<bool>,
433    /// Whether to show selected symbol occurrences in the scrollbar.
434    ///
435    /// Default: true
436    pub selected_symbol: Option<bool>,
437    /// Which diagnostic indicators to show in the scrollbar:
438    ///
439    /// Default: all
440    pub diagnostics: Option<ScrollbarDiagnostics>,
441    /// Whether to show cursor positions in the scrollbar.
442    ///
443    /// Default: true
444    pub cursors: Option<bool>,
445    /// Forcefully enable or disable the scrollbar for each axis
446    pub axes: Option<ScrollbarAxesContent>,
447}
448
449/// Forcefully enable or disable the scrollbar for each axis
450#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Default)]
451pub struct ScrollbarAxesContent {
452    /// When false, forcefully disables the horizontal scrollbar. Otherwise, obey other settings.
453    ///
454    /// Default: true
455    horizontal: Option<bool>,
456
457    /// When false, forcefully disables the vertical scrollbar. Otherwise, obey other settings.
458    ///
459    /// Default: true
460    vertical: Option<bool>,
461}
462
463/// Gutter related settings
464#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
465pub struct GutterContent {
466    /// Whether to show line numbers in the gutter.
467    ///
468    /// Default: true
469    pub line_numbers: Option<bool>,
470    /// Whether to show runnable buttons in the gutter.
471    ///
472    /// Default: true
473    pub runnables: Option<bool>,
474    /// Whether to show breakpoints in the gutter.
475    ///
476    /// Default: true
477    pub breakpoints: Option<bool>,
478    /// Whether to show fold buttons in the gutter.
479    ///
480    /// Default: true
481    pub folds: Option<bool>,
482}
483
484impl EditorSettings {
485    pub fn jupyter_enabled(cx: &App) -> bool {
486        EditorSettings::get_global(cx).jupyter.enabled
487    }
488}
489
490impl Settings for EditorSettings {
491    const KEY: Option<&'static str> = None;
492
493    type FileContent = EditorSettingsContent;
494
495    fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> anyhow::Result<Self> {
496        sources.json_merge()
497    }
498
499    fn import_from_vscode(vscode: &VsCodeSettings, current: &mut Self::FileContent) {
500        vscode.enum_setting(
501            "editor.cursorBlinking",
502            &mut current.cursor_blink,
503            |s| match s {
504                "blink" | "phase" | "expand" | "smooth" => Some(true),
505                "solid" => Some(false),
506                _ => None,
507            },
508        );
509        vscode.enum_setting(
510            "editor.cursorStyle",
511            &mut current.cursor_shape,
512            |s| match s {
513                "block" => Some(CursorShape::Block),
514                "block-outline" => Some(CursorShape::Hollow),
515                "line" | "line-thin" => Some(CursorShape::Bar),
516                "underline" | "underline-thin" => Some(CursorShape::Underline),
517                _ => None,
518            },
519        );
520
521        vscode.enum_setting(
522            "editor.renderLineHighlight",
523            &mut current.current_line_highlight,
524            |s| match s {
525                "gutter" => Some(CurrentLineHighlight::Gutter),
526                "line" => Some(CurrentLineHighlight::Line),
527                "all" => Some(CurrentLineHighlight::All),
528                _ => None,
529            },
530        );
531
532        vscode.bool_setting(
533            "editor.selectionHighlight",
534            &mut current.selection_highlight,
535        );
536        vscode.bool_setting("editor.hover.enabled", &mut current.hover_popover_enabled);
537        vscode.u64_setting("editor.hover.delay", &mut current.hover_popover_delay);
538
539        let mut gutter = GutterContent::default();
540        vscode.enum_setting(
541            "editor.showFoldingControls",
542            &mut gutter.folds,
543            |s| match s {
544                "always" | "mouseover" => Some(true),
545                "never" => Some(false),
546                _ => None,
547            },
548        );
549        vscode.enum_setting(
550            "editor.lineNumbers",
551            &mut gutter.line_numbers,
552            |s| match s {
553                "on" | "relative" => Some(true),
554                "off" => Some(false),
555                _ => None,
556            },
557        );
558        if let Some(old_gutter) = current.gutter.as_mut() {
559            if gutter.folds.is_some() {
560                old_gutter.folds = gutter.folds
561            }
562            if gutter.line_numbers.is_some() {
563                old_gutter.line_numbers = gutter.line_numbers
564            }
565        } else {
566            if gutter != GutterContent::default() {
567                current.gutter = Some(gutter)
568            }
569        }
570        if let Some(b) = vscode.read_bool("editor.scrollBeyondLastLine") {
571            current.scroll_beyond_last_line = Some(if b {
572                ScrollBeyondLastLine::OnePage
573            } else {
574                ScrollBeyondLastLine::Off
575            })
576        }
577
578        let mut scrollbar_axes = ScrollbarAxesContent::default();
579        vscode.enum_setting(
580            "editor.scrollbar.horizontal",
581            &mut scrollbar_axes.horizontal,
582            |s| match s {
583                "auto" | "visible" => Some(true),
584                "hidden" => Some(false),
585                _ => None,
586            },
587        );
588        vscode.enum_setting(
589            "editor.scrollbar.vertical",
590            &mut scrollbar_axes.horizontal,
591            |s| match s {
592                "auto" | "visible" => Some(true),
593                "hidden" => Some(false),
594                _ => None,
595            },
596        );
597
598        if scrollbar_axes != ScrollbarAxesContent::default() {
599            let scrollbar_settings = current.scrollbar.get_or_insert_default();
600            let axes_settings = scrollbar_settings.axes.get_or_insert_default();
601
602            if let Some(vertical) = scrollbar_axes.vertical {
603                axes_settings.vertical = Some(vertical);
604            }
605            if let Some(horizontal) = scrollbar_axes.horizontal {
606                axes_settings.horizontal = Some(horizontal);
607            }
608        }
609
610        // TODO: check if this does the int->float conversion?
611        vscode.f32_setting(
612            "editor.cursorSurroundingLines",
613            &mut current.vertical_scroll_margin,
614        );
615        vscode.f32_setting(
616            "editor.mouseWheelScrollSensitivity",
617            &mut current.scroll_sensitivity,
618        );
619        if Some("relative") == vscode.read_string("editor.lineNumbers") {
620            current.relative_line_numbers = Some(true);
621        }
622
623        vscode.enum_setting(
624            "editor.find.seedSearchStringFromSelection",
625            &mut current.seed_search_query_from_cursor,
626            |s| match s {
627                "always" => Some(SeedQuerySetting::Always),
628                "selection" => Some(SeedQuerySetting::Selection),
629                "never" => Some(SeedQuerySetting::Never),
630                _ => None,
631            },
632        );
633        vscode.bool_setting("search.smartCase", &mut current.use_smartcase_search);
634        vscode.enum_setting(
635            "editor.multiCursorModifier",
636            &mut current.multi_cursor_modifier,
637            |s| match s {
638                "ctrlCmd" => Some(MultiCursorModifier::CmdOrCtrl),
639                "alt" => Some(MultiCursorModifier::Alt),
640                _ => None,
641            },
642        );
643
644        vscode.bool_setting(
645            "editor.parameterHints.enabled",
646            &mut current.auto_signature_help,
647        );
648        vscode.bool_setting(
649            "editor.parameterHints.enabled",
650            &mut current.show_signature_help_after_edits,
651        );
652
653        if let Some(use_ignored) = vscode.read_bool("search.useIgnoreFiles") {
654            let search = current.search.get_or_insert_default();
655            search.include_ignored = use_ignored;
656        }
657    }
658}