Harden embedded composer input handling (#42408)

## What changed

- Keep `!`, `/`, and `?` prefixes literal in plain-text composers instead of activating command modes or shortcuts.
- Preserve buffered characters when changing Vim mode, cancelling a draft, or pressing Enter during a paste burst.
- Show over-limit errors in embedded composer footers, retain the rejected draft, and schedule redraws when the message expires.
- Clear stale validation errors when replacing a draft or switching form fields.

## Testing

- Add coverage for literal prefixes, buffered typing, single-character paste lines, retained over-limit drafts, and clearing validation errors between fields.

GitOrigin-RevId: ed9d5b2c087abdfefd92d93033ea1c20211d56d7
This commit is contained in:
Eric Traut authored and copyberry committed 2026-09-03 01:46:37 +00:00
1 parent 460b63e5f4
commit 93053c7f5d
9 files changed
+205 -22

No files matched your search

@@ -0,0 +1,79 @@
//! Embedded composers preserve literal drafts and show validation feedback in place.
use super::tests::new_test_composer;
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn plain_text_composer_keeps_command_prefixes_literal() {
let (mut composer, _rx) = new_test_composer();
composer.config = ChatComposerConfig::plain_text();
composer.set_disable_paste_burst(/*disabled*/ true);
composer.set_footer_hint_override(Some(Vec::new()));
for (prefix, modifiers) in [
('!', KeyModifiers::NONE),
('/', KeyModifiers::NONE),
('?', KeyModifiers::NONE),
('?', KeyModifiers::SHIFT),
] {
composer.handle_key_event(KeyEvent::new(KeyCode::Char(prefix), modifiers));
composer.handle_paste("keep this literal".into());
assert_eq!(
composer.handle_key_event(KeyCode::Tab.into()).0,
InputResult::Submitted {
text: format!("{prefix}keep this literal"),
text_elements: Vec::new(),
}
);
}
}
#[test]
fn buffered_typing_survives_vim_changes_and_cancellation() {
let (mut composer, _rx) = new_test_composer();
composer.handle_key_event(KeyCode::Char('x').into());
composer.set_vim_enabled(/*enabled*/ true);
assert_eq!(composer.current_text(), "x");
composer.set_vim_enabled(/*enabled*/ false);
composer.handle_key_event(KeyCode::Char('y').into());
assert_eq!(composer.clear_for_ctrl_c(), Some("xy".into()));
assert!(!composer.is_in_paste_burst());
composer.handle_key_event(KeyCode::Up.into());
assert_eq!(composer.current_text(), "xy");
}
#[test]
fn paste_enter_retains_a_one_character_first_line() {
let (mut composer, _rx) = new_test_composer();
composer.handle_key_event(KeyCode::Char('x').into());
assert!(composer.handle_paste_enter(Instant::now()));
assert_eq!(composer.clear_for_ctrl_c(), Some("x\n".into()));
}
#[test]
fn embedded_composer_shows_validation_error_without_losing_draft() {
let (mut composer, mut rx) = new_test_composer();
composer.config = ChatComposerConfig::plain_text();
composer.set_footer_hint_override(Some(vec![("enter".into(), "submit".into())]));
composer.set_context_window_pending(/*pending*/ true);
let text = "x".repeat(MAX_USER_INPUT_TEXT_CHARS + 1);
composer.handle_paste(text.clone());
assert_eq!(
composer.handle_key_event(KeyCode::Enter.into()).0,
InputResult::None
);
assert_eq!(composer.current_text_with_pending(), text);
assert!(matches!(rx.try_recv(), Ok(AppEvent::InsertHistoryCell(_))));
let area = Rect::new(
/*x*/ 0, /*y*/ 0, /*width*/ 80, /*height*/ 5,
);
let mut buffer = Buffer::empty(area);
composer.render(area, &mut buffer);
let footer = (0..area.width)
.map(|x| buffer[(x, area.bottom() - 1)].symbol())
.collect::<String>();
insta::assert_snapshot!(footer.trim_end(), @" Message too long; limit 1048576 characters");
assert!(composer.footer_flash_delay().is_some());
composer.footer.flash.as_mut().unwrap().expires_at = Instant::now() - Duration::from_secs(1);
assert_eq!(composer.footer_flash_delay(), None);
}
@@ -1,4 +1,5 @@
//! Footer and status-row presentation state for the chat composer.
//! Owners schedule flash expiry redraws; replacing a draft clears its flash.
use std::time::Instant;
@@ -9,9 +10,21 @@ use crate::bottom_pane::footer::FooterMode;
use crate::bottom_pane::footer::GoalStatusIndicator;
use crate::key_hint::KeyBinding;
use crate::key_hint::ShortcutHint;
#[cfg(test)]
use std::time::Duration;
impl super::ChatComposer {
pub(crate) fn footer_flash_delay(&self) -> Option<Duration> {
self.footer
.flash
.as_ref()
.and_then(|flash| flash.expires_at.checked_duration_since(Instant::now()))
}
pub(crate) fn show_footer_flash(&mut self, line: Line<'static>, duration: Duration) {
self.footer.show_flash(line, duration);
}
}
pub(super) struct FooterState {
pub(super) quit_shortcut_expires_at: Option<Instant>,
pub(super) quit_shortcut_key: KeyBinding,
@@ -54,7 +67,6 @@ impl FooterState {
.is_some_and(|flash| Instant::now() < flash.expires_at)
}
#[cfg(test)]
pub(super) fn show_flash(&mut self, line: Line<'static>, duration: Duration) {
let expires_at = Instant::now()
.checked_add(duration)