Add Vim replace mode to the TUI composer (#42194)

## What changed

- Add Vim Replace mode, entered with `R`, which overwrites graphemes and appends at the end of a line.
- Restore overwritten text with Backspace and record replace edits for undo and dot-repeat.
- Preserve composer behavior for attachments, completions, paste bursts, history previews, and external editor imports while replacing text.
- Expose `vim_normal.enter_replace_mode` in the configurable TUI keymap.

## Testing

- Add coverage for grapheme replacement, Backspace recovery, dot-repeat, custom bindings, atomic attachments, completions, paste handling, and history interactions.

GitOrigin-RevId: cb04e3adcce013c1eb7b82c994b00e944310c481
This commit is contained in:
Benjamin Carlsson authored and copyberry committed 2026-09-02 03:35:41 +00:00
1 parent 02f47d3fb3
commit 5a0419edb5
22 files changed
+634 -53

No files matched your search

+33 -6
View File
@@ -318,7 +318,10 @@ impl TextArea {
/// Return the keymap context that owns the next editing key.
pub(crate) fn keymap_context(&self) -> KeymapContext {
if !self.vim_enabled || self.vim_mode == VimMode::Insert || self.vim_query().is_some() {
if !self.vim_enabled
|| matches!(self.vim_mode, VimMode::Insert | VimMode::Replace)
|| self.vim_query().is_some()
{
return KeymapContext::Editor;
}
match self.vim_pending {
@@ -338,6 +341,7 @@ impl TextArea {
if self.vim_enabled {
self.vim_mode = VimMode::Insert;
self.vim_pending = VimPending::None;
self.clear_vim_replace_recovery();
self.cancel_vim_search();
if self.vim_commands.pending_change.is_empty() && !self.vim_commands.replaying {
self.start_vim_edit(VimAction::Insert(VimInsertPosition::Cursor));
@@ -357,6 +361,7 @@ impl TextArea {
self.vim_pending = VimPending::None;
self.cancel_vim_search();
self.preferred_col = None;
self.clear_vim_replace_recovery();
}
}
@@ -366,7 +371,7 @@ impl TextArea {
/// like `dd` or `yw` remains command input instead of being converted into
/// literal text.
pub(crate) fn allows_paste_burst(&self) -> bool {
!self.vim_enabled || self.vim_mode == VimMode::Insert
!self.vim_enabled || matches!(self.vim_mode, VimMode::Insert | VimMode::Replace)
}
/// Return whether rendering should use the insert-mode cursor style.
@@ -380,7 +385,8 @@ impl TextArea {
/// transition rather than a popup cancel/backtrack or turn-interrupt shortcut.
pub(crate) fn should_handle_vim_insert_escape(&self, event: KeyEvent) -> bool {
self.vim_enabled
&& (self.vim_mode == VimMode::Insert || self.is_vim_operator_pending())
&& (matches!(self.vim_mode, VimMode::Insert | VimMode::Replace)
|| self.is_vim_operator_pending())
&& event.code == KeyCode::Esc
&& event.modifiers == KeyModifiers::NONE
&& matches!(event.kind, KeyEventKind::Press | KeyEventKind::Repeat)
@@ -399,6 +405,7 @@ impl TextArea {
Some(match self.vim_mode {
VimMode::Normal => "Normal",
VimMode::Insert => "Insert",
VimMode::Replace => "Replace",
})
}
@@ -410,6 +417,7 @@ impl TextArea {
Some(match self.vim_mode {
VimMode::Normal => "Vim: Normal".magenta(),
VimMode::Insert => "Vim: Insert".green(),
VimMode::Replace => "Vim: Replace".cyan(),
})
}
@@ -419,10 +427,15 @@ impl TextArea {
pub fn insert_str(&mut self, text: &str) {
self.record_vim_inserted_text(text);
self.insert_str_at(self.cursor_pos, text);
if self.is_vim_replace_mode() {
self.replace_vim_text(text);
} else {
self.insert_str_at(self.cursor_pos, text);
}
}
pub fn insert_str_at(&mut self, pos: usize, text: &str) {
self.clear_vim_replace_recovery();
let pos = self.clamp_pos_for_insertion(pos);
self.text.insert_str(pos, text);
self.wrap_cache.replace(None);
@@ -434,6 +447,12 @@ impl TextArea {
}
pub fn replace_range(&mut self, range: std::ops::Range<usize>, text: &str) {
self.clear_vim_replace_recovery();
self.replace_range_preserving_recovery(range, text);
}
// Replace typing and Backspace keep contiguous recovery, but still respect atomic elements.
fn replace_range_preserving_recovery(&mut self, range: Range<usize>, text: &str) {
let range = self.expand_range_to_element_boundaries(range);
self.replace_range_raw(range, text);
}
@@ -701,10 +720,12 @@ impl TextArea {
}
let prior_mode = self.vim_mode;
match self.vim_mode {
VimMode::Insert => self.handle_vim_insert(event),
VimMode::Insert | VimMode::Replace => self.handle_vim_insert(event),
VimMode::Normal => self.handle_vim_normal(event),
}
if prior_mode == VimMode::Insert && self.vim_mode == VimMode::Normal {
if matches!(prior_mode, VimMode::Insert | VimMode::Replace)
&& self.vim_mode == VimMode::Normal
{
self.finish_pending_vim_change();
}
}
@@ -714,6 +735,12 @@ impl TextArea {
self.leave_vim_insert_mode();
return;
}
if self.is_vim_replace_mode()
&& self.editor_keymap.delete_backward.is_pressed(event)
&& self.apply_vim_insert_action(VimAction::RestoreReplacedCharacter)
{
return;
}
let keymap = self.editor_keymap.clone();
self.input_with_keymap(event, &keymap);
}