aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/codegen/gen_assists_docs.rs
diff options
context:
space:
mode:
Diffstat (limited to 'xtask/src/codegen/gen_assists_docs.rs')
-rw-r--r--xtask/src/codegen/gen_assists_docs.rs137
1 files changed, 137 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..05afda8f1
--- /dev/null
+++ b/xtask/src/codegen/gen_assists_docs.rs
@@ -0,0 +1,137 @@
1use std::{fs, path::Path};
2
3use crate::{
4 codegen::{self, extract_comment_blocks_with_empty_lines, 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_with_empty_lines(&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!(
49 id.chars().all(|it| it.is_ascii_lowercase() || it == '_'),
50 "invalid assist id: {:?}",
51 id
52 );
53
54 let doc = take_until(lines.by_ref(), "```").trim().to_string();
55 assert!(
56 doc.chars().next().unwrap().is_ascii_uppercase() && doc.ends_with('.'),
57 "\n\n{}: assist docs should be proper sentences, with capitalization and a full stop at the end.\n\n{}\n\n",
58 id, doc,
59 );
60
61 let before = take_until(lines.by_ref(), "```");
62
63 assert_eq!(lines.next().unwrap().as_str(), "->");
64 assert_eq!(lines.next().unwrap().as_str(), "```");
65 let after = take_until(lines.by_ref(), "```");
66 acc.push(Assist { id, doc, before, after })
67 }
68
69 fn take_until<'a>(lines: impl Iterator<Item = &'a String>, marker: &str) -> String {
70 let mut buf = Vec::new();
71 for line in lines {
72 if line == marker {
73 break;
74 }
75 buf.push(line.clone());
76 }
77 buf.join("\n")
78 }
79 Ok(())
80 }
81}
82
83fn generate_tests(assists: &[Assist], mode: Mode) -> Result<()> {
84 let mut buf = String::from("use super::check;\n");
85
86 for assist in assists.iter() {
87 let test = format!(
88 r######"
89#[test]
90fn doctest_{}() {{
91 check(
92 "{}",
93r#####"
94{}
95"#####, r#####"
96{}
97"#####)
98}}
99"######,
100 assist.id, assist.id, assist.before, assist.after
101 );
102
103 buf.push_str(&test)
104 }
105 let buf = codegen::reformat(buf)?;
106 codegen::update(&project_root().join(codegen::ASSISTS_TESTS), &buf, mode)
107}
108
109fn generate_docs(assists: &[Assist], mode: Mode) -> Result<()> {
110 let mut buf = String::from(
111 "# Assists\n\nCursor position or selection is signified by `┃` character.\n\n",
112 );
113
114 for assist in assists {
115 let before = assist.before.replace("<|>", "┃"); // Unicode pseudo-graphics bar
116 let after = assist.after.replace("<|>", "┃");
117 let docs = format!(
118 "
119## `{}`
120
121{}
122
123```rust
124// BEFORE
125{}
126
127// AFTER
128{}
129```
130",
131 assist.id, assist.doc, before, after
132 );
133 buf.push_str(&docs);
134 }
135
136 codegen::update(&project_root().join(codegen::ASSISTS_DOCS), &buf, mode)
137}