Skip to content

fix: diff file headers and unanchored ! in diff and git - #78

Open
lvolland wants to merge 1 commit into
speed-highlight:mainfrom
lvolland:fix/diff-headers
Open

fix: diff file headers and unanchored ! in diff and git#78
lvolland wants to merge 1 commit into
speed-highlight:mainfrom
lvolland:fix/diff-headers

Conversation

@lvolland

@lvolland lvolland commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

hey! the two header lines of every git diff come out wrong.

await highlightText('--- a/file\n+++ b/file\n@@ -1 +1 @@\n-old\n+new', 'diff')

today --- a/file is deleted (red) and +++ b/file is insert (green), as if they were content lines. expected: both section, like the @@ line.

cause: [*-+] in the section rule is a range (U+002A–U+002B), so it holds * and + but not -. /[*-+]/.test('-') is false. and even with the class fixed nothing moves, because the rule sat last and lost the index tie to deleted/insert — the loop runs backwards and takes <=. so the class and the position both have to change.

two more in the same pass:

  • /!.*/gm was unanchored, so if (a !== b) { painted !== b) { as a keyword mid-line. anchored to ^.
  • git.js used expand: 'str', so every apostrophe in a commit message opened a green string. swapped for strDouble plus a prose-aware apostrophe rule.

+16 bytes in diff.js, +144 in git.js. fixture lines in test.diff and test.git.

notes

  • --- <one word> now reads section instead of deleted. that hits one-word deleted sql/lua comments (-- up, -- down) — common in migrations. --- primary key (two words) is unaffected. tightening further means reasoning about the ---/+++ pair, which costs a lot more than 16 bytes.
  • paths with spaces (--- a/my file.txt) are not fixed — ( \S+)?$ wants a single token. no regression, main got them wrong too, but the fix is narrower than the title suggests.
  • the apostrophe rule uses a lookbehind. bash.js already has four, one of them variable-length, so no new floor.
  • escapes still work: a "b\"c" d is one string, same as before. half-typed quotes stay coloured too.

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