Repository navigation
Add a basic diff / patch language - #5595
Conversation
Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com>
|
Hi Dmitriy Vasyura (@dmitrivMS), Henning Dieterichs (@hediet), could you please review this when you have a moment? In short: Add a basic diff / patch language. I think this will be really valuable. You may want to take a look. Thanks! |
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved diff-highlighting issues misclassify commit-message text as headers and treat format-patch signatures and later commit messages as patch content.
2 open issues
What changed in this PR
Adds basic unified-diff syntax highlighting to Monaco Editor’s built-in languages, addressing the language-support portion of #3967. Highlighting applies to text, not whole-line backgrounds.
Changes:
- Registers
difffor.diff,.patch, and.rejfiles. - Adds stateful Monarch rules for headers, hunks, added/removed lines, and no-newline markers.
- Adds tokenization tests and a two-file homepage sample.
| File | Description |
|---|---|
website/src/website/data/home-samples/sample.diff.txt |
Demonstrates highlighting with a two-file patch. |
src/languages/definitions/register.all.ts |
Includes Diff in the built-in language registrations. |
src/languages/definitions/diff/register.ts |
Defines extensions, aliases, and lazy loading. |
src/languages/definitions/diff/diff.ts |
Implements the unified-diff tokenizer. |
src/languages/definitions/diff/diff.test.ts |
Tests headers, hunk content, and file transitions. |
Review effort: Max
Dmitriy Vasyura (dmitrivMS)
left a comment
There was a problem hiding this comment.
Additional findings not already covered in the PR description or existing review comments.
Dmitriy Vasyura (dmitrivMS)
left a comment
There was a problem hiding this comment.
Please consider review feedback
…string token
- match the hunk header with [@]{2} and the unified header shape
- color +/- lines in the root state so a snippet without a header works
- require a word boundary after header words; treat the format-patch
message and the '-- ' signature as plain text
- use number.inserted / invalid.deleted instead of string.deleted
- correct the sample hunk counts
Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com>
Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com>
|
Thanks for going through the Monarch details so closely, I would not have caught the
On my machine |
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved diff-highlighting defects misclassify valid deletions and commit-message prose, mishandle SHA-256 patch mails, and leave additions indistinguishable from ordinary text in hc-black.
4 open issues
2 resolved since last review
Review effort: Max
Dmitriy Vasyura (dmitrivMS)
left a comment
There was a problem hiding this comment.
More feedback
…sible in hc-black - end a format-patch message only at an exact --- followed by a diffstat or diff --git, so a --- or header-like prose in the message stays text - accept 64 hex characters after From (SHA-256 repositories) - keep a removed line that is exactly '- ' on the deletion path - color additions as type.inserted, which is distinct in vs, vs-dark and hc-black Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com>
…ept plain diff -u payloads The message state no longer ends at a quoted 'diff --git' line, the '---' of a message is no longer colored (a line-based tokenizer cannot tell it from the separator), and the separator state returns to patch tokenization for a 'diff -u' line, a '--- a/file' header or a hunk header. Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com>
A hunk in a git format-patch mail now ends at the exact '-- ' line: that line and the git version after it are plain text until the next 'From <id>' line. Outside a mail the same line stays a deletion, since a removed line that is exactly '- ' looks the same. Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com>
Dmitriy Vasyura (dmitrivMS)
left a comment
There was a problem hiding this comment.
The language now registers for all three extensions, and a normal patch mail highlights correctly. The build, 89 grammar tests, and six Edge smoke tests passed on a clean merge onto e72f0cd. I am requesting changes because real Git output can still lose the entire diff (examples inline), or everything after a valid deletion inside a mail (see the reopened thread).
…and mode changes A removed line that is exactly '- ' no longer starts the signature inside a mail hunk: the signature is only taken when a git version line follows. Add --no-stat patches (diff --git followed by a header line), mode-change summaries before the patch, and a signature after a hunk-less rename. Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com>
Dmitriy Vasyura (dmitrivMS)
left a comment
There was a problem hiding this comment.
I verified the fixes for --no-stat, mode-change summaries, and rename-only signatures using the original Git commands in built Monaco and resolved those three threads. The in-hunk deletion no longer hides the rest of the patch; only the deletion itself remains uncolored. I also reopened the quoted-header thread with a real commit message that is still colored as a patch. Inline below: a custom-signature regression and an invalid regression fixture. Both revisions built; all 89 candidate grammar tests, six Edge smoke tests, and sample validation passed.
…om mail signature After a `diff --git` line in a commit message, only a Git header of the right shape (object IDs in `index`, a path prefix or /dev/null in `---`, numeric modes) starts the patch, so header-like prose stays text. After a `-- ` line in a mail hunk, text that cannot continue a unified hunk is the signature, which covers `git format-patch --signature`. The test fixture now has valid hunk counts. Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com>
Dmitriy Vasyura (dmitrivMS)
left a comment
There was a problem hiding this comment.
One change left: the new signature fallback can turn off highlighting for the rest of a valid patch mail. This happens with diff.suppressBlankEmpty=true when a deleted - is followed by unchanged blank context. The same Git output worked on the preceding PR head. Keeping the tokenizer undecided across the empty line fixed the repro in a browser-only trial without changing the custom-signature behavior.
I resolved all four discussions from the last round. The quoted header examples and the two-line Release team signature now stay plain in built Monaco. Git accepts the corrected two-file fixture. I agree with leaving the ambiguous delimiter itself neutral inside mail; a standalone diff still colors the deletion.
Tested latest main (e72f0cd8) and a clean, conflict-free local merge (cb944a18) of 88f3b8be. Both built. Grammar suites passed (88/88 and 89/89), as did all six Edge smoke tests on each and sample validation. Main still opens .diff, .patch and .rej as plaintext; the candidate registers all three as diff. Of 13 Git-generated browser scenarios, only the suppressed-blank-context case failed.
Keep signature lookahead undecided on empty lines and cover subsequent changes in both files with a Git-generated regression fixture.
Dmitriy Vasyura (dmitrivMS)
left a comment
There was a problem hiding this comment.
Approved. I reproduced the suppressed-blank-context failure on 88f3b8be and verified the fix on 25dd594a. The second file stays highlighted with one or several unprefixed blank lines, and custom signature text stays plain. No new code findings.
Tested a conflict-free merge (68e251b3) onto latest main (e72f0cd8). Main still treats .diff, .patch and .rej as plaintext; the candidate recognizes all three. Both builds, both grammar suites (88/88 and 89/89), and six Edge smoke tests on each passed. All 73 candidate token checks passed across 12 Git-generated inputs. I also checked rendered foreground colors in light, dark and high-contrast black themes.
One non-blocking description update: the PR body still names number.inserted / string.deleted. The shipped theme selectors are type.inserted / invalid.deleted.
|
Thanks for the approval and for checking the Git output on each head. I have updated the description to the shipped token names, |

