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