fix(cli): a script's command is built against the cursor it will run against (improvise-6uva)

`improvise cmd` and `improvise script` resolved every command through
`CmdRegistry::parse` — the pure arm, which builds `CursorState::default()`: row
0, col 0, and a grid of zero rows and zero columns. The interactive arm, which
builds `CursorState::from_ctx(ctx)`, was never reached from a script. So every
`Move` and `enter-advance` snapshotted a zeroed cursor at PARSE time, clamped to
(0, 0) and emitted `SetSelected(0, 0)` — worse than a no-op, since it RESETS the
cursor. Nothing could position it, so every cursor-reading command was pinned to
the origin.

Reproduced at a9cad15 on a document with Region on row carrying North, South,
East: `move-selection 2 0 . hide-selected-row-item` and a bare
`hide-selected-row-item` both wrote `hidden: Region/North`. After the fix the
first writes `hidden: Region/East`.

The repair is `parse::segments` + `parse::parse_segment_interactive`, with the
headless loop re-parsing per segment, because command n must see command n-1's
cursor. Line atomicity is preserved by keeping the existing whole-line
`parse_line` as a syntax gate, so a line that parsed before still parses and the
only behavioural change is which cursor the commands see.

Two things the ticket did not predict. The TUI's `:` line had the identical
defect — `ExecuteCommand` also used the pure arm — and is fixed the same way.
And doing that surfaced a second instance of the same shape one level down:
`register_pure` built an interactive arm that refused EMPTY args while its own
pure arm accepted them, so `w` with no path parsed one way and refused the
other, breaking `improvise cmd 'w'` and `:w` the moment either re-parsed
interactively. Both arms are now `parse`.

Evidence: context/test-evidence/cli.md; reasoning: context/code/cli.md section 7.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FC2jVn8Au7E9JPkw6bwXXH
This commit is contained in:
Edward Langley
2026-09-22 18:59:08 -07:00
parent 6e10bb3afa
commit 64bcb821da
7 changed files with 331 additions and 30 deletions
+51
View File
@@ -266,3 +266,54 @@ fn a_run_that_changed_the_document_still_writes_it() {
"the -f document lost an edit because `:w` elsewhere cleared the flag: {text}"
);
}
/// A document whose row axis carries three items, so a cursor move has
/// somewhere to go and the three landing places are distinguishable.
fn three_rows(scratch: &Scratch, name: &str) -> PathBuf {
let doc = scratch.path(name);
run_cmd(&["add-category Region"], &doc);
run_cmd(&["add-items Region North South East"], &doc);
doc
}
fn hidden_line(doc: &Path) -> String {
std::fs::read_to_string(doc)
.expect("the document")
.lines()
.find(|l| l.starts_with("hidden:"))
.unwrap_or("(nothing hidden)")
.to_string()
}
/// Evidence: `context/test-evidence/cli.md`.
#[test]
fn a_script_can_move_the_cursor_and_the_next_command_sees_it() {
let scratch = Scratch::new("cursor");
let moved = three_rows(&scratch, "moved.improv");
let still = three_rows(&scratch, "still.improv");
run_cmd(&["move-selection 2 0 . hide-selected-row-item"], &moved);
run_cmd(&["hide-selected-row-item"], &still);
assert_eq!(
hidden_line(&still),
"hidden: Region/North",
"without a move the cursor is at the origin; if this changes, the other \
half of this test stops meaning anything"
);
assert_eq!(
hidden_line(&moved),
"hidden: Region/East",
"`move-selection 2 0` must move the cursor two rows down before the \
next command reads it (improvise-6uva)"
);
}
/// Evidence: `context/test-evidence/cli.md`.
#[test]
fn a_script_cursor_move_survives_the_line_that_made_it() {
let scratch = Scratch::new("cursor-lines");
let doc = three_rows(&scratch, "lines.improv");
run_cmd(&["move-selection 1 0", "hide-selected-row-item"], &doc);
assert_eq!(hidden_line(&doc), "hidden: Region/South");
}