Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: added `gitcommit` support to `harper-ls`
Elijah Potteraaf0e1161d8c
diff --git a/README.md b/README.mdindex 41d9c9952..05beb1408 100644--- a/README.md+++ b/README.md@@ -29,8 +29,7 @@ Harper is even small enough to load via [WebAssembly.](https://writewithharper.c## Installation-If you want to use Harper on your machine, you will want to look at the [documentation for-`harper-ls`](./harper-ls/README.md), the Language Server Protocol implementation.+If you want to use Harper on your machine, you will want to look at the [documentation for `harper-ls`](./harper-ls/README.md), the Language Server Protocol implementation.## Performance Issuesdiff --git a/harper-core/src/parsers/markdown.rs b/harper-core/src/parsers/markdown.rsindex 186be0d3c..dafdcaa4b 100644--- a/harper-core/src/parsers/markdown.rs+++ b/harper-core/src/parsers/markdown.rs@@ -8,6 +8,8 @@ use crate::{Span, Token, TokenKind};pub struct Markdown;impl Parser for Markdown {+ /// This implementation is quite gross to look at, but it works.+ /// If any issues arise, it would likely help to refactor this out first.fn parse(&mut self, source: &[char]) -> Vec<Token> {let mut english_parser = PlainEnglish;diff --git a/harper-ls/src/backend.rs b/harper-ls/src/backend.rsindex 19070a0a0..df0162aa6 100644--- a/harper-ls/src/backend.rs+++ b/harper-ls/src/backend.rs@@ -50,6 +50,7 @@ use tracing::{error, info, instrument};use crate::config::Config;use crate::diagnostics::{lint_to_code_actions, lints_to_diagnostics};use crate::dictionary_io::{load_dict, save_dict};+use crate::git_commit_parser::GitCommitParser;use crate::pos_conv::range_to_span;use crate::tree_sitter_parser::TreeSitterParser;@@ -256,6 +257,8 @@ impl Backend {Document::new_from_vec(source, Box::new(ts_parser))} else if language_id == "markdown" {Document::new(text, Box::new(Markdown))+ } else if language_id == "gitcommit" {+ Document::new(text, Box::new(GitCommitParser))} else {doc_lock.remove(url);return Ok(());@@ -381,9 +384,7 @@ impl LanguageServer for Backend {}async fn did_save(&self, params: DidSaveTextDocumentParams) {- self.client- .log_message(MessageType::INFO, "File saved!")- .await;+ info!("File saved");let _ = self.update_document_from_file(¶ms.text_document.uri, None)@@ -393,13 +394,12 @@ impl LanguageServer for Backend {}async fn did_open(&self, params: DidOpenTextDocumentParams) {- self.client- .log_message(MessageType::INFO, "File opened!")- .await;+ info!("File opened");let _ = self- .update_document_from_file(+ .update_document(¶ms.text_document.uri,+ ¶ms.text_document.text,Some(¶ms.text_document.language_id)).await;@@ -412,9 +412,7 @@ impl LanguageServer for Backend {return;};- self.client- .log_message(MessageType::INFO, "File changed!")- .await;+ info!("File changed");self.update_document(¶ms.text_document.uri, &last.text, None).await@@ -423,9 +421,7 @@ impl LanguageServer for Backend {}async fn did_close(&self, _params: DidCloseTextDocumentParams) {- self.client- .log_message(MessageType::INFO, "File closed!")- .await;+ info!("File closed");}async fn execute_command(&self, params: ExecuteCommandParams) -> Result<Option<Value>> {diff --git a/harper-ls/src/git_commit_parser.rs b/harper-ls/src/git_commit_parser.rsnew file mode 100644index 000000000..2b2f7d06d--- /dev/null+++ b/harper-ls/src/git_commit_parser.rs@@ -0,0 +1,20 @@+use harper_core::parsers::{Markdown, Parser};++/// A Harper parser for Git commit files+pub struct GitCommitParser;++impl Parser for GitCommitParser {+ /// Admittedly a somewhat naive implementation.+ /// We're going to get _something_ to work, before we polish it off.+ fn parse(&mut self, source: &[char]) -> Vec<harper_core::Token> {+ // Locate the first `#`+ let end = source+ .iter()+ .position(|c| *c == '#')+ .unwrap_or(source.len());++ let mut md_parser = Markdown;++ md_parser.parse(&source[0..end])+ }+}diff --git a/harper-ls/src/main.rs b/harper-ls/src/main.rsindex 7a8d8f39c..a5ef1b826 100644--- a/harper-ls/src/main.rs+++ b/harper-ls/src/main.rs@@ -7,6 +7,7 @@ mod comment_parsers;mod config;mod diagnostics;mod dictionary_io;+mod git_commit_parser;mod pos_conv;mod tree_sitter_parser;