From aaf0e1161d8c834070c6827e5511799d76525cb3 Mon Sep 17 00:00:00 2001 From: Elijah Potter Date: Wed, 15 May 2024 10:07:42 -0600 Subject: [PATCH] feat: added `gitcommit` support to `harper-ls` --- README.md | 3 +-- harper-core/src/parsers/markdown.rs | 2 ++ harper-ls/src/backend.rs | 22 +++++++++------------- harper-ls/src/git_commit_parser.rs | 20 ++++++++++++++++++++ harper-ls/src/main.rs | 1 + 5 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 harper-ls/src/git_commit_parser.rs diff --git a/README.md b/README.md index 41d9c995..05beb140 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 Issues diff --git a/harper-core/src/parsers/markdown.rs b/harper-core/src/parsers/markdown.rs index 186be0d3..dafdcaa4 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 { let mut english_parser = PlainEnglish; diff --git a/harper-ls/src/backend.rs b/harper-ls/src/backend.rs index 19070a0a..df0162aa 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> { diff --git a/harper-ls/src/git_commit_parser.rs b/harper-ls/src/git_commit_parser.rs new file mode 100644 index 00000000..2b2f7d06 --- /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 { + // 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.rs index 7a8d8f39..a5ef1b82 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;