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 serde_json::Value;
use std::process::Command;
use std::io::Write;
use std::process::{Command, Stdio};
use tower_lsp::lsp_types::*;
use tower_lsp::{Client, LanguageServer, LspService, Server};
@ -36,7 +41,7 @@ impl LanguageServer for Backend {
Ok(InitializeResult {
capabilities: ServerCapabilities {
text_document_sync: Some(TextDocumentSyncCapability::Kind(
TextDocumentSyncKind::NONE,
TextDocumentSyncKind::FULL,
)),
..ServerCapabilities::default()
},
@ -72,25 +77,26 @@ impl LanguageServer for Backend {
}
async fn did_open(&self, p: DidOpenTextDocumentParams) {
match find_diagnostics_for_file(p.text_document.uri.clone(), &self.vsg_args).await {
Ok(diags) => {
self.client
.publish_diagnostics(p.text_document.uri, diags, None)
.await;
}
Err(e) => {
self.client
.show_message(MessageType::ERROR, e.to_string())
.await;
}
};
// match find_diagnostics_for_file(p.text_document.uri.clone(), &self.vsg_args).await {
// Ok(diags) => {
// self.client
// .publish_diagnostics(p.text_document.uri, diags, None)
// .await;
// }
// Err(e) => {
// self.client
// .show_message(MessageType::ERROR, e.to_string())
// .await;
// }
// };
}
async fn did_save(&self, p: DidSaveTextDocumentParams) {
self.client
.show_message(MessageType::ERROR, format!("{:?}", self.vsg_args))
.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) => {
self.client
.publish_diagnostics(p.text_document.uri, diags, None)
@ -105,8 +111,9 @@ impl LanguageServer for Backend {
}
}
async fn find_diagnostics_for_file(
uri: Url,
async fn find_diagnostics_for_text(
// uri: Url,
data: String,
config: &Vec<String>,
) -> Result<Vec<Diagnostic>, anyhow::Error> {
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>.*)"
);
let v = &Command::new("vsg")
let mut child = Command::new("vsg")
.stdin(Stdio::piped())
.arg("-f")
.arg(
uri.to_file_path()
.map_err(|_| anyhow!("converting uri to file path"))?,
)
// .arg(
// uri.to_file_path()
// .map_err(|_| anyhow!("converting uri to file path"))?,
// )
.args(config)
.output()
.context("failed to run vsg linter")?
.spawn()
.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;
let output = String::from_utf8_lossy(v);
let output = String::from_utf8_lossy(&output);
let matches = reg.captures_iter(&output);
for m in matches {
if let (Some(rule), Some(line), Some(solution)) =