editor_settings.rs

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