I wrote a Monarch tokenizer for unified diffs (.diff, .patch, .rej) with a test and sample file; the test passes and test:grammars is 89 of 89, but I haven't seen it in the playground, and it colours text, not whole-line backgrounds.
Adds a
diffMonarch tokenizer for unified diffs (diff -u,git diff,git format-patchoutput), registered for.diff,.patchand.rej, as requested in #3967.What it tokenizes:
diff ...,index ...,--- a/x,+++ b/x, rename/mode lines,Binary files ...) askeyword@@ -1,3 +1,3 @@ ...) asmetatagtype.insertedand removed lines asinvalid.deleted,\ No newline at end of fileascommentDesign notes:
--(it shows up as--- ...) is not mistaken for a file header:---/+++are only headers before the first@@of a file; adiff ...orIndex:line inside a hunk starts the next file.typeandinvalid, notstringorcomment, so the string-only editor behaviour (for example keeping trailing whitespace inside strings) does not apply to deleted lines. Themes that want something else can targettype.inserted/invalid.deleted.CONTRIBUTING.md: addeddiff/{register,diff,diff.test}.ts, the import inregister.all.ts, andwebsite/.../home-samples/sample.diff.txt.Checked on the earlier heads (not re-run on 25dd594 for this edit): the new tokenization test (
node --conditions=import --import tsx --import ./test/test-setup.mjs --test src/languages/definitions/diff/diff.test.ts) passes and fails when the removed-line rule is deleted;npm run test:grammarspasses (89 tests);ts-node ./build/check-samplespasses; prettier is clean on the new files (register.all.tsalready failsprettier --checkonmain, unrelated to this change).Not verified: I did not run the website or playground, so I have not looked at the result in a browser. The issue also mentions a green/red line background; a tokenizer alone only colours the text, so that part is not covered. Context-format diffs (
diff -c) and a multi-file plaindiff -uwithoutdifflines are not specially handled: in the latter, the second file's---line inside the previous hunk would be shown as a removed line.Refs #3967
On 25dd594 (current head): CI passed (
gh pr checks, run 46369846051). The maintainer review on this head reports the grammar suites (88/88 and 89/89), six Edge smoke tests and sample validation passing on candidate builds.