1use std::{env, error::Error};
3type DynError = Box<dyn Error>;
12 use helix_core::syntax::Configuration as LangConfig;
14 #[derive(Copy, Clone)]
22 pub fn all() -> &'static [Self] {
23 &[Self::Highlight, Self::TextObjects, Self::AutoIndent]
26 pub fn runtime_filename(&self) -> &'static str {
28 Self::Highlight => "highlights.scm",
29 Self::TextObjects => "textobjects.scm",
30 Self::AutoIndent => "indents.toml",
35 impl Display for TsFeature {
36 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41 Self::Highlight => "Syntax Highlighting",
42 Self::TextObjects => "Treesitter Textobjects",
43 Self::AutoIndent => "Auto Indent",
49 /// Get the list of languages that support a particular tree-sitter
51 pub fn ts_lang_support(feat: TsFeature) -> Vec<String> {
52 let queries_dir = path::ts_queries();
54 find_files(&queries_dir, feat.runtime_filename())
57 // .../helix/runtime/queries/python/highlights.scm
58 let tail = f.strip_prefix(&queries_dir).unwrap(); // python/highlights.scm
59 let lang = tail.components().next().unwrap(); // python
60 lang.as_os_str().to_string_lossy().to_string()
65 /// Get the list of languages that have any form of tree-sitter
66 /// queries defined in the runtime directory.
67 pub fn langs_with_ts_queries() -> Vec<String> {
68 std::fs::read_dir(path::ts_queries())
71 let entry = entry.ok()?;
76 .then(|| entry.file_name().to_string_lossy().to_string())
81 // naive implementation, but suffices for our needs
82 pub fn find_files(dir: &Path, filename: &str) -> Vec<PathBuf> {
83 std::fs::read_dir(dir)
86 let path = entry.ok()?.path();
88 Some(find_files(&path, filename))
90 (path.file_name()?.to_string_lossy() == filename).then(|| vec![path])
97 pub fn lang_config() -> LangConfig {
98 let bytes = std::fs::read(path::lang_config()).unwrap();
99 toml::from_slice(&bytes).unwrap()
110 use helix_term::commands::cmd::TYPABLE_COMMAND_LIST;
112 pub const TYPABLE_COMMANDS_MD_OUTPUT: &str = "typable-cmd.md";
113 pub const LANG_SUPPORT_MD_OUTPUT: &str = "lang-support.md";
115 fn md_table_heading(cols: &[String]) -> String {
116 let mut header = String::new();
117 header += &md_table_row(cols);
118 header += &md_table_row(&vec!["---".to_string(); cols.len()]);
122 fn md_table_row(cols: &[String]) -> String {
123 "| ".to_owned() + &cols.join(" | ") + " |\n"
126 fn md_mono(s: &str) -> String {
130 pub fn typable_commands() -> Result<String, DynError> {
131 let mut md = String::new();
132 md.push_str(&md_table_heading(&[
134 "Description".to_owned(),
137 let cmdify = |s: &str| format!("`:{}`", s);
139 for cmd in TYPABLE_COMMAND_LIST {
140 let names = std::iter::once(&cmd.name)
141 .chain(cmd.aliases.iter())
146 md.push_str(&md_table_row(&[names.to_owned(), cmd.doc.to_owned()]));
152 pub fn lang_features() -> Result<String, DynError> {
153 let mut md = String::new();
154 let ts_features = helpers::TsFeature::all();
156 let mut cols = vec!["Language".to_owned()];
160 .map(|t| t.to_string())
161 .collect::<Vec<_>>(),
163 cols.push("Default LSP".to_owned());
165 md.push_str(&md_table_heading(&cols));
166 let config = helpers::lang_config();
168 let mut langs = config
171 .map(|l| l.language_id.clone())
172 .collect::<Vec<_>>();
173 langs.sort_unstable();
175 let mut ts_features_to_langs = Vec::new();
176 for &feat in ts_features {
177 ts_features_to_langs.push((feat, helpers::ts_lang_support(feat)));
180 let mut row = Vec::new();
185 .find(|l| l.language_id == lang)
186 .unwrap(); // lang comes from config
187 row.push(lc.language_id.clone());
189 for (_feat, support_list) in &ts_features_to_langs {
191 if support_list.contains(&lang) {
202 .map(|s| s.command.clone())
203 .map(|c| md_mono(&c))
204 .unwrap_or_default(),
207 md.push_str(&md_table_row(&row));
214 pub fn write(filename: &str, data: &str) {
215 let error = format!("Could not write to {}", filename);
216 let path = path::book_gen().join(filename);
217 fs::write(path, data).expect(&error);
222 use std::path::{Path, PathBuf};
224 pub fn project_root() -> PathBuf {
225 Path::new(env!("CARGO_MANIFEST_DIR"))
231 pub fn book_gen() -> PathBuf {
232 project_root().join("book/src/generated/")
235 pub fn ts_queries() -> PathBuf {
236 project_root().join("runtime/queries")
239 pub fn lang_config() -> PathBuf {
240 project_root().join("languages.toml")
248 pub fn docgen() -> Result<(), DynError> {
250 write(TYPABLE_COMMANDS_MD_OUTPUT, &typable_commands()?);
251 write(LANG_SUPPORT_MD_OUTPUT, &lang_features()?);
255 pub fn print_help() {
258Usage: Run with `cargo xtask <task>`, eg. `cargo xtask docgen`.
261 docgen: Generate files to be included in the mdbook output.
267fn main() -> Result<(), DynError> {
268 let task = env::args().nth(1);
270 None => tasks::print_help(),
271 Some(t) => match t.as_str() {
272 "docgen" => tasks::docgen()?,
273 invalid => return Err(format!("Invalid task name: {}", invalid).into()),
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list