mirror of
https://github.com/openai/codex.git
synced 2026-11-22 07:13:50 +00:00
feat(tui): add vim text object bindings (#24382)
## Why Vim mode currently supports some normal-mode operators and motions, but common text-object combinations like `ciw`, `daw`, `di(`, and quote/bracket variants are still missing. That makes the composer feel incomplete for users who expect operator + text object editing to work inside prompts. Closes #21383. ## What Changed - Add Vim pending-state support for operator/text-object sequences. - Add `c` as a normal-mode operator for text objects, so combinations like `ciw` delete the object and enter insert mode. - Support word, WORD, delimiter, and quote text objects: - `iw`, `aw`, `iW`, `aW` - `i(`, `a(`, `i)`, `a)`, `ib`, `ab` - `i[`, `a[`, `i]`, `a]` - `i{`, `a{`, `i}`, `a}`, `iB`, `aB` - `i"`, `a"`, `i'`, `a'`, `i\``, `a\`` - Add configurable keymap entries and keymap picker coverage for the new Vim text-object context. - Regenerate the config schema and update keymap picker snapshots. ## How to Test Manual smoke test: 1. Start Codex with Vim composer mode enabled. 2. Type a draft such as: ```text alpha beta gamma call(foo[bar], {"x": "hello world"}) say "one \"two\" three" now ``` 3. Put the cursor on `beta`, press `ciw`, and confirm `beta` is removed and the composer enters insert mode. 4. Escape back to normal mode, put the cursor on `gamma`, press `daw`, and confirm `gamma` plus surrounding whitespace is removed. 5. Put the cursor inside `foo[bar]`, press `di[`, and confirm only `bar` is removed. 6. Put the cursor inside `call(...)`, press `da(`, and confirm the whole parenthesized section is removed. 7. Put the cursor inside the quoted text, press `ci"`, and confirm the quote contents are removed and insert mode starts. 8. Verify cancellation does not edit text: press `d` then `Esc`, and press `d` then `i` then `Esc`. Targeted tests: - `cargo test -p codex-tui --lib vim_` - `cargo nextest run -p codex-tui keymap_setup::tests` Additional local checks: - `just write-config-schema` - `just fmt` - `just fix -p codex-tui` - `git diff --check` - `cargo insta pending-snapshots --manifest-path tui/Cargo.toml` Local full-suite note: `just test -p codex-tui` ran to completion. The keymap snapshot failures were expected and accepted. Two unrelated guardian feature-flag tests still fail locally: - `app::tests::update_feature_flags_disabling_guardian_clears_review_policy_and_restores_default` - `app::tests::update_feature_flags_disabling_guardian_clears_manual_review_policy_without_history` `just argument-comment-lint` is currently blocked locally by Bazel analysis before the lint runs because `compiler-rt` has an empty `include/sanitizer/*.h` glob in the local Bazel cache. The touched Rust diff was manually inspected for opaque positional literals.
This commit is contained in:
1 parent
b1cbf622ad
commit
8d398d3c52
12 files changed
+1148
-81
No files matched your search
@@ -16,6 +16,7 @@ use crate::keymap::EditorKeymap;
|
||||
use crate::keymap::RuntimeKeymap;
|
||||
use crate::keymap::VimNormalKeymap;
|
||||
use crate::keymap::VimOperatorKeymap;
|
||||
use crate::keymap::VimTextObjectKeymap;
|
||||
use codex_protocol::user_input::ByteRange;
|
||||
use codex_protocol::user_input::TextElement as UserTextElement;
|
||||
use crossterm::event::KeyCode;
|
||||
@@ -35,6 +36,13 @@ use textwrap::Options;
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
mod vim;
|
||||
use self::vim::VimMode;
|
||||
use self::vim::VimMotion;
|
||||
use self::vim::VimOperator;
|
||||
use self::vim::VimPending;
|
||||
use self::vim::VimTextObjectScope;
|
||||
|
||||
const WORD_SEPARATORS: &str = "`~!@#$%^&*()-=+[{]}\\|;:'\",.<>/?";
|
||||
|
||||
fn is_word_separator(ch: char) -> bool {
|
||||
@@ -101,10 +109,11 @@ pub(crate) struct TextArea {
|
||||
kill_buffer_kind: KillBufferKind,
|
||||
vim_enabled: bool,
|
||||
vim_mode: VimMode,
|
||||
vim_operator: Option<VimOperator>,
|
||||
vim_pending: VimPending,
|
||||
editor_keymap: EditorKeymap,
|
||||
vim_normal_keymap: VimNormalKeymap,
|
||||
vim_operator_keymap: VimOperatorKeymap,
|
||||
vim_text_object_keymap: VimTextObjectKeymap,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -119,14 +128,6 @@ pub(crate) struct TextAreaState {
|
||||
scroll: u16,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum VimMode {
|
||||
/// Normal mode routes printable keys to movement, operators, and mode transitions.
|
||||
Normal,
|
||||
/// Insert mode routes input through the regular editor keymap until Escape is pressed.
|
||||
Insert,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum KillBufferKind {
|
||||
/// Characterwise kills and yanks paste at the cursor.
|
||||
@@ -135,36 +136,6 @@ enum KillBufferKind {
|
||||
Linewise,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum VimOperator {
|
||||
/// Delete the range selected by the next motion or repeated operator key.
|
||||
Delete,
|
||||
/// Copy the range selected by the next motion or repeated operator key.
|
||||
Yank,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum VimMotion {
|
||||
/// Move one atomic boundary to the left.
|
||||
Left,
|
||||
/// Move one atomic boundary to the right.
|
||||
Right,
|
||||
/// Move one visual row up, preserving preferred display column.
|
||||
Up,
|
||||
/// Move one visual row down, preserving preferred display column.
|
||||
Down,
|
||||
/// Move to the start of the next word-like run.
|
||||
WordForward,
|
||||
/// Move to the start of the previous word-like run.
|
||||
WordBackward,
|
||||
/// Move to the end of the current or next word-like run.
|
||||
WordEnd,
|
||||
/// Move to the start of the current line.
|
||||
LineStart,
|
||||
/// Move to the end of the current line.
|
||||
LineEnd,
|
||||
}
|
||||
|
||||
impl TextArea {
|
||||
pub fn new() -> Self {
|
||||
let defaults = RuntimeKeymap::defaults();
|
||||
@@ -179,10 +150,11 @@ impl TextArea {
|
||||
kill_buffer_kind: KillBufferKind::Characterwise,
|
||||
vim_enabled: false,
|
||||
vim_mode: VimMode::Insert,
|
||||
vim_operator: None,
|
||||
vim_pending: VimPending::None,
|
||||
editor_keymap: defaults.editor,
|
||||
vim_normal_keymap: defaults.vim_normal,
|
||||
vim_operator_keymap: defaults.vim_operator,
|
||||
vim_text_object_keymap: defaults.vim_text_object,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -196,6 +168,7 @@ impl TextArea {
|
||||
self.editor_keymap = keymap.editor.clone();
|
||||
self.vim_normal_keymap = keymap.vim_normal.clone();
|
||||
self.vim_operator_keymap = keymap.vim_operator.clone();
|
||||
self.vim_text_object_keymap = keymap.vim_text_object.clone();
|
||||
}
|
||||
|
||||
/// Replace the visible textarea text and clear any existing text elements.
|
||||
@@ -257,7 +230,7 @@ impl TextArea {
|
||||
/// an old `d` or `y` command.
|
||||
pub(crate) fn set_vim_enabled(&mut self, enabled: bool) {
|
||||
self.vim_enabled = enabled;
|
||||
self.vim_operator = None;
|
||||
self.vim_pending = VimPending::None;
|
||||
self.vim_mode = if enabled {
|
||||
VimMode::Normal
|
||||
} else {
|
||||
@@ -293,7 +266,7 @@ impl TextArea {
|
||||
/// This is observable so the composer can avoid stealing the second key of
|
||||
/// `d{motion}` or `y{motion}` for higher-level shortcuts.
|
||||
pub(crate) fn is_vim_operator_pending(&self) -> bool {
|
||||
self.vim_operator.is_some()
|
||||
!matches!(self.vim_pending, VimPending::None)
|
||||
}
|
||||
|
||||
/// Enter Vim insert mode if modal editing is enabled.
|
||||
@@ -304,7 +277,7 @@ impl TextArea {
|
||||
pub(crate) fn enter_vim_insert_mode(&mut self) {
|
||||
if self.vim_enabled {
|
||||
self.vim_mode = VimMode::Insert;
|
||||
self.vim_operator = None;
|
||||
self.vim_pending = VimPending::None;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -317,7 +290,7 @@ impl TextArea {
|
||||
pub(crate) fn enter_vim_normal_mode(&mut self) {
|
||||
if self.vim_enabled {
|
||||
self.vim_mode = VimMode::Normal;
|
||||
self.vim_operator = None;
|
||||
self.vim_pending = VimPending::None;
|
||||
self.preferred_col = None;
|
||||
}
|
||||
}
|
||||
@@ -669,9 +642,17 @@ impl TextArea {
|
||||
}
|
||||
|
||||
fn handle_vim_normal(&mut self, event: KeyEvent) {
|
||||
if let Some(op) = self.vim_operator.take() {
|
||||
self.handle_vim_operator(op, event);
|
||||
return;
|
||||
let pending = std::mem::replace(&mut self.vim_pending, VimPending::None);
|
||||
match pending {
|
||||
VimPending::None => {}
|
||||
VimPending::Operator(op) => {
|
||||
self.handle_vim_operator(op, event);
|
||||
return;
|
||||
}
|
||||
VimPending::TextObject { operator, scope } => {
|
||||
self.handle_vim_text_object(operator, scope, event);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if self.vim_normal_keymap.enter_insert.is_pressed(event) {
|
||||
@@ -776,15 +757,23 @@ impl TextArea {
|
||||
.start_delete_operator
|
||||
.is_pressed(event)
|
||||
{
|
||||
self.vim_operator = Some(VimOperator::Delete);
|
||||
self.vim_pending = VimPending::Operator(VimOperator::Delete);
|
||||
return;
|
||||
}
|
||||
if self.vim_normal_keymap.start_yank_operator.is_pressed(event) {
|
||||
self.vim_operator = Some(VimOperator::Yank);
|
||||
self.vim_pending = VimPending::Operator(VimOperator::Yank);
|
||||
return;
|
||||
}
|
||||
if self
|
||||
.vim_normal_keymap
|
||||
.start_change_operator
|
||||
.is_pressed(event)
|
||||
{
|
||||
self.vim_pending = VimPending::Operator(VimOperator::Change);
|
||||
return;
|
||||
}
|
||||
if self.vim_normal_keymap.cancel_operator.is_pressed(event) {
|
||||
self.vim_operator = None;
|
||||
self.vim_pending = VimPending::None;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -800,14 +789,41 @@ impl TextArea {
|
||||
if self.vim_operator_keymap.cancel.is_pressed(event) {
|
||||
return true;
|
||||
}
|
||||
if let Some(scope) = self.vim_text_object_scope_for_event(event) {
|
||||
self.vim_pending = VimPending::TextObject {
|
||||
operator: op,
|
||||
scope,
|
||||
};
|
||||
return true;
|
||||
}
|
||||
|
||||
if let Some(motion) = self.vim_motion_for_event(event) {
|
||||
if op != VimOperator::Change
|
||||
&& let Some(motion) = self.vim_motion_for_event(event)
|
||||
{
|
||||
self.apply_vim_operator(op, motion);
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn handle_vim_text_object(
|
||||
&mut self,
|
||||
op: VimOperator,
|
||||
scope: VimTextObjectScope,
|
||||
event: KeyEvent,
|
||||
) -> bool {
|
||||
if self.vim_text_object_keymap.cancel.is_pressed(event) {
|
||||
return true;
|
||||
}
|
||||
let Some(object) = self.vim_text_object_for_event(event) else {
|
||||
return false;
|
||||
};
|
||||
if let Some(range) = self.text_object_range(object, scope) {
|
||||
self.apply_vim_operator_to_range(op, range);
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn vim_motion_for_event(&self, event: KeyEvent) -> Option<VimMotion> {
|
||||
if self.vim_operator_keymap.motion_left.is_pressed(event) {
|
||||
return Some(VimMotion::Left);
|
||||
@@ -854,6 +870,18 @@ impl TextArea {
|
||||
match op {
|
||||
VimOperator::Delete => self.kill_range(range),
|
||||
VimOperator::Yank => self.yank_range(range),
|
||||
VimOperator::Change => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_vim_operator_to_range(&mut self, op: VimOperator, range: Range<usize>) {
|
||||
match op {
|
||||
VimOperator::Delete => self.kill_range(range),
|
||||
VimOperator::Yank => self.yank_range(range),
|
||||
VimOperator::Change => {
|
||||
self.kill_range(range);
|
||||
self.vim_mode = VimMode::Insert;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2232,7 +2260,7 @@ mod tests {
|
||||
let mut t = TextArea::new();
|
||||
t.set_vim_enabled(/*enabled*/ true);
|
||||
|
||||
t.input(KeyEvent::new(KeyCode::Char('i'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('z'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
|
||||
|
||||
assert_eq!(t.vim_mode_label(), Some("Normal"));
|
||||
@@ -2399,6 +2427,180 @@ mod tests {
|
||||
assert_eq!(t.kill_buffer, "hello ");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vim_change_inner_word_deletes_word_and_enters_insert() {
|
||||
let mut t = ta_with("hello world");
|
||||
t.set_cursor(/*pos*/ "hello ".len());
|
||||
t.set_vim_enabled(/*enabled*/ true);
|
||||
|
||||
t.input(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('i'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('w'), KeyModifiers::NONE));
|
||||
|
||||
assert_eq!(t.text(), "hello ");
|
||||
assert_eq!(t.kill_buffer, "world");
|
||||
assert_eq!(t.cursor(), "hello ".len());
|
||||
assert_eq!(t.vim_mode_label(), Some("Insert"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vim_word_text_objects_cover_delete_yank_and_big_word() {
|
||||
let mut t = ta_with("hello world");
|
||||
t.set_cursor(/*pos*/ 1);
|
||||
t.set_vim_enabled(/*enabled*/ true);
|
||||
|
||||
t.input(KeyEvent::new(KeyCode::Char('y'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('w'), KeyModifiers::NONE));
|
||||
|
||||
assert_eq!(t.text(), "hello world");
|
||||
assert_eq!(t.kill_buffer, "hello ");
|
||||
assert_eq!(t.vim_mode_label(), Some("Normal"));
|
||||
|
||||
let mut t = ta_with("foo.bar/baz qux");
|
||||
t.set_cursor(/*pos*/ "foo.".len());
|
||||
t.set_vim_enabled(/*enabled*/ true);
|
||||
|
||||
t.input(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('i'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('W'), KeyModifiers::NONE));
|
||||
|
||||
assert_eq!(t.text(), " qux");
|
||||
assert_eq!(t.kill_buffer, "foo.bar/baz");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vim_word_text_objects_accept_cursor_at_word_end() {
|
||||
let mut t = ta_with("hello world");
|
||||
t.set_cursor(/*pos*/ "hello".len());
|
||||
t.set_vim_enabled(/*enabled*/ true);
|
||||
|
||||
t.input(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('w'), KeyModifiers::NONE));
|
||||
|
||||
assert_eq!(t.text(), "world");
|
||||
assert_eq!(t.kill_buffer, "hello ");
|
||||
|
||||
let mut t = ta_with("foo bar");
|
||||
t.set_cursor(t.text().len());
|
||||
t.set_vim_enabled(/*enabled*/ true);
|
||||
|
||||
t.input(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('i'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('W'), KeyModifiers::NONE));
|
||||
|
||||
assert_eq!(t.text(), "foo ");
|
||||
assert_eq!(t.kill_buffer, "bar");
|
||||
assert_eq!(t.cursor(), "foo ".len());
|
||||
assert_eq!(t.vim_mode_label(), Some("Insert"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vim_delimiter_text_objects_select_innermost_pair_and_aliases() {
|
||||
let mut t = ta_with("a(b(c)d)e");
|
||||
t.set_cursor(/*pos*/ "a(b(".len());
|
||||
t.set_vim_enabled(/*enabled*/ true);
|
||||
|
||||
t.input(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('i'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('b'), KeyModifiers::NONE));
|
||||
|
||||
assert_eq!(t.text(), "a(b()d)e");
|
||||
assert_eq!(t.kill_buffer, "c");
|
||||
assert_eq!(t.vim_mode_label(), Some("Insert"));
|
||||
|
||||
let mut t = ta_with("a [b] c");
|
||||
t.set_cursor(/*pos*/ "a [".len());
|
||||
t.set_vim_enabled(/*enabled*/ true);
|
||||
|
||||
t.input(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char(']'), KeyModifiers::NONE));
|
||||
|
||||
assert_eq!(t.text(), "a c");
|
||||
assert_eq!(t.kill_buffer, "[b]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vim_empty_inner_text_objects_are_valid_targets() {
|
||||
let mut t = ta_with("call()");
|
||||
t.set_cursor(/*pos*/ "call(".len());
|
||||
t.set_vim_enabled(/*enabled*/ true);
|
||||
|
||||
t.input(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('i'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('('), KeyModifiers::NONE));
|
||||
|
||||
assert_eq!(t.text(), "call()");
|
||||
assert_eq!(t.kill_buffer, "");
|
||||
assert_eq!(t.cursor(), "call(".len());
|
||||
assert_eq!(t.vim_mode_label(), Some("Insert"));
|
||||
|
||||
let mut t = ta_with(r#"say "" now"#);
|
||||
t.set_cursor(/*pos*/ r#"say ""#.len());
|
||||
t.set_vim_enabled(/*enabled*/ true);
|
||||
|
||||
t.input(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('i'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('"'), KeyModifiers::NONE));
|
||||
|
||||
assert_eq!(t.text(), r#"say "" now"#);
|
||||
assert_eq!(t.kill_buffer, "");
|
||||
assert_eq!(t.cursor(), r#"say ""#.len());
|
||||
assert_eq!(t.vim_mode_label(), Some("Insert"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vim_quote_text_objects_are_line_local_and_handle_escapes() {
|
||||
let mut t = ta_with(r#"say "a \"b\" c" now"#);
|
||||
t.set_cursor(/*pos*/ r#"say "a \"#.len());
|
||||
t.set_vim_enabled(/*enabled*/ true);
|
||||
|
||||
t.input(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('i'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('"'), KeyModifiers::SHIFT));
|
||||
|
||||
assert_eq!(t.text(), r#"say "" now"#);
|
||||
assert_eq!(t.kill_buffer, r#"a \"b\" c"#);
|
||||
assert_eq!(t.vim_mode_label(), Some("Insert"));
|
||||
|
||||
let mut t = ta_with("one \"two\nthree\" four");
|
||||
t.set_cursor(/*pos*/ "one \"two\n".len());
|
||||
t.set_vim_enabled(/*enabled*/ true);
|
||||
|
||||
t.input(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('i'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('"'), KeyModifiers::NONE));
|
||||
|
||||
assert_eq!(t.text(), "one \"two\nthree\" four");
|
||||
assert_eq!(t.kill_buffer, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vim_text_object_cancellation_and_unsupported_change_motions_do_not_edit() {
|
||||
let mut t = ta_with("hello world");
|
||||
t.set_cursor(/*pos*/ 1);
|
||||
t.set_vim_enabled(/*enabled*/ true);
|
||||
|
||||
t.input(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('$'), KeyModifiers::NONE));
|
||||
|
||||
assert_eq!(t.text(), "hello world");
|
||||
assert_eq!(t.kill_buffer, "");
|
||||
assert_eq!(t.vim_mode_label(), Some("Normal"));
|
||||
assert!(!t.is_vim_operator_pending());
|
||||
|
||||
t.input(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('i'), KeyModifiers::NONE));
|
||||
assert!(t.is_vim_operator_pending());
|
||||
t.input(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
|
||||
|
||||
assert_eq!(t.text(), "hello world");
|
||||
assert_eq!(t.kill_buffer, "");
|
||||
assert!(!t.is_vim_operator_pending());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vim_operator_invalid_motion_is_consumed() {
|
||||
let mut t = ta_with("hello");
|
||||
@@ -2408,7 +2610,7 @@ mod tests {
|
||||
t.input(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::NONE));
|
||||
assert!(t.is_vim_operator_pending());
|
||||
|
||||
t.input(KeyEvent::new(KeyCode::Char('i'), KeyModifiers::NONE));
|
||||
t.input(KeyEvent::new(KeyCode::Char('z'), KeyModifiers::NONE));
|
||||
|
||||
assert_eq!(t.text(), "hello");
|
||||
assert_eq!(t.vim_mode_label(), Some("Normal"));
|
||||
|
||||
@@ -0,0 +1,320 @@
|
||||
use super::TextArea;
|
||||
use super::split_word_pieces;
|
||||
use crate::key_hint::KeyBindingListExt;
|
||||
use crossterm::event::KeyEvent;
|
||||
use std::ops::Range;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub(super) enum VimMode {
|
||||
/// Normal mode routes printable keys to movement, operators, and mode transitions.
|
||||
Normal,
|
||||
/// Insert mode routes input through the regular editor keymap until Escape is pressed.
|
||||
Insert,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub(super) enum VimOperator {
|
||||
Delete,
|
||||
Yank,
|
||||
Change,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub(super) enum VimPending {
|
||||
None,
|
||||
Operator(VimOperator),
|
||||
TextObject {
|
||||
operator: VimOperator,
|
||||
scope: VimTextObjectScope,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub(super) enum VimMotion {
|
||||
Left,
|
||||
Right,
|
||||
Up,
|
||||
Down,
|
||||
WordForward,
|
||||
WordBackward,
|
||||
WordEnd,
|
||||
LineStart,
|
||||
LineEnd,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub(super) enum VimTextObjectScope {
|
||||
Inner,
|
||||
Around,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub(super) enum VimTextObject {
|
||||
Word,
|
||||
BigWord,
|
||||
Parentheses,
|
||||
Brackets,
|
||||
Braces,
|
||||
DoubleQuote,
|
||||
SingleQuote,
|
||||
Backtick,
|
||||
}
|
||||
|
||||
impl TextArea {
|
||||
pub(super) fn vim_text_object_scope_for_event(
|
||||
&self,
|
||||
event: KeyEvent,
|
||||
) -> Option<VimTextObjectScope> {
|
||||
if self
|
||||
.vim_operator_keymap
|
||||
.select_inner_text_object
|
||||
.is_pressed(event)
|
||||
{
|
||||
return Some(VimTextObjectScope::Inner);
|
||||
}
|
||||
if self
|
||||
.vim_operator_keymap
|
||||
.select_around_text_object
|
||||
.is_pressed(event)
|
||||
{
|
||||
return Some(VimTextObjectScope::Around);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub(super) fn vim_text_object_for_event(&self, event: KeyEvent) -> Option<VimTextObject> {
|
||||
if self.vim_text_object_keymap.word.is_pressed(event) {
|
||||
return Some(VimTextObject::Word);
|
||||
}
|
||||
if self.vim_text_object_keymap.big_word.is_pressed(event) {
|
||||
return Some(VimTextObject::BigWord);
|
||||
}
|
||||
if self.vim_text_object_keymap.parentheses.is_pressed(event) {
|
||||
return Some(VimTextObject::Parentheses);
|
||||
}
|
||||
if self.vim_text_object_keymap.brackets.is_pressed(event) {
|
||||
return Some(VimTextObject::Brackets);
|
||||
}
|
||||
if self.vim_text_object_keymap.braces.is_pressed(event) {
|
||||
return Some(VimTextObject::Braces);
|
||||
}
|
||||
if self.vim_text_object_keymap.double_quote.is_pressed(event) {
|
||||
return Some(VimTextObject::DoubleQuote);
|
||||
}
|
||||
if self.vim_text_object_keymap.single_quote.is_pressed(event) {
|
||||
return Some(VimTextObject::SingleQuote);
|
||||
}
|
||||
if self.vim_text_object_keymap.backtick.is_pressed(event) {
|
||||
return Some(VimTextObject::Backtick);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub(super) fn text_object_range(
|
||||
&self,
|
||||
object: VimTextObject,
|
||||
scope: VimTextObjectScope,
|
||||
) -> Option<Range<usize>> {
|
||||
match object {
|
||||
VimTextObject::Word => self.word_text_object_range(scope, /*big_word*/ false),
|
||||
VimTextObject::BigWord => self.word_text_object_range(scope, /*big_word*/ true),
|
||||
VimTextObject::Parentheses => self.paired_text_object_range(scope, '(', ')'),
|
||||
VimTextObject::Brackets => self.paired_text_object_range(scope, '[', ']'),
|
||||
VimTextObject::Braces => self.paired_text_object_range(scope, '{', '}'),
|
||||
VimTextObject::DoubleQuote => self.quoted_text_object_range(scope, '"'),
|
||||
VimTextObject::SingleQuote => self.quoted_text_object_range(scope, '\''),
|
||||
VimTextObject::Backtick => self.quoted_text_object_range(scope, '`'),
|
||||
}
|
||||
}
|
||||
|
||||
fn word_text_object_range(
|
||||
&self,
|
||||
scope: VimTextObjectScope,
|
||||
big_word: bool,
|
||||
) -> Option<Range<usize>> {
|
||||
let inner = if big_word {
|
||||
self.big_word_range_at_cursor()?
|
||||
} else {
|
||||
self.small_word_range_at_cursor()?
|
||||
};
|
||||
Some(match scope {
|
||||
VimTextObjectScope::Inner => inner,
|
||||
VimTextObjectScope::Around => self.expand_word_around(inner),
|
||||
})
|
||||
}
|
||||
|
||||
fn big_word_range_at_cursor(&self) -> Option<Range<usize>> {
|
||||
self.non_ws_runs()
|
||||
.into_iter()
|
||||
.find(|range| self.cursor_overlaps_range(range) || self.cursor_is_at_range_end(range))
|
||||
}
|
||||
|
||||
fn small_word_range_at_cursor(&self) -> Option<Range<usize>> {
|
||||
for run in self.non_ws_runs() {
|
||||
if !self.cursor_overlaps_range(&run) && !self.cursor_is_at_range_end(&run) {
|
||||
continue;
|
||||
}
|
||||
let mut last_piece = None;
|
||||
for (piece_start, piece) in split_word_pieces(&self.text[run.clone()]) {
|
||||
let piece = run.start + piece_start..run.start + piece_start + piece.len();
|
||||
if self.cursor_overlaps_range(&piece) {
|
||||
return Some(piece);
|
||||
}
|
||||
last_piece = Some(piece);
|
||||
}
|
||||
if self.cursor_is_at_range_end(&run) {
|
||||
return last_piece.or(Some(run));
|
||||
}
|
||||
return Some(run);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn non_ws_runs(&self) -> Vec<Range<usize>> {
|
||||
let mut runs = Vec::new();
|
||||
let mut start = None;
|
||||
for (idx, ch) in self.text.char_indices() {
|
||||
if ch.is_whitespace() {
|
||||
if let Some(run_start) = start.take() {
|
||||
runs.push(run_start..idx);
|
||||
}
|
||||
} else if start.is_none() {
|
||||
start = Some(idx);
|
||||
}
|
||||
}
|
||||
if let Some(run_start) = start {
|
||||
runs.push(run_start..self.text.len());
|
||||
}
|
||||
runs
|
||||
}
|
||||
|
||||
fn cursor_overlaps_range(&self, range: &Range<usize>) -> bool {
|
||||
range.start <= self.cursor_pos && self.cursor_pos < range.end
|
||||
}
|
||||
|
||||
fn cursor_is_at_range_end(&self, range: &Range<usize>) -> bool {
|
||||
range.start < range.end && self.cursor_pos == range.end
|
||||
}
|
||||
|
||||
fn expand_word_around(&self, inner: Range<usize>) -> Range<usize> {
|
||||
let following = self.following_whitespace_end(inner.end);
|
||||
if following > inner.end {
|
||||
return inner.start..following;
|
||||
}
|
||||
self.preceding_whitespace_start(inner.start)..inner.end
|
||||
}
|
||||
|
||||
fn following_whitespace_end(&self, start: usize) -> usize {
|
||||
let mut end = start;
|
||||
for (offset, ch) in self.text[start..].char_indices() {
|
||||
if !ch.is_whitespace() {
|
||||
break;
|
||||
}
|
||||
end = start + offset + ch.len_utf8();
|
||||
}
|
||||
end
|
||||
}
|
||||
|
||||
fn preceding_whitespace_start(&self, end: usize) -> usize {
|
||||
let mut start = end;
|
||||
for (idx, ch) in self.text[..end].char_indices().rev() {
|
||||
if !ch.is_whitespace() {
|
||||
break;
|
||||
}
|
||||
start = idx;
|
||||
}
|
||||
start
|
||||
}
|
||||
|
||||
fn paired_text_object_range(
|
||||
&self,
|
||||
scope: VimTextObjectScope,
|
||||
open: char,
|
||||
close: char,
|
||||
) -> Option<Range<usize>> {
|
||||
let mut stack: Vec<usize> = Vec::new();
|
||||
let mut best: Option<Range<usize>> = None;
|
||||
for (idx, ch) in self.text.char_indices() {
|
||||
if self.is_inside_element(idx) {
|
||||
continue;
|
||||
}
|
||||
if ch == open {
|
||||
stack.push(idx);
|
||||
} else if ch == close {
|
||||
let Some(open_idx) = stack.pop() else {
|
||||
continue;
|
||||
};
|
||||
let close_end = idx + ch.len_utf8();
|
||||
if open_idx <= self.cursor_pos && self.cursor_pos <= idx {
|
||||
let candidate = match scope {
|
||||
VimTextObjectScope::Inner => open_idx + open.len_utf8()..idx,
|
||||
VimTextObjectScope::Around => open_idx..close_end,
|
||||
};
|
||||
if candidate.start <= candidate.end
|
||||
&& best
|
||||
.as_ref()
|
||||
.is_none_or(|current| candidate.len() < current.len())
|
||||
{
|
||||
best = Some(candidate);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
best
|
||||
}
|
||||
|
||||
fn quoted_text_object_range(
|
||||
&self,
|
||||
scope: VimTextObjectScope,
|
||||
quote: char,
|
||||
) -> Option<Range<usize>> {
|
||||
let line = self.beginning_of_current_line()..self.end_of_current_line();
|
||||
let mut open = None;
|
||||
let mut best: Option<Range<usize>> = None;
|
||||
for (offset, ch) in self.text[line.clone()].char_indices() {
|
||||
let idx = line.start + offset;
|
||||
if self.is_inside_element(idx) || ch != quote || self.is_escaped(idx) {
|
||||
continue;
|
||||
}
|
||||
if let Some(open_idx) = open.take() {
|
||||
if open_idx <= self.cursor_pos && self.cursor_pos <= idx {
|
||||
let candidate = match scope {
|
||||
VimTextObjectScope::Inner => open_idx + quote.len_utf8()..idx,
|
||||
VimTextObjectScope::Around => idx_range(open_idx, idx, quote),
|
||||
};
|
||||
if candidate.start <= candidate.end
|
||||
&& best
|
||||
.as_ref()
|
||||
.is_none_or(|current| candidate.len() < current.len())
|
||||
{
|
||||
best = Some(candidate);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
open = Some(idx);
|
||||
}
|
||||
}
|
||||
best
|
||||
}
|
||||
|
||||
fn is_inside_element(&self, pos: usize) -> bool {
|
||||
self.elements
|
||||
.iter()
|
||||
.any(|element| pos >= element.range.start && pos < element.range.end)
|
||||
}
|
||||
|
||||
fn is_escaped(&self, pos: usize) -> bool {
|
||||
let mut backslashes = 0;
|
||||
for ch in self.text[..pos].chars().rev() {
|
||||
if ch != '\\' {
|
||||
break;
|
||||
}
|
||||
backslashes += 1;
|
||||
}
|
||||
backslashes % 2 == 1
|
||||
}
|
||||
}
|
||||
|
||||
fn idx_range(open_idx: usize, close_idx: usize, quote: char) -> Range<usize> {
|
||||
open_idx..close_idx + quote.len_utf8()
|
||||
}
|
||||
Reference in new issue
Block a user