editor_settings.rs

  1use core::num;
  2
  3use gpui::App;
  4use language::CursorShape;
  5use project::project_settings::DiagnosticSeverity;
  6pub use settings::{
  7    CompletionDetailAlignment, CurrentLineHighlight, DelayMs, DiffViewStyle, DisplayIn,
  8    DocumentColorsRenderMode, DoubleClickInMultibuffer, GoToDefinitionFallback, HideMouseMode,
  9    MinimapThumb, MinimapThumbBorder, MultiCursorModifier, ScrollBeyondLastLine,
 10    ScrollbarDiagnostics, SeedQuerySetting, ShowMinimap, SnippetSortOrder,
 11};
 12use settings::{RegisterSetting, RelativeLineNumbers, Settings};
 13use ui::scrollbars::ShowScrollbar;
 14
 15/// Imports from the VSCode settings at
 16/// https://code.visualstudio.com/docs/reference/default-settings
 17#[derive(Clone, RegisterSetting)]
 18pub struct EditorSettings {
 19    pub cursor_blink: bool,
 20    pub cursor_shape: Option<CursorShape>,
 21    pub current_line_highlight: CurrentLineHighlight,
 22    pub selection_highlight: bool,
 23    pub rounded_selection: bool,
 24    pub lsp_highlight_debounce: DelayMs,
 25    pub hover_popover_enabled: bool,
 26    pub hover_popover_delay: DelayMs,
 27    pub toolbar: Toolbar,
 28    pub scrollbar: Scrollbar,
 29    pub minimap: Minimap,
 30    pub gutter: Gutter,
 31    pub scroll_beyond_last_line: ScrollBeyondLastLine,
 32    pub vertical_scroll_margin: f64,
 33    pub autoscroll_on_clicks: bool,
 34    pub horizontal_scroll_margin: f32,
 35    pub scroll_sensitivity: f32,
 36    pub fast_scroll_sensitivity: f32,
 37    pub sticky_scroll: StickyScroll,
 38    pub relative_line_numbers: RelativeLineNumbers,
 39    pub seed_search_query_from_cursor: SeedQuerySetting,
 40    pub use_smartcase_search: bool,
 41    pub multi_cursor_modifier: MultiCursorModifier,
 42    pub redact_private_values: bool,
 43    pub expand_excerpt_lines: u32,
 44    pub excerpt_context_lines: u32,
 45    pub middle_click_paste: bool,
 46    pub double_click_in_multibuffer: DoubleClickInMultibuffer,
 47    pub search_wrap: bool,
 48    pub search: SearchSettings,
 49    pub auto_signature_help: bool,
 50    pub show_signature_help_after_edits: bool,
 51    pub go_to_definition_fallback: GoToDefinitionFallback,
 52    pub jupyter: Jupyter,
 53    pub hide_mouse: Option<HideMouseMode>,
 54    pub snippet_sort_order: SnippetSortOrder,
 55    pub diagnostics_max_severity: Option<DiagnosticSeverity>,
 56    pub inline_code_actions: bool,
 57    pub drag_and_drop_selection: DragAndDropSelection,
 58    pub lsp_document_colors: DocumentColorsRenderMode,
 59    pub minimum_contrast_for_highlights: f32,
 60    pub completion_menu_scrollbar: ShowScrollbar,
 61    pub completion_detail_alignment: CompletionDetailAlignment,
 62    pub diff_view_style: DiffViewStyle,
 63    pub minimum_split_diff_width: f32,
 64}
 65#[derive(Debug, Clone)]
 66pub struct Jupyter {
 67    /// Whether the Jupyter feature is enabled.
 68    ///
 69    /// Default: true
 70    pub enabled: bool,
 71}
 72
 73#[derive(Copy, Clone, Debug, PartialEq, Eq)]
 74pub struct StickyScroll {
 75    pub enabled: bool,
 76}
 77
 78#[derive(Clone, Debug, PartialEq, Eq)]
 79pub struct Toolbar {
 80    pub breadcrumbs: bool,
 81    pub quick_actions: bool,
 82    pub selections_menu: bool,
 83    pub agent_review: bool,
 84    pub code_actions: bool,
 85}
 86
 87#[derive(Copy, Clone, Debug, PartialEq, Eq)]
 88pub struct Scrollbar {
 89    pub show: ShowScrollbar,
 90    pub git_diff: bool,
 91    pub selected_text: bool,
 92    pub selected_symbol: bool,
 93    pub search_results: bool,
 94    pub diagnostics: ScrollbarDiagnostics,
 95    pub cursors: bool,
 96    pub axes: ScrollbarAxes,
 97}
 98
 99#[derive(Copy, Clone, Debug, PartialEq)]
