3 Commits
Author SHA1 Message Date
Jan-Bulthuis bfc908f075 feat: generate css from theme 2026-07-19 19:07:09 +02:00
Jan-Bulthuis 255d6e9611 feat: add themes derivation 2026-07-19 18:34:37 +02:00
Jan-Bulthuis 08504aac28 feat: read helix themes 2026-07-19 18:34:06 +02:00
7 changed files with 135 additions and 200 deletions

No files matched your search

Generated
+1 -2
View File
@@ -1417,7 +1417,7 @@ dependencies = [
[[package]]
name = "karnaugh"
version = "0.2.0"
version = "0.1.5"
dependencies = [
"axum",
"clap",
@@ -1427,7 +1427,6 @@ dependencies = [
"tar",
"time",
"tokio",
"toml",
"tower-http",
"tracing",
"tracing-subscriber",
+1 -2
View File
@@ -1,6 +1,6 @@
[package]
name = "karnaugh"
version = "0.2.0"
version = "0.1.5"
edition = "2024"
[dependencies]
@@ -28,4 +28,3 @@ typst-utils = "0.15.1"
tree-house = "0.4.0"
tree-house-bindings = { version = "0.3.2", features = ["ropey"] }
ropey = "1.6.1"
toml = "0.8.23"
Generated
+18 -4
View File
@@ -20,9 +20,7 @@
},
"helix": {
"inputs": {
"nixpkgs": [
"nixpkgs"
],
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
},
"locked": {
@@ -40,6 +38,22 @@
}
},
"nixpkgs": {
"locked": {
"lastModified": 1775036866,
"narHash": "sha256-ZojAnPuCdy657PbTq5V0Y+AHKhZAIwSIT2cb8UgAz/U=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "6201e203d09599479a3b3450ed24fa81537ebc4e",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1784120854,
"narHash": "sha256-KesHgItiZPgGX740axSiQLcIQ8D24MDqNpkKYWIek8k=",
@@ -59,7 +73,7 @@
"inputs": {
"flake-utils": "flake-utils",
"helix": "helix",
"nixpkgs": "nixpkgs"
"nixpkgs": "nixpkgs_2"
}
},
"rust-overlay": {
+8 -26
View File
@@ -3,7 +3,6 @@
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
helix.url = "github:helix-editor/helix";
helix.inputs.nixpkgs.follows = "nixpkgs";
};
outputs =
@@ -28,27 +27,23 @@
in
pkgs.stdenv.mkDerivation {
name = "syntax";
nativeBuildInputs = [ pkgs.patchelf ];
phases = [
"buildPhase"
"installPhase"
];
buildPhase = ''
mkdir -p build
cp -r ${helix}/runtime/queries/* ./build/
chmod u+w ./build -R
cp -r ${helix}/runtime/queries/* ./
chmod u+w ./* -R
for so in ${grammars}/*.so; do
name=$(basename "$so" .so)
if [ -d "./build/$name" ]; then
cp "$so" "./build/$name/$name.so"
chmod u+w "./build/$name/$name.so"
patchelf --shrink-rpath "./build/$name/$name.so"
if [ -d "./$name" ]; then
cp "$so" "./$name/$name.so"
fi
done
'';
installPhase = ''
mkdir -p $out
cp -r ./build/* $out/
cp -r ./* $out/
'';
};
packages.themes = pkgs.stdenv.mkDerivation {
@@ -61,15 +56,8 @@
};
packages.karnaugh = pkgs.rustPlatform.buildRustPackage (final: {
pname = "karnaugh";
version = "0.2.0";
src = pkgs.lib.fileset.toSource {
root = ./.;
fileset = pkgs.lib.fileset.unions [
./Cargo.toml
./Cargo.lock
./src
];
};
version = "0.1.5";
src = self;
cargoLock.lockFile = ./Cargo.lock;
});
packages.container = pkgs.dockerTools.buildImage {
@@ -83,13 +71,7 @@
};
config = {
Entrypoint = [
"/bin/karnaugh"
"--syntax-root"
"${packages.syntax}"
"--themes-root"
"${packages.themes}"
];
Entrypoint = [ "/bin/karnaugh" ];
};
};
packages.default = packages.karnaugh;
+5
View File
@@ -69,6 +69,11 @@ struct AppConfig {
#[arg(short = 't', long, value_name = "DIR", default_value = None)]
themes_root: Option<PathBuf>,
/// Grammars to load, separated by commas.
/// Useful to avoid the memory required to load all languages.
#[arg(short = 'L', long = "lang", value_name = "NAME", value_delimiter = ',')]
languages: Vec<String>,
/// The log level to use
#[arg(short = 'l', long, value_name = "LEVEL", default_value_t = Level::INFO)]
log_level: Level,
+84 -112
View File
@@ -1,10 +1,10 @@
use std::{
borrow::Cow,
collections::{BTreeSet, HashMap},
collections::{HashMap, HashSet},
error::Error,
fs,
path::{Path, PathBuf},
sync::{Arc, OnceLock, RwLock},
path::Path,
sync::OnceLock,
time::Duration,
};
@@ -24,18 +24,48 @@ use typst_html::{HtmlAttr, HtmlElem, attr, tag};
use crate::AppConfig;
/// Where grammars are loaded from, captured at `init`.
static SYNTAX_ROOT: OnceLock<PathBuf> = OnceLock::new();
static REGISTRY: OnceLock<LanguageRegistry> = OnceLock::new();
/// LanguageRegistries keyed by ScopeSet
static REGISTRIES: OnceLock<RwLock<HashMap<BTreeSet<String>, Arc<LanguageRegistry>>>> =
OnceLock::new();
/// Class names used in highlighted code.
const HIGHLIGHT_NAMES: &[&str] = &[
"attribute",
"comment",
"comment.block",
"comment.line",
"constant",
"constant.builtin",
"constructor",
"function",
"function.builtin",
"function.macro",
"function.method",
"keyword",
"keyword.control",
"keyword.operator",
"label",
"namespace",
"number",
"operator",
"property",
"punctuation",
"punctuation.bracket",
"punctuation.delimiter",
"special",
"string",
"string.special",
"tag",
"type",
"type.builtin",
"variable",
"variable.builtin",
"variable.parameter",
];
/// Get the index of the longest prefix of `scope` present in `scopes`.
fn highlight_for_scope(scope: &str, scopes: &[String]) -> Option<Highlight> {
/// Get the longest matching highlight name for a given scope.
fn highlight_for_scope(scope: &str) -> Option<Highlight> {
let mut candidate = scope;
loop {
if let Some(i) = scopes.iter().position(|n| n == candidate) {
if let Some(i) = HIGHLIGHT_NAMES.iter().position(|&n| n == candidate) {
return Some(Highlight::new(i as u32));
}
match candidate.rfind('.') {
@@ -46,89 +76,49 @@ fn highlight_for_scope(scope: &str, scopes: &[String]) -> Option<Highlight> {
}
struct LanguageRegistry {
syntax_root: PathBuf,
/// The scope vocabulary.
scopes: Vec<String>,
/// Name to Language mapping, Option to allow storing None for failed
/// configuration and preventing reloading.
by_name: RwLock<HashMap<String, Option<Language>>>,
configs: RwLock<Vec<&'static LanguageConfig>>,
by_name: HashMap<String, Language>,
configs: Vec<LanguageConfig>,
}
impl LanguageRegistry {
fn new(syntax_root: PathBuf, scopes: Vec<String>) -> Self {
Self {
syntax_root,
scopes,
by_name: RwLock::new(HashMap::new()),
configs: RwLock::new(Vec::new()),
/// Loads grammars from `syntax_root`. When `languages` is non-empty, only
/// grammars with those names are loaded; otherwise every grammar found is.
fn new(syntax_root: &Path, languages: &[String]) -> Self {
let filter: Option<HashSet<&str>> =
(!languages.is_empty()).then(|| languages.iter().map(String::as_str).collect());
let mut by_name = HashMap::new();
let mut configs = Vec::new();
for entry in fs::read_dir(syntax_root).into_iter().flatten().flatten() {
let name = entry.file_name().to_string_lossy().into_owned();
if filter.as_ref().is_some_and(|f| !f.contains(name.as_str())) {
continue;
}
let so_path = entry.path().join(format!("{name}.so"));
if !so_path.exists() {
continue;
}
/// The `Language` for `name`, loading and configuring its grammar on first
/// request. Returns `None` if the grammar is missing or fails to load.
fn language(&self, name: &str) -> Option<Language> {
if let Some(cached) = self.by_name.read().unwrap().get(name) {
return *cached;
}
let loaded = self.load(name);
let mut by_name = self.by_name.write().unwrap();
if let Some(cached) = by_name.get(name) {
return *cached;
}
let language = loaded.map(|config| {
let mut configs = self.configs.write().unwrap();
configs.push(config);
Language::new((configs.len() - 1) as u32)
});
by_name.insert(name.to_owned(), language);
language
}
/// Loads and configures the grammar named `name`, leaking the config to a
/// `'static` borrow. `None` if the `.so` is absent or parsing fails.
fn load(&self, name: &str) -> Option<&'static LanguageConfig> {
let lang_dir = self.syntax_root.join(name);
if !lang_dir.join(format!("{name}.so")).exists() {
warn!("No grammar found for language {name}");
return None;
}
match load_language(name, &lang_dir, &self.syntax_root) {
match load_language(&name, &entry.path(), syntax_root) {
Ok(config) => {
config.configure(|scope| highlight_for_scope(scope, &self.scopes));
Some(Box::leak(Box::new(config)))
by_name.insert(name, Language::new(configs.len() as u32));
configs.push(config);
}
Err(e) => {
warn!("Failed to load grammar {name}: {e}");
None
Err(e) => warn!("Failed to load grammar {name}: {e}"),
}
}
}
}
/// Fetch or build the registry configured for `scopes`.
fn registry_for(scopes: BTreeSet<String>) -> Option<Arc<LanguageRegistry>> {
let registries = REGISTRIES.get()?;
// Fast path: already built.
if let Some(registry) = registries.read().unwrap().get(&scopes) {
return Some(registry.clone());
}
let syntax_root = SYNTAX_ROOT.get()?;
let ordered = scopes.iter().cloned().collect();
let registry = Arc::new(LanguageRegistry::new(syntax_root.clone(), ordered));
// Warn about explicitly requested grammars that were not found.
for lang in languages {
if !by_name.contains_key(lang) {
warn!("Requested grammar {lang} was not found in the syntax root");
}
}
Some(
registries
.write()
.unwrap()
.entry(scopes)
.or_insert(registry)
.clone(),
)
info!("Loaded {} syntax highlighting grammars", configs.len());
Self { by_name, configs }
}
}
fn load_language(
@@ -151,6 +141,7 @@ fn load_language(
&read("injections"),
&read("locals"),
)?;
config.configure(highlight_for_scope);
Ok(config)
}
@@ -158,17 +149,17 @@ fn load_language(
impl LanguageLoader for LanguageRegistry {
fn language_for_marker(&self, marker: InjectionLanguageMarker<'_>) -> Option<Language> {
match marker {
InjectionLanguageMarker::Name(name) => self.language(name),
InjectionLanguageMarker::Name(name) => self.by_name.get(name).copied(),
InjectionLanguageMarker::Match(text) => {
let name: Cow<str> = text.into();
self.language(name.as_ref())
self.by_name.get(name.as_ref()).copied()
}
_ => None,
}
}
fn get_config(&self, lang: Language) -> Option<&LanguageConfig> {
self.configs.read().unwrap().get(lang.idx()).copied()
self.configs.get(lang.idx())
}
}
@@ -176,8 +167,7 @@ pub fn init(config: &AppConfig) {
match &config.syntax_root {
Some(syntax_root) => {
info!("Loading syntax files from {}", syntax_root.display());
SYNTAX_ROOT.get_or_init(|| syntax_root.clone());
REGISTRIES.get_or_init(|| RwLock::new(HashMap::new()));
REGISTRY.get_or_init(|| LanguageRegistry::new(syntax_root, &config.languages));
}
None => {
warn!(
@@ -200,15 +190,8 @@ fn highlight(
#[named]
#[default(None)]
lang: Option<String>,
#[named]
#[default(None)]
light: Option<String>,
#[named]
#[default(None)]
dark: Option<String>,
) -> Content {
let highlighted = highlight_code(&text, lang.as_deref(), light.as_deref(), dark.as_deref())
.unwrap_or_else(|| TextElem::packed(text));
let highlighted = highlight_code(&text, lang.as_deref()).unwrap_or(TextElem::packed(text));
if block {
HtmlElem::new(tag::pre)
@@ -224,25 +207,14 @@ fn highlight(
}
/// Highlights `source` as `lang`.
fn highlight_code(
source: &str,
lang: Option<&str>,
light: Option<&str>,
dark: Option<&str>,
) -> Option<Content> {
let (lang, light, dark) = (lang?, light?, dark?);
let Some(scopes) = crate::themes::shared_scopes(light, dark) else {
warn!("Unknown theme in pair (light={light}, dark={dark})");
return None;
};
let registry = registry_for(scopes)?;
let language = registry.language(lang)?;
fn highlight_code(source: &str, lang: Option<&str>) -> Option<Content> {
let registry = REGISTRY.get()?;
let language = *registry.by_name.get(lang?)?;
let rope = Rope::from_str(source);
let slice = rope.slice(..);
let loader = registry.as_ref();
let syntax = Syntax::new(slice, language, Duration::from_secs(3), loader).ok()?;
let mut hl = Highlighter::new(&syntax, slice, loader, 0..source.len() as u32);
let syntax = Syntax::new(slice, language, Duration::from_millis(500), registry).ok()?;
let mut hl = Highlighter::new(&syntax, slice, registry, 0..source.len() as u32);
let src = source.as_bytes();
let src_len = source.len() as u32;
@@ -279,7 +251,7 @@ fn highlight_code(
HighlightEvent::Push => classes.clone(),
};
classes = highlights.fold(base, |mut acc, h| {
let name = registry.scopes[h.idx()].replace('.', "-");
let name = HIGHLIGHT_NAMES[h.idx()].replace('.', "-");
if !acc.is_empty() {
acc.push(' ');
}
+18 -54
View File
@@ -1,5 +1,6 @@
use std::{
collections::{BTreeSet, HashMap, HashSet},
collections::{HashMap, HashSet},
error::Error,
fs,
path::Path,
sync::OnceLock,
@@ -7,7 +8,10 @@ use std::{
use toml::{Table, Value};
use tracing::{info, warn};
use typst::foundations::{Func, NativeFunc, func};
use typst::{
foundations::{Content, Func, NativeElement, NativeFunc, func},
text::TextElem,
};
use crate::AppConfig;
@@ -82,13 +86,7 @@ impl ThemeRegistry {
let themes = helix_themes
.into_iter()
.flat_map(|(name, table)| match Theme::from_toml(table) {
Some(theme) => Some((name, theme)),
None => {
warn!("Theme {name} is missing an `ui.background`, skipping it");
None
}
})
.flat_map(|(name, table)| Theme::from_toml(table).map(|t| (name, t)))
.collect();
Self { themes }
@@ -110,13 +108,8 @@ impl Color {
fn from_palette(color: &str, palette: &HashMap<String, Color>) -> Option<Self> {
if color.starts_with('#') {
Self::from_str(color)
} else if let Some(color) = palette.get(color) {
Some(color.clone())
} else {
// Neither a hex literal nor a palette entry; assume a named CSS
// colour and pass it through verbatim.
warn!("Unresolved colour {color}, passing it through as a literal");
Some(Self(color.into()))
palette.get(color).cloned()
}
}
}
@@ -225,21 +218,6 @@ pub fn init(config: &AppConfig) {
}
}
/// The intersection of scopes between two themes.
pub fn shared_scopes(light: &str, dark: &str) -> Option<BTreeSet<String>> {
let registry = REGISTRY.get()?;
let light = registry.themes.get(light)?;
let dark = registry.themes.get(dark)?;
Some(
light
.styles
.keys()
.filter(|k| dark.styles.contains_key(*k))
.cloned()
.collect(),
)
}
pub fn theme_css_func() -> Func {
theme_css::func()
}
@@ -273,28 +251,13 @@ fn style_declarations(style: &Style) -> String {
}
fn generate_css(scopes: &[&str], theme: &Theme) -> String {
let mut rules = Vec::new();
// The theme's editor foreground/background colour the code block itself.
let mut block_style = Vec::new();
if let Some(Color(hex)) = &theme.foreground {
block_style.push(format!("color:{hex}"));
}
if let Some(Color(hex)) = &theme.background {
block_style.push(format!("background:{hex}"));
}
if !block_style.is_empty() {
rules.push(format!(".code-block{{{}}}", block_style.join(";")));
}
let mut groups: HashMap<&Style, Vec<&str>> = HashMap::new();
for &scope in scopes {
if let Some(style) = theme.styles.get(scope) {
groups.entry(style).or_default().push(scope);
}
}
let mut group_rules: Vec<String> = groups
groups
.iter()
.map(|(style, scopes)| {
let selectors = scopes
@@ -304,12 +267,8 @@ fn generate_css(scopes: &[&str], theme: &Theme) -> String {
.join(",");
format!("{}{{{}}}", selectors, style_declarations(style))
})
.collect();
// Sort so the emitted stylesheet is deterministic across runs.
group_rules.sort();
rules.extend(group_rules);
rules.join("\n")
.collect::<Vec<_>>()
.join("\n")
}
#[func]
@@ -326,8 +285,13 @@ fn theme_css(light: String, dark: String) -> String {
return String::new();
};
let shared = shared_scopes(&light, &dark).unwrap_or_default();
let shared: Vec<&str> = shared.iter().map(String::as_str).collect();
let mut shared: Vec<&str> = light_theme
.styles
.keys()
.filter(|k| dark_theme.styles.contains_key(*k))
.map(String::as_str)
.collect();
shared.sort();
let light_css = generate_css(&shared, light_theme);
let dark_css = generate_css(&shared, dark_theme);