Skip to content

feat(file-viewer): syntax-highlight added and removed lines in the diff view - #1594

Merged
agegr merged 4 commits into
agegr:mainfrom
songconmaisaix31-design:feat/diff-syntax-highlighting
Nov 12, 2026
Merged

agegr merged 4 commits into
agegr:mainfrom
songconmaisaix31-design:feat/diff-syntax-highlighting

Conversation

@songconmaisaix31-design

Copy link
Copy Markdown
Contributor

Summary

Fixes #1587.

The diff view currently renders every added/removed/context line as plain text, while the source view already colors tokens through Prism. This wires the existing react-syntax-highlighter / prism-light pipeline into the diff view so the code content on each row is token-colored, matching the source view.

Approach

  • Adds a small DiffLineCode helper that wraps a single line's text in an inline <SyntaxHighlighter> (PreTag/CodeTag = span, transparent background, inherited font/line-height) so the existing row layout — red/green background, line-number gutter, +/- marker column, wrap toggle — is untouched.
  • TextFileViewer already computes language, prismReady and isDark for the source view; these are passed into DiffView as optional props (defaulting to plain text) so the diff view stays self-contained and the existing lightweight render path is unchanged.
  • Highlight is gated on the same SOURCE_HIGHLIGHT_MAX_LINES ceiling as the source view, so oversized diffs fall back to plain text.
  • Updated the existing FileViewer test regex to match the new multi-line <DiffView ... /> call site.

Testing

  • node --experimental-strip-types --test components/FileViewer.test.mjs components/FileViewer.state.test.mjs lib/prism-languages.test.mjs → 6/6 pass.
  • npx eslint components/FileViewer.tsx components/FileViewer.test.mjs → clean.
  • npx tsc --noEmit → clean.

Reuse the existing Prism pipeline to color added/removed/context line
content in the file diff view. The row background, gutter and +/- marker
column stay unchanged; only the code tokens are colored. Plain text files
and diffs over SOURCE_HIGHLIGHT_MAX_LINES keep the plain-text fallback.
…ound

Each line's highlighter wrapper took the Prism theme's <pre> rules: vs drew
a 1px border around every line, both themes' fonts grew each row from
20.8px to 21.8-22.8px, `white-space: pre` (and the code span's own
`wrapLongLines`) kept a long line on one row with wrap on, and the
`background` shorthand beside vs's backgroundColor left a white box behind
each line after a dark-to-light switch, with React's conflicting-property
warnings. Reset those on the wrapper and pass the wrap choice through.

Memoize the line and the view: with primitive props only, a sidebar toggle
no longer runs Prism over every diff line again (931 ms -> 94 ms at 4x CPU
throttling on a 651-row diff).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@agegr

agegr commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Thanks for picking up #1587! Reusing the source view's highlighter, language detection and line limit is the right shape. Testing it in the browser turned up a few things, which I pushed as commits on top (main merged in, head 2e22427):

  • The Prism theme's <pre> rules landed on each line's inline wrapper. In the light themes that drew a 1px box around every line, the theme fonts made rows 1–2px taller than the gutter's 20.8px, and white-space: pre (plus the highlighter's own wrapLongLines default) meant long lines no longer wrapped with wrap on. The wrapper now resets border, font and white-space, and gets wrapLongLines={wrapLines}.
  • background: "transparent" next to vs's backgroundColor left white boxes behind each line after a dark→light switch, with React's conflicting-style warnings (the same issue as fix(markdown): preserve code backgrounds across theme changes #1060). It's backgroundColor now.
  • DiffView re-rendered with its parent, so toggling the sidebar re-tokenized every line (about 0.9s on a 650-row diff with 4x CPU throttling). The line and the view are memoized now; that toggle drops to about 0.1s. Opening a highlighted diff costs about the same as the Source view of a similar file.
  • A regression test for these, and a short note in docs/agents/files-and-access.md, including that lines inside multi-line comments and strings are colored line by line (as in most diff viewers; the old side of the file isn't loaded to tokenize it whole).

tsc, lint and npm test pass. Thanks!

@agegr
agegr merged commit 934b5a3 into agegr:main Nov 12, 2026
2 checks passed
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 24, 2026
…ff view (agegr#1594)

* feat(file-viewer): syntax-highlight diff view lines

Reuse the existing Prism pipeline to color added/removed/context line
content in the file diff view. The row background, gutter and +/- marker
column stay unchanged; only the code tokens are colored. Plain text files
and diffs over SOURCE_HIGHLIGHT_MAX_LINES keep the plain-text fallback.

* fix(file-viewer): keep a highlighted diff line's box, wrap and background

Each line's highlighter wrapper took the Prism theme's <pre> rules: vs drew
a 1px border around every line, both themes' fonts grew each row from
20.8px to 21.8-22.8px, `white-space: pre` (and the code span's own
`wrapLongLines`) kept a long line on one row with wrap on, and the
`background` shorthand beside vs's backgroundColor left a white box behind
each line after a dark-to-light switch, with React's conflicting-property
warnings. Reset those on the wrapper and pass the wrap choice through.

Memoize the line and the view: with primitive props only, a sidebar toggle
no longer runs Prism over every diff line again (931 ms -> 94 ms at 4x CPU
throttling on a 651-row diff).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(file-viewer): how the diff view highlights its lines

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: songconmaisaix31-design <songconmaisaix31-design@users.noreply.github.com>
Co-authored-by: Alex Yang <agegcn@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

Feature request: syntax highlighting in the diff view

2 participants