Skip to content

Commit

Permalink
feat: use only tree-sitter (#644)
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats authored Oct 8, 2024
1 parent 4e5275d commit e917e0b
Show file tree
Hide file tree
Showing 27 changed files with 176 additions and 347 deletions.
12 changes: 1 addition & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,11 @@ jobs:
- name: Build (Rust)
run: cargo build --locked --all-targets

- name: Test (no highlighter)
- name: Test
run: |
cargo test --locked --all-targets
deno test --allow-read --allow-net --allow-env --allow-write
- name: Test (syntect)
run: |
cargo test --locked --all-targets --features=syntect
deno test --allow-read --allow-net --allow-env --allow-write
- name: Test (tree-sitter)
run: |
cargo test --locked --all-targets --features=tree-sitter
deno test --allow-read --allow-net --allow-env --allow-write
- name: Publish
if: |
contains(matrix.os, 'ubuntu') &&
Expand Down
81 changes: 2 additions & 79 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 6 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ termcolor = "1.4.1"
html-escape = { version = "0.2.13", optional = true }
comrak = { version = "0.28.0", optional = true, default-features = false }
handlebars = { version = "6.1", optional = true, features = ["string_helpers"] }
syntect = { version = "5.2.0", optional = true, default-features = false, features = [
"parsing",
"default-syntaxes",
"default-themes",
"html",
"dump-load",
"regex-onig",
] }
ammonia = { version = "4.0.0", optional = true }

tree-sitter-highlight = { version = "0.22.6", optional = true }
Expand All @@ -75,10 +67,13 @@ pretty_assertions = "1.4.0"
insta = { version = "1.39.0", features = ["json"] }

[features]
default = ["html", "rust", "ammonia"]
default = ["html", "rust"]
rust = []
html = ["html-escape", "comrak", "handlebars"]
tree-sitter = [
ammonia = ["dep:ammonia"]
html = [
"html-escape",
"comrak",
"handlebars",
"tree-sitter-highlight",
"tree-sitter-javascript",
"tree-sitter-typescript",
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"test": "deno test -A",
"tailwind": "deno run -A build_css.ts",
"gen_html": "deno task tailwind && cargo run --example ddoc --features=html -- --name=Test --html ./tests/testdata/multiple/* --output generated_docs/",
"test:update": "UPDATE=1 cargo test --locked --all-targets && UPDATE=1 cargo test --locked --all-targets --features=tree-sitter && UPDATE=1 cargo test --locked --all-targets --features=syntect && cargo insta test --review"
"test:update": "UPDATE=1 cargo test --locked --all-targets && cargo insta test --accept"
},
"workspace": ["js"],
"exclude": [
Expand Down
88 changes: 4 additions & 84 deletions src/html/comrak_adapters.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// Copied and modified from https://github.com/kivikakk/comrak/blob/main/src/plugins/syntect.rs
#![allow(clippy::print_stderr)]

//! Adapter for the Syntect syntax highlighter plugin.

use crate::html::ShortPath;
use comrak::adapters::HeadingAdapter;
use comrak::adapters::HeadingMeta;
use comrak::adapters::SyntaxHighlighterAdapter;
Expand All @@ -14,13 +11,7 @@ use std::sync::Arc;
use std::sync::Mutex;

#[derive(Debug)]
/// Syntect syntax highlighter plugin.
pub struct HighlightAdapter {
#[cfg(feature = "syntect")]
pub syntax_set: syntect::parsing::SyntaxSet,
#[cfg(feature = "syntect")]
pub theme_set: syntect::highlighting::ThemeSet,
#[cfg(feature = "tree-sitter")]
pub language_cb:
fn(&str) -> Option<&'static tree_sitter_highlight::HighlightConfiguration>,
pub show_line_numbers: bool,
Expand Down Expand Up @@ -85,57 +76,6 @@ impl HighlightAdapter {
}

impl SyntaxHighlighterAdapter for HighlightAdapter {
#[cfg(any(
all(feature = "syntect", not(feature = "tree-sitter")),
all(feature = "syntect", feature = "tree-sitter")
))]
fn write_highlighted(
&self,
output: &mut dyn Write,
lang: Option<&str>,
code: &str,
) -> std::io::Result<()> {
let lang = match lang {
Some(l) if !l.is_empty() => l,
_ => "Plain Text",
};

let syntax =
self
.syntax_set
.find_syntax_by_token(lang)
.unwrap_or_else(|| {
self
.syntax_set
.find_syntax_by_first_line(code)
.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
});

let theme = &self.theme_set.themes["InspiredGitHub"];
let mut highlighter = syntect::easy::HighlightLines::new(syntax, theme);

match self.highlight_html(
syntect::util::LinesWithEndings::from(code),
|lines, line| {
let regions = highlighter.highlight_line(line, &self.syntax_set)?;

syntect::html::append_highlighted_html_for_styled_line(
&regions,
syntect::html::IncludeBackground::No,
lines,
)?;

Ok(())
},
) {
Ok(highlighted_code) => output.write_all(highlighted_code.as_bytes())?,
Err(_) => output.write_all(code.as_bytes())?,
}

self.write_button(output, code)
}

#[cfg(all(feature = "tree-sitter", not(feature = "syntect")))]
fn write_highlighted(
&self,
output: &mut dyn Write,
Expand Down Expand Up @@ -163,7 +103,7 @@ impl SyntaxHighlighterAdapter for HighlightAdapter {
.unwrap();

output.write_all(html.as_bytes())?;
return self.write_button(output, &code);
return self.write_button(output, code);
}
Err(err) => {
eprintln!("Error rendering code: {}", err);
Expand All @@ -176,27 +116,7 @@ impl SyntaxHighlighterAdapter for HighlightAdapter {
}
}
comrak::html::escape(output, source)?;
self.write_button(output, &code)
}

#[cfg(all(not(feature = "syntect"), not(feature = "tree-sitter")))]
fn write_highlighted(
&self,
output: &mut dyn Write,
_lang: Option<&str>,
code: &str,
) -> std::io::Result<()> {
let code = html_escape::encode_text(code);
let html = self
.highlight_html(code.lines(), |lines, line| {
lines.push_str(&format!("{line}\n"));

Ok(())
})
.unwrap();

output.write_all(html.as_bytes())?;
self.write_button(output, &code)
self.write_button(output, code)
}

fn write_pre_tag(
Expand Down Expand Up @@ -384,4 +304,4 @@ impl HeadingAdapter for HeadingToCAdapter {

#[cfg(feature = "ammonia")]
pub type URLRewriter =
Arc<dyn (Fn(Option<&ShortPath>, &str) -> String) + Send + Sync>;
Arc<dyn (Fn(Option<&crate::html::ShortPath>, &str) -> String) + Send + Sync>;
14 changes: 7 additions & 7 deletions src/html/jsdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ lazy_static! {
regex::Regex::new(r"(^\.{0,2}\/)|(^[A-Za-z]+:\S)").unwrap();
static ref MODULE_LINK_RE: regex::Regex =
regex::Regex::new(r"^\[(\S+)\](?:\.(\S+)|\s|)$").unwrap();
}

#[cfg(feature = "ammonia")]
#[cfg(feature = "ammonia")]
lazy_static! {
static ref AMMONIA: ammonia::Builder<'static> = {
let mut ammonia_builder = ammonia::Builder::default();

Expand Down Expand Up @@ -78,19 +80,17 @@ lazy_static! {
],
)
.link_rel(Some("nofollow"))
.url_relative(
ammonia::UrlRelative::Custom(Box::new(AmmoniaRelativeUrlEvaluator())));

#[cfg(feature = "syntect")]
ammonia_builder.add_tag_attributes("span", ["style"]);
.url_relative(ammonia::UrlRelative::Custom(Box::new(
AmmoniaRelativeUrlEvaluator(),
)));

#[cfg(feature = "tree-sitter")]
ammonia_builder.add_allowed_classes("span", super::tree_sitter::CLASSES);

ammonia_builder
};
}

#[cfg(feature = "ammonia")]
thread_local! {
static CURRENT_FILE: RefCell<Option<Option<ShortPath>>> = const { RefCell::new(None) };
static URL_REWRITER: RefCell<Option<Option<URLRewriter>>> = const { RefCell::new(None) };
Expand Down
11 changes: 1 addition & 10 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ pub mod partition;
mod render_context;
mod search;
mod symbols;
#[cfg(feature = "tree-sitter")]
mod tree_sitter;
pub mod tree_sitter;
mod types;
mod usage;
pub mod util;
Expand Down Expand Up @@ -793,14 +792,6 @@ pub fn setup_highlighter(
show_line_numbers: bool,
) -> comrak_adapters::HighlightAdapter {
comrak_adapters::HighlightAdapter {
#[cfg(feature = "syntect")]
syntax_set: syntect::dumps::from_uncompressed_data(include_bytes!(
"./default_newlines.packdump"
))
.unwrap(),
#[cfg(feature = "syntect")]
theme_set: syntect::highlighting::ThemeSet::load_defaults(),
#[cfg(feature = "tree-sitter")]
language_cb: tree_sitter::tree_sitter_language_cb,
show_line_numbers,
}
Expand Down
Loading

0 comments on commit e917e0b

Please sign in to comment.