100pub struct Minimap {
101    pub show: ShowMinimap,
102    pub display_in: DisplayIn,
103    pub thumb: MinimapThumb,
104    pub thumb_border: MinimapThumbBorder,
105    pub current_line_highlight: Option<CurrentLineHighlight>,
106    pub max_width_columns: num::NonZeroU32,
107}
108
109impl Minimap {
110    pub fn minimap_enabled(&self) -> bool {
111        self.show != ShowMinimap::Never
112    }
113
114    #[inline]
115    pub fn on_active_editor(&self) -> bool {
116        self.display_in == DisplayIn::ActiveEditor
117    }
118
119    pub fn with_show_override(self) -> Self {
120        Self {
121            show: ShowMinimap::Always,
122            ..self
123        }
124    }
125}
126
127#[derive(Copy, Clone, Debug, PartialEq, Eq)]
128pub struct Gutter {
129    pub min_line_number_digits: usize,
130    pub line_numbers: bool,
131    pub runnables: bool,
132    pub breakpoints: bool,
133    pub folds: bool,
134}
135
136/// Forcefully enable or disable the scrollbar for each axis
137#[derive(Copy, Clone, Debug, PartialEq, Eq)]
138pub struct ScrollbarAxes {
139    /// When false, forcefully disables the horizontal scrollbar. Otherwise, obey other settings.
140    ///
141    /// Default: true
142    pub horizontal: bool,
143
144    /// When false, forcefully disables the vertical scrollbar. Otherwise, obey other settings.
145    ///
146    /// Default: true
147    pub vertical: bool,
148}
149
150/// Whether to allow drag and drop text selection in buffer.
151#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)]
152pub struct DragAndDropSelection {
153    /// When true, enables drag and drop text selection in buffer.
154    ///
155    /// Default: true
156    pub enabled: bool,
157
158    /// The delay in milliseconds that must elapse before drag and drop is allowed. Otherwise, a new text selection is created.
159    ///
160    /// Default: 300
161    pub delay: DelayMs,
162}
163
164/// Default options for buffer and project search items.
165#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)]
166pub struct SearchSettings {
167    /// Whether to show the project search button in the status bar.
168    pub button: bool,
169    /// Whether to only match on whole words.
170    pub whole_word: bool,
171    /// Whether to match case sensitively.
172    pub case_sensitive: bool,
173    /// Whether to include gitignored files in search results.
174    pub include_ignored: bool,
175    /// Whether to interpret the search query as a regular expression.
176    pub regex: bool,
177    /// Whether to center the cursor on each search match when navigating.
178    pub center_on_match: bool,
179}
180
181impl EditorSettings {
182    pub fn jupyter_enabled(cx: &App) -> bool {
183        EditorSettings::get_global(cx).jupyter.enabled
184    }
185}
186
187impl Settings for EditorSettings {
188    fn from_settings(content: &settings::SettingsContent) -> Self {
189        let editor = content.editor.clone();
190        let scrollbar = editor.scrollbar.unwrap();
191        let minimap = editor.minimap.unwrap();
192        let gutter = editor.gutter.unwrap();
193        let axes = scrollbar.axes.unwrap();
194        let toolbar = editor.toolbar.unwrap();
195        let search = editor.search.unwrap();
196        let drag_and_drop_selection = editor.drag_and_drop_selection.unwrap();
197        let sticky_scroll = editor.sticky_scroll.unwrap();
198        Self {
199            cursor_blink: editor.cursor_blink.unwrap(),
200            cursor_shape: editor.cursor_shape.map(Into::into),
201            current_line_highlight: editor.current_line_highlight.unwrap(),
202            selection_highlight: editor.selection_highlight.unwrap(),
203            rounded_selection: editor.rounded_selection.unwrap(),
204            lsp_highlight_debounce: editor.lsp_highlight_debounce.unwrap(),
205            hover_popover_enabled: editor.hover_popover_enabled.unwrap(),
206            hover_popover_delay: editor.hover_popover_delay.unwrap(),
207            toolbar: Toolbar {
208                breadcrumbs: toolbar.breadcrumbs.unwrap(),
209                quick_actions: toolbar.quick_actions.unwrap(),
210                selections_menu: toolbar.selections_menu.unwrap(),
211                agent_review: toolbar.agent_review.unwrap(),
212                code_actions: toolbar.code_actions.unwrap(),
213            },
214            scrollbar: Scrollbar {
215                show: scrollbar.show.map(ui_scrollbar_settings_from_raw).unwrap(),
216                git_diff: scrollbar.git_diff.unwrap()
217                    && content
218                        .git
219                        .as_ref()
220                        .unwrap()
221                        .enabled
222                        .unwrap()
223                        .is_git_diff_enabled(),
224                selected_text: scrollbar.selected_text.unwrap(),
225                selected_symbol: scrollbar.selected_symbol.unwrap(),
226                search_results: scrollbar.search_results.unwrap(),
227                diagnostics: scrollbar.diagnostics.unwrap(),
228                cursors: scrollbar.cursors.unwrap(),
229                axes: ScrollbarAxes {
230                    horizontal: axes.horizontal.unwrap(),
231                    vertical: axes.vertical.unwrap(),
232                },
233            },
234            minimap: Minimap {
235                show: minimap.show.unwrap(),
236                display_in: minimap.display_in.unwrap(),
237                thumb: minimap.thumb.unwrap(),
238                thumb_border: minimap.thumb_border.unwrap(),
239                current_line_highlight: minimap.current_line_highlight,
240                max_width_columns: minimap.max_width_columns.unwrap(),
241            },
242            gutter: Gutter {
243                min_line_number_digits: gutter.min_line_number_digits.unwrap(),
244                line_numbers: gutter.line_numbers.unwrap(),
245                runnables: gutter.runnables.unwrap(),
246                breakpoints: gutter.breakpoints.unwrap(),
247                folds: gutter.folds.unwrap(),
248            },
249            scroll_beyond_last_line: editor.scroll_beyond_last_line.unwrap(),
250            vertical_scroll_margin: editor.vertical_scroll_margin.unwrap() as f64,
251            autoscroll_on_clicks: editor.autoscroll_on_clicks.unwrap(),
252            horizontal_scroll_margin: editor.horizontal_scroll_margin.unwrap(),
253            scroll_sensitivity: editor.scroll_sensitivity.unwrap(),
254            fast_scroll_sensitivity: editor.fast_scroll_sensitivity.unwrap(),
255            sticky_scroll: StickyScroll {
256                enabled: sticky_scroll.enabled.unwrap(),
257            },
258            relative_line_numbers: editor.relative_line_numbers.unwrap(),
259            seed_search_query_from_cursor: editor.seed_search_query_from_cursor.unwrap(),
260            use_smartcase_search: editor.use_smartcase_search.unwrap(),
261            multi_cursor_modifier: editor.multi_cursor_modifier.unwrap(),
262            redact_private_values: editor.redact_private_values.unwrap(),
263            expand_excerpt_lines: editor.expand_excerpt_lines.unwrap(),
264            excerpt_context_lines: editor.excerpt_context_lines.unwrap(),
265            middle_click_paste: editor.middle_click_paste.unwrap(),
266            double_click_in_multibuffer: editor.double_click_in_multibuffer.unwrap(),
267            search_wrap: editor.search_wrap.unwrap(),
268            search: SearchSettings {
269                button: search.button.unwrap(),
270                whole_word: search.whole_word.unwrap(),
271                case_sensitive: search.case_sensitive.unwrap(),
272                include_ignored: search.include_ignored.unwrap(),
273                regex: search.regex.unwrap(),
274                center_on_match: search.center_on_match.unwrap(),
275            },
276            auto_signature_help: editor.auto_signature_help.unwrap(),
277            show_signature_help_after_edits: editor.show_signature_help_after_edits.unwrap(),
278            go_to_definition_fallback: editor.go_to_definition_fallback.unwrap(),
279            jupyter: Jupyter {
280                enabled: editor.jupyter.unwrap().enabled.unwrap(),
281            },
282            hide_mouse: editor.hide_mouse,
283            snippet_sort_order: editor.snippet_sort_order.unwrap(),
284            diagnostics_max_severity: editor.diagnostics_max_severity.map(Into::into),
285            inline_code_actions: editor.inline_code_actions.unwrap(),
286            drag_and_drop_selection: DragAndDropSelection {
287                enabled: drag_and_drop_selection.enabled.unwrap(),
288                delay: drag_and_drop_selection.delay.unwrap(),
289            },
290            lsp_document_colors: editor.lsp_document_colors.unwrap(),
291            minimum_contrast_for_highlights: editor.minimum_contrast_for_highlights.unwrap().0,
292            completion_menu_scrollbar: editor
293                .completion_menu_scrollbar
294                .map(ui_scrollbar_settings_from_raw)
295                .unwrap(),
296            completion_detail_alignment: editor.completion_detail_alignment.unwrap(),
297            diff_view_style: editor.diff_view_style.unwrap(),
298            minimum_split_diff_width: editor.minimum_split_diff_width.unwrap(),
299        }
300    }
301}
302
303#[derive(Default)]
304pub struct EditorSettingsScrollbarProxy;
305
306impl ui::scrollbars::ScrollbarVisibility for EditorSettingsScrollbarProxy {
307    fn visibility(&self, cx: &App) -> ShowScrollbar {
308        EditorSettings::get_global(cx).scrollbar.show
309    }
310}
311
312pub fn ui_scrollbar_settings_from_raw(
313    value: settings::ShowScrollbar,
314) -> ui::scrollbars::ShowScrollbar {
315    match value {
316        settings::ShowScrollbar::Auto => ShowScrollbar::Auto,
317        settings::ShowScrollbar::System => ShowScrollbar::System,
318        settings::ShowScrollbar::Always => ShowScrollbar::Always,
319        settings::ShowScrollbar::Never => ShowScrollbar::Never,
320    }
321}