2022-06-20 17:07:32 +02:00
|
|
|
use crossterm::{
|
|
|
|
style::{Color, Print, Stylize},
|
|
|
|
tty::IsTty,
|
|
|
|
};
|
2022-02-16 14:57:20 +01:00
|
|
|
use helix_core::config::{default_syntax_loader, user_syntax_loader};
|
|
|
|
use helix_loader::grammar::load_runtime_file;
|
2022-07-31 10:05:34 +02:00
|
|
|
use helix_view::clipboard::get_clipboard_provider;
|
2022-03-30 07:09:25 +02:00
|
|
|
use std::io::Write;
|
2022-03-08 06:25:46 +01:00
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum TsFeature {
|
|
|
|
Highlight,
|
|
|
|
TextObject,
|
|
|
|
AutoIndent,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TsFeature {
|
|
|
|
pub fn all() -> &'static [Self] {
|
|
|
|
&[Self::Highlight, Self::TextObject, Self::AutoIndent]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn runtime_filename(&self) -> &'static str {
|
|
|
|
match *self {
|
|
|
|
Self::Highlight => "highlights.scm",
|
|
|
|
Self::TextObject => "textobjects.scm",
|
2022-03-30 17:08:07 +02:00
|
|
|
Self::AutoIndent => "indents.scm",
|
2022-03-08 06:25:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn long_title(&self) -> &'static str {
|
|
|
|
match *self {
|
|
|
|
Self::Highlight => "Syntax Highlighting",
|
|
|
|
Self::TextObject => "Treesitter Textobjects",
|
|
|
|
Self::AutoIndent => "Auto Indent",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn short_title(&self) -> &'static str {
|
|
|
|
match *self {
|
|
|
|
Self::Highlight => "Highlight",
|
|
|
|
Self::TextObject => "Textobject",
|
|
|
|
Self::AutoIndent => "Indent",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Display general diagnostics.
|
2022-03-30 07:09:25 +02:00
|
|
|
pub fn general() -> std::io::Result<()> {
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let mut stdout = stdout.lock();
|
|
|
|
|
2022-02-16 14:57:20 +01:00
|
|
|
let config_file = helix_loader::config_file();
|
|
|
|
let lang_file = helix_loader::lang_config_file();
|
|
|
|
let log_file = helix_loader::log_file();
|
2023-03-09 15:50:43 +01:00
|
|
|
let rt_dirs = helix_loader::runtime_dirs();
|
2022-07-31 10:05:34 +02:00
|
|
|
let clipboard_provider = get_clipboard_provider();
|
2022-03-08 06:25:46 +01:00
|
|
|
|
|
|
|
if config_file.exists() {
|
2022-03-30 07:09:25 +02:00
|
|
|
writeln!(stdout, "Config file: {}", config_file.display())?;
|
2022-03-08 06:25:46 +01:00
|
|
|
} else {
|
2022-03-30 07:09:25 +02:00
|
|
|
writeln!(stdout, "Config file: default")?;
|
2022-03-08 06:25:46 +01:00
|
|
|
}
|
|
|
|
if lang_file.exists() {
|
2022-03-30 07:09:25 +02:00
|
|
|
writeln!(stdout, "Language file: {}", lang_file.display())?;
|
2022-03-08 06:25:46 +01:00
|
|
|
} else {
|
2022-03-30 07:09:25 +02:00
|
|
|
writeln!(stdout, "Language file: default")?;
|
2022-03-08 06:25:46 +01:00
|
|
|
}
|
2022-03-30 07:09:25 +02:00
|
|
|
writeln!(stdout, "Log file: {}", log_file.display())?;
|
2023-03-09 15:50:43 +01:00
|
|
|
writeln!(
|
|
|
|
stdout,
|
|
|
|
"Runtime directories: {}",
|
|
|
|
rt_dirs
|
|
|
|
.iter()
|
|
|
|
.map(|d| d.to_string_lossy())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(";")
|
|
|
|
)?;
|
|
|
|
for rt_dir in rt_dirs.iter() {
|
|
|
|
if let Ok(path) = std::fs::read_link(rt_dir) {
|
|
|
|
let msg = format!(
|
|
|
|
"Runtime directory {} is symlinked to: {}",
|
|
|
|
rt_dir.display(),
|
|
|
|
path.display()
|
|
|
|
);
|
|
|
|
writeln!(stdout, "{}", msg.yellow())?;
|
|
|
|
}
|
|
|
|
if !rt_dir.exists() {
|
|
|
|
let msg = format!("Runtime directory does not exist: {}", rt_dir.display());
|
|
|
|
writeln!(stdout, "{}", msg.yellow())?;
|
|
|
|
} else if rt_dir.read_dir().ok().map(|it| it.count()) == Some(0) {
|
|
|
|
let msg = format!("Runtime directory is empty: {}", rt_dir.display());
|
|
|
|
writeln!(stdout, "{}", msg.yellow())?;
|
|
|
|
}
|
2022-03-08 06:25:46 +01:00
|
|
|
}
|
2022-07-31 10:05:34 +02:00
|
|
|
writeln!(stdout, "Clipboard provider: {}", clipboard_provider.name())?;
|
2022-03-30 07:09:25 +02:00
|
|
|
|
|
|
|
Ok(())
|
2022-03-08 06:25:46 +01:00
|
|
|
}
|
|
|
|
|
2022-08-31 18:23:45 +02:00
|
|
|
pub fn clipboard() -> std::io::Result<()> {
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let mut stdout = stdout.lock();
|
|
|
|
|
|
|
|
let board = get_clipboard_provider();
|
|
|
|
match board.name().as_ref() {
|
|
|
|
"none" => {
|
|
|
|
writeln!(
|
|
|
|
stdout,
|
|
|
|
"{}",
|
|
|
|
"System clipboard provider: Not installed".red()
|
|
|
|
)?;
|
|
|
|
writeln!(
|
|
|
|
stdout,
|
|
|
|
" {}",
|
|
|
|
"For troubleshooting system clipboard issues, refer".red()
|
|
|
|
)?;
|
|
|
|
writeln!(stdout, " {}",
|
|
|
|
"https://github.com/helix-editor/helix/wiki/Troubleshooting#copypaste-fromto-system-clipboard-not-working"
|
|
|
|
.red().underlined())?;
|
|
|
|
}
|
|
|
|
name => writeln!(stdout, "System clipboard provider: {}", name)?,
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-30 07:09:25 +02:00
|
|
|
pub fn languages_all() -> std::io::Result<()> {
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let mut stdout = stdout.lock();
|
|
|
|
|
|
|
|
let mut syn_loader_conf = match user_syntax_loader() {
|
|
|
|
Ok(conf) => conf,
|
|
|
|
Err(err) => {
|
|
|
|
let stderr = std::io::stderr();
|
|
|
|
let mut stderr = stderr.lock();
|
|
|
|
|
|
|
|
writeln!(
|
|
|
|
stderr,
|
|
|
|
"{}: {}",
|
|
|
|
"Error parsing user language config".red(),
|
|
|
|
err
|
|
|
|
)?;
|
|
|
|
writeln!(stderr, "{}", "Using default language config".yellow())?;
|
|
|
|
default_syntax_loader()
|
|
|
|
}
|
|
|
|
};
|
2022-03-08 06:25:46 +01:00
|
|
|
|
|
|
|
let mut headings = vec!["Language", "LSP", "DAP"];
|
|
|
|
|
|
|
|
for feat in TsFeature::all() {
|
|
|
|
headings.push(feat.short_title())
|
|
|
|
}
|
|
|
|
|
|
|
|
let terminal_cols = crossterm::terminal::size().map(|(c, _)| c).unwrap_or(80);
|
|
|
|
let column_width = terminal_cols as usize / headings.len();
|
2022-06-20 17:07:32 +02:00
|
|
|
let is_terminal = std::io::stdout().is_tty();
|
2022-03-08 06:25:46 +01:00
|
|
|
|
|
|
|
let column = |item: &str, color: Color| {
|
2022-06-20 17:07:32 +02:00
|
|
|
let mut data = format!(
|
2022-03-30 02:08:30 +02:00
|
|
|
"{:width$}",
|
2022-03-08 06:25:46 +01:00
|
|
|
item.get(..column_width - 2)
|
2022-03-30 02:08:30 +02:00
|
|
|
.map(|s| format!("{}…", s))
|
|
|
|
.unwrap_or_else(|| item.to_string()),
|
|
|
|
width = column_width,
|
2022-06-20 17:07:32 +02:00
|
|
|
);
|
|
|
|
if is_terminal {
|
|
|
|
data = data.stylize().with(color).to_string();
|
|
|
|
}
|
2022-03-08 06:25:46 +01:00
|
|
|
|
|
|
|
// We can't directly use println!() because of
|
|
|
|
// https://github.com/crossterm-rs/crossterm/issues/589
|
|
|
|
let _ = crossterm::execute!(std::io::stdout(), Print(data));
|
|
|
|
};
|
|
|
|
|
|
|
|
for heading in headings {
|
|
|
|
column(heading, Color::White);
|
|
|
|
}
|
2022-03-30 07:09:25 +02:00
|
|
|
writeln!(stdout)?;
|
2022-03-08 06:25:46 +01:00
|
|
|
|
|
|
|
syn_loader_conf
|
|
|
|
.language
|
|
|
|
.sort_unstable_by_key(|l| l.language_id.clone());
|
|
|
|
|
|
|
|
let check_binary = |cmd: Option<String>| match cmd {
|
|
|
|
Some(cmd) => match which::which(&cmd) {
|
2022-08-22 15:39:44 +02:00
|
|
|
Ok(_) => column(&format!("✓ {}", cmd), Color::Green),
|
2022-04-12 10:21:16 +02:00
|
|
|
Err(_) => column(&format!("✘ {}", cmd), Color::Red),
|
2022-03-08 06:25:46 +01:00
|
|
|
},
|
|
|
|
None => column("None", Color::Yellow),
|
|
|
|
};
|
|
|
|
|
|
|
|
for lang in &syn_loader_conf.language {
|
|
|
|
column(&lang.language_id, Color::Reset);
|
|
|
|
|
|
|
|
let lsp = lang
|
|
|
|
.language_server
|
|
|
|
.as_ref()
|
|
|
|
.map(|lsp| lsp.command.to_string());
|
|
|
|
check_binary(lsp);
|
|
|
|
|
|
|
|
let dap = lang.debugger.as_ref().map(|dap| dap.command.to_string());
|
|
|
|
check_binary(dap);
|
|
|
|
|
|
|
|
for ts_feat in TsFeature::all() {
|
|
|
|
match load_runtime_file(&lang.language_id, ts_feat.runtime_filename()).is_ok() {
|
2022-08-22 15:39:44 +02:00
|
|
|
true => column("✓", Color::Green),
|
2022-04-02 10:32:36 +02:00
|
|
|
false => column("✘", Color::Red),
|
2022-03-08 06:25:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 07:09:25 +02:00
|
|
|
writeln!(stdout)?;
|
2022-03-08 06:25:46 +01:00
|
|
|
}
|
2022-03-30 07:09:25 +02:00
|
|
|
|
|
|
|
Ok(())
|
2022-03-08 06:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Display diagnostics pertaining to a particular language (LSP,
|
|
|
|
/// highlight queries, etc).
|
2022-03-30 07:09:25 +02:00
|
|
|
pub fn language(lang_str: String) -> std::io::Result<()> {
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let mut stdout = stdout.lock();
|
|
|
|
|
|
|
|
let syn_loader_conf = match user_syntax_loader() {
|
|
|
|
Ok(conf) => conf,
|
|
|
|
Err(err) => {
|
|
|
|
let stderr = std::io::stderr();
|
|
|
|
let mut stderr = stderr.lock();
|
|
|
|
|
|
|
|
writeln!(
|
|
|
|
stderr,
|
|
|
|
"{}: {}",
|
|
|
|
"Error parsing user language config".red(),
|
|
|
|
err
|
|
|
|
)?;
|
|
|
|
writeln!(stderr, "{}", "Using default language config".yellow())?;
|
|
|
|
default_syntax_loader()
|
|
|
|
}
|
|
|
|
};
|
2022-03-08 06:25:46 +01:00
|
|
|
|
|
|
|
let lang = match syn_loader_conf
|
|
|
|
.language
|
|
|
|
.iter()
|
|
|
|
.find(|l| l.language_id == lang_str)
|
|
|
|
{
|
|
|
|
Some(l) => l,
|
|
|
|
None => {
|
2022-03-30 02:08:30 +02:00
|
|
|
let msg = format!("Language '{}' not found", lang_str);
|
2022-03-30 07:09:25 +02:00
|
|
|
writeln!(stdout, "{}", msg.red())?;
|
2022-03-08 06:25:46 +01:00
|
|
|
let suggestions: Vec<&str> = syn_loader_conf
|
|
|
|
.language
|
|
|
|
.iter()
|
|
|
|
.filter(|l| l.language_id.starts_with(lang_str.chars().next().unwrap()))
|
|
|
|
.map(|l| l.language_id.as_str())
|
|
|
|
.collect();
|
|
|
|
if !suggestions.is_empty() {
|
|
|
|
let suggestions = suggestions.join(", ");
|
2022-03-30 07:09:25 +02:00
|
|
|
writeln!(
|
|
|
|
stdout,
|
|
|
|
"Did you mean one of these: {} ?",
|
|
|
|
suggestions.yellow()
|
|
|
|
)?;
|
2022-03-08 06:25:46 +01:00
|
|
|
}
|
2022-03-30 07:09:25 +02:00
|
|
|
return Ok(());
|
2022-03-08 06:25:46 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
probe_protocol(
|
|
|
|
"language server",
|
|
|
|
lang.language_server
|
|
|
|
.as_ref()
|
|
|
|
.map(|lsp| lsp.command.to_string()),
|
2022-03-30 07:09:25 +02:00
|
|
|
)?;
|
2022-03-08 06:25:46 +01:00
|
|
|
|
|
|
|
probe_protocol(
|
|
|
|
"debug adapter",
|
|
|
|
lang.debugger.as_ref().map(|dap| dap.command.to_string()),
|
2022-03-30 07:09:25 +02:00
|
|
|
)?;
|
2022-03-08 06:25:46 +01:00
|
|
|
|
|
|
|
for ts_feat in TsFeature::all() {
|
2022-03-30 07:09:25 +02:00
|
|
|
probe_treesitter_feature(&lang_str, *ts_feat)?
|
2022-03-08 06:25:46 +01:00
|
|
|
}
|
2022-03-30 07:09:25 +02:00
|
|
|
|
|
|
|
Ok(())
|
2022-03-08 06:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Display diagnostics about LSP and DAP.
|
2022-03-30 07:09:25 +02:00
|
|
|
fn probe_protocol(protocol_name: &str, server_cmd: Option<String>) -> std::io::Result<()> {
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let mut stdout = stdout.lock();
|
|
|
|
|
2022-03-08 06:25:46 +01:00
|
|
|
let cmd_name = match server_cmd {
|
|
|
|
Some(ref cmd) => cmd.as_str().green(),
|
|
|
|
None => "None".yellow(),
|
|
|
|
};
|
2022-03-30 07:09:25 +02:00
|
|
|
writeln!(stdout, "Configured {}: {}", protocol_name, cmd_name)?;
|
2022-03-08 06:25:46 +01:00
|
|
|
|
|
|
|
if let Some(cmd) = server_cmd {
|
2022-12-17 20:03:18 +01:00
|
|
|
let path = match which::which(&cmd) {
|
2022-03-08 06:25:46 +01:00
|
|
|
Ok(path) => path.display().to_string().green(),
|
2022-12-17 20:03:18 +01:00
|
|
|
Err(_) => format!("'{}' not found in $PATH", cmd).red(),
|
2022-03-08 06:25:46 +01:00
|
|
|
};
|
2022-03-30 07:09:25 +02:00
|
|
|
writeln!(stdout, "Binary for {}: {}", protocol_name, path)?;
|
2022-03-08 06:25:46 +01:00
|
|
|
}
|
2022-03-30 07:09:25 +02:00
|
|
|
|
|
|
|
Ok(())
|
2022-03-08 06:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Display diagnostics about a feature that requires tree-sitter
|
|
|
|
/// query files (highlights, textobjects, etc).
|
2022-03-30 07:09:25 +02:00
|
|
|
fn probe_treesitter_feature(lang: &str, feature: TsFeature) -> std::io::Result<()> {
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let mut stdout = stdout.lock();
|
|
|
|
|
2022-03-08 06:25:46 +01:00
|
|
|
let found = match load_runtime_file(lang, feature.runtime_filename()).is_ok() {
|
2022-08-22 15:39:44 +02:00
|
|
|
true => "✓".green(),
|
2022-04-02 10:32:36 +02:00
|
|
|
false => "✘".red(),
|
2022-03-08 06:25:46 +01:00
|
|
|
};
|
2022-03-30 07:09:25 +02:00
|
|
|
writeln!(stdout, "{} queries: {}", feature.short_title(), found)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_health(health_arg: Option<String>) -> std::io::Result<()> {
|
|
|
|
match health_arg.as_deref() {
|
2022-08-31 18:23:45 +02:00
|
|
|
Some("languages") => languages_all()?,
|
|
|
|
Some("clipboard") => clipboard()?,
|
|
|
|
None | Some("all") => {
|
2022-03-30 07:09:25 +02:00
|
|
|
general()?;
|
2022-08-31 18:23:45 +02:00
|
|
|
clipboard()?;
|
2022-03-30 07:09:25 +02:00
|
|
|
writeln!(std::io::stdout().lock())?;
|
|
|
|
languages_all()?;
|
|
|
|
}
|
2022-08-31 18:23:45 +02:00
|
|
|
Some(lang) => language(lang.to_string())?,
|
2022-03-30 07:09:25 +02:00
|
|
|
}
|
|
|
|
Ok(())
|
2022-03-08 06:25:46 +01:00
|
|
|
}
|