Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(gitcommit): use tree-sitter for gitcommit (#3681)
Crysthamusb67d32c44c1b
diff --git a/Cargo.lock b/Cargo.lockindex 615bc0c33..6f35afa71 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -4292,6 +4292,17 @@ dependencies = ["tokio",]+[[package]]+name = "harper-git-commit"+version = "2.5.0"+dependencies = [+ "harper-core",+ "harper-tree-sitter",+ "paste",+ "tree-sitter",+ "tree-sitter-gitcommit",+]+[[package]]name = "harper-html"version = "2.5.0"@@ -4349,6 +4360,7 @@ dependencies = ["harper-comments","harper-core","harper-dictionary-wordlist",+ "harper-git-commit","harper-html","harper-ink","harper-jjdescription",@@ -9642,6 +9654,16 @@ dependencies = ["tree-sitter-language",]+[[package]]+name = "tree-sitter-gitcommit"+version = "0.5.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "38b051221607cfde94e2d0132ad99ef50a9f78a990c068380717ea0a4ce61899"+dependencies = [+ "cc",+ "tree-sitter-language",+]+[[package]]name = "tree-sitter-go"version = "0.25.0"diff --git a/Cargo.toml b/Cargo.tomlindex a994ab908..20b76f386 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", "harper-html", "harper-literate-haskell", "harper-typst", "harper-stats", "harper-pos-utils", "harper-brill", "harper-ink", "harper-python", "harper-jjdescription", "harper-thesaurus", "harper-asciidoc", "fuzz", "harper-tex", "harper-desktop/src-tauri"]+members = ["harper-cli", "harper-core", "harper-ls", "harper-comments", "harper-wasm", "harper-tree-sitter", "harper-html", "harper-literate-haskell", "harper-typst", "harper-stats", "harper-pos-utils", "harper-brill", "harper-ink", "harper-python", "harper-jjdescription", "harper-thesaurus", "harper-asciidoc", "fuzz", "harper-tex", "harper-desktop/src-tauri", "harper-git-commit/"]resolver = "2"[profile.test]diff --git a/harper-git-commit/Cargo.toml b/harper-git-commit/Cargo.tomlnew file mode 100644index 000000000..49b5fcbec--- /dev/null+++ b/harper-git-commit/Cargo.toml@@ -0,0 +1,16 @@+[package]+name = "harper-git-commit"+version = "2.5.0"+edition = "2024"+description = "The language checker for developers."+license = "Apache-2.0"+repository = "https://github.com/automattic/harper"++[dependencies]+harper-core = { path = "../harper-core", version = "2.0.0" }+harper-tree-sitter = { path = "../harper-tree-sitter", version = "2.0.0" }+tree-sitter-gitcommit = "0.5.0"+tree-sitter = "0.25.10"++[dev-dependencies]+paste = "1.0.15"diff --git a/harper-git-commit/src/lib.rs b/harper-git-commit/src/lib.rsnew file mode 100644index 000000000..d39362da5--- /dev/null+++ b/harper-git-commit/src/lib.rs@@ -0,0 +1,43 @@+use harper_core::parsers::{self, Markdown, MarkdownOptions, Parser};+use harper_core::{Token, TokenKind};+use harper_tree_sitter::TreeSitterMasker;+use tree_sitter::Node;++pub struct GitCommitParser {+ inner: parsers::Mask<TreeSitterMasker, Markdown>,+}++impl GitCommitParser {+ fn node_condition(n: &Node) -> bool {+ matches!(n.kind(), "subject" | "message_line" | "breaking_change")+ }++ pub fn new(markdown_options: MarkdownOptions) -> Self {+ Self {+ inner: parsers::Mask::new(+ TreeSitterMasker::new(tree_sitter_gitcommit::LANGUAGE.into(), Self::node_condition),+ Markdown::new(markdown_options),+ ),+ }+ }+}++impl Default for GitCommitParser {+ fn default() -> Self {+ Self::new(MarkdownOptions::default())+ }+}++impl Parser for GitCommitParser {+ fn parse(&self, source: &[char]) -> Vec<Token> {+ let mut tokens = self.inner.parse(source);++ for token in &mut tokens {+ if let TokenKind::Space(v) = &mut token.kind {+ *v = (*v).clamp(0, 1);+ }+ }++ tokens+ }+}diff --git a/harper-git-commit/tests/run_tests.rs b/harper-git-commit/tests/run_tests.rsnew file mode 100644index 000000000..a0b0bf3cd--- /dev/null+++ b/harper-git-commit/tests/run_tests.rs@@ -0,0 +1,40 @@+use harper_core::linting::{LintGroup, Linter};+use harper_core::spell::FstDictionary;+use harper_core::{Dialect, Document};+use harper_git_commit::GitCommitParser;++/// Creates a unit test checking that the linting of a git commit document (in+/// `tests_sources`) produces the expected number of lints.+macro_rules! create_test {+ ($filename:ident.txt, $correct_expected:expr) => {+ paste::paste! {+ #[test]+ fn [<lints_ $filename _correctly>]() {+ let source = include_str!(+ concat!(+ "./test_sources/",+ concat!(stringify!($filename), ".txt")+ )+ );++ let dict = FstDictionary::curated();+ let document = Document::new(source, &GitCommitParser::default(), &dict);++ let mut linter = LintGroup::new_curated(dict, Dialect::American);+ let lints = linter.lint(&document);++ dbg!(&lints);+ assert_eq!(lints.len(), $correct_expected);++ for token in document.tokens() {+ assert!(token.span.try_get_content(document.get_source()).is_some());+ }+ }+ }+ };+}++create_test!(simple_commit.txt, 2);+create_test!(complex_verbose_commit.txt, 2);+create_test!(conventional_commit.txt, 3);+create_test!(markdown_body_commit.txt, 3);diff --git a/harper-git-commit/tests/test_sources/complex_verbose_commit.txt b/harper-git-commit/tests/test_sources/complex_verbose_commit.txtnew file mode 100644index 000000000..cb369672b--- /dev/null+++ b/harper-git-commit/tests/test_sources/complex_verbose_commit.txt@@ -0,0 +1,26 @@+This is the the subject++This is a first line without typos+# This is a comment with a typooo that should be ignored+This is a line below the comment with typooos++# Please enter the commit message for your changes. Lines starting+# with '#' will be ignored, and an empty message aborts the commit.+#+# On branch main+#+# Initial commit+#+# Changes to be committed:+#new file: myfile.txt+#+# ------------------------ >8 ------------------------+# Do not modify or remove the line above.+# Everything below it will be ignored.+diff --git a/myfile.txt b/myfile.txt+new file mode 100644+index 0000000..485c415+--- /dev/null++++ b/myfile.txt+@@ -0,0 +1 @@++some textt in the file that was added (the typo should be ignored)diff --git a/harper-git-commit/tests/test_sources/conventional_commit.txt b/harper-git-commit/tests/test_sources/conventional_commit.txtnew file mode 100644index 000000000..1f16b0b69--- /dev/null+++ b/harper-git-commit/tests/test_sources/conventional_commit.txt@@ -0,0 +1,17 @@+feat(stuff): use session-based authentiation++BREAKING CHANGE: JWT authentication removed. API clients mustt now use+session cookies instead of Authorization headers with bearer tokens.++Sessions expire after 24 hours of inactvity.++Closes: #247+Reviewed-by: John Doe <[email protected]>++# Please enter the commit message for your changes. Lines starting+# with '#' will be ignored, and an empty message aborts the commit.+#+# On branch main+# Changes to be committed:+# modified: myfile.txt+#diff --git a/harper-git-commit/tests/test_sources/markdown_body_commit.txt b/harper-git-commit/tests/test_sources/markdown_body_commit.txtnew file mode 100644index 000000000..92b71e2f2--- /dev/null+++ b/harper-git-commit/tests/test_sources/markdown_body_commit.txt@@ -0,0 +1,15 @@+tests: add markhdown test++The spell chekker works great.++```rust+fn main() {+ let str = "intentionaly bad string";+}+```++- First bullet poont+- Second bullet point++# Please enter the commit message for your changes. Lines starting+# with '#' will be ignored, and an empty message aborts the commit.diff --git a/harper-git-commit/tests/test_sources/simple_commit.txt b/harper-git-commit/tests/test_sources/simple_commit.txtnew file mode 100644index 000000000..6dd650830--- /dev/null+++ b/harper-git-commit/tests/test_sources/simple_commit.txt@@ -0,0 +1 @@+A simple commit with a typo: tehdiff --git a/harper-ls/Cargo.toml b/harper-ls/Cargo.tomlindex 304281cbf..8318771cb 100644--- a/harper-ls/Cargo.toml+++ b/harper-ls/Cargo.toml@@ -13,6 +13,7 @@ harper-dictionary-wordlist = { path = "../harper-dictionary-wordlist", version =harper-literate-haskell = { path = "../harper-literate-haskell", version = "2.0.0" }harper-core = { path = "../harper-core", version = "2.0.0", features = ["concurrent"] }harper-comments = { path = "../harper-comments", version = "2.0.0" }+harper-git-commit = { path = "../harper-git-commit", version = "2.0.0" }harper-jjdescription = { path = "../harper-jjdescription", version = "2.0.0" }harper-typst = { path = "../harper-typst", version = "2.0.0" }harper-tex = { path = "../harper-tex", version = "2.0.0" }diff --git a/harper-ls/src/backend.rs b/harper-ls/src/backend.rsindex c71d67ee0..33c86511c 100644--- a/harper-ls/src/backend.rs+++ b/harper-ls/src/backend.rs@@ -6,7 +6,6 @@ use std::sync::Arc;use crate::config::Config;use crate::document_state::DocumentState;-use crate::git_commit_parser::GitCommitParser;use crate::ignored_lints_io::{load_ignored_lints, save_ignored_lints};use crate::io_utils::fileify_path;use anyhow::{Context, Result, anyhow};@@ -20,6 +19,7 @@ use harper_core::parsers::{use harper_core::spell::{Dictionary, FstDictionary, MergedDictionary, MutableDictionary};use harper_core::{Dialect, DictWordMetadata, Document, IgnoredLints};use harper_dictionary_wordlist::{load_dict, save_dict};+use harper_git_commit::GitCommitParser;use harper_html::HtmlParser;use harper_ink::InkParser;use harper_jjdescription::JJDescriptionParser;@@ -361,9 +361,7 @@ impl Backend {Some(Box::new(ts_parser))}}- "git-commit" | "gitcommit" | "octo" => {- Some(Box::new(GitCommitParser::new_markdown(markdown_options)))- }+ "git-commit" | "gitcommit" | "octo" => Some(Box::new(GitCommitParser::default())),"html" => Some(Box::new(HtmlParser::default())),"asciidoc" => Some(Box::new(AsciidocParser::default())),"ink" => Some(Box::new(InkParser::default())),diff --git a/harper-ls/src/git_commit_parser.rs b/harper-ls/src/git_commit_parser.rsdeleted file mode 100644index 4c1bdb40e..000000000--- a/harper-ls/src/git_commit_parser.rs+++ /dev/null@@ -1,32 +0,0 @@-use harper_core::Lrc;-use harper_core::parsers::{Markdown, MarkdownOptions, Parser};--/// A Harper parser for Git commit files-#[derive(Clone)]-pub struct GitCommitParser {- inner: Lrc<dyn Parser>,-}--impl GitCommitParser {- pub fn new(parser: Lrc<dyn Parser>) -> Self {- Self { inner: parser }- }-- pub fn new_markdown(markdown_options: MarkdownOptions) -> Self {- Self::new(Lrc::new(Markdown::new(markdown_options)))- }-}--impl Parser for GitCommitParser {- /// Admittedly a somewhat naive implementation.- /// We're going to get _something_ to work, before we polish it off.- fn parse(&self, source: &[char]) -> Vec<harper_core::Token> {- // Locate the first `#`- let end = source- .iter()- .position(|c| *c == '#')- .unwrap_or(source.len());-- self.inner.parse(&source[0..end])- }-}diff --git a/harper-ls/src/main.rs b/harper-ls/src/main.rsindex 0bd18640a..80a00baff 100644--- a/harper-ls/src/main.rs+++ b/harper-ls/src/main.rs@@ -8,7 +8,7 @@ mod backend;mod config;mod diagnostics;mod document_state;-mod git_commit_parser;+mod ignored_lints_io;mod io_utils;mod pos_conv;