Skip to content

Commit

Permalink
Some DRY refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rayanpiro committed Jun 10, 2023
1 parent dc06713 commit afad7bf
Showing 1 changed file with 8 additions and 24 deletions.
32 changes: 8 additions & 24 deletions helix-term/src/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,19 +250,20 @@ pub fn language(lang_str: String) -> std::io::Result<()> {
}
};

probe_protocol(
probe_binary(
"language server",
lang.language_server
.as_ref()
.map(|lsp| lsp.command.to_string()),
)?;

probe_protocol(
probe_binary(
"debug adapter",
lang.debugger.as_ref().map(|dap| dap.command.to_string()),
)?;

probe_commands(
probe_binary(
"formatter",
lang.formatter
.as_ref()
.map(|fmtcfg| fmtcfg.command.to_string()),
Expand All @@ -275,40 +276,23 @@ pub fn language(lang_str: String) -> std::io::Result<()> {
Ok(())
}

/// Display any additional binaries that are configured as commands for the
/// language.
fn probe_commands(formatter_cmd: Option<String>) -> std::io::Result<()> {
let stdout = std::io::stdout();
let mut stdout = stdout.lock();

if let Some(cmd) = formatter_cmd {
let path = match which::which(&cmd) {
Ok(path) => path.display().to_string().green(),
Err(_) => format!("'{}' not found in $PATH", cmd).red(),
};
writeln!(stdout, "Binary for formatter: {}", path)?;
}

Ok(())
}

/// Display diagnostics about LSP and DAP.
fn probe_protocol(protocol_name: &str, server_cmd: Option<String>) -> std::io::Result<()> {
/// Display diagnostics about binaries related to the language.
fn probe_binary(use_case: &str, server_cmd: Option<String>) -> std::io::Result<()> {
let stdout = std::io::stdout();
let mut stdout = stdout.lock();

let cmd_name = match server_cmd {
Some(ref cmd) => cmd.as_str().green(),
None => "None".yellow(),
};
writeln!(stdout, "Configured {}: {}", protocol_name, cmd_name)?;
writeln!(stdout, "Configured {}: {}", use_case, cmd_name)?;

if let Some(cmd) = server_cmd {
let path = match which::which(&cmd) {
Ok(path) => path.display().to_string().green(),
Err(_) => format!("'{}' not found in $PATH", cmd).red(),
};
writeln!(stdout, "Binary for {}: {}", protocol_name, path)?;
writeln!(stdout, "Binary for {}: {}", use_case, path)?;
}

Ok(())
Expand Down

0 comments on commit afad7bf

Please sign in to comment.