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