Skip to content

Commit

Permalink
inject or update 42 header if initialization_option is present
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLee18 committed Oct 31, 2024
1 parent ebba390 commit d221d89
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
41 changes: 36 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, &params, $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, &params, $f);
}
Err(_) => {}
Expand Down Expand Up @@ -78,7 +104,7 @@ macro_rules! send_diagnostics {
}

fn read_norminette(path: &Path, text: Option<String>) -> io::Result<Vec<Diagnostic>> {
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()]);
Expand Down Expand Up @@ -150,7 +176,11 @@ fn main_loop(
connection: Connection,
params: serde_json::Value,
) -> Result<(), Box<dyn Error + Sync + Send>> {
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:?}");
Expand Down Expand Up @@ -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
);
}
}
Expand Down

0 comments on commit d221d89

Please sign in to comment.