Preserve tabs in non-bracketed paste bursts (#45454)

## Why

When terminals deliver pasted text as individual key events, tabs can trigger completion, submission, or queuing instead of preserving indentation in the draft.

## What changed

Capture unmodified `Tab` events during paste bursts before shortcut dispatch, including after short Unicode prefixes. Refresh the burst idle timeout when appending tabs or newlines, and flush expired bursts before handling manual `Tab` shortcuts.

## Testing

Add regression tests for multiline tab preservation, ASCII and Unicode prefixes, idle timeout refresh, completion suppression, and normal submission and queue shortcuts. Add a snapshot for pasted indentation.

GitOrigin-RevId: ba404cfe66c23f37da5a0db7cccfdd3d4c4af331
This commit is contained in:
Eric Traut authored and copyberry committed 2026-09-14 16:05:17 +00:00
1 parent 4d8eca1ff3
commit 7a48b95c6c
6 files changed
+231 -12

No files matched your search

@@ -0,0 +1,41 @@
//! Capture raw paste tabs before completion and submission shortcuts see them.
use super::*;
impl ChatComposer {
pub(super) fn handle_paste_tab(&mut self, key: KeyEvent, now: Instant) -> bool {
if key.code != KeyCode::Tab
|| !key.modifiers.is_empty()
|| self.draft.disable_paste_burst
|| !self.draft.textarea.allows_paste_burst()
{
return false;
}
self.handle_paste_burst_flush(now);
if self
.draft
.paste_burst
.append_control_char_if_active('\t', now)
{
return true;
}
// Short non-ASCII prefixes are inserted directly, without a held first character.
if self
.draft
.paste_burst
.direct_insert_newline_should_insert(now)
{
self.draft
.paste_burst
.begin_with_retro_grabbed(String::new(), now);
return self
.draft
.paste_burst
.append_control_char_if_active('\t', now);
}
false
}
}
@@ -0,0 +1,147 @@
//! Raw paste tabs remain draft text until the user explicitly submits or queues it.
use super::tests::new_test_composer;
use super::*;
use pretty_assertions::assert_eq;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
#[test]
fn paste_burst_tabs_preserve_multiline_draft() {
for running in [false, true] {
for first_line in ["x", "界", "first line\n"] {
let (mut composer, _rx) = new_test_composer();
composer.set_task_running(running);
let mut now = Instant::now();
let payload = format!("{first_line}\t\tsecond line\n\tthird line\n");
for ch in payload.chars() {
let (result, _) = match ch {
'\t' => {
assert!(composer.handle_paste_tab(KeyEvent::from(KeyCode::Tab), now));
(InputResult::None, true)
}
'\n' => composer.handle_submission_with_time(/*should_queue*/ false, now),
ch => composer
.handle_input_basic_with_time(KeyEvent::from(KeyCode::Char(ch)), now),
};
assert_eq!(result, InputResult::None);
now += Duration::from_millis(/*millis*/ 1);
}
composer.handle_paste_burst_flush(now + PasteBurst::recommended_active_flush_delay());
assert_eq!(composer.current_text(), payload);
if running && first_line == "first line\n" {
let mut terminal = Terminal::new(TestBackend::new(60, 8)).unwrap();
terminal
.draw(|frame| composer.render(frame.area(), frame.buffer_mut()))
.unwrap();
insta::assert_snapshot!("paste_burst_tab_indentation", terminal.backend());
}
let (result, _) = composer.handle_key_event(KeyEvent::from(KeyCode::Tab));
let expected = if running {
InputResult::Queued {
text: payload.trim().to_string(),
text_elements: Vec::new(),
action: QueuedInputAction::Plain,
pending_pastes: Vec::new(),
}
} else {
InputResult::Submitted {
text: payload.trim().to_string(),
text_elements: Vec::new(),
}
};
assert_eq!(result, expected);
}
}
}
#[test]
fn paste_burst_tabs_refresh_idle_timeout() {
for first_char in ['x', '界'] {
let (mut composer, _rx) = new_test_composer();
let mut now = Instant::now();
composer.handle_input_basic_with_time(KeyEvent::from(KeyCode::Char(first_char)), now);
assert!(composer.handle_paste_tab(KeyEvent::from(KeyCode::Tab), now));
for _ in 0..3 {
now += PasteBurst::recommended_active_flush_delay() / 2;
assert!(composer.handle_paste_tab(KeyEvent::from(KeyCode::Tab), now));
}
composer.handle_paste_burst_flush(now + PasteBurst::recommended_active_flush_delay());
assert_eq!(composer.current_text(), format!("{first_char}\t\t\t\t"));
assert!(!composer.handle_paste_tab(
KeyEvent::from(KeyCode::Tab),
now + PasteBurst::recommended_active_flush_delay()
));
}
}
#[test]
fn paste_burst_tab_does_not_accept_a_completion() {
let (mut composer, _rx) = new_test_composer();
composer.insert_str("/");
assert!(matches!(composer.popups.active, ActivePopup::Command(_)));
composer
.draft
.paste_burst
.begin_with_retro_grabbed("review this".to_string(), Instant::now());
let (result, _) = composer.handle_key_event(KeyEvent::from(KeyCode::Tab));
assert_eq!(result, InputResult::None);
let pasted = composer
.draft
.paste_burst
.flush_before_modified_input()
.unwrap();
composer.handle_paste(pasted);
assert_eq!(composer.current_text(), "/review this\t");
}
#[test]
fn paste_burst_expired_before_tab_still_queues() {
let (mut composer, _rx) = new_test_composer();
composer.set_task_running(/*running*/ true);
composer.handle_input_basic_with_time(
KeyEvent::from(KeyCode::Char('x')),
Instant::now() - Duration::from_secs(/*secs*/ 1),
);
let (result, _) = composer.handle_key_event(KeyEvent::from(KeyCode::Tab));
assert_eq!(
result,
InputResult::Queued {
text: "x".to_string(),
text_elements: Vec::new(),
action: QueuedInputAction::Plain,
pending_pastes: Vec::new(),
}
);
}
#[test]
fn paste_burst_modified_queue_binding_still_dispatches() {
let (mut composer, _rx) = new_test_composer();
composer.set_task_running(/*running*/ true);
composer.queue_keys = vec![key_hint::ctrl(KeyCode::Char('q'))];
composer.handle_input_basic_with_time(KeyEvent::from(KeyCode::Char('x')), Instant::now());
let (result, _) =
composer.handle_key_event(KeyEvent::new(KeyCode::Char('q'), KeyModifiers::CONTROL));
assert_eq!(
result,
InputResult::Queued {
text: "x".to_string(),
text_elements: Vec::new(),
action: QueuedInputAction::Plain,
pending_pastes: Vec::new(),
}
);
}
@@ -20,7 +20,10 @@ impl ChatComposer {
if !self.draft.disable_paste_burst
&& self.draft.paste_burst.is_active()
&& !in_slash_context
&& self.draft.paste_burst.append_newline_if_active(now)
&& self
.draft
.paste_burst
.append_control_char_if_active('\n', now)
{
return true;
}
@@ -0,0 +1,13 @@
---
source: tui/src/bottom_pane/chat_composer/paste_tests.rs
assertion_line: 38
expression: terminal.backend()
---
" "
"› first line "
" second line "
" third line "
" "
" "
" "
" tab to queue message 100% context left "