From d221d89f515f7cb926467b126aa26d69d89315b2 Mon Sep 17 00:00:00 2001 From: DavidLee18 Date: Thu, 31 Oct 2024 20:18:26 +0900 Subject: [PATCH] inject or update 42 header if initialization_option is present --- Cargo.toml | 2 +- src/main.rs | 41 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 63cb472..59c71e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,6 @@ edition = "2021" lsp-server = "0.7.7" lsp-types = "0.97.0" nom = "7.1.3" +serde = { version = "1.0.210", features = ["derive"] } serde_json = "1.0.128" tracing-subscriber = "0.3.18" -serde = "1.0.210" diff --git a/src/main.rs b/src/main.rs index 9af28eb..d90336a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,15 +11,41 @@ use lsp_types::{ TextDocumentSyncSaveOptions, WorkDoneProgressOptions, }; use parser::parse_norminette; +use serde::Deserialize; use std::error::Error; -use std::io; use std::path::Path; +use std::{io, process}; + +#[derive(Deserialize)] +pub struct InitOptions { + pub path: String, + pub name: String, + pub email: String, +} macro_rules! diag_on_event { ($conn: expr, $noti: expr, $t: ident, $f: expr) => { match cast_noti::<$t>($noti) { Ok(params) => { - eprintln!("got doc document open notification: {params:?}"); + eprintln!("got document notification: {params:?}"); + notify_diagnostics!($conn, ¶ms, $f); + } + Err(_) => {} + } + }; + ($conn: expr, $noti: expr, $t: ident, $f: expr, $option: expr) => { + match cast_noti::<$t>($noti) { + Ok(params) => { + eprintln!("got document notification: {params:?}"); + if let Ok(ref op) = $option { + let output = process::Command::new(&op.path) + .args(["--name", &op.name, "--email", &op.email]) + .output() + .map_err(|e| format!("failed to execute {}: {e:?}", op.path))?; + if !output.status.success() { + eprintln!("{}", String::from_utf8(output.stderr).unwrap()); + } + } notify_diagnostics!($conn, ¶ms, $f); } Err(_) => {} @@ -78,7 +104,7 @@ macro_rules! send_diagnostics { } fn read_norminette(path: &Path, text: Option) -> io::Result> { - let mut cmd = std::process::Command::new("norminette"); + let mut cmd = process::Command::new("norminette"); match text { Some(text) => { cmd.args(["--cfile", &text, "--filename", path.to_str().unwrap()]); @@ -150,7 +176,11 @@ fn main_loop( connection: Connection, params: serde_json::Value, ) -> Result<(), Box> { - let _params: InitializeParams = serde_json::from_value(params)?; + let params: InitializeParams = serde_json::from_value(params)?; + let options = params + .initialization_options + .ok_or_else(|| "missing initialization options".to_string()) + .and_then(|o| InitOptions::deserialize(&o).map_err(|e| format!("deserialization failed: {e:?}"))); for msg in &connection.receiver { eprintln!("got msg: {msg:?}"); @@ -189,7 +219,8 @@ fn main_loop( Some(|p: &DidSaveTextDocumentParams| p .text .clone() - .expect("includeText set to true yet text was None")) + .expect("includeText set to true yet text was None")), + options ); } }