diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 7178f7e3e4f31aa9fefd3c080a82c5099a934311..05625d2f4e4e66de5c9fe55f62a02eef5d874df9 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -202,7 +202,6 @@ use ui::{ IconSize, Indicator, Key, Tooltip, h_flex, prelude::*, scrollbars::ScrollbarAutoHide, }; use util::{RangeExt, ResultExt, TryFutureExt, maybe, post_inc}; -use vim_mode_setting::VimModeSetting; use workspace::{ CollaboratorId, Item as WorkspaceItem, ItemId, ItemNavHistory, OpenInTerminal, OpenTerminal, RestoreOnStartupBehavior, SERIALIZATION_THROTTLE_TIME, SplitDirection, TabBarSettings, Toast, @@ -1111,6 +1110,9 @@ pub struct Editor { pending_rename: Option, searchable: bool, cursor_shape: CursorShape, + /// Whether the cursor is offset one character to the left when something is + /// selected (needed for vim visual mode) + cursor_offset_on_selection: bool, current_line_highlight: Option, pub collapse_matches: bool, autoindent_mode: Option, @@ -2286,6 +2288,7 @@ impl Editor { cursor_shape: EditorSettings::get_global(cx) .cursor_shape .unwrap_or_default(), + cursor_offset_on_selection: false, current_line_highlight: None, autoindent_mode: Some(AutoindentMode::EachLine), collapse_matches: false, @@ -2472,7 +2475,10 @@ impl Editor { } } EditorEvent::Edited { .. } => { - if !editor.is_vim_mode_enabled(cx) { + let vim_mode = vim_mode_setting::VimModeSetting::try_get(cx) + .map(|vim_mode| vim_mode.0) + .unwrap_or(false); + if !vim_mode { let display_map = editor.display_snapshot(cx); let selections = editor.selections.all_adjusted_display(&display_map); let pop_state = editor @@ -3101,6 +3107,10 @@ impl Editor { self.cursor_shape } + pub fn set_cursor_offset_on_selection(&mut self, set_cursor_offset_on_selection: bool) { + self.cursor_offset_on_selection = set_cursor_offset_on_selection; + } + pub fn set_current_line_highlight( &mut self, current_line_highlight: Option, @@ -22644,7 +22654,10 @@ impl Editor { .and_then(|e| e.to_str()) .map(|a| a.to_string())); - let vim_mode_enabled = self.is_vim_mode_enabled(cx); + let vim_mode = vim_mode_setting::VimModeSetting::try_get(cx) + .map(|vim_mode| vim_mode.0) + .unwrap_or(false); + let edit_predictions_provider = all_language_settings(file, cx).edit_predictions.provider; let copilot_enabled = edit_predictions_provider == language::language_settings::EditPredictionProvider::Copilot; @@ -22662,7 +22675,7 @@ impl Editor { event_type, type = if auto_saved {"autosave"} else {"manual"}, file_extension, - vim_mode_enabled, + vim_mode, copilot_enabled, copilot_enabled_for_language, edit_predictions_provider, @@ -22672,7 +22685,7 @@ impl Editor { telemetry::event!( event_type, file_extension, - vim_mode_enabled, + vim_mode, copilot_enabled, copilot_enabled_for_language, edit_predictions_provider, @@ -23287,14 +23300,6 @@ impl Editor { show_underlines: self.diagnostics_enabled(), } } - - /// Returns the value of the `vim_mode` setting, defaulting `false` if the - /// setting is not set. - pub(crate) fn is_vim_mode_enabled(&self, cx: &App) -> bool { - VimModeSetting::try_get(cx) - .map(|vim_mode| vim_mode.0) - .unwrap_or(false) - } } fn edit_for_markdown_paste<'a>( diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index efb0459b15b7b7e19a485a81753d39d7dd20b5de..8de660275ba9b455aec610568c41347888654495 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -133,7 +133,7 @@ impl SelectionLayout { fn new( selection: Selection, line_mode: bool, - vim_mode_enabled: bool, + cursor_offset: bool, cursor_shape: CursorShape, map: &DisplaySnapshot, is_newest: bool, @@ -154,7 +154,7 @@ impl SelectionLayout { } // any vim visual mode (including line mode) - if vim_mode_enabled && !range.is_empty() && !selection.reversed { + if cursor_offset && !range.is_empty() && !selection.reversed { if head.column() > 0 { head = map.clip_point(DisplayPoint::new(head.row(), head.column() - 1), Bias::Left); } else if head.row().0 > 0 && head != map.max_point() { @@ -1463,7 +1463,7 @@ impl EditorElement { let layout = SelectionLayout::new( selection, editor.selections.line_mode(), - editor.is_vim_mode_enabled(cx), + editor.cursor_offset_on_selection, editor.cursor_shape, &snapshot.display_snapshot, is_newest, @@ -1510,7 +1510,7 @@ impl EditorElement { let drag_cursor_layout = SelectionLayout::new( drop_cursor.clone(), false, - editor.is_vim_mode_enabled(cx), + editor.cursor_offset_on_selection, CursorShape::Bar, &snapshot.display_snapshot, false, @@ -1574,7 +1574,7 @@ impl EditorElement { .push(SelectionLayout::new( selection.selection, selection.line_mode, - editor.is_vim_mode_enabled(cx), + editor.cursor_offset_on_selection, selection.cursor_shape, &snapshot.display_snapshot, false, @@ -1585,7 +1585,8 @@ impl EditorElement { selections.extend(remote_selections.into_values()); } else if !editor.is_focused(window) && editor.show_cursor_when_unfocused { - let player = editor.current_user_player_color(cx); + let cursor_offset_on_selection = editor.cursor_offset_on_selection; + let layouts = snapshot .buffer_snapshot() .selections_in_range(&(start_anchor..end_anchor), true) @@ -1593,7 +1594,7 @@ impl EditorElement { SelectionLayout::new( selection, line_mode, - editor.is_vim_mode_enabled(cx), + cursor_offset_on_selection, cursor_shape, &snapshot.display_snapshot, false, @@ -1602,7 +1603,7 @@ impl EditorElement { ) }) .collect::>(); - + let player = editor.current_user_player_color(cx); selections.push((player, layouts)); } }); @@ -3317,7 +3318,7 @@ impl EditorElement { SelectionLayout::new( newest, editor.selections.line_mode(), - editor.is_vim_mode_enabled(cx), + editor.cursor_offset_on_selection, editor.cursor_shape, &snapshot.display_snapshot, true, @@ -11548,7 +11549,6 @@ mod tests { use log::info; use std::num::NonZeroU32; use util::test::sample_text; - use vim_mode_setting::VimModeSetting; #[gpui::test] async fn test_soft_wrap_editor_width_auto_height_editor(cx: &mut TestAppContext) { @@ -11893,12 +11893,6 @@ mod tests { async fn test_vim_visual_selections(cx: &mut TestAppContext) { init_test(cx, |_| {}); - // Enable `vim_mode` setting so the logic that checks whether this is - // enabled can work as expected. - cx.update(|cx| { - VimModeSetting::override_global(VimModeSetting(true), cx); - }); - 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) @@ -11909,6 +11903,7 @@ mod tests { window .update(cx, |editor, window, cx| { + editor.cursor_offset_on_selection = true; editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| { s.select_ranges([ Point::new(0, 0)..Point::new(1, 0), diff --git a/crates/vim/src/vim.rs b/crates/vim/src/vim.rs index 9a9a1a001c32fcf8b22892ce5300d8d2aec3dd37..26fec968fb261fbb80a9f84211357623147ca0f4 100644 --- a/crates/vim/src/vim.rs +++ b/crates/vim/src/vim.rs @@ -1943,6 +1943,7 @@ impl Vim { editor.set_collapse_matches(collapse_matches); editor.set_input_enabled(vim.editor_input_enabled()); editor.set_autoindent(vim.should_autoindent()); + editor.set_cursor_offset_on_selection(vim.mode.is_visual()); editor .selections .set_line_mode(matches!(vim.mode, Mode::VisualLine));