use anyhow::{Context, Result, anyhow};
use serde::Deserialize;
use serde_json::Value;
use std::collections::BTreeMap;
use std::path::Path;
const MAX_INCLUDE_DEPTH: usize = 8;
#[derive(Debug, Default, Deserialize)]
struct RawTheme {
#[serde(default)]
name: Option<String>,
#[serde(rename = "type", default)]
kind: Option<String>,
#[serde(default)]
include: Option<String>,
#[serde(default)]
colors: BTreeMap<String, String>,
#[serde(default)]
token_colors: Vec<TokenColor>,
#[serde(default)]
semantic: BTreeMap<String, Value>,
#[serde(skip)]
dropped_rules: usize,
}
#[derive(Debug, Clone, Deserialize)]
struct TokenColor {
#[serde(default)]
scope: ScopeField,
#[serde(default)]
settings: TokenSettings,
}
#[derive(Debug, Clone, Default, Deserialize)]
struct TokenSettings {
#[serde(default)]
foreground: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
enum ScopeField {
One(String),
Many(Vec<String>),
}
impl Default for ScopeField {
fn default() -> Self {
ScopeField::Many(Vec::new())
}
}
impl ScopeField {
fn scopes(&self) -> Vec<String> {
let raw: Vec<&str> = match self {
ScopeField::One(s) => vec![s.as_str()],
ScopeField::Many(v) => v.iter().map(String::as_str).collect(),
};
raw.iter()
.flat_map(|s| s.split(','))
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect()
}
}
#[derive(Debug, Clone)]
pub struct Converted {
pub id: String,
pub label: String,
pub manifest: String,
pub notes: Vec<String>,
}
type Rgb = (u8, u8, u8);
fn parse_color(raw: &str, over: Rgb) -> Option<Rgb> {
let s = raw.trim().strip_prefix('#')?;
if !s.is_ascii() {
return None;
}
let hex = |b: &str| u8::from_str_radix(b, 16).ok();
let (r, g, b, a) = match s.len() {
3 => {
let d = |i: usize| hex(&s[i..i + 1]).map(|v| v * 17);
(d(0)?, d(1)?, d(2)?, 255)
}
4 => {
let d = |i: usize| hex(&s[i..i + 1]).map(|v| v * 17);
(d(0)?, d(1)?, d(2)?, d(3)?)
}
6 => (hex(&s[0..2])?, hex(&s[2..4])?, hex(&s[4..6])?, 255),
8 => (
hex(&s[0..2])?,
hex(&s[2..4])?,
hex(&s[4..6])?,
hex(&s[6..8])?,
),
_ => return None,
};
if a == 255 {
return Some((r, g, b));
}
let blend = |c: u8, base: u8| {
let c = c as u32 * a as u32 + base as u32 * (255 - a as u32);
(c / 255) as u8
};
Some((blend(r, over.0), blend(g, over.1), blend(b, over.2)))
}
fn hex_of(c: Rgb) -> String {
format!("#{:02x}{:02x}{:02x}", c.0, c.1, c.2)
}
fn shade(c: Rgb, amount: f32) -> Rgb {
let f = |v: u8| {
let v = v as f32;
let target = if amount >= 0.0 { 255.0 } else { 0.0 };
(v + (target - v) * amount.abs()).clamp(0.0, 255.0) as u8
};
(f(c.0), f(c.1), f(c.2))
}
fn load_chain(path: &Path, depth: usize, out: &mut Vec<RawTheme>) -> Result<()> {
if depth > MAX_INCLUDE_DEPTH {
return Err(anyhow!(
"include chain deeper than {MAX_INCLUDE_DEPTH} files (a cycle?)"
));
}
let raw =
std::fs::read_to_string(path).with_context(|| format!("reading {}", path.display()))?;
let theme = parse_theme(&raw).with_context(|| format!("parsing {}", path.display()))?;
if let Some(inc) = theme.include.clone() {
let inc_path = Path::new(&inc);
if inc_path.is_absolute()
|| inc_path
.components()
.any(|c| matches!(c, std::path::Component::ParentDir))
{
return Err(anyhow!(
"include must be a relative path inside the theme's directory, got {inc}"
));
}
let base = path
.parent()
.unwrap_or_else(|| Path::new("."))
.join(inc_path);
load_chain(&base, depth + 1, out)?;
}
out.push(theme);
Ok(())
}
fn parse_theme(raw: &str) -> Result<RawTheme> {
let stripped = crate::tasks::strip_jsonc(raw);
let value: serde_json::Value = serde_json::from_str(&stripped)?;
let mut theme: RawTheme = serde_json::from_value(value.clone())?;
theme.semantic = value
.get("semanticTokenColors")
.and_then(|v| v.as_object())
.map(|m| {
m.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect::<BTreeMap<String, Value>>()
})
.unwrap_or_default();
let tokens = value
.get("tokenColors")
.or_else(|| value.get("settings"))
.cloned()
.unwrap_or(serde_json::Value::Array(Vec::new()));
let rules: Vec<Value> = serde_json::from_value(tokens).unwrap_or_default();
let total = rules.len();
theme.token_colors = rules
.into_iter()
.filter_map(|r| serde_json::from_value::<TokenColor>(r).ok())
.collect();
theme.dropped_rules = total - theme.token_colors.len();
Ok(theme)
}
fn merge(base: RawTheme, over: RawTheme) -> RawTheme {
let mut colors = base.colors;
colors.extend(over.colors);
let mut semantic = base.semantic;
semantic.extend(over.semantic);
let mut token_colors = base.token_colors;
token_colors.extend(over.token_colors);
RawTheme {
name: over.name.or(base.name),
kind: over.kind.or(base.kind),
include: None,
colors,
token_colors,
semantic,
dropped_rules: base.dropped_rules + over.dropped_rules,
}
}
fn semantic_color(theme: &RawTheme, want: &str, over: Rgb) -> Option<Rgb> {
theme.semantic.iter().find_map(|(key, value)| {
let ty = key.split(['.', ':']).next().unwrap_or(key);
if ty != want {
return None;
}
let raw = match value {
Value::String(s) => Some(s.as_str()),
Value::Object(o) => o.get("foreground").and_then(Value::as_str),
_ => None,
}?;
parse_color(raw, over)
})
}
const SYNTAX_SCOPES: &[(&str, &[&str])] = &[
(
"syn_comment",
&["comment", "punctuation.definition.comment"],
),
(
"syn_keyword",
&["keyword.control", "keyword", "storage.type", "storage"],
),
("syn_string", &["string.quoted", "string"]),
(
"syn_constant",
&[
"constant.numeric",
"constant.language",
"constant",
"variable.parameter",
],
),
(
"syn_function",
&[
"entity.name.function",
"support.function",
"meta.function-call",
],
),
(
"syn_type",
&[
"entity.name.type",
"entity.name.class",
"support.type",
"support.class",
],
),
(
"syn_tag",
&[
"entity.name.tag",
"entity.other.attribute-name",
"support.type.property-name",
],
),
];
const SYNTAX_SEMANTIC: &[(&str, &[&str])] = &[
("syn_comment", &["comment"]),
("syn_keyword", &["keyword", "modifier"]),
("syn_string", &["string"]),
("syn_constant", &["number", "parameter", "enumMember"]),
("syn_function", &["function", "method"]),
("syn_type", &["type", "class", "struct", "interface"]),
("syn_tag", &["property", "decorator", "macro"]),
];
fn scope_color(theme: &RawTheme, scope: &str, over: Rgb) -> Option<Rgb> {
let mut best: Option<(usize, Rgb)> = None;
for rule in &theme.token_colors {
let Some(fg) = rule.settings.foreground.as_deref() else {
continue;
};
let Some(color) = parse_color(fg, over) else {
continue;
};
for claimed in rule.scope.scopes() {
let (claimed, contextual) = match claimed.rsplit_once(' ') {
Some((_, last)) => (last.trim().to_string(), true),
None => (claimed, false),
};
if claimed.is_empty() {
continue;
}
let matches = scope == claimed || scope.starts_with(&format!("{claimed}."));
if !matches {
continue;
}
let specificity = if contextual {
0
} else {
claimed.matches('.').count() + 1
};
if best.map(|(s, _)| specificity >= s).unwrap_or(true) {
best = Some((specificity, color));
}
}
}
best.map(|(_, c)| c)
}
fn default_fg(theme: &RawTheme, over: Rgb) -> Option<Rgb> {
let scopeless = theme
.token_colors
.iter()
.rev()
.find(|r| r.scope.scopes().is_empty())
.and_then(|r| r.settings.foreground.as_deref())
.and_then(|c| parse_color(c, over));
scopeless.or_else(|| {
theme
.colors
.get("editor.foreground")
.and_then(|c| parse_color(c, over))
})
}
const ANSI_KEYS: [&str; 16] = [
"terminal.ansiBlack",
"terminal.ansiRed",
"terminal.ansiGreen",
"terminal.ansiYellow",
"terminal.ansiBlue",
"terminal.ansiMagenta",
"terminal.ansiCyan",
"terminal.ansiWhite",
"terminal.ansiBrightBlack",
"terminal.ansiBrightRed",
"terminal.ansiBrightGreen",
"terminal.ansiBrightYellow",
"terminal.ansiBrightBlue",
"terminal.ansiBrightMagenta",
"terminal.ansiBrightCyan",
"terminal.ansiBrightWhite",
];
pub fn convert_file(path: &Path, id_override: Option<&str>) -> Result<Converted> {
let mut chain = Vec::new();
load_chain(path, 0, &mut chain)?;
let merged = chain
.into_iter()
.reduce(merge)
.ok_or_else(|| anyhow!("empty theme"))?;
let stem = path
.file_stem()
.map(|s| s.to_string_lossy().into_owned())
.unwrap_or_else(|| String::from("imported"));
convert(merged, id_override, &stem)
}
fn slug(s: &str) -> String {
use unicode_normalization::UnicodeNormalization;
static SEPARATORS: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();
let separators = SEPARATORS.get_or_init(|| {
regex::Regex::new(r"[^\p{L}\p{N}\p{M}]+").expect("a literal class compiles")
});
let lowered: String = s.to_lowercase().nfc().collect();
let slug = separators
.replace_all(&lowered, "-")
.trim_matches('-')
.to_string();
if !slug.chars().any(char::is_alphanumeric) {
return String::new();
}
slug
}
const GENERATED_HEADER: &str = "# Imported from a VS Code colour theme by `croft theme-import`.";
fn existing_ids() -> Vec<String> {
crate::theme::Theme::all()
.iter()
.map(|t| t.id().to_string())
.filter(|id| !was_generated_by_import(id))
.collect()
}
fn was_generated_by_import(id: &str) -> bool {
if cfg!(test) {
return false;
}
let path = crate::lsp::manifest::user_extensions_dir()
.join(format!("theme-{id}"))
.join("extension.toml");
std::fs::read_to_string(path)
.map(|text| text.starts_with(GENERATED_HEADER))
.unwrap_or(false)
}
fn unique_id(wanted: String, taken: &[String]) -> Option<String> {
if !taken.contains(&wanted) {
return Some(wanted);
}
(2..1000)
.map(|n| format!("{wanted}-{n}"))
.find(|candidate| !taken.contains(candidate))
}
fn convert(theme: RawTheme, id_override: Option<&str>, stem: &str) -> Result<Converted> {
let existing = existing_ids();
convert_with_ids(theme, id_override, stem, &existing)
}
fn convert_with_ids(
theme: RawTheme,
id_override: Option<&str>,
stem: &str,
taken: &[String],
) -> Result<Converted> {
let light = theme
.kind
.as_deref()
.map(|k| k.eq_ignore_ascii_case("light"))
.unwrap_or(false);
let mut notes: Vec<String> = Vec::new();
let default_bg: Rgb = if light {
(0xff, 0xff, 0xff)
} else {
(0x1e, 0x1e, 0x1e)
};
let bg = theme
.colors
.get("editor.background")
.and_then(|c| parse_color(c, default_bg))
.unwrap_or_else(|| {
notes.push(format!(
"editor.background missing: used VS Code's default {} ground",
if light { "light" } else { "dark" }
));
default_bg
});
let pick = |keys: &[&str]| -> Option<Rgb> {
keys.iter()
.find_map(|k| theme.colors.get(*k).and_then(|c| parse_color(c, bg)))
};
let accent = pick(&[
"activityBarBadge.background",
"button.background",
"textLink.foreground",
"focusBorder",
])
.unwrap_or_else(|| {
notes.push(String::from(
"no activityBarBadge / button / textLink / focusBorder colour: accent derived from the background",
));
shade(bg, if light { -0.45 } else { 0.45 })
});
let selection = pick(&[
"editor.selectionBackground",
"list.activeSelectionBackground",
"editor.inactiveSelectionBackground",
])
.unwrap_or_else(|| {
notes.push(String::from(
"no editor.selectionBackground: selection derived from the accent",
));
shade(accent, -0.35)
});
let search = pick(&["input.background", "editorWidget.background"]).unwrap_or_else(|| {
notes.push(String::from(
"no input.background: search field derived from the background",
));
shade(bg, if light { -0.06 } else { 0.08 })
});
let button =
pick(&["button.background", "statusBarItem.remoteBackground"]).unwrap_or_else(|| {
notes.push(String::from(
"no button.background: button takes the accent",
));
accent
});
let tab_strip = pick(&["editorGroupHeader.tabsBackground", "tab.border"]);
let tab_inactive = pick(&["tab.inactiveBackground"]);
let tab_active = pick(&["tab.activeBackground"]);
let tab_hover = pick(&["tab.hoverBackground"]);
let tab_close_pill = pick(&["tab.activeBorderTop", "tab.activeBorder"]);
let osk_key = pick(&["keybindingLabel.background", "editorWidget.background"])
.unwrap_or_else(|| shade(bg, if light { -0.10 } else { 0.14 }));
let osk_special = shade(osk_key, if light { 0.35 } else { -0.35 });
let osk_armed = button;
let mut ansi: Vec<String> = Vec::new();
let mut missing_ansi = 0usize;
for key in ANSI_KEYS {
match theme.colors.get(key).and_then(|c| parse_color(c, bg)) {
Some(c) => ansi.push(hex_of(c)),
None => {
missing_ansi += 1;
ansi.clear();
break;
}
}
}
if missing_ansi > 0 {
notes.push(String::from(
"the theme does not define a full terminal.ansi* palette: croft keeps VS Code's default 16 colours, so terminal output may not match the editor",
));
}
if theme.dropped_rules > 0 {
notes.push(format!(
"{} tokenColors rule(s) could not be read and were skipped: the code palette may be less complete than the theme intends",
theme.dropped_rules
));
}
let fg = default_fg(&theme, bg);
if fg.is_none() {
notes.push(String::from(
"no editor.foreground and no default tokenColors rule: plain text keeps croft's Base16 foreground",
));
}
let mut syntax: Vec<(String, String)> = Vec::new();
for (role, scopes) in SYNTAX_SCOPES {
let from_scopes = scopes.iter().find_map(|s| scope_color(&theme, s, bg));
let from_semantic = || {
SYNTAX_SEMANTIC
.iter()
.find(|(r, _)| r == role)
.and_then(|(_, types)| types.iter().find_map(|t| semantic_color(&theme, t, bg)))
};
let from_semantic = if from_scopes.is_none() {
from_semantic()
} else {
None
};
if from_semantic.is_some() {
notes.push(format!(
"{} came from semanticTokenColors: the theme sets no TextMate scope for it, so it may differ from VS Code where semantic highlighting is off",
role.trim_start_matches("syn_")
));
}
match from_scopes.or(from_semantic) {
Some(c) => syntax.push(((*role).to_string(), hex_of(c))),
None => notes.push(format!(
"no colour for {}: croft's Base16 default is kept",
role.trim_start_matches("syn_")
)),
}
}
if let Some(fg) = fg {
syntax.push((String::from("syn_fg"), hex_of(fg)));
}
let label = theme
.name
.clone()
.unwrap_or_else(|| stem.replace(['-', '_'], " "));
let wanted = match id_override {
Some(id) => slug(id),
None => slug(&label),
};
if wanted.is_empty() {
return Err(anyhow!("could not derive a theme id from {label:?}"));
}
let id = unique_id(wanted.clone(), taken).ok_or_else(|| {
anyhow!("every id from {wanted:?} to {wanted}-999 is already taken; pass --id")
})?;
if id != slug(id_override.unwrap_or(&label)) {
notes.push(format!(
"a theme with id {:?} already exists, so this one was installed as {id:?}",
slug(id_override.unwrap_or(&label))
));
}
let mut m = String::new();
m.push_str(GENERATED_HEADER);
m.push('\n');
m.push_str(
"# Regenerate rather than hand-editing: re-running the import overwrites this file.\n",
);
for note in ¬es {
m.push_str(&format!("# note: {note}\n"));
}
m.push_str(&format!("id = \"{id}\"\n"));
m.push_str(&format!("name = \"{}\"\n", toml_escape(&label)));
m.push_str(&format!(
"description = \"{} imported from VS Code.\"\n",
toml_escape(&label)
));
m.push_str("api_version = 1\n\n");
m.push_str("[[themes]]\n");
m.push_str(&format!("id = \"{id}\"\n"));
m.push_str(&format!("label = \"{}\"\n", toml_escape(&label)));
m.push_str(&format!("background = \"{}\"\n", hex_of(bg)));
m.push_str(&format!("accent = \"{}\"\n", hex_of(accent)));
m.push_str(&format!("selection = \"{}\"\n", hex_of(selection)));
m.push_str(&format!("search = \"{}\"\n", hex_of(search)));
m.push_str(&format!("button = \"{}\"\n", hex_of(button)));
m.push_str("gradient = false\n");
m.push_str(&format!("osk_key = \"{}\"\n", hex_of(osk_key)));
m.push_str(&format!("osk_special = \"{}\"\n", hex_of(osk_special)));
m.push_str(&format!("osk_armed = \"{}\"\n", hex_of(osk_armed)));
for (key, value) in [
("tab_strip", tab_strip),
("tab_inactive", tab_inactive),
("tab_active", tab_active),
("tab_hover", tab_hover),
("tab_close_pill", tab_close_pill),
] {
if let Some(c) = value {
m.push_str(&format!("{key} = \"{}\"\n", hex_of(c)));
}
}
if !ansi.is_empty() {
let quoted: Vec<String> = ansi.iter().map(|c| format!("\"{c}\"")).collect();
m.push_str(&format!("ansi = [{}]\n", quoted.join(", ")));
}
for (key, value) in &syntax {
m.push_str(&format!("{key} = \"{value}\"\n"));
}
Ok(Converted {
id,
label,
manifest: m,
notes,
})
}
fn toml_escape(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for c in s.chars() {
match c {
'\\' => out.push_str("\\\\"),
'"' => out.push_str("\\\""),
'\n' => out.push_str("\\n"),
'\r' => out.push_str("\\r"),
'\t' => out.push_str("\\t"),
c if c.is_control() => out.push_str(&format!("\\u{:04X}", c as u32)),
c => out.push(c),
}
}
out
}
pub fn install(converted: &Converted) -> Result<Installed> {
install_into(&crate::lsp::manifest::user_extensions_dir(), converted)
}
pub fn install_into(extensions_dir: &Path, converted: &Converted) -> Result<Installed> {
let dir = extensions_dir.join(format!("theme-{}", converted.id));
std::fs::create_dir_all(&dir).with_context(|| format!("creating {}", dir.display()))?;
let path = dir.join("extension.toml");
let replaced = path.is_file();
std::fs::write(&path, &converted.manifest)
.with_context(|| format!("writing {}", path.display()))?;
Ok(Installed { path, replaced })
}
pub struct Installed {
pub path: std::path::PathBuf,
pub replaced: bool,
}
#[cfg(test)]
mod tests {
use super::*;
fn dark_fixture() -> &'static str {
r##"{
"name": "Fixture Dark",
"type": "dark",
"colors": {
"editor.background": "#1e2030",
"editor.foreground": "#cad3f5",
"activityBarBadge.background": "#8aadf4",
"editor.selectionBackground": "#3a3f58",
"input.background": "#181926",
"button.background": "#7dc4e4",
"editorGroupHeader.tabsBackground": "#181926",
"tab.inactiveBackground": "#1e2030",
"tab.activeBackground": "#24273a",
"terminal.ansiBlack": "#000100",
"terminal.ansiRed": "#000101",
"terminal.ansiGreen": "#000102",
"terminal.ansiYellow": "#000103",
"terminal.ansiBlue": "#000104",
"terminal.ansiMagenta": "#000105",
"terminal.ansiCyan": "#000106",
"terminal.ansiWhite": "#000107",
"terminal.ansiBrightBlack": "#000108",
"terminal.ansiBrightRed": "#000109",
"terminal.ansiBrightGreen": "#00010a",
"terminal.ansiBrightYellow": "#00010b",
"terminal.ansiBrightBlue": "#00010c",
"terminal.ansiBrightMagenta": "#00010d",
"terminal.ansiBrightCyan": "#00010e",
"terminal.ansiBrightWhite": "#00010f"
},
"tokenColors": [
{ "settings": { "foreground": "#cad3f5" } },
{ "scope": "comment", "settings": { "foreground": "#6e738d" } },
{ "scope": ["keyword", "storage.type"], "settings": { "foreground": "#c6a0f6" } },
{ "scope": "string", "settings": { "foreground": "#a6da95" } },
{ "scope": "constant.numeric", "settings": { "foreground": "#f5a97f" } },
{ "scope": "entity.name.function", "settings": { "foreground": "#8aadf4" } },
{ "scope": "entity.name.type", "settings": { "foreground": "#eed49f" } },
{ "scope": "entity.name.tag", "settings": { "foreground": "#ed8796" } }
]
}"##
}
fn convert_str(src: &str) -> Converted {
let raw = parse_theme(src).expect("fixture parses");
convert(raw, None, "fixture").expect("fixture converts")
}
#[test]
fn the_generated_manifest_parses_as_a_croft_theme() {
let converted = convert_str(dark_fixture());
let manifest =
crate::lsp::manifest::parse(&converted.manifest).expect("croft parses the manifest");
assert_eq!(manifest.themes.len(), 1);
let t = &manifest.themes[0];
assert_eq!(t.id, "fixture-dark");
assert_eq!(t.label, "Fixture Dark");
assert_eq!(t.background, "#1e2030");
assert_eq!(t.accent, "#8aadf4", "the badge colour, not focusBorder");
assert_eq!(t.selection, "#3a3f58");
assert_eq!(t.search, "#181926");
assert_eq!(t.button, "#7dc4e4");
assert_eq!(t.ansi.len(), 16, "a full terminal palette carries over");
let expected: Vec<String> = (0..16).map(|i| format!("#0001{i:02x}")).collect();
assert_eq!(
t.ansi, expected,
"the sixteen slots must land in croft's order, black..bright white"
);
assert_eq!(t.syn_keyword, "#c6a0f6");
assert_eq!(t.syn_string, "#a6da95");
assert_eq!(t.syn_comment, "#6e738d");
assert_eq!(t.syn_constant, "#f5a97f");
assert_eq!(t.syn_function, "#8aadf4");
assert_eq!(t.syn_type, "#eed49f");
assert_eq!(t.syn_tag, "#ed8796");
assert_eq!(t.syn_fg, "#cad3f5", "the scope-less rule is the default fg");
}
#[test]
fn a_narrower_rule_never_fills_a_general_role() {
let src = r##"{
"type": "dark",
"colors": { "editor.background": "#000000" },
"tokenColors": [
{ "scope": "entity.name.function.decorator", "settings": { "foreground": "#ffff00" } },
{ "scope": "support.function", "settings": { "foreground": "#0000ff" } },
{ "scope": "keyword.operator.arithmetic", "settings": { "foreground": "#ff0000" } },
{ "scope": "keyword", "settings": { "foreground": "#00ff00" } }
]
}"##;
let converted = convert_str(src);
let t = &crate::lsp::manifest::parse(&converted.manifest)
.unwrap()
.themes[0];
assert_eq!(
t.syn_function, "#0000ff",
"the decorator rule must not colour functions in general"
);
assert_eq!(
t.syn_keyword, "#00ff00",
"the arithmetic-operator rule must not colour keywords in general"
);
}
#[test]
fn the_most_specific_covering_rule_wins() {
let src = r##"{
"type": "dark",
"colors": { "editor.background": "#000000" },
"tokenColors": [
{ "scope": "entity", "settings": { "foreground": "#111111" } },
{ "scope": "entity.name", "settings": { "foreground": "#222222" } },
{ "scope": "string", "settings": { "foreground": "#333333" } },
{ "scope": "string", "settings": { "foreground": "#444444" } }
]
}"##;
let converted = convert_str(src);
let t = &crate::lsp::manifest::parse(&converted.manifest)
.unwrap()
.themes[0];
assert_eq!(t.syn_function, "#222222", "entity.name beats entity");
assert_eq!(t.syn_string, "#444444", "the later of two equals wins");
}
#[test]
fn translucent_colours_composite_over_the_background() {
let src = r##"{
"type": "dark",
"colors": {
"editor.background": "#000000",
"editor.selectionBackground": "#ffffff80"
}
}"##;
let converted = convert_str(src);
let t = &crate::lsp::manifest::parse(&converted.manifest)
.unwrap()
.themes[0];
assert_eq!(
t.selection, "#808080",
"50% white over black is mid grey, not white"
);
}
#[test]
fn a_light_theme_falls_back_to_light_ground() {
let src = r##"{ "name": "Pale", "type": "light", "colors": {} }"##;
let converted = convert_str(src);
let t = &crate::lsp::manifest::parse(&converted.manifest)
.unwrap()
.themes[0];
assert_eq!(t.background, "#ffffff");
let bg = parse_color(&t.background, (0, 0, 0)).unwrap();
let search = parse_color(&t.search, (0, 0, 0)).unwrap();
assert!(
search.0 < bg.0 && search.1 < bg.1 && search.2 < bg.2,
"a light theme's search field must be DARKER than its ground, got {}",
t.search
);
assert!(
!converted.notes.is_empty(),
"a theme this bare must report what was derived"
);
}
#[test]
fn short_hex_and_jsonc_extras_parse() {
let src = r##"{
// a comment VS Code tolerates
"name": "Terse",
"type": "dark",
"colors": {
"editor.background": "#123",
"editor.foreground": "#fff",
},
}"##;
let converted = convert_str(src);
let t = &crate::lsp::manifest::parse(&converted.manifest)
.unwrap()
.themes[0];
assert_eq!(t.background, "#112233", "#123 expands to #112233");
assert_eq!(t.syn_fg, "#ffffff");
}
#[test]
fn a_partial_ansi_palette_is_dropped_and_reported() {
let src = r##"{
"type": "dark",
"colors": {
"editor.background": "#000000",
"terminal.ansiRed": "#ff0000",
"terminal.ansiGreen": "#00ff00"
}
}"##;
let converted = convert_str(src);
let t = &crate::lsp::manifest::parse(&converted.manifest)
.unwrap()
.themes[0];
assert!(t.ansi.is_empty(), "a half palette must not ship");
assert!(
converted.notes.iter().any(|n| n.contains("terminal.ansi")),
"and the user must be told, got {:?}",
converted.notes
);
}
#[test]
fn an_include_chain_merges_base_first() {
let dir = tempfile::TempDir::new().unwrap();
let base = dir.path().join("base.json");
std::fs::write(
&base,
r##"{
"type": "dark",
"colors": { "editor.background": "#101010", "editor.foreground": "#aaaaaa" },
"tokenColors": [{ "scope": "comment", "settings": { "foreground": "#555555" } }]
}"##,
)
.unwrap();
let top = dir.path().join("top.json");
std::fs::write(
&top,
r##"{
"name": "Layered",
"include": "./base.json",
"type": "dark",
"colors": { "editor.foreground": "#ffffff" }
}"##,
)
.unwrap();
let converted = convert_file(&top, None).expect("the chain converts");
let t = &crate::lsp::manifest::parse(&converted.manifest)
.unwrap()
.themes[0];
assert_eq!(t.background, "#101010", "the base fills what the top omits");
assert_eq!(t.syn_fg, "#ffffff", "the including file wins");
assert_eq!(t.syn_comment, "#555555", "base token rules carry over");
}
#[test]
fn an_absolute_include_is_refused() {
let dir = tempfile::TempDir::new().unwrap();
let top = dir.path().join("top.json");
std::fs::write(
&top,
r##"{ "include": "/etc/passwd", "type": "dark", "colors": {} }"##,
)
.unwrap();
let err = convert_file(&top, None).expect_err("an absolute include must be refused");
assert!(
format!("{err:#}").contains("relative"),
"the error must say why, got {err:#}"
);
}
#[test]
fn a_non_ascii_theme_name_survives_the_jsonc_strip() {
let src = r##"{
// a comment, so the stripper definitely runs
"name": "Caf\u00e9 Noir \u2014 \u3086\u3081",
"type": "dark",
"colors": { "editor.background": "#101010" }
}"##;
let converted = convert_str(src);
assert_eq!(
converted.label, "Caf\u{e9} Noir \u{2014} \u{3086}\u{3081}",
"the name must come through intact, not as mojibake"
);
let manifest = crate::lsp::manifest::parse(&converted.manifest).unwrap();
assert_eq!(manifest.themes[0].label, converted.label);
}
#[test]
fn a_descendant_selector_fills_a_role_but_yields_to_a_direct_rule() {
let contextual_only = r##"{
"type": "dark",
"colors": { "editor.background": "#000000" },
"tokenColors": [
{ "scope": "source.js entity.name.function", "settings": { "foreground": "#112233" } }
]
}"##;
let t = &crate::lsp::manifest::parse(&convert_str(contextual_only).manifest)
.unwrap()
.themes[0];
assert_eq!(
t.syn_function, "#112233",
"a contextual rule is better than leaving the role unset"
);
let both = r##"{
"type": "dark",
"colors": { "editor.background": "#000000" },
"tokenColors": [
{ "scope": "source.js entity.name.function.member", "settings": { "foreground": "#112233" } },
{ "scope": "entity.name.function", "settings": { "foreground": "#445566" } }
]
}"##;
let t = &crate::lsp::manifest::parse(&convert_str(both).manifest)
.unwrap()
.themes[0];
assert_eq!(
t.syn_function, "#445566",
"a direct rule outranks a contextual one however deep"
);
}
#[test]
fn semantic_token_colours_are_a_fallback_not_an_override() {
let src = r##"{
"type": "dark",
"colors": { "editor.background": "#000000" },
"semanticTokenColors": {
"function": "#aabbcc",
"keyword": { "foreground": "#ddeeff", "bold": true },
"type.defaultLibrary": "#010203"
},
"tokenColors": [
{ "scope": "keyword", "settings": { "foreground": "#999999" } }
]
}"##;
let t = &crate::lsp::manifest::parse(&convert_str(src).manifest)
.unwrap()
.themes[0];
assert_eq!(
t.syn_function, "#aabbcc",
"no scope rule, so semantic fills it"
);
assert_eq!(
t.syn_keyword, "#999999",
"the scope rule wins where the theme set both"
);
assert_eq!(
t.syn_type, "#010203",
"a modifier suffix still matches its token type"
);
}
#[test]
fn an_id_that_collides_with_an_existing_theme_is_suffixed_and_reported() {
let src = r##"{ "name": "Dracula", "type": "dark", "colors": { "editor.background": "#282a36" } }"##;
let raw = parse_theme(src).unwrap();
let taken = vec![String::from("dracula"), String::from("dracula-2")];
let converted = convert_with_ids(raw, None, "dracula", &taken).unwrap();
assert_eq!(converted.id, "dracula-3");
assert!(
converted.notes.iter().any(|n| n.contains("already exists")),
"the user must be told the id changed: {:?}",
converted.notes
);
let ids = existing_ids();
assert!(
ids.iter().any(|i| i == "black"),
"the bundled ids must be what collision is checked against"
);
}
#[test]
fn the_last_scopeless_rule_is_the_default_foreground() {
let src = r##"{
"type": "dark",
"colors": { "editor.background": "#000000" },
"tokenColors": [
{ "settings": { "foreground": "#111111" } },
{ "settings": { "foreground": "#222222" } }
]
}"##;
let t = &crate::lsp::manifest::parse(&convert_str(src).manifest)
.unwrap()
.themes[0];
assert_eq!(t.syn_fg, "#222222");
}
#[test]
fn an_include_that_traverses_upwards_is_refused() {
let dir = tempfile::TempDir::new().unwrap();
let nested = dir.path().join("themes");
std::fs::create_dir(&nested).unwrap();
let top = nested.join("top.json");
std::fs::write(
&top,
r##"{ "include": "../secrets.json", "type": "dark", "colors": {} }"##,
)
.unwrap();
let err = convert_file(&top, None).expect_err("traversal must be refused");
assert!(format!("{err:#}").contains("inside the theme"), "{err:#}");
}
#[test]
fn tab_colours_are_left_out_when_the_theme_does_not_name_them() {
let bare = r##"{ "type": "dark", "colors": { "editor.background": "#101010" } }"##;
let converted = convert_str(bare);
assert!(
!converted.manifest.contains("tab_strip"),
"a derived tab colour must not be frozen into the manifest:\n{}",
converted.manifest
);
let named = r##"{
"type": "dark",
"colors": {
"editor.background": "#101010",
"tab.activeBackground": "#202020"
}
}"##;
let converted = convert_str(named);
assert!(converted.manifest.contains("tab_active = \"#202020\""));
assert!(!converted.manifest.contains("tab_hover"));
}
#[test]
fn control_characters_in_a_name_stay_parseable() {
let src = "{ \"name\": \"Odd\\u0007Name\\tTabbed\", \"type\": \"dark\", \"colors\": {} }";
let converted = convert_str(src);
crate::lsp::manifest::parse(&converted.manifest)
.expect("a control character must not produce an unparseable manifest");
}
#[test]
fn a_reimport_reuses_its_own_id_rather_than_minting_a_new_one() {
let manifest = convert_str(dark_fixture()).manifest;
assert!(
manifest.starts_with(GENERATED_HEADER),
"the header is what marks a manifest as ours: {manifest:.80}"
);
let raw = parse_theme(dark_fixture()).unwrap();
let taken = vec![String::from("fixture-dark")];
let collided = convert_with_ids(raw, None, "fixture", &taken).unwrap();
assert_eq!(collided.id, "fixture-dark-2");
let raw = parse_theme(dark_fixture()).unwrap();
let fresh = convert_with_ids(raw, None, "fixture", &[]).unwrap();
assert_eq!(
fresh.id, "fixture-dark",
"with the id free, the import keeps its natural name"
);
}
#[test]
fn install_writes_the_manifest_and_reports_a_replacement() {
let dir = tempfile::TempDir::new().unwrap();
let converted = convert_str(dark_fixture());
let first = install_into(dir.path(), &converted).expect("first install");
assert!(first.path.is_file());
assert!(!first.replaced, "nothing was there the first time");
assert!(
std::fs::read_to_string(&first.path)
.unwrap()
.starts_with(GENERATED_HEADER)
);
let second = install_into(dir.path(), &converted).expect("re-install");
assert_eq!(
second.path, first.path,
"the same id lands in the same file"
);
assert!(second.replaced, "the second run must say it overwrote");
}
#[test]
fn a_role_filled_from_semantic_tokens_is_reported() {
let src = r##"{
"type": "dark",
"colors": { "editor.background": "#000000" },
"semanticTokenColors": { "function": "#aabbcc" }
}"##;
let converted = convert_str(src);
assert!(
converted
.notes
.iter()
.any(|n| n.contains("semanticTokenColors") && n.contains("function")),
"the note must name the role: {:?}",
converted.notes
);
}
#[test]
fn the_scope_and_semantic_tables_cover_the_same_roles() {
let scope_roles: Vec<&str> = SYNTAX_SCOPES.iter().map(|(r, _)| *r).collect();
let semantic_roles: Vec<&str> = SYNTAX_SEMANTIC.iter().map(|(r, _)| *r).collect();
assert_eq!(
scope_roles, semantic_roles,
"the two tables are read together by role and must stay aligned"
);
}
#[test]
fn one_unreadable_rule_does_not_discard_the_whole_palette() {
let src = r##"{
"type": "dark",
"colors": { "editor.background": "#000000" },
"tokenColors": [
{ "scope": "comment", "settings": { "foreground": "#111111" } },
{ "scope": null, "settings": { "foreground": "#222222" } },
{ "scope": "keyword", "settings": { "foreground": "#333333" } }
]
}"##;
let converted = convert_str(src);
let t = &crate::lsp::manifest::parse(&converted.manifest)
.unwrap()
.themes[0];
assert_eq!(t.syn_comment, "#111111", "rules before the bad one survive");
assert_eq!(t.syn_keyword, "#333333", "and rules after it");
assert!(
converted
.notes
.iter()
.any(|n| n.contains("could not be read")),
"and the skipped rule is reported: {:?}",
converted.notes
);
}
#[test]
fn a_theme_named_in_a_non_latin_script_can_be_imported() {
for (name, want) in [
(
"\u{4e2d}\u{6587}\u{4e3b}\u{9898}",
"\u{4e2d}\u{6587}\u{4e3b}\u{9898}",
),
("\u{30c6}\u{30fc}\u{30de}", "\u{30c6}\u{30fc}\u{30de}"),
(
"\u{422}\u{435}\u{43c}\u{430}",
"\u{442}\u{435}\u{43c}\u{430}",
),
("\u{4e2d}\u{6587} Dark", "\u{4e2d}\u{6587}-dark"),
(
"\u{939}\u{93f}\u{928}\u{94d}\u{926}\u{940}",
"\u{939}\u{93f}\u{928}\u{94d}\u{926}\u{940}",
),
("Cafe\u{301}", "caf\u{e9}"),
("U\u{308}ber", "\u{fc}ber"),
] {
let src = format!(
r##"{{ "name": "{name}", "type": "dark", "colors": {{ "editor.background": "#101010" }} }}"##
);
let raw = parse_theme(&src).expect("parses");
let converted =
convert_with_ids(raw, None, "fixture", &[]).expect("a non-Latin name must import");
assert_eq!(converted.id, want, "id derived from {name:?}");
let manifest = crate::lsp::manifest::parse(&converted.manifest)
.expect("and the manifest must parse");
assert_eq!(manifest.themes[0].label, name);
}
}
#[test]
fn a_non_ascii_colour_value_is_refused_rather_than_sliced() {
for value in [
"#\u{20ac}", "#\u{e9}\u{e9}\u{e9}", "#\u{4e2d}\u{6587}", "#\u{1f600}", ] {
assert_eq!(
parse_color(value, (0, 0, 0)),
None,
"{value:?} is not a colour and must not be indexed as one"
);
}
assert_eq!(parse_color("#fff", (0, 0, 0)), Some((255, 255, 255)));
assert_eq!(parse_color("#102030", (0, 0, 0)), Some((16, 32, 48)));
}
#[test]
fn a_name_with_no_letter_or_digit_is_refused() {
for name in [
"\u{301}\u{308}", "\u{1f600}\u{1f680}", "!!!",
"..",
" ",
] {
assert_eq!(slug(name), "", "{name:?} has nothing to name a theme with");
let src = format!(r##"{{ "name": "{name}", "type": "dark", "colors": {{}} }}"##);
let raw = parse_theme(&src).expect("parses");
assert!(
convert_with_ids(raw, None, "", &[]).is_err(),
"{name:?} must fail the import rather than install an unnameable theme"
);
}
assert_eq!(
slug("\u{939}\u{93f}\u{928}\u{94d}\u{926}\u{940}"),
"\u{939}\u{93f}\u{928}\u{94d}\u{926}\u{940}"
);
assert_eq!(slug("Cafe\u{301}"), "café");
}
#[test]
fn canonically_equivalent_names_share_an_id() {
assert_eq!(slug("Café Dark"), "café-dark");
assert_eq!(slug("Cafe\u{301} Dark"), "café-dark");
assert_eq!(slug("İstanbul"), slug("i\u{307}stanbul"));
assert_eq!(slug("H\u{331}ana"), slug("\u{1e96}ana"));
assert_eq!(slug("H\u{331}ana"), "\u{1e96}ana");
assert_eq!(slug("One Dark Pro"), "one-dark-pro");
assert_eq!(slug("Ñandú"), "ñandú");
let nfc =
parse_theme(r##"{ "name": "Café Dark", "type": "dark", "colors": {} }"##).unwrap();
let nfd =
parse_theme(r##"{ "name": "Café Dark", "type": "dark", "colors": {} }"##).unwrap();
let a = convert_with_ids(nfc, None, "", &[]).expect("converts");
let b = convert_with_ids(nfd, None, "", &[]).expect("converts");
assert_eq!(a.id, b.id);
assert_eq!(a.id, "café-dark");
}
#[test]
fn an_exhausted_id_space_is_an_error_not_a_collision() {
let mut taken = vec![String::from("busy")];
taken.extend((2..1000).map(|n| format!("busy-{n}")));
assert_eq!(unique_id(String::from("busy"), &taken), None);
assert_eq!(
unique_id(String::from("free"), &taken),
Some(String::from("free"))
);
let raw = parse_theme(r##"{ "name": "Busy", "type": "dark", "colors": {} }"##).unwrap();
let err = convert_with_ids(raw, None, "busy", &taken)
.expect_err("an exhausted space must fail loudly");
assert!(format!("{err:#}").contains("--id"), "{err:#}");
}
#[test]
fn a_quoted_theme_name_stays_valid_toml() {
let src = r##"{
"name": "Andromeda \"Bordered\"",
"type": "dark",
"colors": { "editor.background": "#000000" }
}"##;
let converted = convert_str(src);
let manifest = crate::lsp::manifest::parse(&converted.manifest)
.expect("a quoted name must not break the manifest");
assert_eq!(manifest.themes[0].label, "Andromeda \"Bordered\"");
assert_eq!(manifest.themes[0].id, "andromeda-bordered");
}
}