Repository navigation
feat: colour the code in a diff by its language, from the top of the file - #55
Merged
Merged
Conversation
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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::versionsand 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'sbase16-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 intheme.rs, ready for the light theme on the roadmap.What it costs:
syntect,fancy-regex,bincode,bit-setandbit-vec(the rest was already in the tree).bincode1.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, sodeny.tomlignores 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; aMakefileand a script with#!/bin/share 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,
fnhas 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 ofjquery.min.js(coloured, the window stays at rest afterwards); and a diff in a commit.cargo fmt --all,cargo clippy --workspace --all-targets -- -D warningsandcargo test --workspacepassIt 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)