Enable slash-command completion for unavailable threads (#61710)

## Why

Restricted input dismissed the slash-command popup when a thread was unavailable, preventing users from selecting recovery commands from partial input.

## What changed

Keep the command popup available while editing an unavailable thread's draft. Allow navigation, Tab completion, and submission of eligible recovery and local commands, including with custom submit keys. For example, typing `/sub` and pressing Enter selects `/subagents`.

Continue to reject commands unavailable in this state and invalid command arguments. Preserve Vim escape and search behavior without reopening the popup during search.

## Testing

Add regression tests and a popup snapshot covering highlighted recovery completion, blocked commands, custom submit keys, and Vim escape and search behavior.

GitOrigin-RevId: 38021db8cdcd9f7ec7c62cfa0efd8819bf8057da
This commit is contained in:
Eric Traut
2026-11-16 00:03:52 +00:00
committed by copyberry
parent b048d4a95c
commit 96e0673f6e
5 changed files with 188 additions and 3 deletions
@@ -2087,7 +2087,10 @@ impl ChatComposer {
}
let result = match &mut self.popups.active {
ActivePopup::Command(_) => self.handle_key_event_with_slash_popup(key_event),
ActivePopup::Command(_) => self.handle_key_event_with_slash_popup(
key_event,
slash_input::SlashPopupInputMode::Normal,
),
ActivePopup::File(_) => self.handle_key_event_with_file_popup(key_event),
ActivePopup::Skill(_) => self.handle_key_event_with_skill_popup(key_event),
ActivePopup::MentionV2(_) => self.handle_key_event_with_mentions_v2_popup(key_event),
@@ -99,12 +99,17 @@ impl ChatComposer {
}
self.cancel_history_search();
self.attachments.clear_remote_image_selection();
self.popups.active = ActivePopup::None;
if mode != RestrictedInputMode::UnavailableThread
|| !matches!(self.popups.active, ActivePopup::Command(_))
{
self.popups.active = ActivePopup::None;
}
self.set_disable_paste_burst(/*disabled*/ true);
// Expand in reverse order so remaining ranges stay valid. Textarea replacement
// preserves the cursor and other elements, including images and mentions.
let pending_pastes = std::mem::take(&mut self.draft.pending_pastes);
if !pending_pastes.is_empty() {
self.popups.active = ActivePopup::None;
for element in self.draft.textarea.text_elements().into_iter().rev() {
if let Some((_, text)) = pending_pastes.iter().find(|(placeholder, _)| {
element.placeholder(self.draft.textarea.text()) == Some(placeholder.as_str())
@@ -115,6 +120,66 @@ impl ChatComposer {
}
}
}
if mode == RestrictedInputMode::UnavailableThread
&& self.draft.textarea.wants_vim_search_key(key)
&& matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat)
{
self.popups.active = ActivePopup::None;
self.edit_restricted_draft(key);
return InputResult::None;
}
if mode == RestrictedInputMode::UnavailableThread
&& pending_pastes.is_empty()
&& matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat)
&& let ActivePopup::Command(popup) = &self.popups.active
{
let submit = self.submit_keys.is_pressed(key);
if (submit || key.code == KeyCode::Tab)
&& let Some(selected) = popup.selected_item()
{
let text = self.draft.textarea.text();
if !matches!(selected, CommandItem::Builtin(command)
if (command == SlashCommand::Config || command.available_when_thread_unavailable())
&& text.trim().lines().count() == 1
&& (command.supports_inline_args()
|| text.split_once(char::is_whitespace).is_none_or(|(_, args)| args.trim().is_empty())))
{
return InputResult::None;
}
let popup_key = if submit { KeyCode::Enter.into() } else { key };
let result = self
.handle_key_event_with_slash_popup(
popup_key,
slash_input::SlashPopupInputMode::UnavailableThread,
)
.0;
self.sync_command_popup(/*allow*/ true);
return result;
}
if !submit
&& (matches!(key.code, KeyCode::Up | KeyCode::Down | KeyCode::Esc)
|| matches!(
key,
KeyEvent {
code: KeyCode::Char('p' | 'n'),
modifiers: KeyModifiers::CONTROL,
..
}
))
{
let vim_escape = self.should_handle_vim_insert_escape(key);
let result = self
.handle_key_event_with_slash_popup(
key,
slash_input::SlashPopupInputMode::UnavailableThread,
)
.0;
if vim_escape {
self.edit_restricted_draft(key);
}
return result;
}
}
if pending_pastes.is_empty()
&& matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat)
&& self.submit_keys.is_pressed(key)
@@ -142,6 +207,13 @@ impl ChatComposer {
&& matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat)
{
self.edit_restricted_draft(key);
if mode == RestrictedInputMode::UnavailableThread {
self.sync_command_popup(
self.slash_commands_enabled()
&& !self.draft.is_bash_mode
&& self.draft.textarea.vim_query().is_none(),
);
}
}
InputResult::None
}
@@ -194,6 +194,7 @@ fn unavailable_thread_dispatches_recovery_and_local_commands() {
SlashCommand::Raw,
] {
let (mut composer, _) = new_test_composer();
composer.blocks_direct_input = true;
composer.set_text_content(format!("/{}", command.command()), Vec::new(), Vec::new());
assert_eq!(
composer.handle_restricted_key(
@@ -206,6 +207,93 @@ fn unavailable_thread_dispatches_recovery_and_local_commands() {
}
}
#[test]
fn unavailable_thread_accepts_highlighted_recovery_completion() {
let (mut composer, _) = new_test_composer();
for ch in "/sub".chars() {
composer.handle_restricted_key(
KeyCode::Char(ch).into(),
RestrictedInputMode::UnavailableThread,
);
}
let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 10)).unwrap();
terminal
.draw(|frame| composer.render(frame.area(), frame.buffer_mut()))
.unwrap();
insta::assert_snapshot!("unavailable_thread_recovery_completion", terminal.backend());
assert_eq!(
composer.handle_restricted_key(
KeyCode::Enter.into(),
RestrictedInputMode::UnavailableThread
),
InputResult::Command(SlashCommand::MultiAgents),
);
assert_eq!(composer.current_text(), "");
composer.set_text_content("/comp".into(), Vec::new(), Vec::new());
assert_eq!(
composer.handle_restricted_key(
KeyCode::Enter.into(),
RestrictedInputMode::UnavailableThread
),
InputResult::None,
);
assert_eq!(composer.current_text(), "/comp");
composer.set_text_content("/status ".into(), Vec::new(), Vec::new());
composer.handle_restricted_key(KeyCode::Left.into(), RestrictedInputMode::UnavailableThread);
composer.handle_restricted_key(KeyCode::Down.into(), RestrictedInputMode::UnavailableThread);
assert_eq!(
composer.handle_restricted_key(
KeyCode::Enter.into(),
RestrictedInputMode::UnavailableThread
),
InputResult::None,
);
assert_eq!(composer.current_text(), "/status ");
composer.set_text_content("/".into(), Vec::new(), Vec::new());
composer.blocks_direct_input = true;
let ActivePopup::Command(popup) = &composer.popups.active else {
panic!("expected command popup");
};
for _ in 0..popup.filtered_items().len() {
if matches!(&composer.popups.active, ActivePopup::Command(popup)
if popup.selected_item() == Some(CommandItem::Builtin(SlashCommand::MultiAgents)))
{
break;
}
composer
.handle_restricted_key(KeyCode::Down.into(), RestrictedInputMode::UnavailableThread);
}
let key = KeyEvent::new(KeyCode::Down, KeyModifiers::CONTROL | KeyModifiers::ALT);
composer.submit_keys = vec![key_hint::KeyBinding::from_event(key)];
assert_eq!(
composer.handle_restricted_key(key, RestrictedInputMode::UnavailableThread),
InputResult::Command(SlashCommand::MultiAgents),
);
}
#[test]
fn unavailable_thread_vim_escape_and_search_keep_popup_closed() {
let (mut composer, _) = new_test_composer();
composer.enable_vim_in_insert_mode();
for ch in "/sub".chars() {
composer.handle_restricted_key(
KeyCode::Char(ch).into(),
RestrictedInputMode::UnavailableThread,
);
}
composer.handle_restricted_key(KeyCode::Esc.into(), RestrictedInputMode::UnavailableThread);
assert!(composer.draft.textarea.is_vim_normal_mode());
composer.handle_restricted_key(
KeyCode::Char('/').into(),
RestrictedInputMode::UnavailableThread,
);
assert!(composer.draft.textarea.vim_query().is_some());
assert!(matches!(composer.popups.active, ActivePopup::None));
}
#[test]
fn disconnected_input_dispatches_local_config_command() {
let (mut composer, _) = new_test_composer();
@@ -32,6 +32,12 @@ pub(super) enum SlashValidation {
Deferred,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) enum SlashPopupInputMode {
Normal,
UnavailableThread,
}
pub(super) enum SubmissionValidation {
Valid,
UnknownCommand(String),
@@ -238,6 +244,7 @@ impl ChatComposer {
pub(super) fn handle_key_event_with_slash_popup(
&mut self,
key_event: KeyEvent,
mode: SlashPopupInputMode,
) -> (InputResult, bool) {
if self.handle_empty_prompt_shortcut(&key_event) {
return (InputResult::None, true);
@@ -378,7 +385,7 @@ impl ChatComposer {
..
} => {
if let Some(sel) = popup.selected_item() {
if self.blocks_direct_input {
if self.blocks_direct_input && mode == SlashPopupInputMode::Normal {
let command_is_allowed = match &sel {
CommandItem::Builtin(cmd) => {
parse_slash_name(self.draft.textarea.text()).is_some_and(
@@ -0,0 +1,15 @@
---
source: tui/src/bottom_pane/chat_composer/reconnect_tests.rs
assertion_line: 217
expression: terminal.backend()
---
"› /subagents switch between this session's subagents "
" "
"› /sub "
" "
" "
" "
" "
" "
" "
" 100% context left "