Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat: added initial HTML support

Elijah Potterbdbac8dcd420
diff --git a/Cargo.lock b/Cargo.lock
index d85773956..cea3b0456 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -543,6 +543,16 @@ dependencies = [
"unicode-width",
]
+[[package]]
+name = "harper-html"
+version = "0.8.8"
+dependencies = [
+ "harper-core",
+ "harper-tree-sitter",
+ "tree-sitter",
+ "tree-sitter-html",
+]
+
[[package]]
name = "harper-ls"
version = "0.8.8"
@@ -553,6 +563,7 @@ dependencies = [
"futures",
"harper-comments",
"harper-core",
+ "harper-html",
"itertools 0.13.0",
"once_cell",
"open",
@@ -1437,6 +1448,16 @@ dependencies = [
"tree-sitter",
]
+[[package]]
+name = "tree-sitter-html"
+version = "0.19.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "184e6b77953a354303dc87bf5fe36558c83569ce92606e7b382a0dc1b7443443"
+dependencies = [
+ "cc",
+ "tree-sitter",
+]
+
[[package]]
name = "tree-sitter-java"
version = "0.20.2"
diff --git a/Cargo.toml b/Cargo.toml
index 0490665ff..06c5d0ed4 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,5 +1,5 @@
[workspace]
-members = [ "harper-cli", "harper-core", "harper-ls", "harper-comments", "harper-wasm", "harper-tree-sitter"]
+members = [ "harper-cli", "harper-core", "harper-ls", "harper-comments", "harper-wasm", "harper-tree-sitter", "harper-html"]
resolver = "2"
[profile.release]
diff --git a/harper-comments/README.md b/harper-comments/README.md
index 31376a038..4ac6cac28 100644
--- a/harper-comments/README.md
+++ b/harper-comments/README.md
@@ -2,4 +2,4 @@
This crate holds a number of functions, but it is primarily a wrapper around `tree-sitter` that allows Harper to locate the comments of a wide variety of programming languages.
It also has purpose-built parsers for the structured comments of a number of languages, including Go.
-These additional parsers are avaiable through the `TreeSitterParser` and are enabled automatically through there.
+These additional parsers are available through the `CommentParser` and are enabled automatically through there.
diff --git a/harper-html/Cargo.toml b/harper-html/Cargo.toml
new file mode 100644
index 000000000..3ef901c15
--- /dev/null
+++ b/harper-html/Cargo.toml
@@ -0,0 +1,14 @@
+[package]
+name = "harper-html"
+version = "0.8.8"
+edition = "2021"
+description = "The language checker for developers."
+license = "Apache-2.0"
+readme = "README.md"
+repository = "https://github.com/elijah-potter/harper"
+
+[dependencies]
+harper-core = { path = "../harper-core", version = "0.8.0" }
+harper-tree-sitter = { path = "../harper-tree-sitter", version = "0.8.0" }
+tree-sitter-html = "0.19.0"
+tree-sitter = "0.20.10"
diff --git a/harper-html/src/lib.rs b/harper-html/src/lib.rs
new file mode 100644
index 000000000..47cf61eef
--- /dev/null
+++ b/harper-html/src/lib.rs
@@ -0,0 +1,29 @@
+use harper_core::parsers::{self, Parser, PlainEnglish};
+use harper_core::Token;
+use harper_tree_sitter::TreeSitterMasker;
+use tree_sitter::Node;
+
+pub struct HtmlParser {
+ inner: parsers::Mask<TreeSitterMasker, PlainEnglish>
+}
+
+impl HtmlParser {
+ pub fn new() -> Self {
+ Self {
+ inner: parsers::Mask::new(
+ TreeSitterMasker::new(tree_sitter_html::language(), Self::node_condition),
+ PlainEnglish
+ )
+ }
+ }
+
+ fn node_condition(n: &Node) -> bool {
+ n.kind() == "text"
+ }
+}
+
+impl Parser for HtmlParser {
+ fn parse(&mut self, source: &[char]) -> Vec<Token> {
+ self.inner.parse(source)
+ }
+}
diff --git a/harper-ls/Cargo.toml b/harper-ls/Cargo.toml
index b87d4f4d9..78eb1185d 100644
--- a/harper-ls/Cargo.toml
+++ b/harper-ls/Cargo.toml
@@ -10,6 +10,7 @@ repository = "https://github.com/elijah-potter/harper"
[dependencies]
harper-core = { path = "../harper-core", version = "0.8.0", features = ["concurrent"] }
harper-comments = { path = "../harper-comments", version = "0.8.0" }
+harper-html = { path = "../harper-html", version = "0.8.0" }
tower-lsp = "0.20.0"
tokio = { version = "1.36.0", features = ["fs", "rt", "rt-multi-thread", "macros", "io-std", "io-util", "net"] }
clap = { version = "4.5.1", features = ["derive"] }
diff --git a/harper-ls/src/backend.rs b/harper-ls/src/backend.rs
index c9ee9249d..2e176b438 100644
--- a/harper-ls/src/backend.rs
+++ b/harper-ls/src/backend.rs
@@ -14,6 +14,7 @@ use harper_core::{
Token,
TokenKind
};
+use harper_html::HtmlParser;
use serde_json::Value;
use tokio::sync::{Mutex, RwLock};
use tower_lsp::jsonrpc::Result;
@@ -260,6 +261,8 @@ impl Backend {
Document::new(text, Box::new(Markdown))
} else if language_id == "gitcommit" {
Document::new(text, Box::new(GitCommitParser))
+ } else if language_id == "html" {
+ Document::new(text, Box::new(HtmlParser::new()))
} else if language_id == "mail" {
Document::new(text, Box::new(PlainEnglish))
} else {
@@ -372,7 +375,6 @@ impl LanguageServer for Backend {
save: Some(TextDocumentSyncSaveOptions::Supported(true))
}
)),
-
..Default::default()
}
})