2020-05-30 18:54:54 -05:00
|
|
|
//! Generates `assists.md` documentation.
|
|
|
|
|
2021-07-03 14:11:03 -05:00
|
|
|
use std::{fmt, fs, io, path::PathBuf};
|
2020-05-30 18:54:54 -05:00
|
|
|
|
2021-07-03 14:11:03 -05:00
|
|
|
#[test]
|
|
|
|
fn sourcegen_feature_docs() {
|
|
|
|
let features = Feature::collect().unwrap();
|
2020-05-30 18:54:54 -05:00
|
|
|
let contents = features.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n");
|
2021-07-03 14:11:03 -05:00
|
|
|
let contents = format!(
|
|
|
|
"
|
|
|
|
// Generated file, do not edit by hand, see `sourcegen_feature_docs`.
|
|
|
|
{}
|
|
|
|
",
|
|
|
|
contents.trim()
|
|
|
|
);
|
|
|
|
let dst = sourcegen::project_root().join("docs/user/generated_features.adoc");
|
2023-01-09 12:36:22 -06:00
|
|
|
fs::write(dst, contents).unwrap();
|
2020-05-30 18:54:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Feature {
|
|
|
|
id: String,
|
2021-07-03 14:11:03 -05:00
|
|
|
location: sourcegen::Location,
|
2020-05-30 18:54:54 -05:00
|
|
|
doc: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Feature {
|
2021-07-03 14:11:03 -05:00
|
|
|
fn collect() -> io::Result<Vec<Feature>> {
|
|
|
|
let crates_dir = sourcegen::project_root().join("crates");
|
|
|
|
|
2020-05-30 18:54:54 -05:00
|
|
|
let mut res = Vec::new();
|
2021-07-03 14:11:03 -05:00
|
|
|
for path in sourcegen::list_rust_files(&crates_dir) {
|
2020-05-30 18:54:54 -05:00
|
|
|
collect_file(&mut res, path)?;
|
|
|
|
}
|
|
|
|
res.sort_by(|lhs, rhs| lhs.id.cmp(&rhs.id));
|
|
|
|
return Ok(res);
|
|
|
|
|
2021-07-03 14:11:03 -05:00
|
|
|
fn collect_file(acc: &mut Vec<Feature>, path: PathBuf) -> io::Result<()> {
|
|
|
|
let text = std::fs::read_to_string(&path)?;
|
|
|
|
let comment_blocks = sourcegen::CommentBlock::extract("Feature", &text);
|
2020-05-30 18:54:54 -05:00
|
|
|
|
|
|
|
for block in comment_blocks {
|
|
|
|
let id = block.id;
|
2020-10-05 13:22:44 -05:00
|
|
|
if let Err(msg) = is_valid_feature_name(&id) {
|
2023-01-09 12:36:22 -06:00
|
|
|
panic!("invalid feature name: {id:?}:\n {msg}")
|
2020-10-05 13:22:44 -05:00
|
|
|
}
|
2020-05-30 18:54:54 -05:00
|
|
|
let doc = block.contents.join("\n");
|
2021-07-03 14:11:03 -05:00
|
|
|
let location = sourcegen::Location { file: path.clone(), line: block.line };
|
2020-05-31 08:36:20 -05:00
|
|
|
acc.push(Feature { id, location, doc })
|
2020-05-30 18:54:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 13:22:44 -05:00
|
|
|
fn is_valid_feature_name(feature: &str) -> Result<(), String> {
|
2020-05-31 02:45:41 -05:00
|
|
|
'word: for word in feature.split_whitespace() {
|
2021-10-22 01:23:29 -05:00
|
|
|
for short in ["to", "and"] {
|
2020-05-31 02:45:41 -05:00
|
|
|
if word == short {
|
|
|
|
continue 'word;
|
|
|
|
}
|
|
|
|
}
|
2021-10-22 01:23:29 -05:00
|
|
|
for short in ["To", "And"] {
|
2020-05-31 02:45:41 -05:00
|
|
|
if word == short {
|
2023-01-09 12:36:22 -06:00
|
|
|
return Err(format!("Don't capitalize {word:?}"));
|
2020-05-31 02:45:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !word.starts_with(char::is_uppercase) {
|
2023-01-09 12:36:22 -06:00
|
|
|
return Err(format!("Capitalize {word:?}"));
|
2020-05-31 02:45:41 -05:00
|
|
|
}
|
|
|
|
}
|
2020-10-05 13:22:44 -05:00
|
|
|
Ok(())
|
2020-05-31 02:45:41 -05:00
|
|
|
}
|
|
|
|
|
2020-05-30 18:54:54 -05:00
|
|
|
impl fmt::Display for Feature {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-05-31 08:02:12 -05:00
|
|
|
writeln!(f, "=== {}\n**Source:** {}\n{}", self.id, self.location, self.doc)
|
2020-05-30 18:54:54 -05:00
|
|
|
}
|
|
|
|
}
|