mirror of
https://github.com/openai/codex.git
synced 2026-11-17 22:10:37 +00:00
Refactor composer undo and redo history (#61951)
## What changed Rename `VimHistory` to `EditHistory` and update composer edit transactions and history-search state to use it. Consolidate undo and redo stack transfers and history trimming in `EditHistory::step`, preserving the existing Vim-mode bindings and edit grouping. GitOrigin-RevId: 355993a8c2344971c23805b3e21d587a9f368ede
This commit is contained in:
@@ -415,10 +415,10 @@ mod paste_input;
|
||||
mod popup_state;
|
||||
mod reconnect;
|
||||
pub(crate) use reconnect::RestrictedInputMode;
|
||||
mod edit_history;
|
||||
mod slash_input;
|
||||
mod sparkle;
|
||||
mod status_surface;
|
||||
mod vim_history;
|
||||
mod vim_register;
|
||||
mod vim_search;
|
||||
mod warning_notice;
|
||||
@@ -426,6 +426,7 @@ mod warning_notice;
|
||||
use self::attachment_state::AttachmentState;
|
||||
use self::draft_state::ComposerMentionBinding;
|
||||
use self::draft_state::DraftState;
|
||||
use self::edit_history::EditHistory;
|
||||
pub(crate) use self::footer_state::CommandPopupPlacement;
|
||||
use self::footer_state::ComposerLayout;
|
||||
pub(crate) use self::footer_state::ComposerRenderOptions;
|
||||
@@ -438,7 +439,6 @@ use self::popup_state::PopupState;
|
||||
use self::slash_input::SlashInput;
|
||||
use self::slash_input::SlashValidation;
|
||||
use self::slash_input::SubmissionValidation;
|
||||
use self::vim_history::VimHistory;
|
||||
use crate::app_event::AppEvent;
|
||||
use crate::app_event::ConnectorsSnapshot;
|
||||
use crate::app_event_sender::AppEventSender;
|
||||
@@ -677,7 +677,7 @@ pub(crate) struct ChatComposer {
|
||||
windows_degraded_sandbox_active: bool,
|
||||
side_conversation_active: bool,
|
||||
history_search: Option<HistorySearchSession>,
|
||||
vim_history: VimHistory,
|
||||
edit_history: EditHistory,
|
||||
vim_register: vim_register::VimRegister,
|
||||
submit_keys: Vec<KeyBinding>,
|
||||
queue_keys: Vec<KeyBinding>,
|
||||
@@ -862,7 +862,7 @@ impl ChatComposer {
|
||||
windows_degraded_sandbox_active: false,
|
||||
side_conversation_active: false,
|
||||
history_search: None,
|
||||
vim_history: VimHistory::default(),
|
||||
edit_history: EditHistory::default(),
|
||||
vim_register: vim_register::VimRegister::default(),
|
||||
submit_keys: vec![key_hint::plain(KeyCode::Enter)],
|
||||
queue_keys: vec![key_hint::plain(KeyCode::Tab)],
|
||||
@@ -1318,7 +1318,7 @@ impl ChatComposer {
|
||||
/// remote images). Cursor is placed at the end after rebuilding elements.
|
||||
pub(crate) fn apply_external_edit(&mut self, text: String) {
|
||||
self.note_sparkle_replaced_text(&text);
|
||||
self.vim_history = VimHistory::default();
|
||||
self.edit_history = EditHistory::default();
|
||||
self.draft.pending_pastes.clear();
|
||||
let (text, _) = self.imported_text_for_textarea(text, Vec::new());
|
||||
|
||||
@@ -1408,7 +1408,7 @@ impl ChatComposer {
|
||||
}
|
||||
self.draft.textarea.enable_vim_search();
|
||||
self.draft.textarea.set_vim_enabled(enabled);
|
||||
self.vim_history = VimHistory::default();
|
||||
self.edit_history = EditHistory::default();
|
||||
self.draft.paste_burst.clear_after_explicit_paste();
|
||||
self.footer.mode = reset_mode_after_activity(self.footer.mode);
|
||||
}
|
||||
@@ -1625,7 +1625,7 @@ impl ChatComposer {
|
||||
}
|
||||
// Clear any existing content, placeholders, and attachments first.
|
||||
self.footer.flash = None;
|
||||
self.vim_history = VimHistory::default();
|
||||
self.edit_history = EditHistory::default();
|
||||
self.draft.textarea_state.get_mut().follow_cursor();
|
||||
self.draft.textarea.set_text_clearing_elements("");
|
||||
self.draft.is_bash_mode = false;
|
||||
@@ -1923,13 +1923,13 @@ impl ChatComposer {
|
||||
/// Insert an attachment placeholder and track it for the next submission.
|
||||
pub fn attach_image(&mut self, path: PathBuf) {
|
||||
self.dismiss_sparkle();
|
||||
let started_vim_edit = self.begin_direct_vim_edit();
|
||||
let started_edit = self.begin_direct_edit();
|
||||
let elements_before = self.draft.textarea.element_payloads();
|
||||
self.attachments
|
||||
.attach_image(&mut self.draft.textarea, path);
|
||||
self.reconcile_deleted_elements(elements_before);
|
||||
if started_vim_edit {
|
||||
self.finish_vim_edit();
|
||||
if started_edit {
|
||||
self.finish_edit();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2075,7 +2075,7 @@ impl ChatComposer {
|
||||
return self.handle_history_search_key(key_event);
|
||||
}
|
||||
|
||||
if self.handle_vim_history_key(key_event) {
|
||||
if self.handle_edit_history_key(key_event) {
|
||||
return (InputResult::None, true);
|
||||
}
|
||||
|
||||
@@ -2593,7 +2593,7 @@ impl ChatComposer {
|
||||
}
|
||||
|
||||
fn insert_selected_file_path(&mut self, token_range: Range<usize>, selected_path: &str) {
|
||||
let started_vim_edit = self.begin_direct_vim_edit();
|
||||
let started_edit = self.begin_direct_edit();
|
||||
let token_range = self.separate_completion_from_adjacent_element(token_range);
|
||||
if Self::is_image_path(selected_path) {
|
||||
let path_buf = PathBuf::from(selected_path);
|
||||
@@ -2614,8 +2614,8 @@ impl ChatComposer {
|
||||
} else {
|
||||
self.insert_selected_path(token_range, selected_path);
|
||||
}
|
||||
if started_vim_edit {
|
||||
self.finish_vim_edit();
|
||||
if started_edit {
|
||||
self.finish_edit();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3576,12 +3576,12 @@ impl ChatComposer {
|
||||
}
|
||||
let removes_remote_image = matches!(key_event.code, KeyCode::Delete | KeyCode::Backspace)
|
||||
&& self.attachments.selected_remote_image_index.is_some();
|
||||
let started_vim_edit = removes_remote_image && self.begin_direct_vim_edit();
|
||||
let started_edit = removes_remote_image && self.begin_direct_edit();
|
||||
let result = self
|
||||
.attachments
|
||||
.handle_remote_image_selection_key(key_event, &mut self.draft.textarea);
|
||||
if started_vim_edit {
|
||||
self.finish_vim_edit();
|
||||
if started_edit {
|
||||
self.finish_edit();
|
||||
}
|
||||
result
|
||||
}
|
||||
@@ -3873,13 +3873,13 @@ impl ChatComposer {
|
||||
&& self.draft.textarea.is_delete_backward_key(input)
|
||||
&& self.draft.textarea.cursor() == 0
|
||||
{
|
||||
let started_vim_edit = self.begin_direct_vim_edit();
|
||||
let started_edit = self.begin_direct_edit();
|
||||
if self.draft.textarea.is_vim_replace_mode() {
|
||||
self.draft.textarea.input(input);
|
||||
}
|
||||
self.draft.is_bash_mode = false;
|
||||
if started_vim_edit {
|
||||
self.finish_vim_edit();
|
||||
if started_edit {
|
||||
self.finish_edit();
|
||||
}
|
||||
return (InputResult::None, true);
|
||||
}
|
||||
@@ -3930,7 +3930,7 @@ impl ChatComposer {
|
||||
if let Some(elements_before) = elements_before {
|
||||
self.reconcile_deleted_elements(elements_before);
|
||||
}
|
||||
self.finish_vim_edit();
|
||||
self.finish_edit();
|
||||
|
||||
// Update the paste-burst heuristic for text, shortcut, and non-char events.
|
||||
match input.code {
|
||||
|
||||
@@ -131,7 +131,7 @@ fn agents_navigation_hint_snapshots() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn left_respects_editor_and_vim_history_remaps() {
|
||||
fn left_respects_editor_and_edit_history_remaps() {
|
||||
use codex_config::types::KeybindingSpec;
|
||||
use codex_config::types::KeybindingsSpec;
|
||||
use codex_config::types::TuiKeymap;
|
||||
|
||||
+57
-44
@@ -1,4 +1,4 @@
|
||||
//! Bounded Vim undo/redo history for the complete composer-owned draft.
|
||||
//! Bounded composer undo/redo history for the complete composer-owned draft.
|
||||
//!
|
||||
//! A textarea does not own pending paste payloads, image attachments, or mention targets. Keeping
|
||||
//! edit transactions here lets one snapshot restore those values together with visible text. Keys
|
||||
@@ -14,18 +14,23 @@ use super::ComposerDraft;
|
||||
use crate::key_hint::KeyBindingListExt;
|
||||
use crossterm::event::KeyEvent;
|
||||
|
||||
const MAX_VIM_UNDO_STEPS: usize = 64;
|
||||
const MAX_VIM_UNDO_BYTES: usize = 1024 * 1024;
|
||||
const MAX_UNDO_STEPS: usize = 64;
|
||||
const MAX_UNDO_BYTES: usize = 1024 * 1024;
|
||||
|
||||
enum EditDirection {
|
||||
Undo,
|
||||
Redo,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub(super) struct VimHistory {
|
||||
pub(super) struct EditHistory {
|
||||
undo: VecDeque<ComposerDraft>,
|
||||
redo: VecDeque<ComposerDraft>,
|
||||
pending: Option<ComposerDraft>,
|
||||
}
|
||||
|
||||
impl ComposerDraft {
|
||||
fn vim_history_bytes(&self) -> usize {
|
||||
fn history_bytes(&self) -> usize {
|
||||
let mut bytes = self.text.len()
|
||||
+ self.text_elements.len()
|
||||
* std::mem::size_of::<codex_protocol::user_input::TextElement>();
|
||||
@@ -46,16 +51,31 @@ impl ComposerDraft {
|
||||
}
|
||||
}
|
||||
|
||||
impl VimHistory {
|
||||
impl EditHistory {
|
||||
fn step(
|
||||
&mut self,
|
||||
direction: EditDirection,
|
||||
current: impl FnOnce() -> ComposerDraft,
|
||||
) -> Option<ComposerDraft> {
|
||||
let (from, to) = match direction {
|
||||
EditDirection::Undo => (&mut self.undo, &mut self.redo),
|
||||
EditDirection::Redo => (&mut self.redo, &mut self.undo),
|
||||
};
|
||||
let snapshot = from.pop_back()?;
|
||||
to.push_back(current());
|
||||
self.trim();
|
||||
Some(snapshot)
|
||||
}
|
||||
|
||||
fn trim(&mut self) {
|
||||
while self.undo.len() > MAX_VIM_UNDO_STEPS
|
||||
while self.undo.len() > MAX_UNDO_STEPS
|
||||
|| self
|
||||
.undo
|
||||
.iter()
|
||||
.chain(self.redo.iter())
|
||||
.map(ComposerDraft::vim_history_bytes)
|
||||
.map(ComposerDraft::history_bytes)
|
||||
.sum::<usize>()
|
||||
> MAX_VIM_UNDO_BYTES
|
||||
> MAX_UNDO_BYTES
|
||||
{
|
||||
if self.undo.pop_front().is_none() {
|
||||
self.redo.pop_front();
|
||||
@@ -65,7 +85,7 @@ impl VimHistory {
|
||||
}
|
||||
|
||||
impl ChatComposer {
|
||||
pub(super) fn wants_vim_history_key(&self, event: KeyEvent) -> bool {
|
||||
pub(super) fn wants_edit_history_key(&self, event: KeyEvent) -> bool {
|
||||
self.draft.textarea.is_vim_normal_mode()
|
||||
&& !self.draft.textarea.is_vim_operator_pending()
|
||||
&& !self.popups.active()
|
||||
@@ -74,25 +94,19 @@ impl ChatComposer {
|
||||
}
|
||||
|
||||
/// Consume undo and redo before normal-mode commands, even when their history is empty.
|
||||
pub(super) fn handle_vim_history_key(&mut self, event: KeyEvent) -> bool {
|
||||
if !self.wants_vim_history_key(event) {
|
||||
pub(super) fn handle_edit_history_key(&mut self, event: KeyEvent) -> bool {
|
||||
if !self.wants_edit_history_key(event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let redo = self.vim_normal_keymap.redo.is_pressed(event);
|
||||
let snapshot = if redo {
|
||||
self.vim_history.redo.pop_back()
|
||||
let direction = if self.vim_normal_keymap.redo.is_pressed(event) {
|
||||
EditDirection::Redo
|
||||
} else {
|
||||
self.vim_history.undo.pop_back()
|
||||
EditDirection::Undo
|
||||
};
|
||||
let mut history = std::mem::take(&mut self.edit_history);
|
||||
let snapshot = history.step(direction, || self.snapshot_draft());
|
||||
if let Some(snapshot) = snapshot {
|
||||
let current = self.snapshot_draft();
|
||||
if redo {
|
||||
self.vim_history.undo.push_back(current);
|
||||
} else {
|
||||
self.vim_history.redo.push_back(current);
|
||||
}
|
||||
let history = std::mem::take(&mut self.vim_history);
|
||||
let mut vim_state = VimPersistentState::default();
|
||||
self.draft
|
||||
.textarea
|
||||
@@ -101,17 +115,16 @@ impl ChatComposer {
|
||||
self.draft
|
||||
.textarea
|
||||
.swap_vim_persistent_state(&mut vim_state);
|
||||
self.vim_history = history;
|
||||
self.draft.textarea.enter_vim_normal_mode();
|
||||
self.vim_history.trim();
|
||||
}
|
||||
self.edit_history = history;
|
||||
true
|
||||
}
|
||||
|
||||
/// Snapshot only keys that can begin or complete a Vim edit transaction.
|
||||
pub(super) fn begin_vim_edit(&mut self, event: KeyEvent) {
|
||||
if !self.draft.textarea.is_vim_enabled()
|
||||
|| self.vim_history.pending.is_some()
|
||||
|| self.edit_history.pending.is_some()
|
||||
|| self.draft.textarea.is_vim_operator_pending()
|
||||
{
|
||||
return;
|
||||
@@ -139,21 +152,21 @@ impl ChatComposer {
|
||||
return;
|
||||
}
|
||||
|
||||
self.begin_vim_edit_transaction();
|
||||
self.begin_edit_transaction();
|
||||
}
|
||||
|
||||
/// Start one standalone draft edit without splitting an active Vim command.
|
||||
pub(super) fn begin_direct_vim_edit(&mut self) -> bool {
|
||||
pub(super) fn begin_direct_edit(&mut self) -> bool {
|
||||
if !self.draft.textarea.is_vim_enabled()
|
||||
|| self.draft.textarea.is_vim_operator_pending()
|
||||
|| self.vim_history.pending.is_some()
|
||||
|| self.edit_history.pending.is_some()
|
||||
|| self.history_search.is_some()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
self.begin_vim_edit_transaction();
|
||||
if self.vim_history.pending.is_none() {
|
||||
self.begin_edit_transaction();
|
||||
if self.edit_history.pending.is_none() {
|
||||
return false;
|
||||
}
|
||||
let mut vim_state = VimPersistentState::default();
|
||||
@@ -167,7 +180,7 @@ impl ChatComposer {
|
||||
true
|
||||
}
|
||||
|
||||
fn begin_vim_edit_transaction(&mut self) {
|
||||
fn begin_edit_transaction(&mut self) {
|
||||
let visible_bytes = self.draft.textarea.text().len();
|
||||
let paste_bytes = self
|
||||
.draft
|
||||
@@ -175,29 +188,29 @@ impl ChatComposer {
|
||||
.iter()
|
||||
.map(|(placeholder, pasted)| placeholder.len() + pasted.len())
|
||||
.sum::<usize>();
|
||||
if visible_bytes.saturating_add(paste_bytes) > MAX_VIM_UNDO_BYTES {
|
||||
self.vim_history = VimHistory::default();
|
||||
if visible_bytes.saturating_add(paste_bytes) > MAX_UNDO_BYTES {
|
||||
self.edit_history = EditHistory::default();
|
||||
return;
|
||||
}
|
||||
|
||||
let snapshot = self.snapshot_draft();
|
||||
if snapshot.vim_history_bytes() <= MAX_VIM_UNDO_BYTES {
|
||||
self.vim_history.pending = Some(snapshot);
|
||||
self.vim_history.trim();
|
||||
if snapshot.history_bytes() <= MAX_UNDO_BYTES {
|
||||
self.edit_history.pending = Some(snapshot);
|
||||
self.edit_history.trim();
|
||||
} else {
|
||||
self.vim_history = VimHistory::default();
|
||||
self.edit_history = EditHistory::default();
|
||||
}
|
||||
}
|
||||
|
||||
/// Commit a complete normal-mode command or one insert-mode session.
|
||||
pub(super) fn finish_vim_edit(&mut self) {
|
||||
pub(super) fn finish_edit(&mut self) {
|
||||
if !self.draft.textarea.is_vim_normal_mode()
|
||||
|| self.draft.textarea.is_vim_operator_pending()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let Some(snapshot) = self.vim_history.pending.take() else {
|
||||
let Some(snapshot) = self.edit_history.pending.take() else {
|
||||
return;
|
||||
};
|
||||
if snapshot.text == self.current_text() {
|
||||
@@ -208,12 +221,12 @@ impl ChatComposer {
|
||||
}
|
||||
}
|
||||
|
||||
self.vim_history.redo.clear();
|
||||
self.vim_history.undo.push_back(snapshot);
|
||||
self.vim_history.trim();
|
||||
self.edit_history.redo.clear();
|
||||
self.edit_history.undo.push_back(snapshot);
|
||||
self.edit_history.trim();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "vim_history_tests.rs"]
|
||||
#[path = "edit_history_tests.rs"]
|
||||
mod tests;
|
||||
+19
-19
@@ -67,7 +67,7 @@ fn undo_and_redo_group_complete_vim_edits() {
|
||||
}
|
||||
escape(&mut composer);
|
||||
assert_eq!(composer.current_text(), edited);
|
||||
assert_eq!(composer.vim_history.undo.len(), 1);
|
||||
assert_eq!(composer.edit_history.undo.len(), 1);
|
||||
|
||||
keys(&mut composer, "u");
|
||||
assert_eq!(
|
||||
@@ -254,7 +254,7 @@ fn normal_mode_explicit_paste_is_its_own_edit_and_replaces_stale_redo() {
|
||||
|
||||
composer.handle_paste("Z".to_owned());
|
||||
assert_eq!(composer.current_text(), "Zabc");
|
||||
assert!(composer.vim_history.redo.is_empty());
|
||||
assert!(composer.edit_history.redo.is_empty());
|
||||
ctrl_r(&mut composer);
|
||||
assert_eq!(composer.current_text(), "Zabc");
|
||||
assert!(!composer.history_search_active());
|
||||
@@ -270,7 +270,7 @@ fn normal_mode_direct_insert_and_image_attachment_are_undoable() {
|
||||
keys(&mut composer, "xu");
|
||||
composer.insert_str("Z");
|
||||
assert_eq!(composer.current_text(), "Zabc");
|
||||
assert!(composer.vim_history.redo.is_empty());
|
||||
assert!(composer.edit_history.redo.is_empty());
|
||||
keys(&mut composer, "u");
|
||||
assert_eq!(composer.current_text(), "abc");
|
||||
ctrl_r(&mut composer);
|
||||
@@ -281,7 +281,7 @@ fn normal_mode_direct_insert_and_image_attachment_are_undoable() {
|
||||
composer.attach_image(path.clone());
|
||||
let with_image = composer.draft_snapshot();
|
||||
assert_eq!(composer.local_image_paths(), vec![path]);
|
||||
assert!(composer.vim_history.redo.is_empty());
|
||||
assert!(composer.edit_history.redo.is_empty());
|
||||
keys(&mut composer, "u");
|
||||
assert_eq!(composer.current_text(), "abc");
|
||||
assert!(composer.local_image_paths().is_empty());
|
||||
@@ -303,12 +303,12 @@ fn deleting_a_selected_remote_image_is_one_undoable_vim_edit() {
|
||||
let url = "https://example.com/remote.png".to_owned();
|
||||
composer.set_remote_image_urls(vec![url.clone()]);
|
||||
keys(&mut composer, "xu");
|
||||
assert_eq!(composer.vim_history.redo.len(), 1);
|
||||
assert_eq!(composer.edit_history.redo.len(), 1);
|
||||
let _ = composer.handle_key_event(KeyEvent::new(KeyCode::Up, KeyModifiers::NONE));
|
||||
assert!(composer.vim_history.pending.is_none());
|
||||
assert!(composer.edit_history.pending.is_none());
|
||||
let _ = composer.handle_key_event(KeyEvent::new(key, KeyModifiers::NONE));
|
||||
assert!(composer.remote_image_urls().is_empty());
|
||||
assert!(composer.vim_history.redo.is_empty());
|
||||
assert!(composer.edit_history.redo.is_empty());
|
||||
keys(&mut composer, "u");
|
||||
assert_eq!(composer.remote_image_urls(), vec![url]);
|
||||
assert_eq!(composer.current_text(), "abc");
|
||||
@@ -327,7 +327,7 @@ fn direct_edits_join_the_active_vim_insert_transaction() {
|
||||
keys(&mut composer, "D");
|
||||
escape(&mut composer);
|
||||
let edited = composer.draft_snapshot();
|
||||
assert_eq!(composer.vim_history.undo.len(), 1);
|
||||
assert_eq!(composer.edit_history.undo.len(), 1);
|
||||
keys(&mut composer, "u");
|
||||
assert_eq!(composer.current_text(), "abc");
|
||||
assert!(composer.local_image_paths().is_empty());
|
||||
@@ -345,7 +345,7 @@ fn vim_search_query_paste_does_not_invalidate_draft_redo() {
|
||||
composer.draft.textarea.vim_query().unwrap().editor.text(),
|
||||
"beta"
|
||||
);
|
||||
assert!(composer.vim_history.pending.is_none());
|
||||
assert!(composer.edit_history.pending.is_none());
|
||||
|
||||
escape(&mut composer);
|
||||
ctrl_r(&mut composer);
|
||||
@@ -376,13 +376,13 @@ fn vim_redo_keeps_its_binding_when_history_is_empty() {
|
||||
|target| *target = composer,
|
||||
);
|
||||
|
||||
let original = "a".repeat(super::MAX_VIM_UNDO_BYTES / 2 + 1);
|
||||
let original = "a".repeat(super::MAX_UNDO_BYTES / 2 + 1);
|
||||
let mut composer = vim_composer(&original);
|
||||
keys(&mut composer, "xu");
|
||||
for command in ["d", "i"] {
|
||||
keys(&mut composer, command);
|
||||
escape(&mut composer);
|
||||
assert_eq!(composer.vim_history.redo.len(), 1);
|
||||
assert_eq!(composer.edit_history.redo.len(), 1);
|
||||
}
|
||||
ctrl_r(&mut composer);
|
||||
assert_eq!(composer.current_text(), original[1..]);
|
||||
@@ -390,9 +390,9 @@ fn vim_redo_keeps_its_binding_when_history_is_empty() {
|
||||
|
||||
keys(&mut composer, "uiZ");
|
||||
escape(&mut composer);
|
||||
assert!(composer.vim_history.redo.is_empty());
|
||||
assert!(composer.edit_history.redo.is_empty());
|
||||
keys(&mut composer, "x");
|
||||
assert_eq!(composer.vim_history.undo.len(), 1);
|
||||
assert_eq!(composer.edit_history.undo.len(), 1);
|
||||
keys(&mut composer, "u");
|
||||
assert_eq!(composer.current_text(), format!("Z{original}"));
|
||||
}
|
||||
@@ -635,7 +635,7 @@ fn find_jump_and_search_navigation_never_snapshot_the_draft() {
|
||||
for key in ['f', 'F', 'G', '/', '?', 'n', 'N'] {
|
||||
composer.begin_vim_edit(KeyEvent::new(KeyCode::Char(key), KeyModifiers::NONE));
|
||||
assert!(
|
||||
composer.vim_history.pending.is_none(),
|
||||
composer.edit_history.pending.is_none(),
|
||||
"navigation binding {key:?} snapshotted the draft"
|
||||
);
|
||||
}
|
||||
@@ -646,7 +646,7 @@ fn find_jump_and_search_navigation_never_snapshot_the_draft() {
|
||||
.expect("native jump-top chord")
|
||||
.parts();
|
||||
composer.begin_vim_edit(KeyEvent::new(code, modifiers));
|
||||
assert!(composer.vim_history.pending.is_none());
|
||||
assert!(composer.edit_history.pending.is_none());
|
||||
|
||||
for command in ["llwbh0$", "fb", "Fa", "G", "/beta\nnN", "?alpha\n"] {
|
||||
let text = "alpha beta\nalpha gamma";
|
||||
@@ -654,10 +654,10 @@ fn find_jump_and_search_navigation_never_snapshot_the_draft() {
|
||||
for key in command.chars() {
|
||||
keys(&mut composer, &key.to_string());
|
||||
assert!(
|
||||
composer.vim_history.pending.is_none(),
|
||||
composer.edit_history.pending.is_none(),
|
||||
"navigation command {command:?} snapshotted the draft"
|
||||
);
|
||||
assert!(composer.vim_history.undo.is_empty());
|
||||
assert!(composer.edit_history.undo.is_empty());
|
||||
}
|
||||
assert_eq!(composer.current_text(), text);
|
||||
}
|
||||
@@ -670,8 +670,8 @@ fn find_jump_and_search_navigation_never_snapshot_the_draft() {
|
||||
ctrl_r(&mut composer);
|
||||
keys(&mut composer, "alpha");
|
||||
assert_eq!(composer.current_text(), "alpha archived");
|
||||
assert!(composer.vim_history.pending.is_none());
|
||||
assert!(composer.vim_history.undo.is_empty());
|
||||
assert!(composer.edit_history.pending.is_none());
|
||||
assert!(composer.edit_history.undo.is_empty());
|
||||
escape(&mut composer);
|
||||
assert_eq!(composer.current_text(), "alpha beta\nalpha gamma");
|
||||
}
|
||||
@@ -45,7 +45,7 @@ use super::ActivePopup;
|
||||
use super::ChatComposer;
|
||||
use super::ComposerDraft;
|
||||
use super::InputResult;
|
||||
use super::vim_history::VimHistory;
|
||||
use super::edit_history::EditHistory;
|
||||
use crate::app_event::AppEvent;
|
||||
use crate::key_hint;
|
||||
use crate::key_hint::KeyBinding;
|
||||
@@ -65,7 +65,7 @@ pub(super) struct HistorySearchSession {
|
||||
/// Freeze the fallback only when background edits diverge from the saved draft.
|
||||
preview_draft: Option<ComposerDraft>,
|
||||
/// Same-draft Vim edits to restore when a temporary preview is canceled.
|
||||
original_vim_history: VimHistory,
|
||||
original_edit_history: EditHistory,
|
||||
/// Active and completed Vim commands suspended during temporary draft replacement.
|
||||
original_vim_state: VimPersistentState,
|
||||
/// Footer-owned query text typed or pasted while Ctrl+R search is active.
|
||||
@@ -139,7 +139,7 @@ impl ChatComposer {
|
||||
self.popups.active = ActivePopup::None;
|
||||
self.attachments.clear_remote_image_selection();
|
||||
let original_draft = self.snapshot_draft();
|
||||
let original_vim_history = std::mem::take(&mut self.vim_history);
|
||||
let original_edit_history = std::mem::take(&mut self.edit_history);
|
||||
let mut original_vim_state = VimPersistentState::default();
|
||||
self.draft
|
||||
.textarea
|
||||
@@ -147,7 +147,7 @@ impl ChatComposer {
|
||||
self.history_search = Some(HistorySearchSession {
|
||||
preview_draft: None,
|
||||
original_draft,
|
||||
original_vim_history,
|
||||
original_edit_history,
|
||||
original_vim_state,
|
||||
query: String::new(),
|
||||
status: HistorySearchStatus::Idle,
|
||||
@@ -326,7 +326,7 @@ impl ChatComposer {
|
||||
self.history.reset_navigation();
|
||||
self.footer.mode = reset_mode_after_activity(self.footer.mode);
|
||||
self.with_sparkle_history_preview(|composer| composer.restore_draft(search.original_draft));
|
||||
self.vim_history = search.original_vim_history;
|
||||
self.edit_history = search.original_edit_history;
|
||||
self.draft
|
||||
.textarea
|
||||
.swap_vim_persistent_state(&mut search.original_vim_state);
|
||||
|
||||
@@ -48,7 +48,7 @@ impl ChatComposer {
|
||||
.preview_draft
|
||||
.get_or_insert_with(|| search.original_draft.clone());
|
||||
let preview = self.snapshot_draft();
|
||||
let preview_vim_history = std::mem::take(&mut self.vim_history);
|
||||
let preview_edit_history = std::mem::take(&mut self.edit_history);
|
||||
let mut preview_vim_state = VimPersistentState::default();
|
||||
self.draft
|
||||
.textarea
|
||||
@@ -57,10 +57,10 @@ impl ChatComposer {
|
||||
self.draft
|
||||
.textarea
|
||||
.swap_vim_persistent_state(&mut search.original_vim_state);
|
||||
self.vim_history = search.original_vim_history;
|
||||
self.edit_history = search.original_edit_history;
|
||||
edit(self);
|
||||
search.original_draft = self.snapshot_draft();
|
||||
search.original_vim_history = std::mem::take(&mut self.vim_history);
|
||||
search.original_edit_history = std::mem::take(&mut self.edit_history);
|
||||
self.draft
|
||||
.textarea
|
||||
.swap_vim_persistent_state(&mut search.original_vim_state);
|
||||
@@ -69,6 +69,6 @@ impl ChatComposer {
|
||||
self.draft
|
||||
.textarea
|
||||
.swap_vim_persistent_state(&mut preview_vim_state);
|
||||
self.vim_history = preview_vim_history;
|
||||
self.edit_history = preview_edit_history;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,12 +25,12 @@ impl ChatComposer {
|
||||
if self.draft.textarea.is_vim_operator_pending() {
|
||||
self.draft.textarea.enter_vim_normal_mode();
|
||||
}
|
||||
self.finish_vim_edit();
|
||||
self.finish_edit();
|
||||
let mut vim_state = VimPersistentState::default();
|
||||
self.draft
|
||||
.textarea
|
||||
.swap_vim_persistent_state(&mut vim_state);
|
||||
let started_vim_edit = self.begin_direct_vim_edit();
|
||||
let started_edit = self.begin_direct_edit();
|
||||
// Recovered answers turn the draft into a prompt, so escape any command prefix
|
||||
// before appending them. Shell mode stores its `!` outside the textarea.
|
||||
let text = self.draft.textarea.text();
|
||||
@@ -60,8 +60,8 @@ impl ChatComposer {
|
||||
self.draft
|
||||
.textarea
|
||||
.swap_vim_persistent_state(&mut vim_state);
|
||||
if started_vim_edit {
|
||||
self.finish_vim_edit();
|
||||
if started_edit {
|
||||
self.finish_edit();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ impl ChatComposer {
|
||||
}
|
||||
|
||||
pub(crate) fn reset_vim_mode(&mut self) {
|
||||
self.vim_history = VimHistory::default();
|
||||
self.edit_history = EditHistory::default();
|
||||
self.draft.textarea.enter_vim_insert_mode();
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ impl ChatComposer {
|
||||
if !text.is_empty() && self.sparkle.draft.get() == sparkle::SparkleDraft::Untouched {
|
||||
self.dismiss_sparkle();
|
||||
}
|
||||
let started_vim_edit = self.begin_direct_vim_edit();
|
||||
let started_edit = self.begin_direct_edit();
|
||||
let elements_before = target
|
||||
.replaces_text()
|
||||
.then(|| self.draft.textarea.element_payloads());
|
||||
@@ -45,8 +45,8 @@ impl ChatComposer {
|
||||
self.sync_bash_mode_from_text();
|
||||
}
|
||||
self.sync_popups();
|
||||
if started_vim_edit {
|
||||
self.finish_vim_edit();
|
||||
if started_edit {
|
||||
self.finish_edit();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,13 +172,13 @@ impl ChatComposer {
|
||||
}
|
||||
FlushResult::Typed(ch) => {
|
||||
let completion_space = self.draft.skill_completion_space_for_char(ch);
|
||||
let started_vim_edit = completion_space.is_some() && self.begin_direct_vim_edit();
|
||||
let started_edit = completion_space.is_some() && self.begin_direct_edit();
|
||||
if let Some(space) = completion_space {
|
||||
self.draft.textarea.replace_range(space..space + 1, "");
|
||||
}
|
||||
self.insert_str(ch.to_string().as_str());
|
||||
if started_vim_edit {
|
||||
self.finish_vim_edit();
|
||||
if started_edit {
|
||||
self.finish_edit();
|
||||
}
|
||||
true
|
||||
}
|
||||
@@ -228,7 +228,7 @@ impl ChatComposer {
|
||||
return true;
|
||||
}
|
||||
self.draft.textarea_state.get_mut().follow_cursor();
|
||||
let started_vim_edit = self.begin_direct_vim_edit();
|
||||
let started_edit = self.begin_direct_edit();
|
||||
let elements_before = self.draft.textarea.element_payloads();
|
||||
let target = self.draft.textarea.edit_target();
|
||||
let before_cursor = &self.draft.textarea.text()[..target.start()];
|
||||
@@ -271,8 +271,8 @@ impl ChatComposer {
|
||||
self.draft.paste_burst.clear_after_explicit_paste();
|
||||
self.reconcile_deleted_elements(elements_before);
|
||||
self.sync_popups();
|
||||
if started_vim_edit {
|
||||
self.finish_vim_edit();
|
||||
if started_edit {
|
||||
self.finish_edit();
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ impl ChatComposer {
|
||||
self.popups.active = ActivePopup::None;
|
||||
if self.draft.textarea.wants_vim_search_key(key) {
|
||||
self.edit_restricted_draft(key);
|
||||
} else if self.handle_vim_history_key(key) {
|
||||
} else if self.handle_edit_history_key(key) {
|
||||
self.draft.textarea_state.get_mut().follow_cursor();
|
||||
} else {
|
||||
// Enter cannot submit here. Preserve a possible legacy paste
|
||||
|
||||
@@ -177,7 +177,7 @@ impl ChatComposer {
|
||||
if self.history_search.is_some()
|
||||
|| (Self::is_history_search_key(&key, &self.history_search_previous_keys)
|
||||
&& (self.popups.active() || !self.draft.textarea.wants_vim_search_key(key))
|
||||
&& !self.wants_vim_history_key(key))
|
||||
&& !self.wants_edit_history_key(key))
|
||||
|| self.empty_prompt_shortcut_toggle(&key).is_some()
|
||||
{
|
||||
return None;
|
||||
|
||||
@@ -8,7 +8,7 @@ impl ChatComposer {
|
||||
pub(crate) fn cancel_vim_search(&mut self) -> bool {
|
||||
let canceled = self.draft.textarea.cancel_vim_search();
|
||||
if canceled {
|
||||
self.finish_vim_edit();
|
||||
self.finish_edit();
|
||||
}
|
||||
canceled
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user