From 614d95d8ad153e2cee7348e187c2ff03cdb64c71 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Wed, 20 Nov 2024 10:49:14 -0600 Subject: [PATCH] Adds some ressources and works on pattern matching --- README.md | 16 +++------------- src/main.rs | 55 +++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 4e501c2..b96a5a5 100644 --- a/README.md +++ b/README.md @@ -24,22 +24,12 @@ command = "vsg_bridge_ls" [ [language]] name = "vhdl" -# scope = "source.blarg" -# injection-regex = "rs|rust" file-types = ["vhd", "vhdl"] -# roots = ["Cargo.toml", "Cargo.lock"] -# shebangs = ["rust-script", "cargo"] -# auto-format = false -# comment-tokens = ["//", "///", "//!"] -# block-comment-tokens = [ -# { start = "/*", end = "*/" }, -# { start = "/**", end = "*/" }, -# { start = "/*!", end = "*/" }, -# ] language-servers = [ "vsg_bridge_ls" ] -# indent = { tab-width = 4, unit = " " } -# persistent-diagnostic-sources = ["rustc", "clippy"] ``` # Documentation [Tower-lsp](https://github.com/ebkalderon/tower-lsp/blob/master/examples/stdio.rs) +[VSG Github](https://github.com/jeremiah-c-leary/vhdl-style-guide) +[VSG Docs](https://vhdl-style-guide.readthedocs.io/en/latest/editor_integration/vscode.html) +[Lsp Specs](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/) diff --git a/src/main.rs b/src/main.rs index 62fd2a1..7d8353e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,6 @@ -use once_cell::sync::OnceCell; -use regex::Regex; +use anyhow::{anyhow, Context}; use serde_json::Value; use std::process::Command; -use tower_lsp::jsonrpc::{self, Result}; use tower_lsp::lsp_types::*; use tower_lsp::{Client, LanguageServer, LspService, Server}; @@ -20,7 +18,10 @@ struct Backend { #[tower_lsp::async_trait] impl LanguageServer for Backend { - async fn initialize(&self, _: InitializeParams) -> Result { + async fn initialize( + &self, + _: InitializeParams, + ) -> Result { Ok(InitializeResult { server_info: None, capabilities: ServerCapabilities { @@ -69,7 +70,7 @@ impl LanguageServer for Backend { .await; } - async fn shutdown(&self) -> Result<()> { + async fn shutdown(&self) -> Result<(), tower_lsp::jsonrpc::Error> { Ok(()) } @@ -91,7 +92,10 @@ impl LanguageServer for Backend { // .await; } - async fn execute_command(&self, _: ExecuteCommandParams) -> Result> { + async fn execute_command( + &self, + _: ExecuteCommandParams, + ) -> Result, tower_lsp::jsonrpc::Error> { // self.client // .log_message(MessageType::INFO, "command executed!") // .await; @@ -118,7 +122,6 @@ impl LanguageServer for Backend { } async fn did_save(&self, p: DidSaveTextDocumentParams) { - let reg = regex!(r"^ ([^\\s]*_\\d*)\\s*. ([^\\s]*)\\s*.\\s*([^\\s]*) . (.*)$"); self.client .log_message(MessageType::INFO, "file saved!") .await; @@ -143,7 +146,10 @@ impl LanguageServer for Backend { // .await; } - async fn completion(&self, _: CompletionParams) -> Result> { + async fn completion( + &self, + _: CompletionParams, + ) -> Result, tower_lsp::jsonrpc::Error> { // Ok(Some(CompletionResponse::Array(vec![ // CompletionItem::new_simple("Hello".to_string(), "Some detail".to_string()), // CompletionItem::new_simple("Bye".to_string(), "More detail".to_string()), @@ -152,19 +158,36 @@ impl LanguageServer for Backend { } } -async fn find_diagnostics_for_file(uri: Url) -> Result> { - let output = Command::new("ls") - // .arg("file.txt") +async fn find_diagnostics_for_file(uri: Url) -> Result, anyhow::Error> { + let reg = regex!(r"^ ([^\s]*_\d*)\s*. ([^\s]*)\s*.\s*([^\s]*) . (.*)$"); + + let v = &Command::new("vsg") + .arg("-f") + .arg(uri.as_str()) .output() - .unwrap(); + .context("running vsg linter")? + .stdout; + let output = String::from_utf8_lossy(v); + let mut matches = reg.captures_iter(&output); + // for m in matches { + // let rule = m.get(1); + // let severity = m.get(2); + // let line = m.get(3); + // let solution = m.get(4); + // } + + let m = matches.next().ok_or_else(|| anyhow!("Welp..."))?; + let rule = m.get(1); + let severity = m.get(2); + let line = m.get(3); + let solution = m.get(4); + let show = format!("{:?} {:?} {:?} {:?}", rule, severity, line, solution); + let range = make_range(1); let d = Diagnostic { range, severity: Some(DiagnosticSeverity::WARNING), // Everything is a warning - code: Some(NumberOrString::String( - String::from_utf8_lossy(&output.stdout).into(), - )), // The important second part - // code_description: Some(NumberOrString::String(output.stdout.to_string())), + code: Some(NumberOrString::String(show)), // The important second part code_description: Some(CodeDescription { href: uri.clone() }), source: Some("Source ?".into()), message: "El message".to_string(),