diff --git a/Cargo.lock b/Cargo.lock index 31d93d9..443ed47 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -161,6 +161,7 @@ dependencies = [ "itertools 0.13.0", "lazy_static", "log", + "regex", "ropey", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 5f10bce..3c2ad4a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,9 +19,7 @@ dashmap = "6.0" lazy_static = "1.5" itertools = "0.13" ropey = "1.6" +regex = "1.7" [build-dependencies] cc = "1.1" - -[patch.crates-io] -hvm = { git = "https://github.com/HigherOrderCO/hvm.git", branch = "parser-update" } diff --git a/src/core/diagnostics.rs b/src/core/diagnostics.rs index 0dcec5b..0abaf47 100644 --- a/src/core/diagnostics.rs +++ b/src/core/diagnostics.rs @@ -5,6 +5,7 @@ use bend::{check_book, imports::DefaultLoader, CompileOpts}; use tower_lsp::lsp_types::{self as lsp, Position}; use super::document::Document; +use crate::utils::color_wrapper::treat_colors; /// Checks a Bend file and return its diagnostics. pub fn check(doc: &Document) -> Diagnostics { @@ -36,7 +37,7 @@ pub fn lsp_diagnostics(diagnostics: &Diagnostics) -> Vec { fn treat_diagnostic(origin: &DiagnosticOrigin, diag: &Diagnostic) -> Option { Some(lsp::Diagnostic { - message: format!("{}", diag.display_with_origin(origin)), + message: treat_colors(&diag.display_with_origin(origin).to_string()), severity: match diag.severity { Severity::Allow => Some(lsp::DiagnosticSeverity::HINT), Severity::Warning => Some(lsp::DiagnosticSeverity::WARNING), diff --git a/src/server/mod.rs b/src/server/mod.rs index 99a0ef2..8f55196 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -1,6 +1,5 @@ use std::fs; -// use bend::diagnostics::Diagnostic; use dashmap::DashMap; use tower_lsp::jsonrpc::Result; use tower_lsp::lsp_types::{self as lsp, SemanticTokensRangeResult}; diff --git a/src/utils/color_wrapper.rs b/src/utils/color_wrapper.rs new file mode 100644 index 0000000..655ea7a --- /dev/null +++ b/src/utils/color_wrapper.rs @@ -0,0 +1,19 @@ +// Parts of this code are based on https://github.com/Aloso/to-html/blob/main/LICENSE, +// which is MIT-licensed. + +use lazy_static::lazy_static; +use regex::Regex; + +lazy_static! { + pub static ref ANSI_REGEX: Regex = + Regex::new(r"\u{1b}(\[[0-9;?]*[A-HJKSTfhilmnsu]|\(B)").unwrap(); +} + +/// This function receives a string with color and highlighting information +/// in ANSI color code format and treats them to be reported by the language server. +/// +/// As tracked in issue #1, diagnostic highlighting still does not implement color +/// information in VSCode, so for now we just remove color codes. +pub fn treat_colors(text: &str) -> String { + ANSI_REGEX.replace_all(text, "").to_string() +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 8d54e43..46845e1 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1 +1,2 @@ pub(crate) mod lsp_log; +pub(crate) mod color_wrapper;