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}
43
44#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
45#[serde(rename_all = "snake_case")]
46pub enum CurrentLineHighlight {
47 // Don't highlight the current line.
48 None,
49 // Highlight the gutter area.
50 Gutter,
51 // Highlight the editor area.
52 Line,
53 // Highlight the full line.
54 All,
55}
56
57/// When to populate a new search's query based on the text under the cursor.
58#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
59#[serde(rename_all = "snake_case")]
60pub enum SeedQuerySetting {
61 /// Always populate the search query with the word under the cursor.
62 Always,
63 /// Only populate the search query when there is text selected.
64 Selection,
65 /// Never populate the search query
66 Never,
67}
68
69/// What to do when multibuffer is double clicked in some of its excerpts (parts of singleton buffers).
70#[derive(Default, Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
71#[serde(rename_all = "snake_case")]
72pub enum DoubleClickInMultibuffer {
73 /// Behave as a regular buffer and select the whole word.
74 #[default]
75 Select,
76 /// Open the excerpt clicked as a new buffer in the new tab, if no `alt` modifier was pressed during double click.
77 /// Otherwise, behave as a regular buffer and select the whole word.
78 Open,
79}
80
81#[derive(Debug, Clone, Deserialize)]
82pub struct Jupyter {
83 /// Whether the Jupyter feature is enabled.
84 ///
85 /// Default: true
86 pub enabled: bool,
87}
88
89#[derive(Default, Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
90#[serde(rename_all = "snake_case")]
91pub struct JupyterContent {
92 /// Whether the Jupyter feature is enabled.
93 ///
94 /// Default: true
95 pub enabled: Option<bool>,
96}
97
98#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
99pub struct Toolbar {
100 pub breadcrumbs: bool,
101 pub quick_actions: bool,
102 pub selections_menu: bool,
103}
104
105#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
106pub struct Scrollbar {
107 pub show: ShowScrollbar,
108 pub git_diff: bool,
109 pub selected_text: bool,
110 pub selected_symbol: bool,
111 pub search_results: bool,
112 pub diagnostics: ScrollbarDiagnostics,
113 pub cursors: bool,
114 pub axes: ScrollbarAxes,
115}
116
117#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
118pub struct Gutter {
119 pub line_numbers: bool,
120 pub code_actions: bool,
121 pub runnables: bool,
122 pub breakpoints: bool,
123 pub folds: bool,
124}
125
126/// When to show the scrollbar in the editor.
127///
128/// Default: auto
129#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
130#[serde(rename_all = "snake_case")]
131pub enum ShowScrollbar {
132 /// Show the scrollbar if there's important information or
133 /// follow the system's configured behavior.
134 Auto,
135 /// Match the system's configured behavior.
136 System,
137 /// Always show the scrollbar.
138 Always,
139 /// Never show the scrollbar.
140 Never,
141}
142
143/// Forcefully enable or disable the scrollbar for each axis
144#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
145#[serde(rename_all = "lowercase")]
146pub struct ScrollbarAxes {
147 /// When false, forcefully disables the horizontal scrollbar. Otherwise, obey other settings.
148 ///
149 /// Default: true
150 pub horizontal: bool,
151
152 /// When false, forcefully disables the vertical scrollbar. Otherwise, obey other settings.
153 ///
154 /// Default: true
155 pub vertical: bool,
156}
157
158/// Which diagnostic indicators to show in the scrollbar.
159///
160/// Default: all
161#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
162#[serde(rename_all = "lowercase")]
163pub enum ScrollbarDiagnostics {
164 /// Show all diagnostic levels: hint, information, warnings, error.
165 All,
166 /// Show only the following diagnostic levels: information, warning, error.
167 Information,
168 /// Show only the following diagnostic levels: warning, error.
169 Warning,
170 /// Show only the following diagnostic level: error.
171 Error,
172 /// Do not show diagnostics.
173 None,
174}
175
176/// The key to use for adding multiple cursors
177///
178/// Default: alt
179#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
180#[serde(rename_all = "snake_case")]
181pub enum MultiCursorModifier {
182 Alt,
183 #[serde(alias = "cmd", alias = "ctrl")]
184 CmdOrCtrl,
185}
186
187/// Whether the editor will scroll beyond the last line.
188///
189/// Default: one_page
190#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
191#[serde(rename_all = "snake_case")]
192pub enum ScrollBeyondLastLine {
193 /// The editor will not scroll beyond the last line.
194 Off,
195
196 /// The editor will scroll beyond the last line by one page.
197 OnePage,
198
199 /// The editor will scroll beyond the last line by the same number of lines as vertical_scroll_margin.
200 VerticalScrollMargin,
201}
202
203/// Default options for buffer and project search items.
204#[derive(Copy, Clone, Default, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
205pub struct SearchSettings {
206 #[serde(default)]
207 pub whole_word: bool,
208 #[serde(default)]
209 pub case_sensitive: bool,
210 #[serde(default)]
211 pub include_ignored: bool,
212 #[serde(default)]
213 pub regex: bool,
214}
215
216/// What to do when go to definition yields no results.
217#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
218#[serde(rename_all = "snake_case")]
219pub enum GoToDefinitionFallback {
220 /// Disables the fallback.
221 None,
222 /// Looks up references of the same symbol instead.
223 #[default]
224 FindAllReferences,
225}
226
227/// Determines when the mouse cursor should be hidden in an editor or input box.
228///
229/// Default: on_typing_and_movement
230#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
231#[serde(rename_all = "snake_case")]
232pub enum HideMouseMode {
233 /// Never hide the mouse cursor
234 Never,
235 /// Hide only when typing
236 OnTyping,
237 /// Hide on both typing and cursor movement
238 #[default]
239 OnTypingAndMovement,
240}
241
242#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
243pub struct EditorSettingsContent {
244 /// Whether the cursor blinks in the editor.
245 ///
246 /// Default: true
247 pub cursor_blink: Option<bool>,
248 /// Cursor shape for the default editor.
249 /// Can be "bar", "block", "underline", or "hollow".
250 ///
251 /// Default: None
252 pub cursor_shape: Option<CursorShape>,
253 /// Determines when the mouse cursor should be hidden in an editor or input box.
254 ///
255 /// Default: on_typing_and_movement
256 pub hide_mouse: Option<HideMouseMode>,
257 /// How to highlight the current line in the editor.
258 ///
259 /// Default: all
260 pub current_line_highlight: Option<CurrentLineHighlight>,
261 /// Whether to highlight all occurrences of the selected text in an editor.
262 ///
263 /// Default: true
264 pub selection_highlight: Option<bool>,
265 /// The debounce delay before querying highlights from the language
266 /// server based on the current cursor location.
267 ///
268 /// Default: 75
269 pub lsp_highlight_debounce: Option<u64>,
270 /// Whether to show the informational hover box when moving the mouse
271 /// over symbols in the editor.
272 ///
273 /// Default: true
274 pub hover_popover_enabled: Option<bool>,
275 /// Time to wait before showing the informational hover box
276 ///
277 /// Default: 350
278 pub hover_popover_delay: Option<u64>,
279 /// Toolbar related settings
280 pub toolbar: Option<ToolbarContent>,
281 /// Scrollbar related settings
282 pub scrollbar: Option<ScrollbarContent>,
283 /// Gutter related settings
284 pub gutter: Option<GutterContent>,
285 /// Whether the editor will scroll beyond the last line.
286 ///
287 /// Default: one_page
288 pub scroll_beyond_last_line: Option<ScrollBeyondLastLine>,
289 /// The number of lines to keep above/below the cursor when auto-scrolling.
290 ///
291 /// Default: 3.
292 pub vertical_scroll_margin: Option<f32>,
293 /// Whether to scroll when clicking near the edge of the visible text area.
294 ///
295 /// Default: false
296 pub autoscroll_on_clicks: Option<bool>,
297 /// The number of characters to keep on either side when scrolling with the mouse.
298 ///
299 /// Default: 5.
300 pub horizontal_scroll_margin: Option<f32>,
301 /// Scroll sensitivity multiplier. This multiplier is applied
302 /// to both the horizontal and vertical delta values while scrolling.
303 ///
304 /// Default: 1.0
305 pub scroll_sensitivity: Option<f32>,
306 /// Whether the line numbers on editors gutter are relative or not.
307 ///
308 /// Default: false
309 pub relative_line_numbers: Option<bool>,
310 /// When to populate a new search's query based on the text under the cursor.
311 ///
312 /// Default: always
313 pub seed_search_query_from_cursor: Option<SeedQuerySetting>,
314 pub use_smartcase_search: Option<bool>,
315 /// The key to use for adding multiple cursors
316 ///
317 /// Default: alt
318 pub multi_cursor_modifier: Option<MultiCursorModifier>,
319 /// Hide the values of variables in `private` files, as defined by the
320 /// private_files setting. This only changes the visual representation,
321 /// the values are still present in the file and can be selected / copied / pasted
322 ///
323 /// Default: false
324 pub redact_private_values: Option<bool>,
325
326 /// How many lines to expand the multibuffer excerpts by default
327 ///
328 /// Default: 3
329 pub expand_excerpt_lines: Option<u32>,
330
331 /// Whether to enable middle-click paste on Linux
332 ///
333 /// Default: true
334 pub middle_click_paste: Option<bool>,
335
336 /// What to do when multibuffer is double clicked in some of its excerpts
337 /// (parts of singleton buffers).
338 ///
339 /// Default: select
340 pub double_click_in_multibuffer: Option<DoubleClickInMultibuffer>,
341 /// Whether the editor search results will loop
342 ///
343 /// Default: true
344 pub search_wrap: Option<bool>,
345
346 /// Defaults to use when opening a new buffer and project search items.
347 ///
348 /// Default: nothing is enabled
349 pub search: Option<SearchSettings>,
350
351 /// Whether to automatically show a signature help pop-up or not.
352 ///
353 /// Default: false
354 pub auto_signature_help: Option<bool>,
355
356 /// Whether to show the signature help pop-up after completions or bracket pairs inserted.
357 ///
358 /// Default: false
359 pub show_signature_help_after_edits: Option<bool>,
360
361 /// Whether to follow-up empty go to definition responses from the language server or not.
362 /// `FindAllReferences` allows to look up references of the same symbol instead.
363 /// `None` disables the fallback.
364 ///
365 /// Default: FindAllReferences
366 pub go_to_definition_fallback: Option<GoToDefinitionFallback>,
367
368 /// Jupyter REPL settings.
369 pub jupyter: Option<JupyterContent>,
370}
371
372// Toolbar related settings
373#[derive(Clone, Default, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
374pub struct ToolbarContent {
375 /// Whether to display breadcrumbs in the editor toolbar.
376 ///
377 /// Default: true
378 pub breadcrumbs: Option<bool>,
379 /// Whether to display quick action buttons in the editor toolbar.
380 ///
381 /// Default: true
382 pub quick_actions: Option<bool>,
383
384 /// Whether to show the selections menu in the editor toolbar
385 ///
386 /// Default: true
387 pub selections_menu: Option<bool>,
388}
389
390/// Scrollbar related settings
391#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Default)]
392pub struct ScrollbarContent {
393 /// When to show the scrollbar in the editor.
394 ///
395 /// Default: auto
396 pub show: Option<ShowScrollbar>,
397 /// Whether to show git diff indicators in the scrollbar.
398 ///
399 /// Default: true
400 pub git_diff: Option<bool>,
401 /// Whether to show buffer search result indicators in the scrollbar.
402 ///
403 /// Default: true
404 pub search_results: Option<bool>,
405 /// Whether to show selected text occurrences in the scrollbar.
406 ///
407 /// Default: true
408 pub selected_text: Option<bool>,
409 /// Whether to show selected symbol occurrences in the scrollbar.
410 ///
411 /// Default: true
412 pub selected_symbol: Option<bool>,
413 /// Which diagnostic indicators to show in the scrollbar:
414 ///
415 /// Default: all
416 pub diagnostics: Option<ScrollbarDiagnostics>,
417 /// Whether to show cursor positions in the scrollbar.
418 ///
419 /// Default: true
420 pub cursors: Option<bool>,
421 /// Forcefully enable or disable the scrollbar for each axis
422 pub axes: Option<ScrollbarAxesContent>,
423}
424
425/// Forcefully enable or disable the scrollbar for each axis
426#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Default)]
427pub struct ScrollbarAxesContent {
428 /// When false, forcefully disables the horizontal scrollbar. Otherwise, obey other settings.
429 ///
430 /// Default: true
431 horizontal: Option<bool>,
432
433 /// When false, forcefully disables the vertical scrollbar. Otherwise, obey other settings.
434 ///
435 /// Default: true
436 vertical: Option<bool>,
437}
438
439/// Gutter related settings
440#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
441pub struct GutterContent {
442 /// Whether to show line numbers in the gutter.
443 ///
444 /// Default: true
445 pub line_numbers: Option<bool>,
446 /// Whether to show code action buttons in the gutter.
447 ///
448 /// Default: true
449 pub code_actions: Option<bool>,
450 /// Whether to show runnable buttons in the gutter.
451 ///
452 /// Default: true
453 pub runnables: Option<bool>,
454 /// Whether to show breakpoints in the gutter.
455 ///
456 /// Default: true
457 pub breakpoints: Option<bool>,
458 /// Whether to show fold buttons in the gutter.
459 ///
460 /// Default: true
461 pub folds: Option<bool>,
462}
463
464impl EditorSettings {
465 pub fn jupyter_enabled(cx: &App) -> bool {
466 EditorSettings::get_global(cx).jupyter.enabled
467 }
468}
469
470impl Settings for EditorSettings {
471 const KEY: Option<&'static str> = None;
472
473 type FileContent = EditorSettingsContent;
474
475 fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> anyhow::Result<Self> {
476 sources.json_merge()
477 }
478
479 fn import_from_vscode(vscode: &VsCodeSettings, current: &mut Self::FileContent) {
480 vscode.enum_setting(
481 "editor.cursorBlinking",
482 &mut current.cursor_blink,
483 |s| match s {
484 "blink" | "phase" | "expand" | "smooth" => Some(true),
485 "solid" => Some(false),
486 _ => None,
487 },
488 );
489 vscode.enum_setting(
490 "editor.cursorStyle",
491 &mut current.cursor_shape,
492 |s| match s {
493 "block" => Some(CursorShape::Block),
494 "block-outline" => Some(CursorShape::Hollow),
495 "line" | "line-thin" => Some(CursorShape::Bar),
496 "underline" | "underline-thin" => Some(CursorShape::Underline),
497 _ => None,
498 },
499 );
500
501 vscode.enum_setting(
502 "editor.renderLineHighlight",
503 &mut current.current_line_highlight,
504 |s| match s {
505 "gutter" => Some(CurrentLineHighlight::Gutter),
506 "line" => Some(CurrentLineHighlight::Line),
507 "all" => Some(CurrentLineHighlight::All),
508 _ => None,
509 },
510 );
511
512 vscode.bool_setting(
513 "editor.selectionHighlight",
514 &mut current.selection_highlight,
515 );
516 vscode.bool_setting("editor.hover.enabled", &mut current.hover_popover_enabled);
517 vscode.u64_setting("editor.hover.delay", &mut current.hover_popover_delay);
518
519 let mut gutter = GutterContent::default();
520 vscode.enum_setting(
521 "editor.showFoldingControls",
522 &mut gutter.folds,
523 |s| match s {
524 "always" | "mouseover" => Some(true),
525 "never" => Some(false),
526 _ => None,
527 },
528 );
529 vscode.enum_setting(
530 "editor.lineNumbers",
531 &mut gutter.line_numbers,
532 |s| match s {
533 "on" | "relative" => Some(true),
534 "off" => Some(false),
535 _ => None,
536 },
537 );
538 if let Some(old_gutter) = current.gutter.as_mut() {
539 if gutter.folds.is_some() {
540 old_gutter.folds = gutter.folds
541 }
542 if gutter.line_numbers.is_some() {
543 old_gutter.line_numbers = gutter.line_numbers
544 }
545 } else {
546 if gutter != GutterContent::default() {
547 current.gutter = Some(gutter)
548 }
549 }
550 if let Some(b) = vscode.read_bool("editor.scrollBeyondLastLine") {
551 current.scroll_beyond_last_line = Some(if b {
552 ScrollBeyondLastLine::OnePage
553 } else {
554 ScrollBeyondLastLine::Off
555 })
556 }
557
558 let mut scrollbar_axes = ScrollbarAxesContent::default();
559 vscode.enum_setting(
560 "editor.scrollbar.horizontal",
561 &mut scrollbar_axes.horizontal,
562 |s| match s {
563 "auto" | "visible" => Some(true),
564 "hidden" => Some(false),
565 _ => None,
566 },
567 );
568 vscode.enum_setting(
569 "editor.scrollbar.vertical",
570 &mut scrollbar_axes.horizontal,
571 |s| match s {
572 "auto" | "visible" => Some(true),
573 "hidden" => Some(false),
574 _ => None,
575 },
576 );
577
578 if scrollbar_axes != ScrollbarAxesContent::default() {
579 let scrollbar_settings = current.scrollbar.get_or_insert_default();
580 let axes_settings = scrollbar_settings.axes.get_or_insert_default();
581
582 if let Some(vertical) = scrollbar_axes.vertical {
583 axes_settings.vertical = Some(vertical);
584 }
585 if let Some(horizontal) = scrollbar_axes.horizontal {
586 axes_settings.horizontal = Some(horizontal);
587 }
588 }
589
590 // TODO: check if this does the int->float conversion?
591 vscode.f32_setting(
592 "editor.cursorSurroundingLines",
593 &mut current.vertical_scroll_margin,
594 );
595 vscode.f32_setting(
596 "editor.mouseWheelScrollSensitivity",
597 &mut current.scroll_sensitivity,
598 );
599 if Some("relative") == vscode.read_string("editor.lineNumbers") {
600 current.relative_line_numbers = Some(true);
601 }
602
603 vscode.enum_setting(
604 "editor.find.seedSearchStringFromSelection",
605 &mut current.seed_search_query_from_cursor,
606 |s| match s {
607 "always" => Some(SeedQuerySetting::Always),
608 "selection" => Some(SeedQuerySetting::Selection),
609 "never" => Some(SeedQuerySetting::Never),
610 _ => None,
611 },
612 );
613 vscode.bool_setting("search.smartCase", &mut current.use_smartcase_search);
614 vscode.enum_setting(
615 "editor.multiCursorModifier",
616 &mut current.multi_cursor_modifier,
617 |s| match s {
618 "ctrlCmd" => Some(MultiCursorModifier::CmdOrCtrl),
619 "alt" => Some(MultiCursorModifier::Alt),
620 _ => None,
621 },
622 );
623
624 vscode.bool_setting(
625 "editor.parameterHints.enabled",
626 &mut current.auto_signature_help,
627 );
628 vscode.bool_setting(
629 "editor.parameterHints.enabled",
630 &mut current.show_signature_help_after_edits,
631 );
632
633 if let Some(use_ignored) = vscode.read_bool("search.useIgnoreFiles") {
634 let search = current.search.get_or_insert_default();
635 search.include_ignored = use_ignored;
636 }
637 }
638}