From d0264b756e02078c16eb51e96556a2a494421887 Mon Sep 17 00:00:00 2001 From: Ian MacLeod Date: Thu, 19 Nov 2026 23:41:54 +0000 Subject: [PATCH] Share slash command argument range mapping in the TUI (#62624) ## What changed Use `slash_args_elements` in both the chat composer and slash command dispatch. The shared helper converts text element byte ranges to offsets relative to the command arguments and clips them to the argument bounds. GitOrigin-RevId: 72e2fa42bf0d3fc385a58cc13fb6741c896db900 --- codex-rs/tui/src/bottom_pane/chat_composer.rs | 5 +-- .../bottom_pane/chat_composer/slash_input.rs | 30 ----------------- codex-rs/tui/src/bottom_pane/prompt_args.rs | 31 +++++++++++++++++ codex-rs/tui/src/chatwidget/slash_dispatch.rs | 33 ++----------------- 4 files changed, 37 insertions(+), 62 deletions(-) diff --git a/codex-rs/tui/src/bottom_pane/chat_composer.rs b/codex-rs/tui/src/bottom_pane/chat_composer.rs index 2bb5b7d129..89bc83b55f 100644 --- a/codex-rs/tui/src/bottom_pane/chat_composer.rs +++ b/codex-rs/tui/src/bottom_pane/chat_composer.rs @@ -395,6 +395,7 @@ use super::mentions_v2::MentionV2Selection; use super::paste_burst::CharDecision; use super::paste_burst::PasteBurst; use super::prompt_args::parse_slash_name; +use super::prompt_args::slash_args_elements; use super::skill_popup::MentionItem; use super::skill_popup::SkillPopup; use super::slash_commands::ServiceTierCommand; @@ -3534,7 +3535,7 @@ impl ChatComposer { self.stage_slash_command_history(&command); - let mut args_elements = slash_input::args_elements( + let mut args_elements = slash_args_elements( inline_command.rest, inline_command.rest_offset, &self.draft.textarea.text_elements(), @@ -3568,7 +3569,7 @@ impl ChatComposer { let (prepared_text, prepared_elements) = self.prepare_submission_text(record_history)?; let (prepared_rest, prepared_rest_offset) = slash_input::prepared_args(&prepared_text)?; let mut args_elements = - slash_input::args_elements(prepared_rest, prepared_rest_offset, &prepared_elements); + slash_args_elements(prepared_rest, prepared_rest_offset, &prepared_elements); let trimmed_rest = prepared_rest.trim(); args_elements = Self::trim_text_elements(prepared_rest, trimmed_rest, args_elements); self.draft.textarea.enter_vim_insert_mode(); diff --git a/codex-rs/tui/src/bottom_pane/chat_composer/slash_input.rs b/codex-rs/tui/src/bottom_pane/chat_composer/slash_input.rs index 51eba279e8..c59e72f0e5 100644 --- a/codex-rs/tui/src/bottom_pane/chat_composer/slash_input.rs +++ b/codex-rs/tui/src/bottom_pane/chat_composer/slash_input.rs @@ -16,8 +16,6 @@ use crate::bottom_pane::slash_commands::SlashCommandItem; use crate::bottom_pane::slash_commands::find_slash_command; use crate::bottom_pane::slash_commands::has_slash_command_prefix; use crate::slash_command::SlashCommand; -use codex_protocol::user_input::ByteRange; -use codex_protocol::user_input::TextElement; use super::super::footer::reset_mode_after_activity; use super::ActivePopup; @@ -556,34 +554,6 @@ pub(super) fn prepared_args(prepared_text: &str) -> Option<(&str, usize)> { Some((prepared_rest, prepared_rest_offset)) } -/// Translate full-text element ranges into command-argument ranges. -/// -/// `rest_offset` is the byte offset where `rest` begins in the full text. -pub(super) fn args_elements( - rest: &str, - rest_offset: usize, - text_elements: &[TextElement], -) -> Vec { - if rest.is_empty() || text_elements.is_empty() { - return Vec::new(); - } - text_elements - .iter() - .filter_map(|elem| { - if elem.byte_range.end <= rest_offset { - return None; - } - let start = elem.byte_range.start.saturating_sub(rest_offset); - let mut end = elem.byte_range.end.saturating_sub(rest_offset); - if start >= rest.len() { - return None; - } - end = end.min(rest.len()); - (start < end).then_some(elem.map_range(|_| ByteRange { start, end })) - }) - .collect() -} - pub(super) fn command_popup_filter_text(first_line: &str, cursor: usize) -> Option { let (name, _rest) = command_under_cursor(first_line, cursor)?; Some(format!("/{name}")) diff --git a/codex-rs/tui/src/bottom_pane/prompt_args.rs b/codex-rs/tui/src/bottom_pane/prompt_args.rs index 7c816d5ee7..ce63d0f583 100644 --- a/codex-rs/tui/src/bottom_pane/prompt_args.rs +++ b/codex-rs/tui/src/bottom_pane/prompt_args.rs @@ -1,3 +1,6 @@ +use codex_protocol::user_input::ByteRange; +use codex_protocol::user_input::TextElement; + /// Parse a first-line slash command of the form `/name `. /// Returns `(name, rest_after_name, rest_offset)` if the line begins with `/` /// and contains a non-empty name; otherwise returns `None`. @@ -23,3 +26,31 @@ pub fn parse_slash_name(line: &str) -> Option<(&str, &str, usize)> { let rest_offset = rest_start_in_stripped + 1; Some((name, rest, rest_offset)) } + +/// Translate full-text element ranges into command-argument ranges. +/// +/// `rest_offset` is the byte offset where `rest` begins in the full text. +pub(crate) fn slash_args_elements( + rest: &str, + rest_offset: usize, + text_elements: &[TextElement], +) -> Vec { + if rest.is_empty() || text_elements.is_empty() { + return Vec::new(); + } + text_elements + .iter() + .filter_map(|elem| { + if elem.byte_range.end <= rest_offset { + return None; + } + let start = elem.byte_range.start.saturating_sub(rest_offset); + let mut end = elem.byte_range.end.saturating_sub(rest_offset); + if start >= rest.len() { + return None; + } + end = end.min(rest.len()); + (start < end).then_some(elem.map_range(|_| ByteRange { start, end })) + }) + .collect() +} diff --git a/codex-rs/tui/src/chatwidget/slash_dispatch.rs b/codex-rs/tui/src/chatwidget/slash_dispatch.rs index b4fd5e2232..f9d1ccb8a7 100644 --- a/codex-rs/tui/src/chatwidget/slash_dispatch.rs +++ b/codex-rs/tui/src/chatwidget/slash_dispatch.rs @@ -10,6 +10,7 @@ use crate::app::WindowsSandboxHost; use crate::app_event::ManagedWorktreeMode; use crate::app_event::ThreadGoalSetMode; use crate::bottom_pane::prompt_args::parse_slash_name; +use crate::bottom_pane::prompt_args::slash_args_elements; use crate::bottom_pane::slash_commands::BuiltinCommandFlags; use crate::bottom_pane::slash_commands::ServiceTierCommand; use crate::bottom_pane::slash_commands::SlashCommandItem; @@ -1267,11 +1268,8 @@ impl ChatWidget { let trimmed_start = rest.trim_start(); let leading_trimmed = rest.len().saturating_sub(trimmed_start.len()); let trimmed_rest = trimmed_start.trim_end(); - let args_elements = Self::slash_command_args_elements( - trimmed_rest, - rest_offset + leading_trimmed, - &text_elements, - ); + let args_elements = + slash_args_elements(trimmed_rest, rest_offset + leading_trimmed, &text_elements); self.dispatch_prepared_command_with_args( cmd, PreparedSlashCommandArgs { @@ -1407,31 +1405,6 @@ impl ChatWidget { } } - fn slash_command_args_elements( - rest: &str, - rest_offset: usize, - text_elements: &[TextElement], - ) -> Vec { - if rest.is_empty() || text_elements.is_empty() { - return Vec::new(); - } - text_elements - .iter() - .filter_map(|elem| { - if elem.byte_range.end <= rest_offset { - return None; - } - let start = elem.byte_range.start.saturating_sub(rest_offset); - let mut end = elem.byte_range.end.saturating_sub(rest_offset); - if start >= rest.len() { - return None; - } - end = end.min(rest.len()); - (start < end).then_some(elem.map_range(|_| ByteRange { start, end })) - }) - .collect() - } - fn ensure_slash_command_allowed_in_side_conversation(&mut self, cmd: SlashCommand) -> bool { if !self.active_side_conversation || cmd.available_in_side_conversation() { return true;