LangColorMap
This is an index I went looking for and could not find. The goal is a different colour for every kind of syntax, so that code can be understood much more quickly without reading all of it — you see a certain colour and you already know what something is, before you have actually read the word. It looks bad right now, I know. I am still tuning it, and I want to open it up so other people can have a go at setting the colours better too.
Ordered by hue, so neighbouring entries are neighbouring colours. Hover a row for what it covers.
Every role, one snippet
Each colour above appears at least once below.
#![allow(dead_code)]
use std::collections::HashMap;
const MAX_ENTRIES: usize = 128;
static UNIT: char = '·';
/// Counts words, keyed by label.
#[derive(Debug, Clone)]
pub struct Counter<'a, T> {
label: &'a str,
counts: HashMap<String, T>,
ratio: f64,
}
pub enum Mode {
Fast,
Careful,
}
pub trait Summarize {
fn summary(&self) -> String;
}
impl<'a, T: Default> Counter<'a, T> {
pub fn new(label: &'a str) -> Self {
Counter {
label,
counts: HashMap::new(),
ratio: 0.5,
}
}
fn record(&mut self, word: &str) -> Option<usize> {
if self.counts.len() >= MAX_ENTRIES {
return None;
}
self.counts.insert(word.to_string(), T::default());
Some(self.counts.len())
}
fn first_len(&self) -> Option<usize> {
let len = self.counts.keys().next()?.len();
Some(len)
}
}
impl<'a, T: Default> Summarize for Counter<'a, T> {
fn summary(&self) -> String {
format!("{} {UNIT} {}", self.label, self.counts.len())
}
}
fn tally(values: &[usize]) -> usize {
values.iter().copied().sum()
}
fn main() {
let mut counter = Counter::<String>::new("words");
let added = counter.record("ferris").unwrap_or(0);
let doubled: Vec<usize> = [1, 2, 3].iter().map(|n| n * 2).collect();
let total = tally(&doubled);
let verbose: bool = true;
let mode = Mode::Fast;
if let Mode::Careful = mode { return; }
if verbose && added > 0 {
println!("{} {}", counter.summary(), total);
}
unsafe {
let raw = &UNIT as *const char;
let _ = *raw;
}
'outer: for value in &doubled {
if *value == 4 { break 'outer; }
}
}
Use it in VS Code
The same table generates a VS Code theme, so an editor can paint code the way this page does. It needs rust-analyzer: the role colours ride on its semantic tokens, which is how the editor tells Type::new() from value.method(). Without it you get comments, literals and keywords and nothing more.
- Download the
vscode-themefolder from the repository. - Copy it into your extensions directory:
%USERPROFILE%\.vscode\extensions\on Windows,~/.vscode/extensions/on macOS and Linux. - Reload the window —
Ctrl+Shift+P, then Developer: Reload Window. VS Code only reads that folder at startup, so it will not appear before you do. - Choose it with
Ctrl+K Ctrl+T: Rusty Yellow Pages Dark or Light.
For an installable package instead, run npx @vscode/vsce package inside that folder and install the .vsix it produces through Extensions: Install from VSIX…. Give rust-analyzer a moment to finish indexing after you switch — the role colours only arrive once it has.