editor: Refactor EditorMode::Full (#28546)

This PR lightly refactors the `EditorMode::Full` exposing two new
methods: `is_full` and `set_mode`.

Motivation is to expose fields that modify the behavior when the editor
is in `Full` mode. By using is `mode.is_full()` instead of
`EditorMode::Full` we can introduce new fields without breaking other
places in the code.

Release Notes:

- N/A

Co-authored-by: Bennet Bo Fenner <bennetbo@gmx.de>
This commit is contained in:
Danilo Leal 2025-04-10 20:22:27 -03:00 committed by GitHub
parent 294a1b63c0
commit a2a3d1a4bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 75 additions and 61 deletions

View file

@ -874,7 +874,7 @@ mod tests {
let editor = workspace.update_in(&mut cx, |workspace, window, cx| {
let editor = cx.new(|cx| {
Editor::new(
editor::EditorMode::Full,
editor::EditorMode::full(),
multi_buffer::MultiBuffer::build_simple("", cx),
None,
window,

View file

@ -1172,7 +1172,7 @@ async fn test_send_breakpoints_when_editor_has_been_saved(
let (editor, cx) = cx.add_window_view(|window, cx| {
Editor::new(
EditorMode::Full,
EditorMode::full(),
MultiBuffer::build_from_buffer(buffer, cx),
Some(project.clone()),
window,
@ -1347,7 +1347,7 @@ async fn test_unsetting_breakpoints_on_clear_breakpoint_action(
let (first_editor, cx) = cx.add_window_view(|window, cx| {
Editor::new(
EditorMode::Full,
EditorMode::full(),
MultiBuffer::build_from_buffer(first, cx),
Some(project.clone()),
window,
@ -1357,7 +1357,7 @@ async fn test_unsetting_breakpoints_on_clear_breakpoint_action(
let (second_editor, cx) = cx.add_window_view(|window, cx| {
Editor::new(
EditorMode::Full,
EditorMode::full(),
MultiBuffer::build_from_buffer(second, cx),
Some(project.clone()),
window,

View file

@ -401,6 +401,16 @@ pub enum EditorMode {
Full,
}
impl EditorMode {
pub fn full() -> Self {
Self::Full
}
pub fn is_full(&self) -> bool {
matches!(self, Self::Full { .. })
}
}
#[derive(Copy, Clone, Debug)]
pub enum SoftWrap {
/// Prefer not to wrap at all.
@ -1198,7 +1208,7 @@ impl Editor {
pub fn multi_line(window: &mut Window, cx: &mut Context<Self>) -> Self {
let buffer = cx.new(|cx| Buffer::local("", cx));
let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
Self::new(EditorMode::Full, buffer, None, window, cx)
Self::new(EditorMode::full(), buffer, None, window, cx)
}
pub fn auto_width(window: &mut Window, cx: &mut Context<Self>) -> Self {
@ -1232,7 +1242,7 @@ impl Editor {
cx: &mut Context<Self>,
) -> Self {
let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
Self::new(EditorMode::Full, buffer, project, window, cx)
Self::new(EditorMode::full(), buffer, project, window, cx)
}
pub fn for_multibuffer(
@ -1241,7 +1251,7 @@ impl Editor {
window: &mut Window,
cx: &mut Context<Self>,
) -> Self {
Self::new(EditorMode::Full, buffer, project, window, cx)
Self::new(EditorMode::full(), buffer, project, window, cx)
}
pub fn clone(&self, window: &mut Window, cx: &mut Context<Self>) -> Self {
@ -1329,7 +1339,7 @@ impl Editor {
.then(|| language_settings::SoftWrap::None);
let mut project_subscriptions = Vec::new();
if mode == EditorMode::Full {
if mode.is_full() {
if let Some(project) = project.as_ref() {
project_subscriptions.push(cx.subscribe_in(
project,
@ -1417,7 +1427,7 @@ impl Editor {
};
let breakpoint_store = match (mode, project.as_ref()) {
(EditorMode::Full, Some(project)) => Some(project.read(cx).breakpoint_store()),
(EditorMode::Full { .. }, Some(project)) => Some(project.read(cx).breakpoint_store()),
_ => None,
};
@ -1468,7 +1478,7 @@ impl Editor {
show_scrollbars: true,
mode,
show_breadcrumbs: EditorSettings::get_global(cx).toolbar.breadcrumbs,
show_gutter: mode == EditorMode::Full,
show_gutter: mode.is_full(),
show_line_numbers: None,
use_relative_line_numbers: None,
show_git_diff_gutter: None,
@ -1510,7 +1520,7 @@ impl Editor {
collapse_matches: false,
workspace: None,
input_enabled: true,
use_modal_editing: mode == EditorMode::Full,
use_modal_editing: mode.is_full(),
read_only: false,
use_autoclose: true,
use_auto_surround: true,
@ -1527,7 +1537,7 @@ impl Editor {
edit_prediction_preview: EditPredictionPreview::Inactive {
released_too_fast: false,
},
inline_diagnostics_enabled: mode == EditorMode::Full,
inline_diagnostics_enabled: mode.is_full(),
inlay_hint_cache: InlayHintCache::new(inlay_hint_settings),
gutter_hovered: false,
@ -1635,7 +1645,7 @@ impl Editor {
this.scroll_manager.show_scrollbars(window, cx);
jsx_tag_auto_close::refresh_enabled_in_any_buffer(&mut this, &buffer, cx);
if mode == EditorMode::Full {
if mode.is_full() {
let should_auto_hide_scrollbars = cx.should_auto_hide_scrollbars();
cx.set_global(ScrollbarAutoHide(should_auto_hide_scrollbars));
@ -1697,7 +1707,7 @@ impl Editor {
let mode = match self.mode {
EditorMode::SingleLine { .. } => "single_line",
EditorMode::AutoHeight { .. } => "auto_height",
EditorMode::Full => "full",
EditorMode::Full { .. } => "full",
};
if EditorSettings::jupyter_enabled(cx) {
@ -1984,6 +1994,10 @@ impl Editor {
self.mode
}
pub fn set_mode(&mut self, mode: EditorMode) {
self.mode = mode;
}
pub fn collaboration_hub(&self) -> Option<&dyn CollaborationHub> {
self.collaboration_hub.as_deref()
}
@ -3006,7 +3020,7 @@ impl Editor {
return;
}
if self.mode == EditorMode::Full
if self.mode.is_full()
&& self.change_selections(Some(Autoscroll::fit()), window, cx, |s| s.try_cancel())
{
return;
@ -3049,7 +3063,7 @@ impl Editor {
return true;
}
if self.mode == EditorMode::Full && self.active_diagnostics.is_some() {
if self.mode.is_full() && self.active_diagnostics.is_some() {
self.dismiss_diagnostics(cx);
return true;
}
@ -4037,7 +4051,7 @@ impl Editor {
}
fn refresh_inlay_hints(&mut self, reason: InlayHintRefreshReason, cx: &mut Context<Self>) {
if self.semantics_provider.is_none() || self.mode != EditorMode::Full {
if self.semantics_provider.is_none() || !self.mode.is_full() {
return;
}
@ -5545,7 +5559,7 @@ impl Editor {
buffer_position: language::Anchor,
cx: &App,
) -> EditPredictionSettings {
if self.mode != EditorMode::Full
if !self.mode.is_full()
|| !self.show_inline_completions_override.unwrap_or(true)
|| self.inline_completions_disabled_in_scope(buffer, buffer_position, cx)
{
@ -17258,7 +17272,7 @@ impl Editor {
let project_settings = ProjectSettings::get_global(cx);
self.serialize_dirty_buffers = project_settings.session.restore_unsaved_buffers;
if self.mode == EditorMode::Full {
if self.mode.is_full() {
let show_inline_diagnostics = project_settings.diagnostics.inline.enabled;
let inline_blame_enabled = project_settings.git.inline_blame_enabled();
if self.show_inline_diagnostics != show_inline_diagnostics {
@ -19542,7 +19556,7 @@ impl Render for Editor {
line_height: relative(settings.buffer_line_height.value()),
..Default::default()
},
EditorMode::Full => TextStyle {
EditorMode::Full { .. } => TextStyle {
color: cx.theme().colors().editor_foreground,
font_family: settings.buffer_font.family.clone(),
font_features: settings.buffer_font.features.clone(),
@ -19560,7 +19574,7 @@ impl Render for Editor {
let background = match self.mode {
EditorMode::SingleLine { .. } => cx.theme().system().transparent,
EditorMode::AutoHeight { max_lines: _ } => cx.theme().system().transparent,
EditorMode::Full => cx.theme().colors().editor_background,
EditorMode::Full { .. } => cx.theme().colors().editor_background,
};
EditorElement::new(

View file

@ -8053,7 +8053,7 @@ async fn test_multibuffer_format_during_save(cx: &mut TestAppContext) {
});
let multi_buffer_editor = cx.new_window_entity(|window, cx| {
Editor::new(
EditorMode::Full,
EditorMode::full(),
multi_buffer,
Some(project.clone()),
window,
@ -14500,7 +14500,7 @@ async fn test_mutlibuffer_in_navigation_history(cx: &mut TestAppContext) {
let cx = &mut VisualTestContext::from_window(*workspace.deref(), cx);
let multi_buffer_editor = cx.new_window_entity(|window, cx| {
Editor::new(
EditorMode::Full,
EditorMode::full(),
multi_buffer,
Some(project.clone()),
window,
@ -14959,7 +14959,7 @@ async fn test_toggle_diff_expand_in_multi_buffer(cx: &mut TestAppContext) {
});
let editor =
cx.add_window(|window, cx| Editor::new(EditorMode::Full, multi_buffer, None, window, cx));
cx.add_window(|window, cx| Editor::new(EditorMode::full(), multi_buffer, None, window, cx));
editor
.update(cx, |editor, _window, cx| {
for (buffer, diff_base) in [
@ -15070,7 +15070,7 @@ async fn test_expand_diff_hunk_at_excerpt_boundary(cx: &mut TestAppContext) {
});
let editor =
cx.add_window(|window, cx| Editor::new(EditorMode::Full, multi_buffer, None, window, cx));
cx.add_window(|window, cx| Editor::new(EditorMode::full(), multi_buffer, None, window, cx));
editor
.update(cx, |editor, _window, cx| {
let diff = cx.new(|cx| BufferDiff::new_with_base_text(base, &buffer, cx));
@ -16626,7 +16626,7 @@ async fn test_display_diff_hunks(cx: &mut TestAppContext) {
});
let editor = cx.add_window(|window, cx| {
Editor::new(EditorMode::Full, multibuffer, Some(project), window, cx)
Editor::new(EditorMode::full(), multibuffer, Some(project), window, cx)
});
cx.run_until_parked();
@ -17143,7 +17143,7 @@ async fn test_find_enclosing_node_with_task(cx: &mut TestAppContext) {
let editor = cx.new_window_entity(|window, cx| {
Editor::new(
EditorMode::Full,
EditorMode::full(),
multi_buffer,
Some(project.clone()),
window,
@ -17270,7 +17270,7 @@ async fn test_folding_buffers(cx: &mut TestAppContext) {
});
let multi_buffer_editor = cx.new_window_entity(|window, cx| {
Editor::new(
EditorMode::Full,
EditorMode::full(),
multi_buffer.clone(),
Some(project.clone()),
window,
@ -17427,7 +17427,7 @@ async fn test_folding_buffers_with_one_excerpt(cx: &mut TestAppContext) {
let multi_buffer_editor = cx.new_window_entity(|window, cx| {
Editor::new(
EditorMode::Full,
EditorMode::full(),
multi_buffer,
Some(project.clone()),
window,
@ -17545,7 +17545,7 @@ async fn test_folding_buffer_when_multibuffer_has_only_one_excerpt(cx: &mut Test
});
let multi_buffer_editor = cx.new_window_entity(|window, cx| {
Editor::new(
EditorMode::Full,
EditorMode::full(),
multi_buffer,
Some(project.clone()),
window,
@ -17595,7 +17595,7 @@ async fn test_multi_buffer_navigation_with_folded_buffers(cx: &mut TestAppContex
],
cx,
);
let mut editor = Editor::new(EditorMode::Full, multi_buffer.clone(), None, window, cx);
let mut editor = Editor::new(EditorMode::full(), multi_buffer.clone(), None, window, cx);
let buffer_ids = multi_buffer.read(cx).excerpt_buffer_ids();
// fold all but the second buffer, so that we test navigating between two
@ -17907,7 +17907,7 @@ async fn assert_highlighted_edits(
) {
let window = cx.add_window(|window, cx| {
let buffer = MultiBuffer::build_simple(text, cx);
Editor::new(EditorMode::Full, buffer, None, window, cx)
Editor::new(EditorMode::full(), buffer, None, window, cx)
});
let cx = &mut VisualTestContext::from_window(*window, cx);
@ -18065,7 +18065,7 @@ async fn test_breakpoint_toggling(cx: &mut TestAppContext) {
let (editor, cx) = cx.add_window_view(|window, cx| {
Editor::new(
EditorMode::Full,
EditorMode::full(),
MultiBuffer::build_from_buffer(buffer, cx),
Some(project.clone()),
window,
@ -18182,7 +18182,7 @@ async fn test_log_breakpoint_editing(cx: &mut TestAppContext) {
let (editor, cx) = cx.add_window_view(|window, cx| {
Editor::new(
EditorMode::Full,
EditorMode::full(),
MultiBuffer::build_from_buffer(buffer, cx),
Some(project.clone()),
window,
@ -18357,7 +18357,7 @@ async fn test_breakpoint_enabling_and_disabling(cx: &mut TestAppContext) {
let (editor, cx) = cx.add_window_view(|window, cx| {
Editor::new(
EditorMode::Full,
EditorMode::full(),
MultiBuffer::build_from_buffer(buffer, cx),
Some(project.clone()),
window,

View file

@ -1402,7 +1402,7 @@ impl EditorElement {
window: &mut Window,
cx: &mut App,
) -> Option<EditorScrollbars> {
if snapshot.mode != EditorMode::Full {
if !snapshot.mode.is_full() {
return None;
}
@ -2371,7 +2371,7 @@ impl EditorElement {
cx: &mut App,
) -> Arc<HashMap<MultiBufferRow, LineNumberLayout>> {
let include_line_numbers = snapshot.show_line_numbers.unwrap_or_else(|| {
EditorSettings::get_global(cx).gutter.line_numbers && snapshot.mode == EditorMode::Full
EditorSettings::get_global(cx).gutter.line_numbers && snapshot.mode.is_full()
});
if !include_line_numbers {
return Arc::default();
@ -2477,7 +2477,7 @@ impl EditorElement {
cx: &mut App,
) -> Vec<Option<AnyElement>> {
let include_fold_statuses = EditorSettings::get_global(cx).gutter.folds
&& snapshot.mode == EditorMode::Full
&& snapshot.mode.is_full()
&& self.editor.read(cx).is_singleton(cx);
if include_fold_statuses {
row_infos
@ -4176,7 +4176,7 @@ impl EditorElement {
self.style.background,
));
if let EditorMode::Full = layout.mode {
if let EditorMode::Full { .. } = layout.mode {
let mut active_rows = layout.active_rows.iter().peekable();
while let Some((start_row, contains_non_empty_selection)) = active_rows.next() {
let mut end_row = start_row.0;
@ -6059,7 +6059,7 @@ impl LineWithInvisibles {
strikethrough: text_style.strikethrough,
});
if editor_mode == EditorMode::Full {
if editor_mode.is_full() {
// Line wrap pads its contents with fake whitespaces,
// avoid printing them
let is_soft_wrapped = is_row_soft_wrapped(row);
@ -6414,7 +6414,7 @@ impl EditorElement {
/// This allows UI elements to scale based on the `buffer_font_size`.
fn rem_size(&self, cx: &mut App) -> Option<Pixels> {
match self.editor.read(cx).mode {
EditorMode::Full => {
EditorMode::Full { .. } => {
let buffer_font_size = self.style.text.font_size;
match buffer_font_size {
AbsoluteLength::Pixels(pixels) => {
@ -6531,7 +6531,7 @@ impl Element for EditorElement {
},
)
}
EditorMode::Full => {
EditorMode::Full { .. } => {
let mut style = Style::default();
style.size.width = relative(1.).into();
style.size.height = relative(1.).into();
@ -8507,7 +8507,7 @@ mod tests {
init_test(cx, |_| {});
let window = cx.add_window(|window, cx| {
let buffer = MultiBuffer::build_simple(&sample_text(6, 6, 'a'), cx);
Editor::new(EditorMode::Full, buffer, None, window, cx)
Editor::new(EditorMode::full(), buffer, None, window, cx)
});
let editor = window.root(cx).unwrap();
@ -8608,7 +8608,7 @@ mod tests {
let window = cx.add_window(|window, cx| {
let buffer = MultiBuffer::build_simple(&(sample_text(6, 6, 'a') + "\n"), cx);
Editor::new(EditorMode::Full, buffer, None, window, cx)
Editor::new(EditorMode::full(), buffer, None, window, cx)
});
let cx = &mut VisualTestContext::from_window(*window, cx);
let editor = window.root(cx).unwrap();
@ -8679,7 +8679,7 @@ mod tests {
let window = cx.add_window(|window, cx| {
let buffer = MultiBuffer::build_simple("", cx);
Editor::new(EditorMode::Full, buffer, None, window, cx)
Editor::new(EditorMode::full(), buffer, None, window, cx)
});
let cx = &mut VisualTestContext::from_window(*window, cx);
let editor = window.root(cx).unwrap();
@ -8765,7 +8765,7 @@ mod tests {
let actual_invisibles = collect_invisibles_from_new_editor(
cx,
EditorMode::Full,
EditorMode::full(),
input_text,
px(500.0),
show_line_numbers,
@ -8859,7 +8859,7 @@ mod tests {
let actual_invisibles = collect_invisibles_from_new_editor(
cx,
EditorMode::Full,
EditorMode::full(),
&input_text,
px(editor_width),
show_line_numbers,

View file

@ -1,6 +1,6 @@
use crate::{
Copy, CopyAndTrim, CopyPermalinkToLine, Cut, DebuggerEvaluateSelectedText, DisplayPoint,
DisplaySnapshot, Editor, EditorMode, FindAllReferences, GoToDeclaration, GoToDefinition,
DisplaySnapshot, Editor, FindAllReferences, GoToDeclaration, GoToDefinition,
GoToImplementation, GoToTypeDefinition, Paste, Rename, RevealInFileManager, SelectMode,
ToDisplayPoint, ToggleCodeActions,
actions::{Format, FormatSelections},
@ -123,7 +123,7 @@ pub fn deploy_context_menu(
}
// Don't show context menu for inline editors
if editor.mode() != EditorMode::Full {
if !editor.mode().is_full() {
return;
}

View file

@ -108,7 +108,7 @@ pub(crate) fn build_editor(
window: &mut Window,
cx: &mut Context<Editor>,
) -> Editor {
Editor::new(EditorMode::Full, buffer, None, window, cx)
Editor::new(EditorMode::full(), buffer, None, window, cx)
}
pub(crate) fn build_editor_with_project(
@ -117,5 +117,5 @@ pub(crate) fn build_editor_with_project(
window: &mut Window,
cx: &mut Context<Editor>,
) -> Editor {
Editor::new(EditorMode::Full, buffer, Some(project), window, cx)
Editor::new(EditorMode::full(), buffer, Some(project), window, cx)
}

View file

@ -95,7 +95,7 @@ impl CursorPosition {
cursor_position.position = None;
cursor_position.context = None;
}
editor::EditorMode::Full => {
editor::EditorMode::Full { .. } => {
let mut last_selection = None::<Selection<Point>>;
let snapshot = editor.buffer().read(cx).snapshot(cx);
if snapshot.excerpts().count() > 0 {

View file

@ -4,7 +4,7 @@ use std::{
sync::Arc,
};
use editor::{Anchor, AnchorRangeExt, Editor, EditorMode, scroll::Autoscroll};
use editor::{Anchor, AnchorRangeExt, Editor, scroll::Autoscroll};
use fuzzy::StringMatch;
use gpui::{
App, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, HighlightStyle,
@ -87,7 +87,7 @@ impl Render for OutlineView {
impl OutlineView {
fn register(editor: &mut Editor, _: Option<&mut Window>, cx: &mut Context<Editor>) {
if editor.mode() == EditorMode::Full {
if editor.mode().is_full() {
let handle = cx.entity().downgrade();
editor
.register_action(move |action, window, cx| {

View file

@ -18,7 +18,7 @@ use anyhow::Context as _;
use collections::{BTreeSet, HashMap, HashSet, hash_map};
use db::kvp::KEY_VALUE_STORE;
use editor::{
AnchorRangeExt, Bias, DisplayPoint, Editor, EditorEvent, EditorMode, EditorSettings, ExcerptId,
AnchorRangeExt, Bias, DisplayPoint, Editor, EditorEvent, EditorSettings, ExcerptId,
ExcerptRange, MultiBufferSnapshot, RangeToAnchorExt, ShowScrollbar,
display_map::ToDisplayPoint,
items::{entry_git_aware_label_color, entry_label_color},
@ -4725,7 +4725,7 @@ fn workspace_active_editor(
let active_item = workspace.active_item(cx)?;
let active_editor = active_item
.act_as::<Editor>(cx)
.filter(|editor| editor.read(cx).mode() == EditorMode::Full)?;
.filter(|editor| editor.read(cx).mode().is_full())?;
Some((active_item, active_editor))
}

View file

@ -1729,7 +1729,7 @@ async fn test_folded_multibuffer_excerpts(cx: &mut gpui::TestAppContext) {
],
cx,
);
let mut editor = Editor::new(EditorMode::Full, multi_buffer.clone(), None, window, cx);
let mut editor = Editor::new(EditorMode::full(), multi_buffer.clone(), None, window, cx);
let buffer_ids = multi_buffer.read(cx).excerpt_buffer_ids();
// fold all but the second buffer, so that we test navigating between two

View file

@ -22,7 +22,7 @@ mod visual;
use anyhow::Result;
use collections::HashMap;
use editor::{
Anchor, Bias, Editor, EditorEvent, EditorMode, EditorSettings, HideMouseCursorOrigin, ToPoint,
Anchor, Bias, Editor, EditorEvent, EditorSettings, HideMouseCursorOrigin, ToPoint,
movement::{self, FindRange},
};
use gpui::{
@ -1127,7 +1127,7 @@ impl Vim {
let editor = editor.read(cx);
let editor_mode = editor.mode();
if editor_mode == EditorMode::Full
if editor_mode.is_full()
&& !newest_selection_empty
&& self.mode == Mode::Normal
// When following someone, don't switch vim mode.

View file

@ -1,7 +1,7 @@
use client::{Client, UserStore};
use collections::HashMap;
use copilot::{Copilot, CopilotCompletionProvider};
use editor::{Editor, EditorMode};
use editor::Editor;
use gpui::{AnyWindowHandle, App, AppContext as _, Context, Entity, WeakEntity};
use language::language_settings::{EditPredictionProvider, all_language_settings};
use settings::SettingsStore;
@ -18,7 +18,7 @@ pub fn init(client: Arc<Client>, user_store: Entity<UserStore>, cx: &mut App) {
let client = client.clone();
let user_store = user_store.clone();
move |editor: &mut Editor, window, cx: &mut Context<Editor>| {
if editor.mode() != EditorMode::Full {
if !editor.mode().is_full() {
return;
}