Add support for highlighting python based on the nix highlighter.

This commit is contained in:
Tom Alexander 2025-02-22 17:56:56 -05:00
parent 749f6d7a55
commit 04952895cf
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
5 changed files with 122 additions and 18 deletions

11
Cargo.lock generated
View File

@ -465,6 +465,7 @@ dependencies = [
"toml",
"tree-sitter-highlight",
"tree-sitter-nix",
"tree-sitter-python",
"url",
]
@ -817,6 +818,16 @@ dependencies = [
"tree-sitter-language",
]
[[package]]
name = "tree-sitter-python"
version = "0.23.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d065aaa27f3aaceaf60c1f0e0ac09e1cb9eb8ed28e7bcdaa52129cffc7f4b04"
dependencies = [
"cc",
"tree-sitter-language",
]
[[package]]
name = "unicode-bidi"
version = "0.3.17"

View File

@ -33,6 +33,7 @@ tokio = { version = "1.30.0", default-features = false, features = ["rt", "rt-mu
toml = "0.8.2"
tree-sitter-highlight = "0.25.2"
tree-sitter-nix = "0.0.2"
tree-sitter-python = "0.23.6"
url = "2.5.0"
# Optimized build for any sort of release.

View File

@ -0,0 +1,39 @@
:root {
--srclg-python-srchl-keyword-color: #1a936f;
--srclg-python-srchl-comment-color: #048a81;
--srclg-python-srchl-property-color: #bfbccb;
--srclg-python-srchl-string-color: #ecc30b;
--srclg-python-srchl-string-special-path-color: #067bc2;
}
@media (prefers-color-scheme: light) {
:root {
--srclg-python-srchl-keyword-color: #e56c90;
--srclg-python-srchl-comment-color: #fb757e;
--srclg-python-srchl-property-color: #404334;
--srclg-python-srchl-string-color: #133cf4;
--srclg-python-srchl-string-special-path-color: #f9843d;
}
}
.main_content {
.src_block {
&.srclg_python {
.srchl_keyword {
color: var(--srclg-python-srchl-keyword-color);
}
.srchl_comment {
color: var(--srclg-python-srchl-comment-color);
}
.srchl_property {
color: var(--srclg-python-srchl-property-color);
}
.srchl_string {
color: var(--srclg-python-srchl-string-color);
}
.srchl_string_special_path {
color: var(--srclg-python-srchl-string-special-path-color);
}
}
}
}

View File

@ -63,6 +63,13 @@ render!(RenderSrcBlock, ISrcBlock, original, render_context, {
.unwrap()
.include_css("language_nix.css")?;
}
Some("python") => {
render_context
.dependency_manager
.lock()
.unwrap()
.include_css("language_python.css")?;
}
_ => {}
};
Ok(RenderSrcBlock {

View File

@ -80,7 +80,7 @@ intermediate!(
let language = original.language.map(str::to_owned);
match language.as_ref().map(String::as_str) {
Some("nix") => {
Some(lang @ "nix") => {
let highlighted = highlight_nix(&lines);
if let Ok(highlighted) = highlighted {
return Ok(ISrcBlock {
@ -88,8 +88,25 @@ intermediate!(
language,
post_blank: original.get_post_blank(),
});
} else {
println!("Warning: Failed to highlight {} source.", lang);
}
}
Some(lang @ "python") => {
let highlighted = highlight_python(&lines);
if let Ok(highlighted) = highlighted {
return Ok(ISrcBlock {
lines: highlighted,
language,
post_blank: original.get_post_blank(),
});
} else {
println!("Warning: Failed to highlight {} source.", lang);
}
}
Some(lang) => {
println!("Warning: No highlighting for language: {}", lang);
}
_ => {}
};
let highlighted = highlight_plain(&lines)?;
@ -133,27 +150,18 @@ where
.collect())
}
fn highlight_nix<L>(lines: &[L]) -> Result<Vec<ISrcLine>, CustomError>
fn highlight_tree_sitter<L>(
config: HighlightConfiguration,
highlight_names: &[&str],
lines: &[L],
) -> Result<Vec<ISrcLine>, CustomError>
where
L: Borrow<str>,
{
let highlight_names = [
"comment",
"keyword",
"property",
"string",
"string.special.path",
// "string.special.uri",
];
let combined_text = lines.join("");
// Need 1 highlighter per thread
let mut highlighter = Highlighter::new();
let language = tree_sitter_nix::LANGUAGE.into();
let mut config =
HighlightConfiguration::new(language, "nix", tree_sitter_nix::HIGHLIGHTS_QUERY, "", "")
.unwrap();
config.configure(&highlight_names);
let combined_text = lines.join("");
let highlights = highlighter
.highlight(&config, combined_text.as_bytes(), None, |_| None)
@ -182,7 +190,7 @@ where
}
HighlightEvent::HighlightStart(s) => {
current_line.children.push(ISrcSegment::HighlightStart {
name: highlight_names[s.0].to_owned(),
name: highlight_names[s.0].into(),
});
}
HighlightEvent::HighlightEnd => {
@ -194,6 +202,44 @@ where
Ok(highlighted_text)
}
fn highlight_nix<L>(lines: &[L]) -> Result<Vec<ISrcLine>, CustomError>
where
L: Borrow<str>,
{
let highlight_names = [
"comment",
"keyword",
"property",
"string",
"string.special.path",
// "string.special.uri",
];
let language = tree_sitter_nix::LANGUAGE.into();
let mut config =
HighlightConfiguration::new(language, "nix", tree_sitter_nix::HIGHLIGHTS_QUERY, "", "")
.unwrap();
config.configure(&highlight_names);
highlight_tree_sitter(config, &highlight_names, lines)
}
fn highlight_python<L>(lines: &[L]) -> Result<Vec<ISrcLine>, CustomError>
where
L: Borrow<str>,
{
let highlight_names = ["comment"];
let language = tree_sitter_python::LANGUAGE.into();
let mut config = HighlightConfiguration::new(
language,
"python",
tree_sitter_python::HIGHLIGHTS_QUERY,
"",
"",
)
.unwrap();
config.configure(&highlight_names);
highlight_tree_sitter(config, &highlight_names, lines)
}
// use tree_sitter::Parser;
// fn dump_nix<B>(body: B) -> Result<(), CustomError>
// where