Add Vim search motions to the composer (#41586)

## What changed

- Add draft-local forward and backward literal search with `/` and `?`, plus
  wrapped repeat navigation with `n` and `N`.
- Support search motions after delete, change, and yank operators, while keeping
  queries separate from the draft and skipping atomic elements and partial
  grapheme matches.
- Render and edit the query in the composer footer, highlight matches, and add
  configurable `tui.keymap.vim_search` bindings to the keymap picker and schema.

## Testing

- Cover navigation, wrapping, operator composition, repeat behavior, Unicode
  graphemes, atomic elements, query editing, paste handling, and rendering.

GitOrigin-RevId: 087b080ff8fd74e48456c6b899c07ea5eed38e0c
This commit is contained in:
Benjamin Carlsson authored and copyberry committed 2026-08-30 00:42:00 +00:00
1 parent b8c86376a2
commit 63d213884d
27 files changed
+745 -42

No files matched your search

@@ -0,0 +1,23 @@
//! Composer UI bridge for textarea-owned Vim queries, independent of prompt-history recall.
use super::ChatComposer;
use super::inset_footer_hint_area;
use ratatui::layout::Rect;
impl ChatComposer {
pub(crate) fn cancel_vim_search(&mut self) -> bool {
self.draft.textarea.cancel_vim_search()
}
pub(super) fn vim_search_cursor_pos(&self, area: Rect) -> Option<(u16, u16)> {
let query = self.draft.textarea.vim_query()?;
let [_, _, _, mut footer] = self.layout_areas(area);
footer.y = footer.bottom().saturating_sub(1);
footer.height = footer.height.min(1);
query.cursor_pos(inset_footer_hint_area(footer))
}
}
#[cfg(test)]
#[path = "vim_search_tests.rs"]
mod tests;
@@ -0,0 +1,58 @@
//! Search input goes through real composer routing and rendering without changing the draft.
use super::super::tests::snapshot_composer_state_with_width;
use crate::keymap::KeyChordMatch;
use crate::keymap::KeyChordMatcher;
use crate::keymap::RuntimeKeymap;
use codex_config::types::TuiKeymap;
use crossterm::event::KeyCode;
use crossterm::event::KeyEvent;
use crossterm::event::KeyModifiers;
use pretty_assertions::assert_eq;
use tokio::time::Instant;
#[test]
fn vim_search_query_edits_and_paste_preserve_the_draft() {
snapshot_composer_state_with_width(
"vim_composer_search",
/*width*/ 40,
/*enhanced_keys_supported*/ false,
|composer| {
composer.set_text_content("a g\u{301}g b हि gg".into(), Vec::new(), Vec::new());
composer.set_vim_enabled(/*enabled*/ true);
composer.draft.textarea.set_cursor(/*pos*/ 0);
let config: TuiKeymap = toml::from_str("[vim_search]\nforward='g s'").unwrap();
let keymap = RuntimeKeymap::from_config(&config).unwrap();
composer.set_keymap_bindings(&keymap);
let mut matcher = KeyChordMatcher::default();
for ch in "dgse\u{301}\u{8}gg".chars() {
let code = if ch == '\u{8}' {
KeyCode::Backspace
} else {
KeyCode::Char(ch)
};
let event = KeyEvent::new(code, KeyModifiers::NONE);
let event = match matcher.advance(
event,
&keymap.chords,
composer.keymap_contexts(),
Instant::now(),
) {
KeyChordMatch::PassThrough => event,
KeyChordMatch::Completed(event) => event,
KeyChordMatch::Pending(_) => continue,
other => panic!("unexpected chord result: {other:?}"),
};
composer.handle_key_event(event);
}
composer.handle_paste("photo.png".into());
let input = composer.draft.textarea.vim_query().unwrap();
assert_eq!(input.editor.text(), "ggphoto.png");
assert_eq!(composer.draft.textarea.text(), "a g\u{301}g b हि gg");
assert!(composer.attachments.local_image_paths().is_empty());
composer.handle_key_event(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
composer.handle_key_event(KeyEvent::new(KeyCode::Char('?'), KeyModifiers::NONE));
composer.handle_paste("ह".into());
},
);
}