diff --git a/Cargo.lock b/Cargo.lock index 6922bd7d68..1274e0b4aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4023,6 +4023,7 @@ version = "0.106.2" dependencies = [ "assert-json-diff", "crossbeam-channel", + "fancy-regex", "lsp-server", "lsp-textdocument", "lsp-types", diff --git a/crates/nu-lsp/Cargo.toml b/crates/nu-lsp/Cargo.toml index fc890bf3a8..245fe260fe 100644 --- a/crates/nu-lsp/Cargo.toml +++ b/crates/nu-lsp/Cargo.toml @@ -15,6 +15,7 @@ nu-protocol = { path = "../nu-protocol", version = "0.106.2" } nu-utils = { path = "../nu-utils", version = "0.106.2" } crossbeam-channel = { workspace = true } +fancy-regex = { workspace = true } lsp-server = { workspace = true } lsp-textdocument = { workspace = true } lsp-types = { workspace = true } diff --git a/crates/nu-lsp/src/hover.rs b/crates/nu-lsp/src/hover.rs index 297bdb5328..05204b836c 100644 --- a/crates/nu-lsp/src/hover.rs +++ b/crates/nu-lsp/src/hover.rs @@ -1,5 +1,6 @@ use lsp_types::{Hover, HoverContents, HoverParams, MarkupContent, MarkupKind}; use nu_protocol::{PositionalArg, engine::Command}; +use std::borrow::Cow; use crate::{ Id, LanguageServer, @@ -188,6 +189,15 @@ impl LanguageServer { } Id::Value(t) => markdown_hover(format!("`{t}`")), Id::External(cmd) => { + fn fix_manpage_ascii_shenanigans(text: &str) -> Cow { + if cfg!(windows) { + Cow::Borrowed(text) + } else { + let re = + fancy_regex::Regex::new(r".\x08").expect("regular expression error"); + re.replace_all(text, "") + } + } let command_output = if cfg!(windows) { std::process::Command::new("powershell.exe") .args(["-NoProfile", "-Command", "help", &cmd]) @@ -197,7 +207,10 @@ impl LanguageServer { }; let manpage_str = match command_output { Ok(output) => nu_utils::strip_ansi_likely( - String::from_utf8_lossy(&output.stdout).as_ref(), + fix_manpage_ascii_shenanigans( + String::from_utf8_lossy(&output.stdout).as_ref(), + ) + .as_ref(), ) .to_string(), Err(_) => format!("No command help found for {}", &cmd),