aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/codegen
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-05-31 00:54:54 +0100
committerAleksey Kladov <[email protected]>2020-05-31 00:54:54 +0100
commitc8f27a4a886413a15a2a6af4a87b39b901c873a8 (patch)
treeae4f7c31fa30581ef09b3c29d2673dee922e5845 /xtask/src/codegen
parent383247a9ae8202f20ce6f01d1429c1cd2a11d516 (diff)
Generate features docs from source
Diffstat (limited to 'xtask/src/codegen')
-rw-r--r--xtask/src/codegen/gen_feature_docs.rs72
1 files changed, 72 insertions, 0 deletions
diff --git a/xtask/src/codegen/gen_feature_docs.rs b/xtask/src/codegen/gen_feature_docs.rs
new file mode 100644
index 000000000..583185648
--- /dev/null
+++ b/xtask/src/codegen/gen_feature_docs.rs
@@ -0,0 +1,72 @@
1//! Generates `assists.md` documentation.
2
3use std::{fmt, fs, path::PathBuf};
4
5use crate::{
6 codegen::{self, extract_comment_blocks_with_empty_lines, Mode},
7 project_root, rust_files, Result,
8};
9
10pub 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
14 let dst = project_root().join("docs/user/generated_features.adoc");
15 codegen::update(&dst, &contents, mode)?;
16 Ok(())
17}
18
19#[derive(Debug)]
20struct Feature {
21 id: String,
22 path: PathBuf,
23 doc: String,
24}
25
26impl Feature {
27 fn collect() -> Result<Vec<Feature>> {
28 let mut res = Vec::new();
29 for path in rust_files(&project_root()) {
30 collect_file(&mut res, path)?;
31 }
32 res.sort_by(|lhs, rhs| lhs.id.cmp(&rhs.id));
33 return Ok(res);
34
35 fn collect_file(acc: &mut Vec<Feature>, path: PathBuf) -> Result<()> {
36 let text = fs::read_to_string(&path)?;
37 let comment_blocks = extract_comment_blocks_with_empty_lines("Feature", &text);
38
39 for block in comment_blocks {
40 let id = block.id;
41 assert!(
42 id.split_ascii_whitespace().all(|it| it.starts_with(char::is_uppercase)),
43 "bad feature: {}",
44 id
45 );
46 let doc = block.contents.join("\n");
47 acc.push(Feature { id, path: path.clone(), doc })
48 }
49
50 Ok(())
51 }
52 }
53}
54
55impl fmt::Display for Feature {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 writeln!(f, "=== {}", self.id)?;
58 let path = self.path.strip_prefix(&project_root()).unwrap();
59 let name = self.path.file_name().unwrap();
60
61 //FIXME: generate line number as well
62 writeln!(
63 f,
64 "**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/{}[{}]",
65 path.display(),
66 name.to_str().unwrap(),
67 )?;
68
69 writeln!(f, "\n{}", self.doc)?;
70 Ok(())
71 }
72}