@@ -1,6 +1,7 @@
1use crossterm::style::{Color, Print, Stylize};
1use crossterm::style::{Color, Print, Stylize};
2use helix_core::config::{default_syntax_loader, user_syntax_loader};
2use helix_core::config::{default_syntax_loader, user_syntax_loader};
3use helix_loader::grammar::load_runtime_file;
3use helix_loader::grammar::load_runtime_file;
5#[derive(Copy, Clone)]
6#[derive(Copy, Clone)]
6pub enum TsFeature {
7pub enum TsFeature {
@@ -40,43 +41,62 @@ impl TsFeature {
42/// Display general diagnostics.
43/// Display general diagnostics.
43−pub fn general() {
44+pub fn general() -> std::io::Result<()> {
45+ let stdout = std::io::stdout();
46+ let mut stdout = stdout.lock();
44 let config_file = helix_loader::config_file();
48 let config_file = helix_loader::config_file();
45 let lang_file = helix_loader::lang_config_file();
49 let lang_file = helix_loader::lang_config_file();
46 let log_file = helix_loader::log_file();
50 let log_file = helix_loader::log_file();
47 let rt_dir = helix_loader::runtime_dir();
51 let rt_dir = helix_loader::runtime_dir();
49 if config_file.exists() {
53 if config_file.exists() {
50− println!("Config file: {}", config_file.display());
54+ writeln!(stdout, "Config file: {}", config_file.display())?;
52− println!("Config file: default")
56+ writeln!(stdout, "Config file: default")?;
54 if lang_file.exists() {
58 if lang_file.exists() {
55− println!("Language file: {}", lang_file.display());
59+ writeln!(stdout, "Language file: {}", lang_file.display())?;
57− println!("Language file: default")
61+ writeln!(stdout, "Language file: default")?;
59− println!("Log file: {}", log_file.display());
63+ writeln!(stdout, "Log file: {}", log_file.display())?;
60− println!("Runtime directory: {}", rt_dir.display());
64+ writeln!(stdout, "Runtime directory: {}", rt_dir.display())?;
62 if let Ok(path) = std::fs::read_link(&rt_dir) {
66 if let Ok(path) = std::fs::read_link(&rt_dir) {
63 let msg = format!("Runtime directory is symlinked to {}", path.display());
67 let msg = format!("Runtime directory is symlinked to {}", path.display());
64− println!("{}", msg.yellow());
68+ writeln!(stdout, "{}", msg.yellow())?;
66 if !rt_dir.exists() {
70 if !rt_dir.exists() {
67− println!("{}", "Runtime directory does not exist.".red());
71+ writeln!(stdout, "{}", "Runtime directory does not exist.".red())?;
69 if rt_dir.read_dir().ok().map(|it| it.count()) == Some(0) {
73 if rt_dir.read_dir().ok().map(|it| it.count()) == Some(0) {
70− println!("{}", "Runtime directory is empty.".red());
74+ writeln!(stdout, "{}", "Runtime directory is empty.".red())?;
74−pub fn languages_all() {
80+pub fn languages_all() -> std::io::Result<()> {
81+ let stdout = std::io::stdout();
82+ let mut stdout = stdout.lock();
75− let mut syn_loader_conf = user_syntax_loader().unwrap_or_else(|err| {
84+ let mut syn_loader_conf = match user_syntax_loader() {
87+ let stderr = std::io::stderr();
88+ let mut stderr = stderr.lock();
76− eprintln!("{}: {}", "Error parsing user language config".red(), err);
93+ "Error parsing user language config".red(),
77− eprintln!("{}", "Using default language config".yellow());
96+ writeln!(stderr, "{}", "Using default language config".yellow())?;
78− default_syntax_loader()
97+ default_syntax_loader()
81 let mut headings = vec!["Language", "LSP", "DAP"];
101 let mut headings = vec!["Language", "LSP", "DAP"];
@@ -106,7 +126,7 @@ pub fn languages_all() {
106 for heading in headings {
126 for heading in headings {
107 column(heading, Color::White);
127 column(heading, Color::White);
109− println!();
129+ writeln!(stdout)?;
111 syn_loader_conf
131 syn_loader_conf
112 .language
132 .language
@@ -139,18 +159,34 @@ pub fn languages_all() {
142− println!();
162+ writeln!(stdout)?;
146/// Display diagnostics pertaining to a particular language (LSP,
168/// Display diagnostics pertaining to a particular language (LSP,
147/// highlight queries, etc).
169/// highlight queries, etc).
148−pub fn language(lang_str: String) {
170+pub fn language(lang_str: String) -> std::io::Result<()> {
171+ let stdout = std::io::stdout();
172+ let mut stdout = stdout.lock();
149− let syn_loader_conf = user_syntax_loader().unwrap_or_else(|err| {
174+ let syn_loader_conf = match user_syntax_loader() {
177+ let stderr = std::io::stderr();
178+ let mut stderr = stderr.lock();
150− eprintln!("{}: {}", "Error parsing user language config".red(), err);
183+ "Error parsing user language config".red(),
151− eprintln!("{}", "Using default language config".yellow());
186+ writeln!(stderr, "{}", "Using default language config".yellow())?;
152− default_syntax_loader()
187+ default_syntax_loader()
155 let lang = match syn_loader_conf
191 let lang = match syn_loader_conf
156 .language
192 .language
@@ -160,7 +196,7 @@ pub fn language(lang_str: String) {
160 Some(l) => l,
196 Some(l) => l,
161 None => {
197 None => {
162 let msg = format!("Language '{}' not found", lang_str);
198 let msg = format!("Language '{}' not found", lang_str);
163− println!("{}", msg.red());
199+ writeln!(stdout, "{}", msg.red())?;
164 let suggestions: Vec<&str> = syn_loader_conf
200 let suggestions: Vec<&str> = syn_loader_conf
165 .language
201 .language
@@ -169,9 +205,13 @@ pub fn language(lang_str: String) {
169 .collect();
205 .collect();
170 if !suggestions.is_empty() {
206 if !suggestions.is_empty() {
171 let suggestions = suggestions.join(", ");
207 let suggestions = suggestions.join(", ");
172− println!("Did you mean one of these: {} ?", suggestions.yellow());
210+ "Did you mean one of these: {} ?",
211+ suggestions.yellow()
174− return;
214+ return Ok(());
@@ -180,41 +220,66 @@ pub fn language(lang_str: String) {
180 lang.language_server
220 lang.language_server
181 .as_ref()
221 .as_ref()
182 .map(|lsp| lsp.command.to_string()),
222 .map(|lsp| lsp.command.to_string()),
185 probe_protocol(
225 probe_protocol(
186 "debug adapter",
226 "debug adapter",
187 lang.debugger.as_ref().map(|dap| dap.command.to_string()),
227 lang.debugger.as_ref().map(|dap| dap.command.to_string()),
190 for ts_feat in TsFeature::all() {
230 for ts_feat in TsFeature::all() {
191− probe_treesitter_feature(&lang_str, *ts_feat)
231+ probe_treesitter_feature(&lang_str, *ts_feat)?
195/// Display diagnostics about LSP and DAP.
237/// Display diagnostics about LSP and DAP.
196−fn probe_protocol(protocol_name: &str, server_cmd: Option<String>) {
238+fn probe_protocol(protocol_name: &str, server_cmd: Option<String>) -> std::io::Result<()> {
239+ let stdout = std::io::stdout();
240+ let mut stdout = stdout.lock();
197 let cmd_name = match server_cmd {
242 let cmd_name = match server_cmd {
198 Some(ref cmd) => cmd.as_str().green(),
243 Some(ref cmd) => cmd.as_str().green(),
199 None => "None".yellow(),
244 None => "None".yellow(),
201− println!("Configured {}: {}", protocol_name, cmd_name);
246+ writeln!(stdout, "Configured {}: {}", protocol_name, cmd_name)?;
203 if let Some(cmd) = server_cmd {
248 if let Some(cmd) = server_cmd {
204 let path = match which::which(&cmd) {
249 let path = match which::which(&cmd) {
205 Ok(path) => path.display().to_string().green(),
250 Ok(path) => path.display().to_string().green(),
206 Err(_) => "Not found in $PATH".to_string().red(),
251 Err(_) => "Not found in $PATH".to_string().red(),
208− println!("Binary for {}: {}", protocol_name, path);
253+ writeln!(stdout, "Binary for {}: {}", protocol_name, path)?;
212/// Display diagnostics about a feature that requires tree-sitter
259/// Display diagnostics about a feature that requires tree-sitter
213/// query files (highlights, textobjects, etc).
260/// query files (highlights, textobjects, etc).
214−fn probe_treesitter_feature(lang: &str, feature: TsFeature) {
261+fn probe_treesitter_feature(lang: &str, feature: TsFeature) -> std::io::Result<()> {
262+ let stdout = std::io::stdout();
263+ let mut stdout = stdout.lock();
215 let found = match load_runtime_file(lang, feature.runtime_filename()).is_ok() {
265 let found = match load_runtime_file(lang, feature.runtime_filename()).is_ok() {
216 true => "Found".green(),
266 true => "Found".green(),
217 false => "Not found".red(),
267 false => "Not found".red(),
219− println!("{} queries: {}", feature.short_title(), found);
269+ writeln!(stdout, "{} queries: {}", feature.short_title(), found)?;
274+pub fn print_health(health_arg: Option<String>) -> std::io::Result<()> {
275+ match health_arg.as_deref() {
276+ Some("all") => languages_all()?,
277+ Some(lang) => language(lang.to_string())?,
280+ writeln!(std::io::stdout().lock())?;