Skip to content

Add a basic diff / patch language - #5595

Merged
Dmitriy Vasyura (dmitrivMS) merged 10 commits into
microsoft:mainfrom
MaxonT:contrib/3967-auto
Nov 30, 2026
Merged

Dmitriy Vasyura (dmitrivMS) merged 10 commits into
microsoft:mainfrom
MaxonT:contrib/3967-auto

Conversation

@MaxonT

@MaxonT Ming (MaxonT) commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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 diff Monarch tokenizer for unified diffs (diff -u, git diff, git format-patch output), registered for .diff, .patch and .rej, as requested in #3967.

What it tokenizes:

  • file header lines (diff ..., index ..., --- a/x, +++ b/x, rename/mode lines, Binary files ...) as keyword
  • hunk headers (@@ -1,3 +1,3 @@ ...) as metatag
  • added lines as type.inserted and removed lines as invalid.deleted, \ No newline at end of file as comment

Design notes:

  • A small state is used so that a removed line whose content starts with -- (it shows up as --- ...) is not mistaken for a file header: --- / +++ are only headers before the first @@ of a file; a diff ... or Index: line inside a hunk starts the next file.
  • The prefixes are type and invalid, not string or comment, 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 target type.inserted / invalid.deleted.
  • Per CONTRIBUTING.md: added diff/{register,diff,diff.test}.ts, the import in register.all.ts, and website/.../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:grammars passes (89 tests); ts-node ./build/check-samples passes; prettier is clean on the new files (register.all.ts already fails prettier --check on main, 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 plain diff -u without diff lines 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.

Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com>
@MaxonT

Copy link
Copy Markdown
Contributor Author

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!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 diff for .diff, .patch, and .rej files.
  • 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

Comment thread src/languages/definitions/diff/diff.ts Resolved
Comment thread src/languages/definitions/diff/diff.ts Resolved

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional findings not already covered in the PR description or existing review comments.

Comment thread src/languages/definitions/diff/diff.ts Resolved
Comment thread src/languages/definitions/diff/diff.ts Resolved
Comment thread src/languages/definitions/diff/diff.ts Resolved
Comment thread src/languages/definitions/diff/diff.ts Resolved

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@MaxonT

Copy link
Copy Markdown
Contributor Author

Thanks for going through the Monarch details so closely, I would not have caught the @@ escape on my own. I pushed 81e9f96 (661125a only puts back package-lock.json, which 81e9f96 dropped by mistake, so the diff is the original five files again). By thread:

  • @@ in the rule: the hunk header is now [@]{2} plus the unified shape (-N[,N] +N[,N]), so @user and @@ not a header stay plain.
  • +/- in the root state: added, after the ---/+++ header rule, so a header-less -old / +new snippet is colored.
  • Theme comment and string.deleted: the tokens are now number.inserted and invalid.deleted, so nothing carries the string prefix. The comment names the full selectors and says number is white in hc-black.
  • Sample hunk counts: @@ -1,6 +1,7 @@, and git apply --stat accepts the sample now.
  • The two Copilot threads: header words need whitespace or end of line after them, and a From <sha> mail line starts a message state that ends at ---. A -- line or the next From <sha> line inside a hunk goes back to it. One side effect: a removed line whose content is exactly - would read as the signature.

On my machine npm run test:grammars passes (89 of 89), and the new diff tests fail against the previous diff.ts. I did not run the build, check-samples or the smoke tests, and CI on the new head was still pending when I wrote this. If the invalid prefix is not the choice you would make for deleted lines, I am happy to switch.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread src/languages/definitions/diff/diff.ts Resolved
Comment thread src/languages/definitions/diff/diff.ts Resolved
Comment thread src/languages/definitions/diff/diff.ts Resolved
Comment thread src/languages/definitions/diff/diff.ts Resolved

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment thread src/languages/definitions/diff/diff.ts Resolved
Comment thread src/languages/definitions/diff/diff.ts Resolved
Comment thread src/languages/definitions/diff/diff.ts Resolved
…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>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/languages/definitions/diff/diff.ts Resolved
…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>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@MaxonT

Copy link
Copy Markdown
Contributor Author

Thanks for the approval and for checking the Git output on each head. I have updated the description to the shipped token names, type.inserted and invalid.deleted.

@dmitrivMS
Dmitriy Vasyura (dmitrivMS) merged commit af47b02 into microsoft:main Nov 30, 2026
2 checks passed
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.

4 participants