Skip to content

highlight: apply a region's rules regardless of its start offset - #4240

Closed
Londopy wants to merge 1 commit into
micro-editor:masterfrom
Londopy:fix-region-rules-start-offset
Closed

Londopy wants to merge 1 commit into
micro-editor:masterfrom
Londopy:fix-region-rules-start-offset

Conversation

@Londopy

@Londopy Londopy commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

highlightRegion finds the region's end inside the region's own slice of the line, but compares that line-relative index against the region's absolute start offset:

endLoc := findIndex(curRegion.end, curRegion.skip, line)
if endLoc != nil {
    if start == endLoc[0] {
        searchNesting = false
    }

When the two happen to be equal, searchNesting is cleared and every rule inside the region is skipped, so the region's contents keep the region's own group.

This is visible on master today, with no other changes — in a C file:

line result
int a;// TODO: x TODO not highlighted
int ab;// TODO: x TODO highlighted

The only difference is the offsets: the comment region starts at 8 and its remaining slice TODO: x is also 8 long, so the comparison matches by coincidence and the todo rule never runs. Any region with inner rules can hit this at some start offset.

Comparing against the start of the slice (endLoc[0] == 0) preserves the intent — "the region ends immediately, so there is nothing inside it to search" — without mixing coordinate systems.

Added a regression test that puts a region at every start offset on the line and checks its inner rule still applies; it fails at offset 3 before the change. pkg/highlight had no tests, so this adds the file.

Found while working on #4236, where the C #include region made it show up often (#include is exactly the length that collides with short header names).

highlightRegion located the region's end inside the region's own slice of the
line, but compared that line-relative index against the region's absolute start
offset. When the two happened to be equal, searchNesting was cleared and every
rule inside the region was skipped, leaving the contents with the region's own
group.

This is visible on master today: in a C file, 'int a;// TODO: x' does not
highlight the TODO, while 'int ab;// TODO: x' does, purely because of the
offsets involved.

Compare against the start of the slice instead, and add a regression test
covering a region starting at every offset on the line.
@Londopy

Londopy commented Sep 22, 2026

Copy link
Copy Markdown
Contributor Author

Closing this as a duplicate — I missed that #4022 already fixes it (and that #4018 tracks it). #4022 has the same endLoc[0] == 0 change plus fixes for nested-region ordering, so it supersedes this.

I've offered the regression test from here over on #4022, since that PR has no tests and the open question there was whether it breaks anything else. Sorry for the noise.

@Londopy Londopy closed this Sep 22, 2026
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