aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/codegen/gen_assists_docs.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-10-25 12:16:46 +0100
committerAleksey Kladov <[email protected]>2019-10-25 12:47:48 +0100
commit0dd35ff2b2ceffdb926953fdacc7d30e1968047d (patch)
treea8bcab00ef1c434e56845034f22a5595d119dea7 /xtask/src/codegen/gen_assists_docs.rs
parent518f99e16b993e3414a81181c8bad7a89e590ece (diff)
auto-generate assists docs and tests
Diffstat (limited to 'xtask/src/codegen/gen_assists_docs.rs')
-rw-r--r--xtask/src/codegen/gen_assists_docs.rs123
1 files changed, 123 insertions, 0 deletions
diff --git a/xtask/src/codegen/gen_assists_docs.rs b/xtask/src/codegen/gen_assists_docs.rs
new file mode 100644
index 000000000..654ae09d6
--- /dev/null
+++ b/xtask/src/codegen/gen_assists_docs.rs
@@ -0,0 +1,123 @@
1use std::{fs, path::Path};
2
3use crate::{
4 codegen::{self, extract_comment_blocks, Mode},
5 project_root, Result,
6};
7
8pub fn generate_assists_docs(mode: Mode) -> Result<()> {
9 let assists = collect_assists()?;
10 generate_tests(&assists, mode)?;
11 generate_docs(&assists, mode)?;
12 Ok(())
13}
14
15#[derive(Debug)]
16struct Assist {
17 id: String,
18 doc: String,
19 before: String,
20 after: String,
21}
22
23fn collect_assists() -> Result<Vec<Assist>> {
24 let mut res = Vec::new();
25 for entry in fs::read_dir(project_root().join(codegen::ASSISTS_DIR))? {
26 let entry = entry?;
27 let path = entry.path();
28 if path.is_file() {
29 collect_file(&mut res, path.as_path())?;
30 }
31 }
32 res.sort_by(|lhs, rhs| lhs.id.cmp(&rhs.id));
33 return Ok(res);
34
35 fn collect_file(acc: &mut Vec<Assist>, path: &Path) -> Result<()> {
36 let text = fs::read_to_string(path)?;
37 let comment_blocks = extract_comment_blocks(&text);
38
39 for block in comment_blocks {
40 // FIXME: doesn't support blank lines yet, need to tweak
41 // `extract_comment_blocks` for that.
42 let mut lines = block.iter();
43 let first_line = lines.next().unwrap();
44 if !first_line.starts_with("Assist: ") {
45 continue;
46 }
47 let id = first_line["Assist: ".len()..].to_string();
48 assert!(id.chars().all(|it| it.is_ascii_lowercase() || it == '_'));
49
50 let doc = take_until(lines.by_ref(), "```");
51 let before = take_until(lines.by_ref(), "```");
52
53 assert_eq!(lines.next().unwrap().as_str(), "->");
54 assert_eq!(lines.next().unwrap().as_str(), "```");
55 let after = take_until(lines.by_ref(), "```");
56 acc.push(Assist { id, doc, before, after })
57 }
58
59 fn take_until<'a>(lines: impl Iterator<Item = &'a String>, marker: &str) -> String {
60 let mut buf = Vec::new();
61 for line in lines {
62 if line == marker {
63 break;
64 }
65 buf.push(line.clone());
66 }
67 buf.join("\n")
68 }
69 Ok(())
70 }
71}
72
73fn generate_tests(assists: &[Assist], mode: Mode) -> Result<()> {
74 let mut buf = String::from("use super::check;\n");
75
76 for assist in assists.iter() {
77 let test = format!(
78 r######"
79#[test]
80fn doctest_{}() {{
81 check(
82 "{}",
83r#####"
84{}
85"#####, r#####"
86{}
87"#####)
88}}
89"######,
90 assist.id, assist.id, assist.before, assist.after
91 );
92
93 buf.push_str(&test)
94 }
95 let buf = codegen::reformat(buf)?;
96 codegen::update(&project_root().join(codegen::ASSISTS_TESTS), &buf, mode)
97}
98
99fn generate_docs(assists: &[Assist], mode: Mode) -> Result<()> {
100 let mut buf = String::from("# Assists\n");
101
102 for assist in assists {
103 let docs = format!(
104 "
105## `{}`
106
107{}
108
109```rust
110// BEFORE
111{}
112
113// AFTER
114{}
115```
116",
117 assist.id, assist.doc, assist.before, assist.after
118 );
119 buf.push_str(&docs);
120 }
121
122 codegen::update(&project_root().join(codegen::ASSISTS_DOCS), &buf, mode)
123}