Skip to content

feat: colour the code in a diff by its language, from the top of the file - #55

Merged
Lars- merged 2 commits into
mainfrom
feat/syntax-colours
Nov 19, 2026
Merged

Lars- merged 2 commits into
mainfrom
feat/syntax-colours

Conversation

@Lars-

@Lars- Lars- commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

What this changes

Code in a diff now has the colours of its language: keywords, strings, comments, numbers and names each stand out, in the unstaged, staged and untracked changes, in a commit and under a file's history, one under the other or side by side. The words that changed within a line still stand out on top of that.

The colours come from the whole file, not from the lines in the diff: both versions are read with Repo::versions and coloured from the top on a background thread, so a hunk that starts halfway through a block comment or a multi-line string still colours those lines as comment or string. The diff shows at once, and the colours follow a moment later (some 50 µs a line in a release build; a 7,000-line C file takes about 0.4 s per version). A line only takes its colours when the version read has exactly the same text at that line number, so a clean filter or a change in between never colours the wrong text. Files over 512 KB, files that are not UTF-8, plain text and languages without a grammar keep the colour of plain text; links and submodules are not read at all.

Colouring uses syntect 5.3 with its built-in Sublime Text grammars (about fifty languages, among them Rust, C, C++, C#, Go, Java, JavaScript, PHP, Python, Ruby, shell, SQL, HTML, CSS, JSON, YAML, XML and Markdown) and its pure-Rust regex engine (fancy-regex), so no C library is needed on any platform. The colours are syntect's base16-ocean.dark, with its default text colour mapped to the app's own, so a name looks the same as text without colours; the theme is a constant in theme.rs, ready for the light theme on the roadmap.

What it costs:

  • Five new crates: syntect, fancy-regex, bincode, bit-set and bit-vec (the rest was already in the tree).
  • The download grows by about 1.1 MB (Linux x64, gzip: 5.7 MB to 6.9 MB), mostly the grammars and the regex engine.
  • bincode 1.3.3, which syntect uses to read its grammars, has an informational advisory (RUSTSEC-2025-0141): its authors stopped working on it and call 1.3.3 complete. There is no flaw in it, so deny.toml ignores that advisory with the reason.

This is part 4 of "Better diffs" on the roadmap, and the last part of it.

How it is tested

  • Unit tests in syntax.rs: a Rust line gives a keyword, a string and a comment three different colours and a name the plain colour; a C comment over two lines colours the second line as comment and ends where it ends; a Makefile and a script with #!/bin/sh are known without an extension; plain text, an unknown extension, code that is not UTF-8 and a file over the limit stay plain.

  • A new app test changes a Rust file whose hunk starts inside a block comment that begins above it, and checks the painted colours: the comment line in the hunk has the comment colour, fn has its keyword colour in the old and the new line, and the changed words are still marked. It fails without reading the versions. The same test stages a submodule and checks that its diff opens without an error (it fails without skipping links and submodules: gitoxide refuses to read a commit as a blob).

  • Tried in the app on Linux, in a clone of this repository: a change in the middle of a block comment in git's sequencer.c (the comment lines above the change, in the hunk but below where the comment starts, are coloured as comment), one under the other and side by side; staging a single line, after which the diff and its colours follow; a Rust file; a new Python script, with its #! line and docstring; a change in the 87 KB single line of jquery.min.js (coloured, the window stays at rest afterwards); and a diff in a commit.

  • cargo fmt --all, cargo clippy --workspace --all-targets -- -D warnings and cargo test --workspace pass

  • It adds as little code as the change needs, and removes what it makes unnecessary

  • Nothing in it comes from GitKraken or another closed-source client (see CONTRIBUTING.md)

@Lars-
Lars- merged commit 4f26fc8 into main Nov 19, 2026
7 checks passed
@Lars-
Lars- deleted the feat/syntax-colours branch November 19, 2026 10:26
@Lars- Lars- mentioned this pull request Nov 19, 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