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