Skip to content

fix(highlight): trim whitespace from capture spans#84

Merged
MichaelHatherly merged 3 commits into
masterfrom
mh/fix-capture-whitespace-bleed
Jul 6, 2026
Merged

fix(highlight): trim whitespace from capture spans#84
MichaelHatherly merged 3 commits into
masterfrom
mh/fix-capture-whitespace-bleed

Conversation

@MichaelHatherly

@MichaelHatherly MichaelHatherly commented Jun 23, 2026

Copy link
Copy Markdown
Member

Node-level grammar captures style their full byte range, so a capture spanning a whole statement (such as Fortran's (program_statement) matched as @keyword) bleeds styling onto the structural spaces and newlines between its children. In styled themes this underlines or bolds whitespace and line breaks.

Trim leading and trailing whitespace from the final styled spans, dropping whitespace-only ones, so they fall back to the unstyled gap path. Gate the trim on the capture name: string, comment, and character own their whitespace and are left untouched.

Rewrite three samples that were invalid source, which put the tree-sitter parser into error recovery and garbled the output: an invalid parametric struct in fib.jl, keyless datetimes in config.toml, and statement fragments in hello.f90 (now a valid module, program, and subroutine). Regenerate the affected references.

Add regression tests for the trim: unit cases for trim_capture, plus inputs verified to bleed without it (a Fortran program captured whole as @keyword, a MATLAB multi-assignment yielding a whitespace-only @variable span, and invalid Julia source that drives error recovery, restoring the coverage the sample rewrites removed).

Node-level grammar captures style their full byte range, so a capture
spanning a whole statement (such as Fortran's `(program_statement)` matched
as `@keyword`) bleeds styling onto the structural spaces and newlines between
its children. In styled themes this underlines or bolds whitespace and line
breaks.

Trim leading and trailing whitespace from the final styled spans, dropping
whitespace-only ones, so they fall back to the unstyled gap path. Gate the
trim on the capture name: `string`, `comment`, and `character` own their
whitespace and are left untouched.

Rewrite three samples that were invalid source, which put the tree-sitter
parser into error recovery and garbled the output: an invalid parametric
struct in `fib.jl`, keyless datetimes in `config.toml`, and statement
fragments in `hello.f90` (now a valid module, program, and subroutine).
Regenerate the affected references.
The composite trimming testset used a valid `struct Foo`, which the Julia
grammar highlights entirely through leaf captures. Its assertions pass
without the trimming in `highlight_tokens`, so it did not guard the fix.

Replace the input with cases verified to bleed when trimming is removed:
a Fortran program whose statements are captured whole as `@keyword`, a
MATLAB multi-assignment that yields a whitespace-only `@variable` capture,
and invalid Julia source that drives tree-sitter error recovery into
multi-line `@type` spans. The error-recovery case also restores coverage
lost when the samples were rewritten as valid source.
@MichaelHatherly MichaelHatherly merged commit b32c0e8 into master Jul 6, 2026
13 checks passed
@MichaelHatherly MichaelHatherly deleted the mh/fix-capture-whitespace-bleed branch July 6, 2026 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant