feat(input): readline keymap — Esc-clear + Ctrl-A/E/W/K/U (#29)
Implement the deferred I1b readline shortcuts in the command input field (ADR-0049, closing issue #29): Esc clear a partly-typed command (only when no completion memo) Ctrl-A cursor to line start (Home alias) Ctrl-E cursor to line end (End alias) Ctrl-W delete the previous word (readline-style, UTF-8 safe) Ctrl-K kill to end of line Ctrl-U kill to start of line Esc precedence is preserved: a live Tab-completion memo still wins (Esc undoes the completion first, ADR-0022); Esc clears only when no memo is alive. While a sidebar panel is focused (Ctrl-O), Esc exits navigation mode upstream and never clears the input draft. Cursor-only keys leave history navigation intact like Home/End; buffer-mutating keys end it like Backspace. New helpers clear_input / delete_prev_word / kill_to_end / kill_to_start in src/app.rs. 22 new Tier-1 tests (2458 pass / 0 fail / 0 skip, clippy clean). ADR-0049 amends ADR-0046's OOS list; requirements.md I1b marked done.
This commit is contained in:
+317
-2
@@ -1217,6 +1217,13 @@ impl App {
|
||||
match (key.code, key.modifiers) {
|
||||
(KeyCode::Char('c'), KeyModifiers::CONTROL) => vec![Action::Quit],
|
||||
(KeyCode::Enter, _) => self.submit(),
|
||||
// ADR-0049 (issue #29): Esc clears a partly-typed command.
|
||||
// Reached only when no completion memo is alive — the memo
|
||||
// block above consumes Esc first to undo a completion.
|
||||
(KeyCode::Esc, _) => {
|
||||
self.clear_input();
|
||||
Vec::new()
|
||||
}
|
||||
(KeyCode::Up, _) => {
|
||||
self.history_back();
|
||||
Vec::new()
|
||||
@@ -1233,11 +1240,15 @@ impl App {
|
||||
self.cursor_right();
|
||||
Vec::new()
|
||||
}
|
||||
(KeyCode::Home, _) => {
|
||||
// ADR-0049: Ctrl-A / Ctrl-E are readline aliases for
|
||||
// Home / End — line start / end — for keyboards without
|
||||
// those keys. Cursor-only, so (like Home/End) they do not
|
||||
// cancel history navigation.
|
||||
(KeyCode::Home, _) | (KeyCode::Char('a'), KeyModifiers::CONTROL) => {
|
||||
self.input_cursor = 0;
|
||||
Vec::new()
|
||||
}
|
||||
(KeyCode::End, _) => {
|
||||
(KeyCode::End, _) | (KeyCode::Char('e'), KeyModifiers::CONTROL) => {
|
||||
self.input_cursor = self.input.len();
|
||||
Vec::new()
|
||||
}
|
||||
@@ -1251,6 +1262,23 @@ impl App {
|
||||
self.delete_at_cursor();
|
||||
Vec::new()
|
||||
}
|
||||
// ADR-0049: readline kill shortcuts. Each mutates the
|
||||
// buffer, so each ends history navigation like Backspace.
|
||||
(KeyCode::Char('w'), KeyModifiers::CONTROL) => {
|
||||
self.cancel_history_navigation();
|
||||
self.delete_prev_word();
|
||||
Vec::new()
|
||||
}
|
||||
(KeyCode::Char('k'), KeyModifiers::CONTROL) => {
|
||||
self.cancel_history_navigation();
|
||||
self.kill_to_end();
|
||||
Vec::new()
|
||||
}
|
||||
(KeyCode::Char('u'), KeyModifiers::CONTROL) => {
|
||||
self.cancel_history_navigation();
|
||||
self.kill_to_start();
|
||||
Vec::new()
|
||||
}
|
||||
(KeyCode::PageUp, _) => {
|
||||
self.scroll_output_up();
|
||||
Vec::new()
|
||||
@@ -1545,6 +1573,54 @@ impl App {
|
||||
self.input.replace_range(self.input_cursor..idx, "");
|
||||
}
|
||||
|
||||
/// Esc — clear a partly-typed command (ADR-0049). Empties the
|
||||
/// buffer, parks the cursor at the start, drops any horizontal
|
||||
/// scroll, and ends history navigation (the cleared line *is* the
|
||||
/// new draft). Only reached when no completion memo is alive — Esc
|
||||
/// undoes a live completion first (handle_key precedence).
|
||||
fn clear_input(&mut self) {
|
||||
self.cancel_history_navigation();
|
||||
self.input.clear();
|
||||
self.input_cursor = 0;
|
||||
self.input_scroll_offset = 0;
|
||||
}
|
||||
|
||||
/// Ctrl-W — delete the word before the cursor (ADR-0049). Eats any
|
||||
/// run of trailing whitespace, then the preceding run of
|
||||
/// non-whitespace, readline-style. UTF-8 safe: word boundaries are
|
||||
/// found on char boundaries, so multi-byte words delete cleanly.
|
||||
fn delete_prev_word(&mut self) {
|
||||
if self.input_cursor == 0 {
|
||||
return;
|
||||
}
|
||||
let prefix = &self.input[..self.input_cursor];
|
||||
// Strip trailing whitespace, then locate the start of the
|
||||
// word that now ends the prefix.
|
||||
let after_ws = prefix.trim_end_matches(char::is_whitespace);
|
||||
// `idx` is the byte offset of the last whitespace char before
|
||||
// the word; the word starts at the next char. No whitespace at
|
||||
// all → the word starts at the buffer start.
|
||||
let start = after_ws.rfind(char::is_whitespace).map_or(0, |idx| {
|
||||
idx + after_ws[idx..].chars().next().map_or(0, char::len_utf8)
|
||||
});
|
||||
self.input.replace_range(start..self.input_cursor, "");
|
||||
self.input_cursor = start;
|
||||
}
|
||||
|
||||
/// Ctrl-K — kill from the cursor to the end of the line (ADR-0049).
|
||||
/// The cursor is always a char boundary, so a plain truncate is
|
||||
/// safe.
|
||||
fn kill_to_end(&mut self) {
|
||||
self.input.truncate(self.input_cursor);
|
||||
}
|
||||
|
||||
/// Ctrl-U — kill from the start of the line to the cursor
|
||||
/// (ADR-0049). The cursor moves to the start.
|
||||
fn kill_to_start(&mut self) {
|
||||
self.input.replace_range(0..self.input_cursor, "");
|
||||
self.input_cursor = 0;
|
||||
}
|
||||
|
||||
/// Move backwards in history (towards older entries).
|
||||
fn history_back(&mut self) {
|
||||
if self.history.is_empty() {
|
||||
@@ -5756,6 +5832,245 @@ mod tests {
|
||||
assert_eq!(app.input_cursor, 0);
|
||||
}
|
||||
|
||||
// ---- ADR-0049 (issue #29): input-field readline keymap ----
|
||||
|
||||
fn ctrl(c: char) -> AppEvent {
|
||||
key_mod(KeyCode::Char(c), KeyModifiers::CONTROL)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn esc_clears_a_partly_typed_command() {
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "drop table T");
|
||||
app.update(key(KeyCode::Esc));
|
||||
assert_eq!(app.input, "");
|
||||
assert_eq!(app.input_cursor, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn esc_clear_resets_horizontal_scroll() {
|
||||
// A long line that has been horizontally scrolled must
|
||||
// reset its scroll offset on clear, exactly like submit.
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "drop table T");
|
||||
app.input_scroll_offset = 5;
|
||||
app.update(key(KeyCode::Esc));
|
||||
assert_eq!(app.input, "");
|
||||
assert_eq!(app.input_scroll_offset, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn esc_clear_cancels_history_navigation() {
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "drop table A");
|
||||
submit(&mut app);
|
||||
app.update(key(KeyCode::Up));
|
||||
assert_eq!(app.input, "drop table A");
|
||||
app.update(key(KeyCode::Esc));
|
||||
assert_eq!(app.input, "");
|
||||
assert!(app.history_cursor.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn esc_with_live_completion_memo_undoes_rather_than_clears() {
|
||||
// Precedence: while a multi-candidate Tab memo is alive, Esc
|
||||
// undoes the completion (restoring the original text), it does
|
||||
// NOT clear the whole input.
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "show ");
|
||||
app.update(key(KeyCode::Tab)); // → "show data", memo alive
|
||||
assert!(app.last_completion.is_some());
|
||||
app.update(key(KeyCode::Esc));
|
||||
assert_eq!(app.input, "show ");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ctrl_a_moves_cursor_to_start() {
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "hello");
|
||||
app.update(ctrl('a'));
|
||||
assert_eq!(app.input_cursor, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ctrl_e_moves_cursor_to_end() {
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "hello");
|
||||
app.update(key(KeyCode::Home));
|
||||
assert_eq!(app.input_cursor, 0);
|
||||
app.update(ctrl('e'));
|
||||
assert_eq!(app.input_cursor, 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ctrl_w_deletes_the_previous_word() {
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "drop table T");
|
||||
app.update(ctrl('w'));
|
||||
assert_eq!(app.input, "drop table ");
|
||||
assert_eq!(app.input_cursor, "drop table ".len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ctrl_w_eats_trailing_whitespace_then_the_word() {
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "foo bar ");
|
||||
app.update(ctrl('w'));
|
||||
assert_eq!(app.input, "foo ");
|
||||
assert_eq!(app.input_cursor, 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ctrl_w_at_start_is_a_noop() {
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "hello");
|
||||
app.input_cursor = 0;
|
||||
app.update(ctrl('w'));
|
||||
assert_eq!(app.input, "hello");
|
||||
assert_eq!(app.input_cursor, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ctrl_w_only_deletes_back_to_the_cursor() {
|
||||
// Mid-line: deletes the word before the cursor, leaving the
|
||||
// suffix untouched.
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "drop table T");
|
||||
app.input_cursor = "drop table".len(); // cursor right after "table"
|
||||
app.update(ctrl('w'));
|
||||
assert_eq!(app.input, "drop T");
|
||||
assert_eq!(app.input_cursor, "drop ".len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ctrl_w_handles_multibyte_words() {
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "héllo wörld");
|
||||
app.update(ctrl('w'));
|
||||
assert_eq!(app.input, "héllo ");
|
||||
assert_eq!(app.input_cursor, "héllo ".len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ctrl_k_kills_to_end_of_line() {
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "hello world");
|
||||
app.input_cursor = 5; // after "hello"
|
||||
app.update(ctrl('k'));
|
||||
assert_eq!(app.input, "hello");
|
||||
assert_eq!(app.input_cursor, 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ctrl_u_kills_to_start_of_line() {
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "hello world");
|
||||
app.input_cursor = 6; // after "hello "
|
||||
app.update(ctrl('u'));
|
||||
assert_eq!(app.input, "world");
|
||||
assert_eq!(app.input_cursor, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ctrl_u_cancels_history_navigation() {
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "drop table A");
|
||||
submit(&mut app);
|
||||
app.update(key(KeyCode::Up));
|
||||
assert_eq!(app.input, "drop table A");
|
||||
app.update(ctrl('u')); // cursor is at end after recall → clears all
|
||||
assert_eq!(app.input, "");
|
||||
assert!(app.history_cursor.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ctrl_w_cancels_history_navigation() {
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "drop table A");
|
||||
submit(&mut app);
|
||||
app.update(key(KeyCode::Up));
|
||||
assert_eq!(app.input, "drop table A");
|
||||
app.update(ctrl('w')); // deletes the recalled "A" word
|
||||
assert_eq!(app.input, "drop table ");
|
||||
assert!(app.history_cursor.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ctrl_w_on_whitespace_only_clears_it() {
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, " ");
|
||||
app.update(ctrl('w'));
|
||||
assert_eq!(app.input, "");
|
||||
assert_eq!(app.input_cursor, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ctrl_k_at_end_of_line_is_a_noop() {
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "hello");
|
||||
// Cursor at end.
|
||||
app.update(ctrl('k'));
|
||||
assert_eq!(app.input, "hello");
|
||||
assert_eq!(app.input_cursor, 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ctrl_k_at_start_kills_the_whole_line() {
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "hello");
|
||||
app.input_cursor = 0;
|
||||
app.update(ctrl('k'));
|
||||
assert_eq!(app.input, "");
|
||||
assert_eq!(app.input_cursor, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ctrl_u_at_start_of_line_is_a_noop() {
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "hello");
|
||||
app.input_cursor = 0;
|
||||
app.update(ctrl('u'));
|
||||
assert_eq!(app.input, "hello");
|
||||
assert_eq!(app.input_cursor, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ctrl_u_at_end_kills_the_whole_line() {
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "hello");
|
||||
// Cursor at end.
|
||||
app.update(ctrl('u'));
|
||||
assert_eq!(app.input, "");
|
||||
assert_eq!(app.input_cursor, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn esc_on_empty_input_is_harmless() {
|
||||
let mut app = App::new();
|
||||
app.update(key(KeyCode::Esc));
|
||||
assert_eq!(app.input, "");
|
||||
assert_eq!(app.input_cursor, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn esc_exiting_nav_mode_does_not_clear_the_input() {
|
||||
// ADR-0049 / ADR-0046 DC3: while a sidebar panel is focused
|
||||
// (Ctrl-O), Esc exits navigation mode — the nav handler
|
||||
// consumes it upstream of the input-field keymap, so the
|
||||
// partly-typed command is preserved, NOT cleared.
|
||||
let mut app = App::new();
|
||||
type_str(&mut app, "create table T");
|
||||
app.update(key_mod(KeyCode::Char('o'), KeyModifiers::CONTROL));
|
||||
assert_eq!(app.nav_focus, NavFocus::SidebarTables);
|
||||
// The draft survives entering nav mode.
|
||||
assert_eq!(app.input, "create table T");
|
||||
app.update(key(KeyCode::Esc));
|
||||
// Esc returned focus to the input WITHOUT clearing it.
|
||||
assert_eq!(app.nav_focus, NavFocus::Input);
|
||||
assert_eq!(app.input, "create table T");
|
||||
assert_eq!(app.input_cursor, "create table T".len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn relationships_refreshed_event_updates_the_field() {
|
||||
// ADR-0046 DB2: the runtime posts RelationshipsRefreshed; the
|
||||
|
||||
Reference in New Issue
Block a user