feat: setup for tree-sitter syntax highlighting
This commit is contained in:
1 parent
853efc406d
commit
d5ecc05345
5 files changed
+233
-8
No files matched your search
@@ -25,6 +25,7 @@ use tracing_subscriber::FmtSubscriber;
|
||||
|
||||
use crate::typst::TypstContext;
|
||||
|
||||
mod syntax;
|
||||
mod typst;
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -59,6 +60,10 @@ struct AppConfig {
|
||||
#[arg(short = 'd', long, value_name = "DIR", default_value = "/tmp/karnaugh")]
|
||||
packages_root: PathBuf,
|
||||
|
||||
/// The directory where the syntax files are stored.
|
||||
#[arg(short = 'g', long, value_name = "DIR", default_value = None)]
|
||||
syntax_root: Option<PathBuf>,
|
||||
|
||||
/// The log level to use
|
||||
#[arg(short = 'l', long, value_name = "LEVEL", default_value_t = Level::INFO)]
|
||||
log_level: Level,
|
||||
@@ -94,6 +99,8 @@ async fn main() {
|
||||
.with_max_level(state.config.log_level)
|
||||
.init();
|
||||
|
||||
syntax::init(&state.config);
|
||||
|
||||
// TODO: Replace with config option
|
||||
let mut favicon = state.config.get_full_assets_path();
|
||||
favicon.push(PathBuf::from("favicon.svg"));
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
use std::{path::PathBuf, sync::OnceLock};
|
||||
|
||||
use tracing::{info, warn};
|
||||
use typst::foundations::{Func, NativeFunc, func};
|
||||
|
||||
use crate::AppConfig;
|
||||
|
||||
static REGISTRY: OnceLock<LanguageRegistry> = OnceLock::new();
|
||||
|
||||
struct LanguageRegistry {}
|
||||
|
||||
impl LanguageRegistry {
|
||||
fn new(syntax_root: &PathBuf) -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(config: &AppConfig) {
|
||||
match &config.syntax_root {
|
||||
Some(syntax_root) => {
|
||||
info!("Loading syntax files");
|
||||
REGISTRY.get_or_init(|| LanguageRegistry::new(syntax_root));
|
||||
}
|
||||
None => {
|
||||
warn!(
|
||||
"No syntax root has been configured, improved tree-sitter highlighting will not be possible."
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn highlight_func() -> Func {
|
||||
highlight::func()
|
||||
}
|
||||
|
||||
#[func]
|
||||
fn highlight(
|
||||
text: String,
|
||||
#[named]
|
||||
#[default(false)]
|
||||
block: bool,
|
||||
#[named]
|
||||
#[default(None)]
|
||||
lang: Option<String>,
|
||||
) -> String {
|
||||
let rev = text.chars().rev();
|
||||
rev.collect()
|
||||
}
|
||||
+9
-1
@@ -5,7 +5,7 @@ use tracing::info;
|
||||
use typst::{
|
||||
Library, LibraryExt, World,
|
||||
diag::{FileError, FileResult, PackageError, PackageResult, SourceDiagnostic},
|
||||
foundations::{Bytes, Datetime, Duration},
|
||||
foundations::{Bytes, Datetime, Dict, Duration, Value},
|
||||
syntax::{FileId, RootedPath, Source, VirtualPath, VirtualRoot, package::PackageSpec},
|
||||
text::{Font, FontBook},
|
||||
utils::LazyHash,
|
||||
@@ -23,7 +23,15 @@ pub struct TypstContext {
|
||||
|
||||
impl TypstContext {
|
||||
pub fn new(config: AppConfig) -> Self {
|
||||
let mut inputs = Dict::new();
|
||||
if config.syntax_root.is_some() {
|
||||
inputs.insert(
|
||||
"highlight".into(),
|
||||
Value::Func(crate::syntax::highlight_func()),
|
||||
);
|
||||
}
|
||||
let library = Library::builder()
|
||||
.with_inputs(inputs)
|
||||
.with_features([typst::Feature::Html].into_iter().collect())
|
||||
.build();
|
||||
let library = LazyHash::new(library);
|
||||
|
||||
Reference in new issue
Block a user