Compare commits

...

1 Commits

Author SHA1 Message Date
Dandaedre 61cbbaf7aa Wip for stdio 2024-11-21 06:47:27 -06:00
1 changed files with 46 additions and 26 deletions

View File

@ -1,7 +1,12 @@
use anyhow::{anyhow, Context}; //! Hello
//!
#![warn(clippy::missing_docs_in_private_items)]
use anyhow::Context;
use clap::Parser; use clap::Parser;
use serde_json::Value; use serde_json::Value;
use std::process::Command; use std::io::Write;
use std::process::{Command, Stdio};
use tower_lsp::lsp_types::*; use tower_lsp::lsp_types::*;
use tower_lsp::{Client, LanguageServer, LspService, Server}; use tower_lsp::{Client, LanguageServer, LspService, Server};
@ -36,7 +41,7 @@ impl LanguageServer for Backend {
Ok(InitializeResult { Ok(InitializeResult {
capabilities: ServerCapabilities { capabilities: ServerCapabilities {
text_document_sync: Some(TextDocumentSyncCapability::Kind( text_document_sync: Some(TextDocumentSyncCapability::Kind(
TextDocumentSyncKind::NONE, TextDocumentSyncKind::FULL,
)), )),
..ServerCapabilities::default() ..ServerCapabilities::default()
}, },
@ -72,25 +77,26 @@ impl LanguageServer for Backend {
} }
async fn did_open(&self, p: DidOpenTextDocumentParams) { async fn did_open(&self, p: DidOpenTextDocumentParams) {
match find_diagnostics_for_file(p.text_document.uri.clone(), &self.vsg_args).await {
Ok(diags) => { // match find_diagnostics_for_file(p.text_document.uri.clone(), &self.vsg_args).await {
self.client // Ok(diags) => {
.publish_diagnostics(p.text_document.uri, diags, None) // self.client
.await; // .publish_diagnostics(p.text_document.uri, diags, None)
} // .await;
Err(e) => { // }
self.client // Err(e) => {
.show_message(MessageType::ERROR, e.to_string()) // self.client
.await; // .show_message(MessageType::ERROR, e.to_string())
} // .await;
}; // }
// };
} }
async fn did_save(&self, p: DidSaveTextDocumentParams) { async fn did_save(&self, p: DidSaveTextDocumentParams) {
self.client self.client
.show_message(MessageType::ERROR, format!("{:?}", self.vsg_args)) .show_message(MessageType::ERROR, format!("{:?}", self.vsg_args))
.await; .await;
match find_diagnostics_for_file(p.text_document.uri.clone(), &self.vsg_args).await { match find_diagnostics_for_text(p.text.unwrap(), &self.vsg_args).await {
Ok(diags) => { Ok(diags) => {
self.client self.client
.publish_diagnostics(p.text_document.uri, diags, None) .publish_diagnostics(p.text_document.uri, diags, None)
@ -105,8 +111,9 @@ impl LanguageServer for Backend {
} }
} }
async fn find_diagnostics_for_file( async fn find_diagnostics_for_text(
uri: Url, // uri: Url,
data: String,
config: &Vec<String>, config: &Vec<String>,
) -> Result<Vec<Diagnostic>, anyhow::Error> { ) -> Result<Vec<Diagnostic>, anyhow::Error> {
let mut vec_diag = Vec::new(); let mut vec_diag = Vec::new();
@ -114,18 +121,31 @@ async fn find_diagnostics_for_file(
r" (?<rule>[^\s]*_\d*)\s*\| (?<severity>[^\s]*)\s*\|\s*(?<line>[^\s]*) \| (?<solution>.*)" r" (?<rule>[^\s]*_\d*)\s*\| (?<severity>[^\s]*)\s*\|\s*(?<line>[^\s]*) \| (?<solution>.*)"
); );
let v = &Command::new("vsg") let mut child = Command::new("vsg")
.stdin(Stdio::piped())
.arg("-f") .arg("-f")
.arg( // .arg(
uri.to_file_path() // uri.to_file_path()
.map_err(|_| anyhow!("converting uri to file path"))?, // .map_err(|_| anyhow!("converting uri to file path"))?,
) // )
.args(config) .args(config)
.output() .spawn()
.context("failed to run vsg linter")? .context("failed to run vsg linter")?;
// .stdout;
let mut stdin = child.stdin.take().context("adding stdin pipe")?;
std::thread::spawn(move || {
stdin
.write_all(data.as_bytes())
.expect("Failed to write to stdin");
});
let output = child
.wait_with_output()
.context("Failed to read stdout")?
.stdout; .stdout;
let output = String::from_utf8_lossy(v); let output = String::from_utf8_lossy(&output);
let matches = reg.captures_iter(&output); let matches = reg.captures_iter(&output);
for m in matches { for m in matches {
if let (Some(rule), Some(line), Some(solution)) = if let (Some(rule), Some(line), Some(solution)) =