2021-11-21 22:55:08 +04:00
|
|
|
use std::{env, error::Error};
|
|
|
|
|
|
|
|
type DynError = Box<dyn Error>;
|
|
|
|
|
|
|
|
pub mod helpers {
|
2022-03-08 09:25:46 +04:00
|
|
|
use std::path::{Path, PathBuf};
|
2021-11-21 22:55:08 +04:00
|
|
|
|
|
|
|
use crate::path;
|
|
|
|
use helix_core::syntax::Configuration as LangConfig;
|
2022-03-08 09:25:46 +04:00
|
|
|
use helix_term::health::TsFeature;
|
2021-11-21 22:55:08 +04:00
|
|
|
|
|
|
|
/// Get the list of languages that support a particular tree-sitter
|
|
|
|
/// based feature.
|
|
|
|
pub fn ts_lang_support(feat: TsFeature) -> Vec<String> {
|
|
|
|
let queries_dir = path::ts_queries();
|
|
|
|
|
|
|
|
find_files(&queries_dir, feat.runtime_filename())
|
|
|
|
.iter()
|
|
|
|
.map(|f| {
|
|
|
|
// .../helix/runtime/queries/python/highlights.scm
|
|
|
|
let tail = f.strip_prefix(&queries_dir).unwrap(); // python/highlights.scm
|
|
|
|
let lang = tail.components().next().unwrap(); // python
|
|
|
|
lang.as_os_str().to_string_lossy().to_string()
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the list of languages that have any form of tree-sitter
|
|
|
|
/// queries defined in the runtime directory.
|
|
|
|
pub fn langs_with_ts_queries() -> Vec<String> {
|
|
|
|
std::fs::read_dir(path::ts_queries())
|
|
|
|
.unwrap()
|
|
|
|
.filter_map(|entry| {
|
|
|
|
let entry = entry.ok()?;
|
|
|
|
entry
|
|
|
|
.file_type()
|
|
|
|
.ok()?
|
|
|
|
.is_dir()
|
|
|
|
.then(|| entry.file_name().to_string_lossy().to_string())
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
// naive implementation, but suffices for our needs
|
|
|
|
pub fn find_files(dir: &Path, filename: &str) -> Vec<PathBuf> {
|
|
|
|
std::fs::read_dir(dir)
|
|
|
|
.unwrap()
|
|
|
|
.filter_map(|entry| {
|
|
|
|
let path = entry.ok()?.path();
|
|
|
|
if path.is_dir() {
|
|
|
|
Some(find_files(&path, filename))
|
|
|
|
} else {
|
|
|
|
(path.file_name()?.to_string_lossy() == filename).then(|| vec![path])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lang_config() -> LangConfig {
|
|
|
|
let bytes = std::fs::read(path::lang_config()).unwrap();
|
|
|
|
toml::from_slice(&bytes).unwrap()
|
|
|
|
}
|
|
|
|
}
|
2021-11-17 17:30:11 +04:00
|
|
|
|
|
|
|
pub mod md_gen {
|
2021-11-21 22:55:08 +04:00
|
|
|
use crate::DynError;
|
|
|
|
|
|
|
|
use crate::helpers;
|
|
|
|
use crate::path;
|
2022-03-04 08:42:47 +04:00
|
|
|
use helix_term::commands::TYPABLE_COMMAND_LIST;
|
2022-03-08 09:25:46 +04:00
|
|
|
use helix_term::health::TsFeature;
|
|
|
|
use std::fs;
|
2021-11-17 17:30:11 +04:00
|
|
|
|
|
|
|
pub const TYPABLE_COMMANDS_MD_OUTPUT: &str = "typable-cmd.md";
|
2021-11-21 22:55:08 +04:00
|
|
|
pub const LANG_SUPPORT_MD_OUTPUT: &str = "lang-support.md";
|
|
|
|
|
|
|
|
fn md_table_heading(cols: &[String]) -> String {
|
|
|
|
let mut header = String::new();
|
|
|
|
header += &md_table_row(cols);
|
|
|
|
header += &md_table_row(&vec!["---".to_string(); cols.len()]);
|
|
|
|
header
|
|
|
|
}
|
|
|
|
|
|
|
|
fn md_table_row(cols: &[String]) -> String {
|
2022-02-07 09:03:04 +04:00
|
|
|
format!("| {} |\n", cols.join(" | "))
|
2021-11-21 22:55:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn md_mono(s: &str) -> String {
|
|
|
|
format!("`{}`", s)
|
|
|
|
}
|
2021-11-17 17:30:11 +04:00
|
|
|
|
2021-11-21 22:55:08 +04:00
|
|
|
pub fn typable_commands() -> Result<String, DynError> {
|
2021-11-17 17:30:11 +04:00
|
|
|
let mut md = String::new();
|
2021-11-21 22:55:08 +04:00
|
|
|
md.push_str(&md_table_heading(&[
|
|
|
|
"Name".to_owned(),
|
|
|
|
"Description".to_owned(),
|
|
|
|
]));
|
2021-11-17 17:30:11 +04:00
|
|
|
|
|
|
|
let cmdify = |s: &str| format!("`:{}`", s);
|
|
|
|
|
|
|
|
for cmd in TYPABLE_COMMAND_LIST {
|
|
|
|
let names = std::iter::once(&cmd.name)
|
|
|
|
.chain(cmd.aliases.iter())
|
|
|
|
.map(|a| cmdify(a))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", ");
|
|
|
|
|
2022-05-04 06:17:08 +04:00
|
|
|
let doc = cmd.doc.replace("\n", "<br>");
|
|
|
|
|
|
|
|
md.push_str(&md_table_row(&[names.to_owned(), doc.to_owned()]));
|
2021-11-21 22:55:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(md)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lang_features() -> Result<String, DynError> {
|
|
|
|
let mut md = String::new();
|
2022-03-08 09:25:46 +04:00
|
|
|
let ts_features = TsFeature::all();
|
2021-11-21 22:55:08 +04:00
|
|
|
|
|
|
|
let mut cols = vec!["Language".to_owned()];
|
|
|
|
cols.append(
|
|
|
|
&mut ts_features
|
|
|
|
.iter()
|
2022-03-08 09:25:46 +04:00
|
|
|
.map(|t| t.long_title().to_string())
|
2021-11-21 22:55:08 +04:00
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
);
|
|
|
|
cols.push("Default LSP".to_owned());
|
|
|
|
|
|
|
|
md.push_str(&md_table_heading(&cols));
|
|
|
|
let config = helpers::lang_config();
|
|
|
|
|
|
|
|
let mut langs = config
|
|
|
|
.language
|
|
|
|
.iter()
|
|
|
|
.map(|l| l.language_id.clone())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
langs.sort_unstable();
|
|
|
|
|
|
|
|
let mut ts_features_to_langs = Vec::new();
|
|
|
|
for &feat in ts_features {
|
|
|
|
ts_features_to_langs.push((feat, helpers::ts_lang_support(feat)));
|
2021-11-17 17:30:11 +04:00
|
|
|
}
|
|
|
|
|
2021-11-21 22:55:08 +04:00
|
|
|
let mut row = Vec::new();
|
|
|
|
for lang in langs {
|
|
|
|
let lc = config
|
|
|
|
.language
|
|
|
|
.iter()
|
|
|
|
.find(|l| l.language_id == lang)
|
|
|
|
.unwrap(); // lang comes from config
|
2021-12-07 19:57:21 +04:00
|
|
|
row.push(lc.language_id.clone());
|
2021-11-21 22:55:08 +04:00
|
|
|
|
|
|
|
for (_feat, support_list) in &ts_features_to_langs {
|
|
|
|
row.push(
|
|
|
|
if support_list.contains(&lang) {
|
|
|
|
"✓"
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
}
|
|
|
|
.to_owned(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
row.push(
|
|
|
|
lc.language_server
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| s.command.clone())
|
|
|
|
.map(|c| md_mono(&c))
|
|
|
|
.unwrap_or_default(),
|
|
|
|
);
|
|
|
|
|
|
|
|
md.push_str(&md_table_row(&row));
|
|
|
|
row.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(md)
|
2021-11-17 17:30:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write(filename: &str, data: &str) {
|
|
|
|
let error = format!("Could not write to {}", filename);
|
|
|
|
let path = path::book_gen().join(filename);
|
|
|
|
fs::write(path, data).expect(&error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod path {
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
pub fn project_root() -> PathBuf {
|
|
|
|
Path::new(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.to_path_buf()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn book_gen() -> PathBuf {
|
|
|
|
project_root().join("book/src/generated/")
|
|
|
|
}
|
2021-11-21 22:55:08 +04:00
|
|
|
|
|
|
|
pub fn ts_queries() -> PathBuf {
|
|
|
|
project_root().join("runtime/queries")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lang_config() -> PathBuf {
|
|
|
|
project_root().join("languages.toml")
|
|
|
|
}
|
2021-11-17 17:30:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub mod tasks {
|
2021-11-21 22:55:08 +04:00
|
|
|
use crate::md_gen;
|
|
|
|
use crate::DynError;
|
2021-11-17 17:30:11 +04:00
|
|
|
|
2021-11-21 22:55:08 +04:00
|
|
|
pub fn docgen() -> Result<(), DynError> {
|
|
|
|
use md_gen::*;
|
|
|
|
write(TYPABLE_COMMANDS_MD_OUTPUT, &typable_commands()?);
|
|
|
|
write(LANG_SUPPORT_MD_OUTPUT, &lang_features()?);
|
|
|
|
Ok(())
|
2021-11-17 17:30:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_help() {
|
|
|
|
println!(
|
|
|
|
"
|
2021-11-21 22:55:08 +04:00
|
|
|
Usage: Run with `cargo xtask <task>`, eg. `cargo xtask docgen`.
|
2021-11-17 17:30:11 +04:00
|
|
|
|
|
|
|
Tasks:
|
2021-11-21 22:55:08 +04:00
|
|
|
docgen: Generate files to be included in the mdbook output.
|
2021-11-17 17:30:11 +04:00
|
|
|
"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 22:55:08 +04:00
|
|
|
fn main() -> Result<(), DynError> {
|
2021-11-17 17:30:11 +04:00
|
|
|
let task = env::args().nth(1);
|
|
|
|
match task {
|
|
|
|
None => tasks::print_help(),
|
|
|
|
Some(t) => match t.as_str() {
|
2021-11-21 22:55:08 +04:00
|
|
|
"docgen" => tasks::docgen()?,
|
|
|
|
invalid => return Err(format!("Invalid task name: {}", invalid).into()),
|
2021-11-17 17:30:11 +04:00
|
|
|
},
|
|
|
|
};
|
|
|
|
Ok(())
|
|
|
|
}
|