Skip to content

Commit

Permalink
feat: added gitcommit support to harper-ls
Browse files Browse the repository at this point in the history
  • Loading branch information
elijah-potter committed May 15, 2024
1 parent 09a9180 commit aaf0e11
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions harper-core/src/parsers/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
22 changes: 9 additions & 13 deletions harper-ls/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(());
Expand Down Expand Up @@ -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(&params.text_document.uri, None)
Expand All @@ -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(
&params.text_document.uri,
&params.text_document.text,
Some(&params.text_document.language_id)
)
.await;
Expand All @@ -412,9 +412,7 @@ impl LanguageServer for Backend {
return;
};

self.client
.log_message(MessageType::INFO, "File changed!")
.await;
info!("File changed");

self.update_document(&params.text_document.uri, &last.text, None)
.await
Expand All @@ -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>> {
Expand Down
20 changes: 20 additions & 0 deletions harper-ls/src/git_commit_parser.rs
Original file line number Diff line number Diff line change
@@ -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])
}
}
1 change: 1 addition & 0 deletions harper-ls/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit aaf0e11

Please sign in to comment.