Repository: nushell/nushell
File: crates/nu-lsp/src/hover.rs (Rust)

Code before:
            }
            Id::Declaration(decl_id) => markdown_hover(Self::get_decl_description(
                working_set.get_decl(decl_id),
                false,
            )),
            Id::Module(module_id, _) => {
                let description = working_set
                    .get_module_comments(module_id)?
                    .iter()
                    .map(|sp| String::from_utf8_lossy(working_set.get_span_contents(*sp)).into())
                    .collect::<Vec<String>>()
                    .join("\n");
                markdown_hover(description)
            }
            Id::Value(t) => markdown_hover(format!("`{t}`")),
            Id::External(cmd) => {
                fn fix_manpage_ascii_shenanigans(text: &str) -> String {
                    if cfg!(windows) {
                        String::from(text)
                    } else {
                        let re =
                            fancy_regex::Regex::new(r".\x08").expect("regular expression error");
                        re.replace_all(text, "").to_string()
                    }
                }
                let command_output = if cfg!(windows) {
                    std::process::Command::new("powershell.exe")
                        .args(["-NoProfile", "-Command", "help", &cmd])
                        .output()
                } else {
                    std::process::Command::new("man").arg(&cmd).output()
                };
                let manpage_str = match command_output {
                    Ok(output) => nu_utils::strip_ansi_likely(
                        fix_manpage_ascii_shenanigans(
                            String::from_utf8_lossy(&output.stdout).as_ref(),
                        )
                        .as_ref(),
                    )
                    .to_string(),
                    Err(_) => format!("No command help found for {}", &cmd),
                };
                markdown_hover(manpage_str)
            }
        }
    }
}

#[cfg(test)]
mod hover_tests {
    use crate::{

Review comment (suggestion):
```suggestion
                use std::borrow::Cow;

                fn fix_manpage_ascii_shenanigans(text: &str) -> Cow<str> {
                    if cfg!(windows) {
                        Cow::Borrowed(text)
                    } else {
                        let re =
                            fancy_regex::Regex::new(r".\x08").expect("regular expression error");
                        re.replace_all(text, "")
                    }
                }
```

Code after:
                )
            }
            Id::Declaration(decl_id) => markdown_hover(Self::get_decl_description(
                working_set.get_decl(decl_id),
                false,
            )),
            Id::Module(module_id, _) => {
                let description = working_set
                    .get_module_comments(module_id)?
                    .iter()
                    .map(|sp| String::from_utf8_lossy(working_set.get_span_contents(*sp)).into())
                    .collect::<Vec<String>>()
                    .join("\n");
                markdown_hover(description)
            }
            Id::Value(t) => markdown_hover(format!("`{t}`")),
            Id::External(cmd) => {
                fn fix_manpage_ascii_shenanigans(text: &str) -> Cow<str> {
                    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])
                        .output()
                } else {
                    std::process::Command::new("man").arg(&cmd).output()
                };
                let manpage_str = match command_output {
                    Ok(output) => nu_utils::strip_ansi_likely(
                        fix_manpage_ascii_shenanigans(
                            String::from_utf8_lossy(&output.stdout).as_ref(),
                        )
                        .as_ref(),
                    )
                    .to_string(),
                    Err(_) => format!("No command help found for {}", &cmd),
                };
                markdown_hover(manpage_str)
            }
        }
    }
}

#[cfg(test)]
mod hover_tests {