Repository navigation
fix(hook): rewrite commands without rewriting the text between them - #4132
Merged
Merged
Conversation
📊 Automated PR Analysis
SummaryThis PR fixes command-rewriting logic in the hook/discover registry so that whitespace and text between rewritten command segments (in pipelines, compound commands, and wrapped prefixes) is preserved verbatim from the source rather than being rebuilt with normalized single spaces. It introduces helper functions ( Review Checklist
Analyzed automatically by wshm · This is an automated analysis, not a human review. |
2 tasks done
KuSh
marked this pull request as ready for review
September 19, 2026 16:22
KuSh
force-pushed
the
lex/04-span-emitter
branch
from
September 19, 2026 21:13
dc1e62c to
9ea0fce
Compare
KuSh
force-pushed
the
lex/04-span-emitter
branch
from
September 20, 2026 23:23
9ea0fce to
5108a50
Compare
KuSh
force-pushed
the
lex/04-span-emitter
branch
from
September 25, 2026 22:55
5108a50 to
6cc0197
Compare
KuSh
force-pushed
the
lex/04-span-emitter
branch
2 times, most recently
from
September 25, 2026 23:29
0721aea to
4bfc7fd
Compare
KuSh
force-pushed
the
lex/04-span-emitter
branch
from
September 26, 2026 17:56
4bfc7fd to
087ddeb
Compare
KuSh
force-pushed
the
lex/04-span-emitter
branch
6 times, most recently
from
October 5, 2026 22:36
bd172e0 to
c603a04
Compare
KuSh
force-pushed
the
lex/04-span-emitter
branch
from
October 7, 2026 07:16
c603a04 to
fe931ec
Compare
KuSh
force-pushed
the
lex/04-span-emitter
branch
from
October 9, 2026 11:07
fe931ec to
90198ae
Compare
KuSh
force-pushed
the
lex/04-span-emitter
branch
from
October 9, 2026 16:16
90198ae to
1d1e670
Compare
KuSh
force-pushed
the
lex/04-span-emitter
branch
2 times, most recently
from
October 9, 2026 20:29
42db953 to
9d8df99
Compare
KuSh
force-pushed
the
lex/04-span-emitter
branch
from
October 15, 2026 00:24
9d8df99 to
22bae1a
Compare
KuSh
force-pushed
the
lex/04-span-emitter
branch
2 times, most recently
from
October 19, 2026 16:38
3ad1f88 to
beef9c1
Compare
KuSh
force-pushed
the
lex/04-span-emitter
branch
2 times, most recently
from
October 26, 2026 11:29
ecd81b0 to
7bf0a40
Compare
KuSh
force-pushed
the
lex/04-span-emitter
branch
from
October 28, 2026 23:40
7bf0a40 to
7955dff
Compare
`rewrite_compound` split a compound command on each operator, rewrote the
segments, and then rebuilt the line by emitting `" {op} "` around every
operator it had split on. The operators came back, but the spacing was the
emitter's rather than the author's, and anything it did not model was
invented: `;;` came back as `; ;` and `;&` as `; &`, which is how a `case`
statement became a syntax error and a fall-through became a background job.
Those two had to be fixed in the lexer because the emitter could not tell
them apart from two separate operators.
The emitter now copies the gaps instead of rebuilding them. A segment is
emitted as its own leading whitespace, its rewrite, and its own trailing
whitespace; the operator between two segments is copied byte for byte from
the source. The multi-line path keeps a line's trailing whitespace as well as
its indent.
Wrapper prefixes had it too: `strip_word_prefix` trims the remainder so the
inner command can be matched, and the three sites that rejoined it used a
single space, so `uv run ls` and `timeout 5 cargo build` came back
respaced. `prefix_gap` hands the original back instead.
The pipeline rewriters had the same habit one level down, joining producer,
`|` and consumer with `format!("{} {}", ..)` over trimmed text, so
`ls -la | head -3` came back single-spaced around the pipe. They now carry
the gaps either side of the `|` through as well. That was not a regression —
it predates this commit — but it is the same defect, and leaving it would
have made this commit's claim false for every pipeline.
What changes, all of it the author's formatting coming back intact:
ls -la && ls -> rtk ls -la && rtk ls (was ` && `)
ls -la | head -3 -> rtk ls -la | head -3 (was ` | `)
uv run ls -la -> uv run rtk ls -la (was `uv run `)
ls ; ls -> rtk ls ; rtk ls (was `; `)
ls;ls -> rtk ls;rtk ls (was `; `)
ls &&\nls -> rtk ls &&\nrtk ls (was ` && `)
case … 1;;esac -> … 1;;esac (was `;; `)
Five tests pinned the old normalisation and now pin the input's own spacing.
Each rewritten form was checked with `bash -n`.
The property is what makes this checkable rather than claimed: for a
generated corpus, deleting every inserted `rtk ` from the output must give
back the input. It fails on a named input if the emitter is put back the way
it was. It also caught a case no example covered — a line's trailing
whitespace was dropped before a newline — which is fixed here rather than
excused in the assertion. Its one exemption is the whole-command `trim()`
that happens before the emitter is reached.
Its first version asserted nothing about pipelines: none of the commands it
generated is a safe pipe consumer, so every pipe case returned `None`. The
corpus now crosses real producers and consumers with irregular spacing around
the `|`, which is what surfaced the pipeline rewriters above.
Separators are safe to reproduce verbatim: `;`, `&` and `|` each end a word
on their own, so a rewrite ending in a digit cannot fuse with them. A
redirect can — `--head-lines 1` beside `>out` reads as an fd-1 redirect — but
that is settled before the emitter by `join_redirect_suffix`, and both cases
already had tests.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
KuSh
force-pushed
the
lex/04-span-emitter
branch
from
October 28, 2026 23:54
7955dff to
05e7f80
Compare
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
rewrite_compoundsplit a compound command on each operator, rewrote thesegments, and then rebuilt the line by emitting
" {op} "around everyoperator it had split on. The operators came back, but the spacing was the
emitter's rather than the author's, and anything it did not model was
invented:
;;came back as; ;and;&as; &, which is how acasestatement became a syntax error and a fall-through became a background job.
Those two had to be fixed in the lexer because the emitter could not tell
them apart from two separate operators.
The emitter now copies the gaps instead of rebuilding them. A segment is
emitted as its own leading whitespace, its rewrite, and its own trailing
whitespace; the operator between two segments is copied byte for byte from
the source. The multi-line path keeps a line's trailing whitespace as well as
its indent.
Wrapper prefixes had it too:
strip_word_prefixtrims the remainder so theinner command can be matched, and the three sites that rejoined it used a
single space, so
uv run lsandtimeout 5 cargo buildcame backrespaced.
prefix_gaphands the original back instead.The pipeline rewriters had the same habit one level down, joining producer,
|and consumer withformat!("{} {}", ..)over trimmed text, sols -la | head -3came back single-spaced around the pipe. They now carrythe gaps either side of the
|through as well. That was not a regression —it predates this commit — but it is the same defect, and leaving it would
have made this commit's claim false for every pipeline.
What changes, all of it the author's formatting coming back intact:
Five tests pinned the old normalisation and now pin the input's own spacing.
Each rewritten form was checked with
bash -n.The property is what makes this checkable rather than claimed: for a
generated corpus, deleting every inserted
rtkfrom the output must giveback the input. It fails on a named input if the emitter is put back the way
it was. It also caught a case no example covered — a line's trailing
whitespace was dropped before a newline — which is fixed here rather than
excused in the assertion. Its one exemption is the whole-command
trim()that happens before the emitter is reached.
Its first version asserted nothing about pipelines: none of the commands it
generated is a safe pipe consumer, so every pipe case returned
None. Thecorpus now crosses real producers and consumers with irregular spacing around
the
|, which is what surfaced the pipeline rewriters above.Separators are safe to reproduce verbatim:
;,&and|each end a wordon their own, so a rewrite ending in a digit cannot fuse with them. A
redirect can —
--head-lines 1beside>outreads as an fd-1 redirect — butthat is settled before the emitter by
join_redirect_suffix, and both casesalready had tests.
Test plan
cargo fmt --all && cargo clippy --all-targets && cargo test --all— 3855 passing, clippy clean, at this commit rather than only at the tip of the stackhead -1 f>outdoes not become--head-lines 1>outPart of stack #4139 (#4129 → #4138). Based on #4131. Each commit gates green on its own, so the stack is bisectable.
🤖 Generated with Claude Code