| 1 |
use crate::helpers; |
| 2 |
use crate::path; |
| 3 |
use crate::DynError; |
| 4 |
|
| 5 |
use helix_term::commands::MappableCommand; |
| 6 |
use helix_term::commands::TYPABLE_COMMAND_LIST; |
| 7 |
use helix_term::health::TsFeature; |
| 8 |
use helix_view::document::Mode; |
| 9 |
|
| 10 |
use std::collections::HashSet; |
| 11 |
use std::fs; |
| 12 |
|
| 13 |
pub const TYPABLE_COMMANDS_MD_OUTPUT: &str = "typable-cmd.md"; |
| 14 |
pub const STATIC_COMMANDS_MD_OUTPUT: &str = "static-cmd.md"; |
| 15 |
pub const LANG_SUPPORT_MD_OUTPUT: &str = "lang-support.md"; |
| 16 |
|
| 17 |
fn md_table_heading(cols: &[String]) -> String { |
| 18 |
let mut header = String::new(); |
| 19 |
header += &md_table_row(cols); |
| 20 |
header += &md_table_row(&vec!["---".to_string(); cols.len()]); |
| 21 |
header |
| 22 |
} |
| 23 |
|
| 24 |
fn md_table_row(cols: &[String]) -> String { |
| 25 |
format!("| {} |\n", cols.join(" | ")) |
| 26 |
} |
| 27 |
|
| 28 |
fn md_mono(s: &str) -> String { |
| 29 |
format!("`{}`", s) |
| 30 |
} |
| 31 |
|
| 32 |
pub fn typable_commands() -> Result<String, DynError> { |
| 33 |
let mut md = String::new(); |
| 34 |
md.push_str(&md_table_heading(&[ |
| 35 |
"Name".to_owned(), |
| 36 |
"Description".to_owned(), |
| 37 |
])); |
| 38 |
|
| 39 |
|
| 40 |
let cmdify = |s: &str| format!("`:{}`", s.replace('|', "\\|")); |
| 41 |
|
| 42 |
for cmd in TYPABLE_COMMAND_LIST { |
| 43 |
let names = std::iter::once(&cmd.name) |
| 44 |
.chain(cmd.aliases.iter()) |
| 45 |
.map(|a| cmdify(a)) |
| 46 |
.collect::<Vec<_>>() |
| 47 |
.join(", "); |
| 48 |
|
| 49 |
let doc = cmd.doc.replace('\n', "<br>"); |
| 50 |
|
| 51 |
md.push_str(&md_table_row(&[names.to_owned(), doc.to_owned()])); |
| 52 |
} |
| 53 |
|
| 54 |
Ok(md) |
| 55 |
} |
| 56 |
|
| 57 |
pub fn static_commands() -> Result<String, DynError> { |
| 58 |
let mut md = String::new(); |
| 59 |
let keymap = helix_term::keymap::default(); |
| 60 |
let keymaps = [ |
| 61 |
("normal", keymap[&Mode::Normal].reverse_map()), |
| 62 |
("select", keymap[&Mode::Select].reverse_map()), |
| 63 |
("insert", keymap[&Mode::Insert].reverse_map()), |
| 64 |
]; |
| 65 |
|
| 66 |
md.push_str(&md_table_heading(&[ |
| 67 |
"Name".to_owned(), |
| 68 |
"Description".to_owned(), |
| 69 |
"Default keybinds".to_owned(), |
| 70 |
])); |
| 71 |
|
| 72 |
for cmd in MappableCommand::STATIC_COMMAND_LIST { |
| 73 |
let keymap_strings: Vec<_> = keymaps |
| 74 |
.iter() |
| 75 |
.map(|(mode, keymap)| { |
| 76 |
let bindings = keymap |
| 77 |
.get(cmd.name()) |
| 78 |
.map(|bindings| { |
| 79 |
let mut bind_strings: Vec<_> = bindings |
| 80 |
.iter() |
| 81 |
.map(|bind| { |
| 82 |
let keys = &bind |
| 83 |
.iter() |
| 84 |
.map(|key| key.key_sequence_format()) |
| 85 |
.collect::<String>() |
| 86 |
|
| 87 |
.replace('|', "\\|"); |
| 88 |
format!("`` {} ``", keys) |
| 89 |
}) |
| 90 |
.collect(); |
| 91 |
|
| 92 |
|
| 93 |
bind_strings.sort_by_key(|s| (s.len(), s.to_owned())); |
| 94 |
bind_strings.join(", ") |
| 95 |
}) |
| 96 |
.unwrap_or_default(); |
| 97 |
|
| 98 |
(mode, bindings) |
| 99 |
}) |
| 100 |
.collect(); |
| 101 |
|
| 102 |
let keymap_string = keymap_strings |
| 103 |
.iter() |
| 104 |
.filter(|(_, bindings)| !bindings.is_empty()) |
| 105 |
.map(|(mode, bindings)| format!("{}: {}", mode, bindings)) |
| 106 |
.collect::<Vec<_>>() |
| 107 |
.join(", "); |
| 108 |
|
| 109 |
md.push_str(&md_table_row(&[ |
| 110 |
md_mono(cmd.name()), |
| 111 |
cmd.doc().to_owned(), |
| 112 |
keymap_string, |
| 113 |
])); |
| 114 |
} |
| 115 |
|
| 116 |
Ok(md) |
| 117 |
} |
| 118 |
|
| 119 |
pub fn lang_features() -> Result<String, DynError> { |
| 120 |
let mut md = String::new(); |
| 121 |
let ts_features = TsFeature::all(); |
| 122 |
|
| 123 |
let mut cols = vec!["Language".to_owned()]; |
| 124 |
cols.append( |
| 125 |
&mut ts_features |
| 126 |
.iter() |
| 127 |
.map(|t| t.long_title().to_string()) |
| 128 |
.collect::<Vec<_>>(), |
| 129 |
); |
| 130 |
cols.push("Default language servers".to_owned()); |
| 131 |
|
| 132 |
md.push_str(&md_table_heading(&cols)); |
| 133 |
let config = helix_core::config::default_lang_config(); |
| 134 |
|
| 135 |
let mut langs = config |
| 136 |
.language |
| 137 |
.iter() |
| 138 |
.map(|l| l.language_id.clone()) |
| 139 |
.collect::<Vec<_>>(); |
| 140 |
langs.sort_unstable(); |
| 141 |
|
| 142 |
let mut ts_features_to_langs = Vec::new(); |
| 143 |
for &feat in ts_features { |
| 144 |
ts_features_to_langs.push((feat, helpers::ts_lang_support(feat))); |
| 145 |
} |
| 146 |
|
| 147 |
let mut row = Vec::new(); |
| 148 |
for lang in langs { |
| 149 |
let lc = config |
| 150 |
.language |
| 151 |
.iter() |
| 152 |
.find(|l| l.language_id == lang) |
| 153 |
.unwrap(); |
| 154 |
row.push(lc.language_id.clone()); |
| 155 |
|
| 156 |
for (_feat, support_list) in &ts_features_to_langs { |
| 157 |
row.push( |
| 158 |
if support_list.contains(&lang) { |
| 159 |
"✓" |
| 160 |
} else { |
| 161 |
"" |
| 162 |
} |
| 163 |
.to_owned(), |
| 164 |
); |
| 165 |
} |
| 166 |
let mut seen_commands = HashSet::new(); |
| 167 |
let mut commands = String::new(); |
| 168 |
for ls_config in lc |
| 169 |
.language_servers |
| 170 |
.iter() |
| 171 |
.filter_map(|ls| config.language_server.get(&ls.name)) |
| 172 |
{ |
| 173 |
let command = &ls_config.command; |
| 174 |
if !seen_commands.insert(command) { |
| 175 |
continue; |
| 176 |
} |
| 177 |
|
| 178 |
if !commands.is_empty() { |
| 179 |
commands.push_str(", "); |
| 180 |
} |
| 181 |
|
| 182 |
commands.push_str(&md_mono(command)); |
| 183 |
} |
| 184 |
row.push(commands); |
| 185 |
|
| 186 |
md.push_str(&md_table_row(&row)); |
| 187 |
row.clear(); |
| 188 |
} |
| 189 |
|
| 190 |
Ok(md) |
| 191 |
} |
| 192 |
|
| 193 |
pub fn write(filename: &str, data: &str) { |
| 194 |
let error = format!("Could not write to {}", filename); |
| 195 |
let path = path::book_gen().join(filename); |
| 196 |
fs::write(path, data).expect(&error); |
| 197 |
} |