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.rs209
1 files changed, 105 insertions, 104 deletions
diff --git a/xtask/src/codegen/gen_assists_docs.rs b/xtask/src/codegen/gen_assists_docs.rs
index 31d606535..6c1be5350 100644
--- a/xtask/src/codegen/gen_assists_docs.rs
+++ b/xtask/src/codegen/gen_assists_docs.rs
@@ -1,114 +1,124 @@
1//! Generates `assists.md` documentation. 1//! Generates `assists.md` documentation.
2 2
3use std::{fs, path::Path}; 3use std::{fmt, fs, path::Path};
4 4
5use crate::{ 5use crate::{
6 codegen::{self, extract_comment_blocks_with_empty_lines, Mode}, 6 codegen::{self, extract_comment_blocks_with_empty_lines, Location, Mode},
7 project_root, rust_files, Result, 7 project_root, rust_files, Result,
8}; 8};
9 9
10pub fn generate_assists_docs(mode: Mode) -> Result<()> { 10pub fn generate_assists_docs(mode: Mode) -> Result<()> {
11 let assists = collect_assists()?; 11 let assists = Assist::collect()?;
12 generate_tests(&assists, mode)?; 12 generate_tests(&assists, mode)?;
13 generate_docs(&assists, mode)?; 13
14 let contents = assists.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n");
15 let contents = contents.trim().to_string() + "\n";
16 let dst = project_root().join("docs/user/generated_assists.adoc");
17 codegen::update(&dst, &contents, mode)?;
18
14 Ok(()) 19 Ok(())
15} 20}
16 21
17#[derive(Debug)] 22#[derive(Debug)]
18struct Assist { 23struct Assist {
19 id: String, 24 id: String,
25 location: Location,
20 doc: String, 26 doc: String,
21 before: String, 27 before: String,
22 after: String, 28 after: String,
23} 29}
24 30
25fn hide_hash_comments(text: &str) -> String { 31impl Assist {
26 text.split('\n') // want final newline 32 fn collect() -> Result<Vec<Assist>> {
27 .filter(|&it| !(it.starts_with("# ") || it == "#")) 33 let mut res = Vec::new();
28 .map(|it| format!("{}\n", it)) 34 for path in rust_files(&project_root().join(codegen::ASSISTS_DIR)) {
29 .collect() 35 collect_file(&mut res, path.as_path())?;
30}
31
32fn reveal_hash_comments(text: &str) -> String {
33 text.split('\n') // want final newline
34 .map(|it| {
35 if it.starts_with("# ") {
36 &it[2..]
37 } else if it == "#" {
38 ""
39 } else {
40 it
41 }
42 })
43 .map(|it| format!("{}\n", it))
44 .collect()
45}
46
47fn collect_assists() -> Result<Vec<Assist>> {
48 let mut res = Vec::new();
49 for path in rust_files(&project_root().join(codegen::ASSISTS_DIR)) {
50 collect_file(&mut res, path.as_path())?;
51 }
52 res.sort_by(|lhs, rhs| lhs.id.cmp(&rhs.id));
53 return Ok(res);
54
55 fn collect_file(acc: &mut Vec<Assist>, path: &Path) -> Result<()> {
56 let text = fs::read_to_string(path)?;
57 let comment_blocks = extract_comment_blocks_with_empty_lines(&text);
58
59 for block in comment_blocks {
60 // FIXME: doesn't support blank lines yet, need to tweak
61 // `extract_comment_blocks` for that.
62 let mut lines = block.iter();
63 let first_line = lines.next().unwrap();
64 if !first_line.starts_with("Assist: ") {
65 continue;
66 }
67 let id = first_line["Assist: ".len()..].to_string();
68 assert!(
69 id.chars().all(|it| it.is_ascii_lowercase() || it == '_'),
70 "invalid assist id: {:?}",
71 id
72 );
73
74 let doc = take_until(lines.by_ref(), "```").trim().to_string();
75 assert!(
76 doc.chars().next().unwrap().is_ascii_uppercase() && doc.ends_with('.'),
77 "\n\n{}: assist docs should be proper sentences, with capitalization and a full stop at the end.\n\n{}\n\n",
78 id, doc,
79 );
80
81 let before = take_until(lines.by_ref(), "```");
82
83 assert_eq!(lines.next().unwrap().as_str(), "->");
84 assert_eq!(lines.next().unwrap().as_str(), "```");
85 let after = take_until(lines.by_ref(), "```");
86 acc.push(Assist { id, doc, before, after })
87 } 36 }
37 res.sort_by(|lhs, rhs| lhs.id.cmp(&rhs.id));
38 return Ok(res);
39
40 fn collect_file(acc: &mut Vec<Assist>, path: &Path) -> Result<()> {
41 let text = fs::read_to_string(path)?;
42 let comment_blocks = extract_comment_blocks_with_empty_lines("Assist", &text);
43
44 for block in comment_blocks {
45 // FIXME: doesn't support blank lines yet, need to tweak
46 // `extract_comment_blocks` for that.
47 let id = block.id;
48 assert!(
49 id.chars().all(|it| it.is_ascii_lowercase() || it == '_'),
50 "invalid assist id: {:?}",
51 id
52 );
53 let mut lines = block.contents.iter();
54
55 let doc = take_until(lines.by_ref(), "```").trim().to_string();
56 assert!(
57 doc.chars().next().unwrap().is_ascii_uppercase() && doc.ends_with('.'),
58 "\n\n{}: assist docs should be proper sentences, with capitalization and a full stop at the end.\n\n{}\n\n",
59 id, doc,
60 );
61
62 let before = take_until(lines.by_ref(), "```");
63
64 assert_eq!(lines.next().unwrap().as_str(), "->");
65 assert_eq!(lines.next().unwrap().as_str(), "```");
66 let after = take_until(lines.by_ref(), "```");
67 let location = Location::new(path.to_path_buf(), block.line);
68 acc.push(Assist { id, location, doc, before, after })
69 }
88 70
89 fn take_until<'a>(lines: impl Iterator<Item = &'a String>, marker: &str) -> String { 71 fn take_until<'a>(lines: impl Iterator<Item = &'a String>, marker: &str) -> String {
90 let mut buf = Vec::new(); 72 let mut buf = Vec::new();
91 for line in lines { 73 for line in lines {
92 if line == marker { 74 if line == marker {
93 break; 75 break;
76 }
77 buf.push(line.clone());
94 } 78 }
95 buf.push(line.clone()); 79 buf.join("\n")
96 } 80 }
97 buf.join("\n") 81 Ok(())
98 } 82 }
99 Ok(()) 83 }
84}
85
86impl fmt::Display for Assist {
87 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88 let before = self.before.replace("<|>", "┃"); // Unicode pseudo-graphics bar
89 let after = self.after.replace("<|>", "┃");
90 writeln!(
91 f,
92 "[discrete]\n=== `{}`
93**Source:** {}
94
95{}
96
97.Before
98```rust
99{}```
100
101.After
102```rust
103{}```",
104 self.id,
105 self.location,
106 self.doc,
107 hide_hash_comments(&before),
108 hide_hash_comments(&after)
109 )
100 } 110 }
101} 111}
102 112
103fn generate_tests(assists: &[Assist], mode: Mode) -> Result<()> { 113fn generate_tests(assists: &[Assist], mode: Mode) -> Result<()> {
104 let mut buf = String::from("use super::check;\n"); 114 let mut buf = String::from("use super::check_doc_test;\n");
105 115
106 for assist in assists.iter() { 116 for assist in assists.iter() {
107 let test = format!( 117 let test = format!(
108 r######" 118 r######"
109#[test] 119#[test]
110fn doctest_{}() {{ 120fn doctest_{}() {{
111 check( 121 check_doc_test(
112 "{}", 122 "{}",
113r#####" 123r#####"
114{}"#####, r#####" 124{}"#####, r#####"
@@ -127,33 +137,24 @@ r#####"
127 codegen::update(&project_root().join(codegen::ASSISTS_TESTS), &buf, mode) 137 codegen::update(&project_root().join(codegen::ASSISTS_TESTS), &buf, mode)
128} 138}
129 139
130fn generate_docs(assists: &[Assist], mode: Mode) -> Result<()> { 140fn hide_hash_comments(text: &str) -> String {
131 let mut buf = String::from( 141 text.split('\n') // want final newline
132 "# Assists\n\nCursor position or selection is signified by `┃` character.\n\n", 142 .filter(|&it| !(it.starts_with("# ") || it == "#"))
133 ); 143 .map(|it| format!("{}\n", it))
134 144 .collect()
135 for assist in assists { 145}
136 let before = assist.before.replace("<|>", "┃"); // Unicode pseudo-graphics bar
137 let after = assist.after.replace("<|>", "┃");
138 let docs = format!(
139 "
140## `{}`
141
142{}
143
144```rust
145// BEFORE
146{}
147// AFTER
148{}```
149",
150 assist.id,
151 assist.doc,
152 hide_hash_comments(&before),
153 hide_hash_comments(&after)
154 );
155 buf.push_str(&docs);
156 }
157 146
158 codegen::update(&project_root().join(codegen::ASSISTS_DOCS), &buf, mode) 147fn reveal_hash_comments(text: &str) -> String {
148 text.split('\n') // want final newline
149 .map(|it| {
150 if it.starts_with("# ") {
151 &it[2..]
152 } else if it == "#" {
153 ""
154 } else {
155 it
156 }
157 })
158 .map(|it| format!("{}\n", it))
159 .collect()
159} 160}