Skip to content

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

Added `clap` and fixed off-by-one error

Elijah Potter042b26bfbd5c
diff --git a/Cargo.lock b/Cargo.lock
index 82a3e8ca8..8588629cf 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -51,12 +51,54 @@ version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
+[[package]]
+name = "anstream"
+version = "0.6.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3fde6067df7359f2d6335ec1a50c1f8f825801687d10da0cc4c6b08e3f6afd15"
+dependencies = [
+ "anstyle",
+ "anstyle-parse",
+ "anstyle-query",
+ "anstyle-wincon",
+ "colorchoice",
+ "utf8parse",
+]
+
[[package]]
name = "anstyle"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87"
+[[package]]
+name = "anstyle-parse"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c"
+dependencies = [
+ "utf8parse",
+]
+
+[[package]]
+name = "anstyle-query"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648"
+dependencies = [
+ "windows-sys 0.52.0",
+]
+
+[[package]]
+name = "anstyle-wincon"
+version = "3.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7"
+dependencies = [
+ "anstyle",
+ "windows-sys 0.52.0",
+]
+
[[package]]
name = "anyhow"
version = "1.0.79"
@@ -218,21 +260,36 @@ dependencies = [
[[package]]
name = "clap"
-version = "4.4.16"
+version = "4.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "58e54881c004cec7895b0068a0a954cd5d62da01aef83fa35b1e594497bf5445"
+checksum = "1e578d6ec4194633722ccf9544794b71b1385c3c027efe0c55db226fc880865c"
dependencies = [
"clap_builder",
+ "clap_derive",
]
[[package]]
name = "clap_builder"
-version = "4.4.16"
+version = "4.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "59cb82d7f531603d2fd1f507441cdd35184fa81beff7bd489570de7f773460bb"
+checksum = "4df4df40ec50c46000231c914968278b1eb05098cf8f1b3a518a95030e71d1c7"
dependencies = [
+ "anstream",
"anstyle",
"clap_lex",
+ "strsim",
+]
+
+[[package]]
+name = "clap_derive"
+version = "4.4.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442"
+dependencies = [
+ "heck",
+ "proc-macro2",
+ "quote",
+ "syn",
]
[[package]]
@@ -241,6 +298,12 @@ version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1"
+[[package]]
+name = "colorchoice"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
+
[[package]]
name = "console_error_panic_hook"
version = "0.1.7"
@@ -438,6 +501,7 @@ name = "harper-ls"
version = "0.1.0"
dependencies = [
"anyhow",
+ "clap",
"ctrlc",
"harper-core",
"lsp-server",
@@ -475,6 +539,12 @@ dependencies = [
"wasm-bindgen",
]
+[[package]]
+name = "heck"
+version = "0.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
+
[[package]]
name = "hermit-abi"
version = "0.3.3"
@@ -1088,6 +1158,12 @@ dependencies = [
"windows-sys 0.48.0",
]
+[[package]]
+name = "strsim"
+version = "0.10.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
+
[[package]]
name = "syn"
version = "2.0.48"
@@ -1306,6 +1382,12 @@ dependencies = [
"serde",
]
+[[package]]
+name = "utf8parse"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
+
[[package]]
name = "valuable"
version = "0.1.0"
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rs
index 5b5be8588..d83cd6f79 100644
--- a/harper-core/src/document.rs
+++ b/harper-core/src/document.rs
@@ -23,11 +23,18 @@ impl Document {
let tokens = lex_to_end(&source);
let mut doc = Self { source, tokens };
- doc.match_quotes();
+ doc.parse();
doc
}
+ /// Re-parse important language constructs.
+ ///
+ /// Should be run after every change to the underlying [`Self::source`].
+ fn parse(&mut self) {
+ self.match_quotes();
+ }
+
pub fn iter_quote_indices(&self) -> impl Iterator<Item = usize> + '_ {
self.tokens.iter().enumerate().filter_map(|(idx, token)| {
if let TokenKind::Punctuation(Punctuation::Quote(_)) = &token.kind {
@@ -145,6 +152,8 @@ impl Document {
}
}
}
+
+ self.parse();
}
}
diff --git a/harper-ls/Cargo.toml b/harper-ls/Cargo.toml
index 20c9b8ac4..a17366663 100644
--- a/harper-ls/Cargo.toml
+++ b/harper-ls/Cargo.toml
@@ -13,3 +13,4 @@ serde_json = "1.0.111"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
harper-core = { path = "../harper-core" }
+clap = { version = "4.4.18", features = ["derive"] }
diff --git a/harper-ls/src/generate_diagnostics.rs b/harper-ls/src/generate_diagnostics.rs
index 118d109bb..05e6f490d 100644
--- a/harper-ls/src/generate_diagnostics.rs
+++ b/harper-ls/src/generate_diagnostics.rs
@@ -2,7 +2,7 @@ use harper_core::{all_linters, Dictionary, Document, Lint, Span};
use lsp_types::{Diagnostic, Position, Range, Url};
use std::fs::read;
-pub fn generate_diagnostics(file_uri: Url) -> anyhow::Result<Vec<Diagnostic>> {
+pub fn generate_diagnostics(file_uri: &Url) -> anyhow::Result<Vec<Diagnostic>> {
let file = read(file_uri.path())?;
let file_str = String::from_utf8(file)?;
@@ -52,7 +52,7 @@ fn index_to_position(source: &[char], index: usize) -> Position {
.collect();
let lines = newline_indices.len();
- let cols = index - newline_indices.last().copied().unwrap_or(0);
+ let cols = index - newline_indices.last().copied().unwrap_or(1) - 1;
Position {
line: lines as u32,
diff --git a/harper-ls/src/main.rs b/harper-ls/src/main.rs
index a8b46c3c4..09f1d639b 100644
--- a/harper-ls/src/main.rs
+++ b/harper-ls/src/main.rs
@@ -1,19 +1,32 @@
mod generate_diagnostics;
mod server;
-
+use clap::Parser;
use lsp_server::Connection;
use tracing::Level;
use crate::server::Server;
+#[derive(Debug, Parser)]
+struct Args {
+ #[arg(short, long, default_value_t = false)]
+ stdio: bool,
+}
+
fn main() -> anyhow::Result<()> {
- let subscriber = tracing_subscriber::FmtSubscriber::builder()
- .with_max_level(Level::DEBUG)
- .finish();
+ let args = Args::parse();
+
+ let (connection, io_threads) = if !args.stdio {
+ let subscriber = tracing_subscriber::FmtSubscriber::builder()
+ .with_max_level(Level::DEBUG)
+ .finish();
+
+ tracing::subscriber::set_global_default(subscriber)?;
- tracing::subscriber::set_global_default(subscriber)?;
+ Connection::listen("127.0.0.1:4000")?
+ } else {
+ Connection::stdio()
+ };
- let (connection, io_threads) = Connection::stdio();
let mut server = Server::new(connection, io_threads)?;
server.main_loop()?;
server.join()?;
diff --git a/harper-ls/src/server.rs b/harper-ls/src/server.rs
index d3a7283e1..02a84818a 100644
--- a/harper-ls/src/server.rs
+++ b/harper-ls/src/server.rs
@@ -1,15 +1,15 @@
-use anyhow::Ok;
use lsp_server::{
Connection, ExtractError, IoThreads, Message, Notification, Request, RequestId, Response,
};
use lsp_types::{
notification::{
- DidOpenTextDocument, DidSaveTextDocument, Notification as NotificationTrait,
- PublishDiagnostics,
+ DidChangeTextDocument, DidOpenTextDocument, DidSaveTextDocument,
+ Notification as NotificationTrait, PublishDiagnostics,
},
request::GotoDefinition,
- Diagnostic, DiagnosticOptions, GotoDefinitionResponse, InitializedParams, Location, Position,
- PublishDiagnosticsParams, Range, ServerCapabilities,
+ CodeActionProviderCapability, Diagnostic, DiagnosticOptions, GotoDefinitionResponse,
+ InitializedParams, Location, Position, PublishDiagnosticsParams, Range, ServerCapabilities,
+ Url,
};
use tracing::{error, info};
@@ -30,6 +30,7 @@ impl Server {
diagnostic_provider: Some(lsp_types::DiagnosticServerCapabilities::Options(
DiagnosticOptions::default(),
)),
+ code_action_provider: Some(CodeActionProviderCapability::Simple(true)),
..Default::default()
})
.unwrap();
@@ -90,21 +91,27 @@ impl Server {
Ok(self.io_threads.join()?)
}
+ fn handle_save(&self, notif: &Notification) -> anyhow::Result<()> {
+ let params = cast_notif::<DidSaveTextDocument>(notif.clone())?;
+
+ self.publish_diagnostics(&params.text_document.uri)?;
+
+ Ok(())
+ }
+
fn handle_open(&self, req: &Notification) -> anyhow::Result<()> {
let params = cast_notif::<DidOpenTextDocument>(req.clone())?;
- dbg!(params);
+ self.publish_diagnostics(&params.text_document.uri)?;
Ok(())
}
- fn handle_save(&self, req: &Notification) -> anyhow::Result<()> {
- let params = cast_notif::<DidSaveTextDocument>(req.clone())?;
-
- let diagnostics = generate_diagnostics(params.text_document.uri.clone())?;
+ fn publish_diagnostics(&self, uri: &Url) -> anyhow::Result<()> {
+ let diagnostics = generate_diagnostics(uri)?;
let result = PublishDiagnosticsParams {
- uri: params.text_document.uri,
+ uri: uri.clone(),
diagnostics,
version: None,
};
@@ -137,7 +144,7 @@ impl Server {
},
},
}]));
- let result = serde_json::to_value(&result).unwrap();
+ let result = serde_json::to_value(result).unwrap();
let resp = Response {
id,
result: Some(result),