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 StatusBar {
 69    /// Whether to display the active language button in the status bar.
 70    ///
 71    /// Default: true
 72    pub active_language_button: bool,
 73    /// Whether to show the cursor position button in the status bar.
 74    ///
 75    /// Default: true
 76    pub cursor_position_button: 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    pub whole_word: bool,
171    pub case_sensitive: bool,
172    pub include_ignored: bool,
173    pub regex: bool,
174}
175
176impl EditorSettings {
177    pub fn jupyter_enabled(cx: &App) -> bool {
178        EditorSettings::get_global(cx).jupyter.enabled
179    }
180}
181
182impl ScrollbarVisibility for EditorSettings {
183    fn visibility(&self, _cx: &App) -> ShowScrollbar {
184        self.scrollbar.show
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        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(Into::into).unwrap(),
216                git_diff: scrollbar.git_diff.unwrap(),
217                selected_text: scrollbar.selected_text.unwrap(),
218                selected_symbol: scrollbar.selected_symbol.unwrap(),
219                search_results: scrollbar.search_results.unwrap(),
220                diagnostics: scrollbar.diagnostics.unwrap(),
221                cursors: scrollbar.cursors.unwrap(),
222                axes: ScrollbarAxes {
223                    horizontal: axes.horizontal.unwrap(),
224                    vertical: axes.vertical.unwrap(),
225                },
226            },
227            minimap: Minimap {
228                show: minimap.show.unwrap(),
229                display_in: minimap.display_in.unwrap(),
230                thumb: minimap.thumb.unwrap(),
231                thumb_border: minimap.thumb_border.unwrap(),
232                current_line_highlight: minimap.current_line_highlight,
233                max_width_columns: minimap.max_width_columns.unwrap(),
234            },
235            gutter: Gutter {
236                min_line_number_digits: gutter.min_line_number_digits.unwrap(),
237                line_numbers: gutter.line_numbers.unwrap(),
238                runnables: gutter.runnables.unwrap(),
239                breakpoints: gutter.breakpoints.unwrap(),
240                folds: gutter.folds.unwrap(),
241            },
242            scroll_beyond_last_line: editor.scroll_beyond_last_line.unwrap(),
243            vertical_scroll_margin: editor.vertical_scroll_margin.unwrap() as f64,
244            autoscroll_on_clicks: editor.autoscroll_on_clicks.unwrap(),
245            horizontal_scroll_margin: editor.horizontal_scroll_margin.unwrap(),
246            scroll_sensitivity: editor.scroll_sensitivity.unwrap(),
247            fast_scroll_sensitivity: editor.fast_scroll_sensitivity.unwrap(),
248            relative_line_numbers: editor.relative_line_numbers.unwrap(),
249            seed_search_query_from_cursor: editor.seed_search_query_from_cursor.unwrap(),
250            use_smartcase_search: editor.use_smartcase_search.unwrap(),
251            multi_cursor_modifier: editor.multi_cursor_modifier.unwrap(),
252            redact_private_values: editor.redact_private_values.unwrap(),
253            expand_excerpt_lines: editor.expand_excerpt_lines.unwrap(),
254            excerpt_context_lines: editor.excerpt_context_lines.unwrap(),
255            middle_click_paste: editor.middle_click_paste.unwrap(),
256            double_click_in_multibuffer: editor.double_click_in_multibuffer.unwrap(),
257            search_wrap: editor.search_wrap.unwrap(),
258            search: SearchSettings {
259                button: search.button.unwrap(),
260                whole_word: search.whole_word.unwrap(),
261                case_sensitive: search.case_sensitive.unwrap(),
262                include_ignored: search.include_ignored.unwrap(),
263                regex: search.regex.unwrap(),
264            },
265            auto_signature_help: editor.auto_signature_help.unwrap(),
266            show_signature_help_after_edits: editor.show_signature_help_after_edits.unwrap(),
267            go_to_definition_fallback: editor.go_to_definition_fallback.unwrap(),
268            jupyter: Jupyter {
269                enabled: editor.jupyter.unwrap().enabled.unwrap(),
270            },
271            hide_mouse: editor.hide_mouse,
272            snippet_sort_order: editor.snippet_sort_order.unwrap(),
273            diagnostics_max_severity: editor.diagnostics_max_severity.map(Into::into),
274            inline_code_actions: editor.inline_code_actions.unwrap(),
275            drag_and_drop_selection: DragAndDropSelection {
276                enabled: drag_and_drop_selection.enabled.unwrap(),
277                delay: drag_and_drop_selection.delay.unwrap(),
278            },
279            lsp_document_colors: editor.lsp_document_colors.unwrap(),
280            minimum_contrast_for_highlights: editor.minimum_contrast_for_highlights.unwrap().0,
281        }
282    }
283}