editor_settings.rs

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