Skip to content

Commit

Permalink
Remove colors from Bend diagnostic output
Browse files Browse the repository at this point in the history
Related to issue #1
  • Loading branch information
edusporto committed Aug 12, 2024
1 parent 05990d2 commit 96a10b1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
3 changes: 2 additions & 1 deletion src/core/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -36,7 +37,7 @@ pub fn lsp_diagnostics(diagnostics: &Diagnostics) -> Vec<lsp::Diagnostic> {

fn treat_diagnostic(origin: &DiagnosticOrigin, diag: &Diagnostic) -> Option<lsp::Diagnostic> {
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),
Expand Down
1 change: 0 additions & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
19 changes: 19 additions & 0 deletions src/utils/color_wrapper.rs
Original file line number Diff line number Diff line change
@@ -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()
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub(crate) mod lsp_log;
pub(crate) mod color_wrapper;

0 comments on commit 96a10b1

Please sign in to comment.