Skip to content

Commit dbec6aa

Browse files
committed
Clean ups
1 parent 885bf8e commit dbec6aa

File tree

1 file changed

+13
-87
lines changed

1 file changed

+13
-87
lines changed

codex-rs/tui/src/bottom_pane/chat_composer.rs

Lines changed: 13 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,12 @@ impl ChatComposer {
439439
let (text, cursor) = Self::prompt_command_text(&prompt.name, &args);
440440
self.textarea.set_text(&text);
441441
let target = cursor.or_else(|| {
442-
(!self.textarea.text().is_empty())
443-
.then_some(self.textarea.text().len())
442+
let text = self.textarea.text();
443+
if text.is_empty() {
444+
None
445+
} else {
446+
Some(text.len())
447+
}
444448
});
445449
if let Some(pos) = target {
446450
self.textarea.set_cursor(pos);
@@ -1193,7 +1197,7 @@ impl ChatComposer {
11931197
let cursor = self.textarea.cursor();
11941198
let caret_on_first_line = cursor <= first_line_end;
11951199

1196-
let in_slash_name_context = if first_line.starts_with('/') && caret_on_first_line {
1200+
let is_editing_slash_command_name = if first_line.starts_with('/') && caret_on_first_line {
11971201
let token_end = first_line
11981202
.char_indices()
11991203
.find(|(_, c)| c.is_whitespace())
@@ -1206,14 +1210,14 @@ impl ChatComposer {
12061210

12071211
match &mut self.active_popup {
12081212
ActivePopup::Command(popup) => {
1209-
if in_slash_name_context {
1213+
if is_editing_slash_command_name {
12101214
popup.on_composer_text_change(first_line.to_string());
12111215
} else {
12121216
self.active_popup = ActivePopup::None;
12131217
}
12141218
}
12151219
_ => {
1216-
if in_slash_name_context {
1220+
if is_editing_slash_command_name {
12171221
let mut command_popup = CommandPopup::new(self.custom_prompts.clone());
12181222
command_popup.on_composer_text_change(first_line.to_string());
12191223
self.active_popup = ActivePopup::Command(command_popup);
@@ -1644,10 +1648,6 @@ mod tests {
16441648

16451649
#[test]
16461650
fn handle_paste_small_inserts_text() {
1647-
use crossterm::event::KeyCode;
1648-
use crossterm::event::KeyEvent;
1649-
use crossterm::event::KeyModifiers;
1650-
16511651
let (tx, _rx) = unbounded_channel::<AppEvent>();
16521652
let sender = AppEventSender::new(tx);
16531653
let mut composer = ChatComposer::new(
@@ -1673,10 +1673,6 @@ mod tests {
16731673

16741674
#[test]
16751675
fn empty_enter_returns_none() {
1676-
use crossterm::event::KeyCode;
1677-
use crossterm::event::KeyEvent;
1678-
use crossterm::event::KeyModifiers;
1679-
16801676
let (tx, _rx) = unbounded_channel::<AppEvent>();
16811677
let sender = AppEventSender::new(tx);
16821678
let mut composer = ChatComposer::new(
@@ -1700,10 +1696,6 @@ mod tests {
17001696

17011697
#[test]
17021698
fn handle_paste_large_uses_placeholder_and_replaces_on_submit() {
1703-
use crossterm::event::KeyCode;
1704-
use crossterm::event::KeyEvent;
1705-
use crossterm::event::KeyModifiers;
1706-
17071699
let (tx, _rx) = unbounded_channel::<AppEvent>();
17081700
let sender = AppEventSender::new(tx);
17091701
let mut composer = ChatComposer::new(
@@ -1734,10 +1726,6 @@ mod tests {
17341726

17351727
#[test]
17361728
fn edit_clears_pending_paste() {
1737-
use crossterm::event::KeyCode;
1738-
use crossterm::event::KeyEvent;
1739-
use crossterm::event::KeyModifiers;
1740-
17411729
let large = "y".repeat(LARGE_PASTE_CHAR_THRESHOLD + 1);
17421730
let (tx, _rx) = unbounded_channel::<AppEvent>();
17431731
let sender = AppEventSender::new(tx);
@@ -1759,9 +1747,6 @@ mod tests {
17591747

17601748
#[test]
17611749
fn ui_snapshots() {
1762-
use crossterm::event::KeyCode;
1763-
use crossterm::event::KeyEvent;
1764-
use crossterm::event::KeyModifiers;
17651750
use ratatui::Terminal;
17661751
use ratatui::backend::TestBackend;
17671752

@@ -1878,9 +1863,6 @@ mod tests {
18781863

18791864
// Test helper: simulate human typing with a brief delay and flush the paste-burst buffer
18801865
fn type_chars_humanlike(composer: &mut ChatComposer, chars: &[char]) {
1881-
use crossterm::event::KeyCode;
1882-
use crossterm::event::KeyEvent;
1883-
use crossterm::event::KeyModifiers;
18841866
for &ch in chars {
18851867
let _ = composer.handle_key_event(KeyEvent::new(KeyCode::Char(ch), KeyModifiers::NONE));
18861868
std::thread::sleep(ChatComposer::recommended_paste_flush_delay());
@@ -1890,10 +1872,6 @@ mod tests {
18901872

18911873
#[test]
18921874
fn slash_init_dispatches_command_and_does_not_submit_literal_text() {
1893-
use crossterm::event::KeyCode;
1894-
use crossterm::event::KeyEvent;
1895-
use crossterm::event::KeyModifiers;
1896-
18971875
let (tx, _rx) = unbounded_channel::<AppEvent>();
18981876
let sender = AppEventSender::new(tx);
18991877
let mut composer = ChatComposer::new(
@@ -1927,10 +1905,6 @@ mod tests {
19271905

19281906
#[test]
19291907
fn slash_tab_completion_moves_cursor_to_end() {
1930-
use crossterm::event::KeyCode;
1931-
use crossterm::event::KeyEvent;
1932-
use crossterm::event::KeyModifiers;
1933-
19341908
let (tx, _rx) = unbounded_channel::<AppEvent>();
19351909
let sender = AppEventSender::new(tx);
19361910
let mut composer = ChatComposer::new(
@@ -1952,10 +1926,6 @@ mod tests {
19521926

19531927
#[test]
19541928
fn slash_mention_dispatches_command_and_inserts_at() {
1955-
use crossterm::event::KeyCode;
1956-
use crossterm::event::KeyEvent;
1957-
use crossterm::event::KeyModifiers;
1958-
19591929
let (tx, _rx) = unbounded_channel::<AppEvent>();
19601930
let sender = AppEventSender::new(tx);
19611931
let mut composer = ChatComposer::new(
@@ -1987,10 +1957,6 @@ mod tests {
19871957

19881958
#[test]
19891959
fn test_multiple_pastes_submission() {
1990-
use crossterm::event::KeyCode;
1991-
use crossterm::event::KeyEvent;
1992-
use crossterm::event::KeyModifiers;
1993-
19941960
let (tx, _rx) = unbounded_channel::<AppEvent>();
19951961
let sender = AppEventSender::new(tx);
19961962
let mut composer = ChatComposer::new(
@@ -2066,10 +2032,6 @@ mod tests {
20662032

20672033
#[test]
20682034
fn test_placeholder_deletion() {
2069-
use crossterm::event::KeyCode;
2070-
use crossterm::event::KeyEvent;
2071-
use crossterm::event::KeyModifiers;
2072-
20732035
let (tx, _rx) = unbounded_channel::<AppEvent>();
20742036
let sender = AppEventSender::new(tx);
20752037
let mut composer = ChatComposer::new(
@@ -2138,10 +2100,6 @@ mod tests {
21382100

21392101
#[test]
21402102
fn test_partial_placeholder_deletion() {
2141-
use crossterm::event::KeyCode;
2142-
use crossterm::event::KeyEvent;
2143-
use crossterm::event::KeyModifiers;
2144-
21452103
let (tx, _rx) = unbounded_channel::<AppEvent>();
21462104
let sender = AppEventSender::new(tx);
21472105
let mut composer = ChatComposer::new(
@@ -2276,10 +2234,6 @@ mod tests {
22762234

22772235
#[test]
22782236
fn backspace_with_multibyte_text_before_placeholder_does_not_panic() {
2279-
use crossterm::event::KeyCode;
2280-
use crossterm::event::KeyEvent;
2281-
use crossterm::event::KeyModifiers;
2282-
22832237
let (tx, _rx) = unbounded_channel::<AppEvent>();
22842238
let sender = AppEventSender::new(tx);
22852239
let mut composer = ChatComposer::new(
@@ -2373,6 +2327,8 @@ mod tests {
23732327

23742328
#[test]
23752329
fn selecting_custom_prompt_without_args_submits_content() {
2330+
let prompt_text = "Hello from saved prompt";
2331+
23762332
let (tx, _rx) = unbounded_channel::<AppEvent>();
23772333
let sender = AppEventSender::new(tx);
23782334
let mut composer = ChatComposer::new(
@@ -2387,7 +2343,7 @@ mod tests {
23872343
composer.set_custom_prompts(vec![CustomPrompt {
23882344
name: "my-prompt".to_string(),
23892345
path: "/tmp/my-prompt.md".to_string().into(),
2390-
content: "Hello from saved prompt".to_string(),
2346+
content: prompt_text.to_string(),
23912347
}]);
23922348

23932349
type_chars_humanlike(
@@ -2398,19 +2354,12 @@ mod tests {
23982354
let (result, _needs_redraw) =
23992355
composer.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
24002356

2401-
assert_eq!(
2402-
InputResult::Submitted("Hello from saved prompt".to_string()),
2403-
result
2404-
);
2357+
assert_eq!(InputResult::Submitted(prompt_text.to_string()), result);
24052358
assert!(composer.textarea.is_empty());
24062359
}
24072360

24082361
#[test]
24092362
fn custom_prompt_submission_expands_arguments() {
2410-
use crossterm::event::KeyCode;
2411-
use crossterm::event::KeyEvent;
2412-
use crossterm::event::KeyModifiers;
2413-
24142363
let (tx, _rx) = unbounded_channel::<AppEvent>();
24152364
let sender = AppEventSender::new(tx);
24162365
let mut composer = ChatComposer::new(
@@ -2441,15 +2390,8 @@ mod tests {
24412390
assert!(composer.textarea.is_empty());
24422391
}
24432392

2444-
// Note: manual UI flow test omitted for Enter-with-popup; core logic
2445-
// is covered by other prompt expansion tests.
2446-
24472393
#[test]
24482394
fn custom_prompt_submission_accepts_quoted_values() {
2449-
use crossterm::event::KeyCode;
2450-
use crossterm::event::KeyEvent;
2451-
use crossterm::event::KeyModifiers;
2452-
24532395
let (tx, _rx) = unbounded_channel::<AppEvent>();
24542396
let sender = AppEventSender::new(tx);
24552397
let mut composer = ChatComposer::new(
@@ -2482,10 +2424,6 @@ mod tests {
24822424

24832425
#[test]
24842426
fn custom_prompt_invalid_args_reports_error() {
2485-
use crossterm::event::KeyCode;
2486-
use crossterm::event::KeyEvent;
2487-
use crossterm::event::KeyModifiers;
2488-
24892427
let (tx, mut rx) = unbounded_channel::<AppEvent>();
24902428
let sender = AppEventSender::new(tx);
24912429
let mut composer = ChatComposer::new(
@@ -2529,10 +2467,6 @@ mod tests {
25292467

25302468
#[test]
25312469
fn custom_prompt_missing_required_args_reports_error() {
2532-
use crossterm::event::KeyCode;
2533-
use crossterm::event::KeyEvent;
2534-
use crossterm::event::KeyModifiers;
2535-
25362470
let (tx, mut rx) = unbounded_channel::<AppEvent>();
25372471
let sender = AppEventSender::new(tx);
25382472
let mut composer = ChatComposer::new(
@@ -2581,10 +2515,6 @@ mod tests {
25812515

25822516
#[test]
25832517
fn burst_paste_fast_small_buffers_and_flushes_on_stop() {
2584-
use crossterm::event::KeyCode;
2585-
use crossterm::event::KeyEvent;
2586-
use crossterm::event::KeyModifiers;
2587-
25882518
let (tx, _rx) = unbounded_channel::<AppEvent>();
25892519
let sender = AppEventSender::new(tx);
25902520
let mut composer = ChatComposer::new(
@@ -2625,10 +2555,6 @@ mod tests {
26252555

26262556
#[test]
26272557
fn burst_paste_fast_large_inserts_placeholder_on_flush() {
2628-
use crossterm::event::KeyCode;
2629-
use crossterm::event::KeyEvent;
2630-
use crossterm::event::KeyModifiers;
2631-
26322558
let (tx, _rx) = unbounded_channel::<AppEvent>();
26332559
let sender = AppEventSender::new(tx);
26342560
let mut composer = ChatComposer::new(

0 commit comments

Comments
 (0)