on 376c19e help sign in
helix/xtask/src/docgen.rs
117 lines 3.0 kB history raw
1use crate::helpers;
2use crate::path;
3use crate::DynError;
5use helix_term::commands::TYPABLE_COMMAND_LIST;
6use helix_term::health::TsFeature;
7use std::fs;
9pub const TYPABLE_COMMANDS_MD_OUTPUT: &str = "typable-cmd.md";
10pub const LANG_SUPPORT_MD_OUTPUT: &str = "lang-support.md";
12fn md_table_heading(cols: &[String]) -> String {
13 let mut header = String::new();
14 header += &md_table_row(cols);
15 header += &md_table_row(&vec!["---".to_string(); cols.len()]);
16 header
19fn md_table_row(cols: &[String]) -> String {
20 format!("| {} |\n", cols.join(" | "))
23fn md_mono(s: &str) -> String {
24 format!("`{}`", s)
27pub fn typable_commands() -> Result<String, DynError> {
28 let mut md = String::new();
29 md.push_str(&md_table_heading(&[
30 "Name".to_owned(),
31 "Description".to_owned(),
32 ]));
34 let cmdify = |s: &str| format!("`:{}`", s);
36 for cmd in TYPABLE_COMMAND_LIST {
37 let names = std::iter::once(&cmd.name)
38 .chain(cmd.aliases.iter())
39 .map(|a| cmdify(a))
40 .collect::<Vec<_>>()
41 .join(", ");
43 let doc = cmd.doc.replace('\n', "<br>");
45 md.push_str(&md_table_row(&[names.to_owned(), doc.to_owned()]));
46 }
48 Ok(md)
51pub fn lang_features() -> Result<String, DynError> {
52 let mut md = String::new();
53 let ts_features = TsFeature::all();
55 let mut cols = vec!["Language".to_owned()];
56 cols.append(
57 &mut ts_features
58 .iter()
59 .map(|t| t.long_title().to_string())
60 .collect::<Vec<_>>(),
61 );
62 cols.push("Default LSP".to_owned());
64 md.push_str(&md_table_heading(&cols));
65 let config = helpers::lang_config();
67 let mut langs = config
68 .language
69 .iter()
70 .map(|l| l.language_id.clone())
71 .collect::<Vec<_>>();
72 langs.sort_unstable();
74 let mut ts_features_to_langs = Vec::new();
75 for &feat in ts_features {
76 ts_features_to_langs.push((feat, helpers::ts_lang_support(feat)));
77 }
79 let mut row = Vec::new();
80 for lang in langs {
81 let lc = config
82 .language
83 .iter()
84 .find(|l| l.language_id == lang)
85 .unwrap(); // lang comes from config
86 row.push(lc.language_id.clone());
88 for (_feat, support_list) in &ts_features_to_langs {
89 row.push(
90 if support_list.contains(&lang) {
91 "✓"
92 } else {
93 ""
94 }
95 .to_owned(),
96 );
97 }
98 row.push(
99 lc.language_server
100 .as_ref()
101 .map(|s| s.command.clone())
102 .map(|c| md_mono(&c))
103 .unwrap_or_default(),
104 );
106 md.push_str(&md_table_row(&row));
107 row.clear();
108 }
110 Ok(md)
113pub fn write(filename: &str, data: &str) {
114 let error = format!("Could not write to {}", filename);
115 let path = path::book_gen().join(filename);
116 fs::write(path, data).expect(&error);