Files
codex/codex-rs/tui/src/startup_draft_input.rs
T
Eric Traut c7f0d3da0c Preserve multiline pastes without bracketed paste markers (#63785)
## Why

When a terminal sends pasted text as individual keystrokes, the TUI can
mistake a pasted newline for a submit key. This can happen after a short
Unicode prefix or after a redraw has already displayed a pasted line.

## What changed

Keep the paste detection window open after buffered text becomes visible.
Treat plain Enter within that window as a pasted newline and buffer any
following indentation with it. A single IME character still allows Enter
to submit in the regular composer.

Apply the same paste timing to startup drafts so Enter can confirm the
draft once the paste window expires. Keep newline-and-tab-only pastes
literal inside blockquotes.

## Testing

Cover multiline pastes with Unicode prefixes, tabs after pasted newlines,
and Enter handling before and after the paste window expires.

GitOrigin-RevId: e8f63dfead0cc97d44001000e961a6f6d05d4900
2026-11-25 00:57:54 +00:00

278 lines
12 KiB
Rust

//! Edit and confirm the provisional startup draft without executing actions before initialization.
//!
//! Terminal input stays with the original pump through startup. Paste-newline ambiguity is
//! preserved, protected screens do not consume draft input, and confirmation never removes text.
use std::io;
use tokio::time::Instant;
use crossterm::event::KeyCode;
use crossterm::event::KeyEvent;
use crossterm::event::KeyEventKind;
use crossterm::event::KeyModifiers;
use super::STARTUP_PASTE_NEWLINE_TIMEOUT;
use super::StartupCancelled;
use super::StartupDraftInitialScreen;
use super::StartupDraftPump;
use crate::bottom_pane::BottomPane;
use crate::key_hint;
use crate::tui::Tui;
use crate::tui::TuiEvent;
impl StartupDraftPump {
pub(super) fn handle_event(&mut self, tui: &mut Tui, event: TuiEvent) -> io::Result<()> {
let screen_size = tui.screen_size_for_event(&event)?;
if matches!(&event, TuiEvent::Resize(_) | TuiEvent::Resume) {
self.blossom.get_mut().invalidate_window_size();
}
if tui.dismiss_shortcut_on_input(&event, &mut self.key_chord_matcher) {
return Ok(());
}
if let Some((started_at, mut newlines)) = self.pending_paste_newline.take() {
let continues_paste = match &event {
TuiEvent::Key(KeyEvent {
code: KeyCode::Char(_),
modifiers,
kind: KeyEventKind::Press | KeyEventKind::Repeat,
..
}) => {
!key_hint::has_ctrl_or_alt(*modifiers)
&& !modifiers.intersects(
KeyModifiers::SUPER | KeyModifiers::HYPER | KeyModifiers::META,
)
}
TuiEvent::Key(KeyEvent {
code: KeyCode::Enter,
modifiers,
kind: KeyEventKind::Press | KeyEventKind::Repeat,
..
}) if modifiers.is_empty() => {
// Text may still follow a deferred newline, but a fresh Enter
// can confirm the visible draft once its paste window closes.
if started_at.elapsed() <= STARTUP_PASTE_NEWLINE_TIMEOUT
&& self
.bottom_pane
.composer_paste_deadline()
.is_none_or(|deadline| Instant::now().into_std() <= deadline)
{
newlines.push('\n');
self.pending_paste_newline = Some((Instant::now(), newlines));
return Ok(());
}
false
}
TuiEvent::Paste(text) => !text.is_empty(),
TuiEvent::Draw | TuiEvent::Resize(_) | TuiEvent::Resume | TuiEvent::FocusGained => {
self.pending_paste_newline = Some((started_at, newlines));
if self.initial_screen == StartupDraftInitialScreen::Composer {
self.draw(tui, screen_size)?;
}
return Ok(());
}
TuiEvent::FocusLost | TuiEvent::Mouse(_) => {
self.pending_paste_newline = Some((started_at, newlines));
return Ok(());
}
TuiEvent::Key(_) => false,
};
if continues_paste && started_at.elapsed() <= STARTUP_PASTE_NEWLINE_TIMEOUT {
self.bottom_pane.handle_paste(newlines);
}
}
match event {
TuiEvent::Key(key) => {
if self.initial_screen != StartupDraftInitialScreen::Composer
&& !key_hint::ctrl(KeyCode::Char('c')).is_press(key)
&& !key_hint::ctrl(KeyCode::Char('d')).is_press(key)
{
return Ok(());
}
let key = match self.key_chord_matcher.advance(
key,
&self.key_chords,
self.bottom_pane.keymap_contexts(),
) {
crate::keymap::KeyChordMatch::Completed(key) => key,
crate::keymap::KeyChordMatch::PassThrough => key,
crate::keymap::KeyChordMatch::Pending(_)
| crate::keymap::KeyChordMatch::Cancelled => {
tui.frame_requester().schedule_frame();
return Ok(());
}
crate::keymap::KeyChordMatch::Ignored => return Ok(()),
};
if key.code == KeyCode::Enter
&& key.modifiers.is_empty()
&& (self.bottom_pane.is_startup_composer_action(key)
|| !self.bottom_pane.is_safe_startup_editor_key(key))
{
let text_len = self.bottom_pane.composer_text().len();
let retained_paste = self
.bottom_pane
.has_startup_paste_window(Instant::now().into_std());
self.bottom_pane.flush_composer_paste_burst();
if retained_paste
|| self
.bottom_pane
.composer_text()
.len()
.saturating_sub(text_len)
> 1
{
self.pending_paste_newline = Some((Instant::now(), "\n".to_string()));
}
}
if self.initial_screen == StartupDraftInitialScreen::Composer
&& matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat)
&& self.session_action != super::StartupDraftSessionAction::NewWhileRunning
&& self.bottom_pane.is_startup_submit_key(key)
{
self.bottom_pane.flush_composer_paste_burst();
if self.pending_paste_newline.is_none()
&& !self.bottom_pane.composer_text().trim().is_empty()
{
self.submission_pending = true;
self.bottom_pane.set_footer_hint_override(Some(vec![
("Waiting for startup".into(), String::new()),
("esc".into(), "cancel".into()),
]));
}
crate::startup_recovery::remember(
self.bottom_pane.composer_recovery_snapshot(),
);
self.draw(tui, screen_size)?;
return Ok(());
}
if self.submission_pending
&& matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat)
&& key.code == KeyCode::Esc
{
self.cancel_submission();
self.draw(tui, screen_size)?;
return Ok(());
}
let previous_text = self.bottom_pane.composer_text();
if let Err(error) = handle_startup_draft_key(&mut self.bottom_pane, key)
&& !(self.session_action == super::StartupDraftSessionAction::NewWhileRunning
&& StartupCancelled::matches(&error))
{
if StartupCancelled::matches(&error)
&& !tui.terminal.viewport_area.is_empty()
&& let Err(clear_error) = tui.draw(/*height*/ 0, |_| {})
{
tracing::warn!(
error = %clear_error,
"failed to clear the cancelled startup composer"
);
}
return Err(error);
}
if self.bottom_pane.composer_text() != previous_text
|| self.bottom_pane.is_in_paste_burst()
{
self.cancel_submission();
}
}
TuiEvent::Paste(text) => {
if self.initial_screen == StartupDraftInitialScreen::Composer {
self.cancel_submission();
self.bottom_pane.flush_composer_paste_burst();
self.bottom_pane.handle_paste(text);
}
}
TuiEvent::Draw | TuiEvent::Resize(_) | TuiEvent::Resume | TuiEvent::FocusGained => {}
TuiEvent::FocusLost => {
self.blossom.borrow_mut().cancel_replay();
return Ok(());
}
TuiEvent::Mouse(mouse) => {
if !tui.is_owned_screen()
|| self.initial_screen != StartupDraftInitialScreen::Composer
|| !matches!(
self.session_action,
super::StartupDraftSessionAction::New
| super::StartupDraftSessionAction::NewWhileRunning
)
{
return Ok(());
}
// Size, composer, and modal ownership must be current before hit testing.
self.draw(tui, screen_size)?;
if !self.blossom.borrow_mut().handle_mouse(mouse) {
return Ok(());
}
}
}
crate::startup_recovery::remember(self.bottom_pane.composer_recovery_snapshot());
if self.initial_screen == StartupDraftInitialScreen::Composer {
self.draw(tui, screen_size)?;
}
while self.app_event_rx.try_recv().is_ok() {}
Ok(())
}
}
pub(super) fn handle_startup_draft_key(
bottom_pane: &mut BottomPane,
key: KeyEvent,
) -> io::Result<()> {
let _ = bottom_pane.flush_paste_burst_if_due();
if key.kind == KeyEventKind::Release
|| key.code == KeyCode::Enter
&& key.modifiers.is_empty()
&& !bottom_pane.is_safe_startup_editor_key(key)
|| matches!(key.code, KeyCode::Tab | KeyCode::BackTab)
|| bottom_pane.is_startup_composer_action(key)
{
return Ok(());
}
if let KeyCode::Char(_) = key.code {
let (code, modifiers) = key_hint::normalize_key_parts(key.code, key.modifiers);
if key_hint::has_ctrl_or_alt(modifiers) && matches!(code, KeyCode::Char('r' | 'v')) {
return Ok(());
}
let is_ctrl_c = key_hint::ctrl(KeyCode::Char('c')).is_press(key);
let is_ctrl_d = key_hint::ctrl(KeyCode::Char('d')).is_press(key);
if key.kind == KeyEventKind::Press && (is_ctrl_c || is_ctrl_d) {
bottom_pane.flush_composer_paste_burst();
if bottom_pane.composer_is_empty() {
return Err(io::Error::new(io::ErrorKind::Interrupted, StartupCancelled));
}
if is_ctrl_c {
bottom_pane.on_ctrl_c();
return Ok(());
}
}
}
let is_safe_startup_editor_key = bottom_pane.is_safe_startup_editor_key(key);
if !is_safe_startup_editor_key
&& (key_hint::has_ctrl_or_alt(key.modifiers)
|| key.code == KeyCode::Enter
|| key
.modifiers
.intersects(KeyModifiers::SUPER | KeyModifiers::HYPER | KeyModifiers::META)
|| !matches!(
key.code,
KeyCode::Char(_)
| KeyCode::Enter
| KeyCode::Esc
| KeyCode::Left
| KeyCode::Right
| KeyCode::Up
| KeyCode::Down
| KeyCode::Home
| KeyCode::End
| KeyCode::Backspace
| KeyCode::Delete
))
{
return Ok(());
}
let _ = bottom_pane.handle_key_event(key);
Ok(())
}