Skip to content

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

Re-write with `tower-lsp`

Elijah Potter9b60aea8a0c6
diff --git a/Cargo.lock b/Cargo.lock
index 8588629cf..503150e5e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -51,20 +51,6 @@ 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"
@@ -72,48 +58,26 @@ 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"
+name = "async-trait"
+version = "0.1.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7"
+checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9"
dependencies = [
- "anstyle",
- "windows-sys 0.52.0",
+ "proc-macro2",
+ "quote",
+ "syn 2.0.48",
]
[[package]]
-name = "anyhow"
-version = "1.0.79"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "080e9890a082662b09c1ad45f567faeeb47f22b5fb23895fbe1e651e718e25ca"
-
-[[package]]
-name = "async-trait"
-version = "0.1.77"
+name = "auto_impl"
+version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9"
+checksum = "fee3da8ef1276b0bee5dd1c7258010d8fffd31801447323115a25560e1327b89"
dependencies = [
+ "proc-macro-error",
"proc-macro2",
"quote",
- "syn",
+ "syn 1.0.109",
]
[[package]]
@@ -265,7 +229,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e578d6ec4194633722ccf9544794b71b1385c3c027efe0c55db226fc880865c"
dependencies = [
"clap_builder",
- "clap_derive",
]
[[package]]
@@ -274,22 +237,8 @@ version = "4.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
@@ -298,12 +247,6 @@ 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"
@@ -350,15 +293,6 @@ dependencies = [
"itertools 0.10.5",
]
-[[package]]
-name = "crossbeam-channel"
-version = "0.5.11"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "176dc175b78f56c0f321911d9c8eb2b77a78a4860b9c19db83835fea1a46649b"
-dependencies = [
- "crossbeam-utils",
-]
-
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
@@ -385,13 +319,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345"
[[package]]
-name = "ctrlc"
-version = "3.4.2"
+name = "dashmap"
+version = "5.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b467862cc8610ca6fc9a1532d7777cee0804e678ab45410897b9396495994a0b"
+checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856"
dependencies = [
- "nix",
- "windows-sys 0.52.0",
+ "cfg-if",
+ "hashbrown",
+ "lock_api",
+ "once_cell",
+ "parking_lot_core",
]
[[package]]
@@ -425,6 +362,20 @@ dependencies = [
"percent-encoding",
]
+[[package]]
+name = "futures"
+version = "0.3.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0"
+dependencies = [
+ "futures-channel",
+ "futures-core",
+ "futures-io",
+ "futures-sink",
+ "futures-task",
+ "futures-util",
+]
+
[[package]]
name = "futures-channel"
version = "0.3.30"
@@ -432,6 +383,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78"
dependencies = [
"futures-core",
+ "futures-sink",
]
[[package]]
@@ -440,6 +392,29 @@ version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
+[[package]]
+name = "futures-io"
+version = "0.3.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1"
+
+[[package]]
+name = "futures-macro"
+version = "0.3.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.48",
+]
+
+[[package]]
+name = "futures-sink"
+version = "0.3.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5"
+
[[package]]
name = "futures-task"
version = "0.3.30"
@@ -452,10 +427,16 @@ version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48"
dependencies = [
+ "futures-channel",
"futures-core",
+ "futures-io",
+ "futures-macro",
+ "futures-sink",
"futures-task",
+ "memchr",
"pin-project-lite",
"pin-utils",
+ "slab",
]
[[package]]
@@ -500,16 +481,9 @@ dependencies = [
name = "harper-ls"
version = "0.1.0"
dependencies = [
- "anyhow",
- "clap",
- "ctrlc",
"harper-core",
- "lsp-server",
- "lsp-types",
- "serde",
- "serde_json",
- "tracing",
- "tracing-subscriber",
+ "tokio",
+ "tower-lsp",
]
[[package]]
@@ -540,10 +514,10 @@ dependencies = [
]
[[package]]
-name = "heck"
-version = "0.4.1"
+name = "hashbrown"
+version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
+checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
[[package]]
name = "hermit-abi"
@@ -627,7 +601,7 @@ dependencies = [
"Inflector",
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.48",
]
[[package]]
@@ -708,23 +682,11 @@ version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
-[[package]]
-name = "lsp-server"
-version = "0.7.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "248f65b78f6db5d8e1b1604b4098a28b43d21a8eb1deeca22b1c421b276c7095"
-dependencies = [
- "crossbeam-channel",
- "log",
- "serde",
- "serde_json",
-]
-
[[package]]
name = "lsp-types"
-version = "0.95.0"
+version = "0.94.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "158c1911354ef73e8fe42da6b10c0484cb65c7f1007f28022e847706c1ab6984"
+checksum = "c66bfd44a06ae10647fe3f8214762e9369fd4248df1350924b4ef9e770a85ea1"
dependencies = [
"bitflags 1.3.2",
"serde",
@@ -771,17 +733,6 @@ dependencies = [
"windows-sys 0.48.0",
]
-[[package]]
-name = "nix"
-version = "0.27.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053"
-dependencies = [
- "bitflags 2.4.1",
- "cfg-if",
- "libc",
-]
-
[[package]]
name = "nu-ansi-term"
version = "0.46.0"
@@ -884,7 +835,7 @@ checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.48",
]
[[package]]
@@ -927,6 +878,30 @@ dependencies = [
"plotters-backend",
]
+[[package]]
+name = "proc-macro-error"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
+dependencies = [
+ "proc-macro-error-attr",
+ "proc-macro2",
+ "quote",
+ "syn 1.0.109",
+ "version_check",
+]
+
+[[package]]
+name = "proc-macro-error-attr"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "version_check",
+]
+
[[package]]
name = "proc-macro2"
version = "1.0.76"
@@ -1077,7 +1052,7 @@ checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.48",
]
[[package]]
@@ -1109,7 +1084,7 @@ checksum = "0b2e6b945e9d3df726b65d6ee24060aff8e3533d431f677a9695db04eff9dfdb"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.48",
]
[[package]]
@@ -1142,6 +1117,15 @@ dependencies = [
"libc",
]
+[[package]]
+name = "slab"
+version = "0.4.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67"
+dependencies = [
+ "autocfg",
+]
+
[[package]]
name = "smallvec"
version = "1.12.0"
@@ -1159,10 +1143,15 @@ dependencies = [
]
[[package]]
-name = "strsim"
-version = "0.10.0"
+name = "syn"
+version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
+checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
[[package]]
name = "syn"
@@ -1243,7 +1232,21 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.48",
+]
+
+[[package]]
+name = "tokio-util"
+version = "0.7.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5419f34732d9eb6ee4c3578b7989078579b7f039cbbb9ca2c4da015749371e15"
+dependencies = [
+ "bytes",
+ "futures-core",
+ "futures-sink",
+ "pin-project-lite",
+ "tokio",
+ "tracing",
]
[[package]]
@@ -1268,6 +1271,40 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0"
+[[package]]
+name = "tower-lsp"
+version = "0.20.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d4ba052b54a6627628d9b3c34c176e7eda8359b7da9acd497b9f20998d118508"
+dependencies = [
+ "async-trait",
+ "auto_impl",
+ "bytes",
+ "dashmap",
+ "futures",
+ "httparse",
+ "lsp-types",
+ "memchr",
+ "serde",
+ "serde_json",
+ "tokio",
+ "tokio-util",
+ "tower",
+ "tower-lsp-macros",
+ "tracing",
+]
+
+[[package]]
+name = "tower-lsp-macros"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "84fd902d4e0b9a4b27f2f440108dc034e1758628a9b702f8ec61ad66355422fa"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.48",
+]
+
[[package]]
name = "tower-service"
version = "0.3.2"
@@ -1294,7 +1331,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.48",
]
[[package]]
@@ -1382,12 +1419,6 @@ 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"
@@ -1446,7 +1477,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.48",
"wasm-bindgen-shared",
]
@@ -1468,7 +1499,7 @@ checksum = "bae1abb6806dc1ad9e560ed242107c0f6c84335f1749dd4e8ddb012ebd5e25a7"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.48",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@@ -1669,5 +1700,5 @@ checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.48",
]
diff --git a/harper-ls/Cargo.toml b/harper-ls/Cargo.toml
index a17366663..a57d39c95 100644
--- a/harper-ls/Cargo.toml
+++ b/harper-ls/Cargo.toml
@@ -4,13 +4,6 @@ version = "0.1.0"
edition = "2021"
[dependencies]
-anyhow = "1.0.79"
-ctrlc = "3.4.2"
-lsp-server = "0.7.6"
-lsp-types = "0.95.0"
-serde = "1.0.195"
-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"] }
+tower-lsp = "0.20.0"
+tokio = { version = "1.35.1", features = ["full"] }
diff --git a/harper-ls/src/backend.rs b/harper-ls/src/backend.rs
new file mode 100644
index 000000000..58e27cb9c
--- /dev/null
+++ b/harper-ls/src/backend.rs
@@ -0,0 +1,51 @@
+use tower_lsp::{
+ jsonrpc::Result,
+ lsp_types::{
+ notification::PublishDiagnostics, DidSaveTextDocumentParams, InitializeParams,
+ InitializeResult, InitializedParams, MessageType, PublishDiagnosticsParams,
+ },
+ Client, LanguageServer,
+};
+
+use crate::diagnostics::generate_diagnostics;
+
+pub struct Backend {
+ client: Client,
+}
+
+impl Backend {
+ pub fn new(client: Client) -> Self {
+ Self { client }
+ }
+}
+
+#[tower_lsp::async_trait]
+impl LanguageServer for Backend {
+ async fn initialize(&self, _: InitializeParams) -> Result<InitializeResult> {
+ Ok(InitializeResult::default())
+ }
+
+ async fn initialized(&self, _: InitializedParams) {
+ self.client
+ .log_message(MessageType::INFO, "Server initialized!")
+ .await;
+ }
+
+ async fn shutdown(&self) -> Result<()> {
+ Ok(())
+ }
+
+ async fn did_save(&self, params: DidSaveTextDocumentParams) {
+ let diagnostics = generate_diagnostics(&params.text_document.uri).unwrap();
+
+ let result = PublishDiagnosticsParams {
+ uri: params.text_document.uri,
+ diagnostics,
+ version: None,
+ };
+
+ self.client
+ .send_notification::<PublishDiagnostics>(result)
+ .await;
+ }
+}
diff --git a/harper-ls/src/diagnostics.rs b/harper-ls/src/diagnostics.rs
index 105164600..89d9db58e 100644
--- a/harper-ls/src/diagnostics.rs
+++ b/harper-ls/src/diagnostics.rs
@@ -1,8 +1,11 @@
use harper_core::{all_linters, Dictionary, Document, Lint, Span, Suggestion};
-use lsp_types::{CodeAction, CodeActionKind, Diagnostic, Location, Position, Range, Url};
use std::{fs::read, io::Read};
+use tower_lsp::jsonrpc::{ErrorCode, Result};
+use tower_lsp::lsp_types::{
+ CodeAction, CodeActionKind, Diagnostic, Location, Position, Range, Url,
+};
-pub fn generate_diagnostics(file_url: &Url) -> anyhow::Result<Vec<Diagnostic>> {
+pub fn generate_diagnostics(file_url: &Url) -> Result<Vec<Diagnostic>> {
let file_str = open_url(file_url)?;
let source_chars: Vec<_> = file_str.chars().collect();
let lints = lint_string(&file_str);
@@ -15,7 +18,7 @@ pub fn generate_diagnostics(file_url: &Url) -> anyhow::Result<Vec<Diagnostic>> {
Ok(diagnostics)
}
-pub fn generate_code_actions(url: &Url, range: Range) -> anyhow::Result<Vec<CodeAction>> {
+pub fn generate_code_actions(url: &Url, range: Range) -> Result<Vec<CodeAction>> {
let file_str = open_url(url)?;
let source_chars: Vec<_> = file_str.chars().collect();
let lints = lint_string(&file_str);
@@ -45,9 +48,10 @@ fn lint_to_code_actions(lint: &Lint) -> impl Iterator<Item = CodeAction> + '_ {
})
}
-fn open_url(url: &Url) -> anyhow::Result<String> {
- let file = read(url.path())?;
- Ok(String::from_utf8(file)?)
+fn open_url(url: &Url) -> Result<String> {
+ let file =
+ read(url.path()).map_err(|err| tower_lsp::jsonrpc::Error::new(ErrorCode::InternalError))?;
+ Ok(String::from_utf8(file).unwrap())
}
fn lint_string(text: &str) -> Vec<Lint> {
diff --git a/harper-ls/src/main.rs b/harper-ls/src/main.rs
index 563b22361..2653f7d42 100644
--- a/harper-ls/src/main.rs
+++ b/harper-ls/src/main.rs
@@ -1,35 +1,13 @@
+mod backend;
mod diagnostics;
-mod server;
-use clap::Parser;
-use lsp_server::Connection;
-use tracing::Level;
+use backend::Backend;
+use tower_lsp::{LspService, Server};
-use crate::server::Server;
+#[tokio::main]
+async fn main() {
+ let (service, socket) = LspService::new(Backend::new);
-#[derive(Debug, Parser)]
-struct Args {
- #[arg(short, long, default_value_t = false)]
- stdio: bool,
-}
-
-fn main() -> anyhow::Result<()> {
- 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)?;
-
- Connection::listen("127.0.0.1:4000")?
- } else {
- Connection::stdio()
- };
-
- let mut server = Server::new(connection, io_threads)?;
- server.main_loop()?;
- server.join()?;
-
- Ok(())
+ let stdin = tokio::io::stdin();
+ let stdout = tokio::io::stdout();
+ Server::new(stdin, stdout, socket).serve(service).await;
}
diff --git a/harper-ls/src/server.rs b/harper-ls/src/server.rs
deleted file mode 100644
index d61de50a8..000000000
--- a/harper-ls/src/server.rs
+++ /dev/null
@@ -1,210 +0,0 @@
-use lsp_server::{
- Connection, ExtractError, IoThreads, Message, Notification, Request, RequestId, Response,
-};
-use lsp_types::{
- notification::{
- DidOpenTextDocument, DidSaveTextDocument, Notification as NotificationTrait,
- PublishDiagnostics,
- },
- request::{CodeActionRequest, GotoDefinition},
- CodeAction, CodeActionKind, CodeActionOrCommand, CodeActionProviderCapability,
- CodeActionResponse, Diagnostic, DiagnosticOptions, GotoDefinitionResponse, InitializedParams,
- Location, OneOf, Position, PublishDiagnosticsParams, Range, ServerCapabilities, Url,
- WorkDoneProgressOptions,
-};
-use serde::Serialize;
-use tracing::{error, info};
-
-use crate::diagnostics::{generate_code_actions, generate_diagnostics};
-
-pub struct Server {
- connection: Connection,
- io_threads: IoThreads,
- params: InitializedParams,
-}
-
-type RequestHandler = fn(server: &Server, req: &Request) -> anyhow::Result<()>;
-type NotificationHandler = fn(server: &Server, notif: &Notification) -> anyhow::Result<()>;
-
-impl Server {
- pub fn new(connection: Connection, io_threads: IoThreads) -> anyhow::Result<Self> {
- let server_capabilities = serde_json::to_value(ServerCapabilities {
- diagnostic_provider: Some(lsp_types::DiagnosticServerCapabilities::Options(
- DiagnosticOptions::default(),
- )),
- definition_provider: Some(OneOf::Left(true)),
- code_action_provider: Some(CodeActionProviderCapability::Options(
- lsp_types::CodeActionOptions {
- code_action_kinds: Some(vec![CodeActionKind::QUICKFIX]),
- work_done_progress_options: WorkDoneProgressOptions::default(),
- resolve_provider: None,
- },
- )),
- ..Default::default()
- })
- .unwrap();
- let initialization_params =
- serde_json::from_value(connection.initialize(server_capabilities)?)?;
-
- Ok(Self {
- connection,
- io_threads,
- params: initialization_params,
- })
- }
-
- pub fn main_loop(&mut self) -> anyhow::Result<()> {
- info!("Starting example main loop");
- for msg in &self.connection.receiver {
- info!("Got msg: {msg:?}");
- match msg {
- Message::Request(req) => {
- if self.connection.handle_shutdown(&req)? {
- return Ok(());
- }
-
- info!("Got request: {req:?}");
-
- let handlers: [RequestHandler; 2] =
- [Self::handle_goto, Self::handle_code_action];
-
- for handler in handlers {
- let res = handler(self, &req);
-
- if let Err(err) = res {
- error!("{}", err.to_string());
- }
- }
- }
- Message::Response(resp) => {
- info!("Got response: {resp:?}");
- }
- Message::Notification(not) => {
- info!("Got notification: {not:?}");
-
- let handlers: [NotificationHandler; 2] = [Self::handle_open, Self::handle_save];
-
- for handler in handlers {
- let res = handler(self, &not);
-
- if let Err(err) = res {
- error!("{}", err.to_string());
- }
- }
- }
- };
- }
-
- Ok(())
- }
-
- pub fn join(self) -> anyhow::Result<()> {
- 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())?;
-
- self.publish_diagnostics(&params.text_document.uri)?;
-
- Ok(())
- }
-
- fn publish_diagnostics(&self, uri: &Url) -> anyhow::Result<()> {
- let diagnostics = generate_diagnostics(uri)?;
-
- let result = PublishDiagnosticsParams {
- uri: uri.clone(),
- diagnostics,
- version: None,
- };
-
- let result = serde_json::to_value(result)?;
- self.connection
- .sender
- .send(Message::Notification(Notification {
- method: PublishDiagnostics::METHOD.to_string(),
- params: result,
- }))?;
-
- Ok(())
- }
-
- fn handle_goto(&self, req: &Request) -> anyhow::Result<()> {
- let (id, params) = cast_request::<GotoDefinition>(req.clone())?;
-
- info!("Got gotoDefinition request #{id}: {params:?}");
- let result = Some(GotoDefinitionResponse::Array(vec![Location {
- uri: params.text_document_position_params.text_document.uri,
- range: lsp_types::Range {
- start: Position {
- line: 0,
- character: 0,
- },
- end: Position {
- line: 0,
- character: 0,
- },
- },
- }]));
-
- self.send_response(result, id)?;
-
- Ok(())
- }
-
- fn handle_code_action(&self, req: &Request) -> anyhow::Result<()> {
- let (id, params) = cast_request::<CodeActionRequest>(req.clone())?;
-
- info!("Got code action request request #{id}: {params:?}");
-
- let actions = generate_code_actions(&params.text_document.uri, params.range)?;
- let response: CodeActionResponse = actions
- .into_iter()
- .map(CodeActionOrCommand::CodeAction)
- .collect();
-
- let result = Some(response);
-
- self.send_response(result, id)?;
-
- Ok(())
- }
-
- fn send_response<V: Serialize>(&self, result: V, id: RequestId) -> anyhow::Result<()> {
- let result = serde_json::to_value(result).unwrap();
- let resp = Response {
- id,
- result: Some(result),
- error: None,
- };
-
- self.connection.sender.send(Message::Response(resp))?;
-
- Ok(())
- }
-}
-
-fn cast_request<R>(req: Request) -> Result<(RequestId, R::Params), ExtractError<Request>>
-where
- R: lsp_types::request::Request,
- R::Params: serde::de::DeserializeOwned,
-{
- req.extract(R::METHOD)
-}
-
-fn cast_notif<R>(notif: Notification) -> Result<R::Params, ExtractError<Notification>>
-where
- R: lsp_types::notification::Notification,
- R::Params: serde::de::DeserializeOwned,
-{
- notif.extract(R::METHOD)
-}
diff --git a/nvim.lua b/nvim.lua
index 73d809cc5..d0f703c30 100644
--- a/nvim.lua
+++ b/nvim.lua
@@ -1,9 +1,14 @@
-- This is a script used to debug `harper-ls` in NeoVim.
+-- vim.lsp.start({
+-- name = "example",
+-- cmd = vim.lsp.rpc.connect("127.0.0.1", 4000),
+-- root_dir = "."
+-- })
+
+
vim.lsp.start({
- name = "example",
- cmd = vim.lsp.rpc.connect("127.0.0.1", 4000),
+ name = "harper_ls",
+ cmd = {"cargo", "run", "--bin", "harper-ls"},
root_dir = "."
})
-
-