1 //! Generates `assists.md` documentation.
3 use std::{fmt, path::PathBuf};
6 codegen::{self, extract_comment_blocks_with_empty_lines, Location, Mode, PREAMBLE},
7 project_root, rust_files, Result,
10 pub fn generate_feature_docs(mode: Mode) -> Result<()> {
11 let features = Feature::collect()?;
12 let contents = features.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n");
13 let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim());
14 let dst = project_root().join("docs/user/generated_features.adoc");
15 codegen::update(&dst, &contents, mode)?;
27 fn collect() -> Result<Vec<Feature>> {
28 let mut res = Vec::new();
29 for path in rust_files() {
30 collect_file(&mut res, path)?;
32 res.sort_by(|lhs, rhs| lhs.id.cmp(&rhs.id));
35 fn collect_file(acc: &mut Vec<Feature>, path: PathBuf) -> Result<()> {
36 let text = xshell::read_file(&path)?;
37 let comment_blocks = extract_comment_blocks_with_empty_lines("Feature", &text);
39 for block in comment_blocks {
41 if let Err(msg) = is_valid_feature_name(&id) {
42 panic!("invalid feature name: {:?}:\n {}", id, msg)
44 let doc = block.contents.join("\n");
45 let location = Location::new(path.clone(), block.line);
46 acc.push(Feature { id, location, doc })
54 fn is_valid_feature_name(feature: &str) -> Result<(), String> {
55 'word: for word in feature.split_whitespace() {
56 for &short in ["to", "and"].iter() {
61 for &short in ["To", "And"].iter() {
63 return Err(format!("Don't capitalize {:?}", word));
66 if !word.starts_with(char::is_uppercase) {
67 return Err(format!("Capitalize {:?}", word));
73 impl fmt::Display for Feature {
74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75 writeln!(f, "=== {}\n**Source:** {}\n{}", self.id, self.location, self.doc)