about summary refs log tree commit diff
path: root/xtask/src/codegen
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2020-02-06 18:10:25 +0100
committerAleksey Kladov <aleksey.kladov@gmail.com>2020-02-06 18:14:44 +0100
commit755077e3720bd97e1e506bf8fbe0a2534389f282 (patch)
tree11eb0c7c1c829228c546cd94c2e48331941c4891 /xtask/src/codegen
parentd1e8b8d134da802eecef5cbcd5486bd542ad75b5 (diff)
downloadrust-755077e3720bd97e1e506bf8fbe0a2534389f282.tar.gz
rust-755077e3720bd97e1e506bf8fbe0a2534389f282.zip
Doctest autoimport
Diffstat (limited to 'xtask/src/codegen')
-rw-r--r--xtask/src/codegen/gen_assists_docs.rs42
1 files changed, 33 insertions, 9 deletions
diff --git a/xtask/src/codegen/gen_assists_docs.rs b/xtask/src/codegen/gen_assists_docs.rs
index 69f9b487288..697e830df40 100644
--- a/xtask/src/codegen/gen_assists_docs.rs
+++ b/xtask/src/codegen/gen_assists_docs.rs
@@ -20,6 +20,28 @@ struct Assist {
     after: String,
 }
 
+fn hide_hash_comments(text: &str) -> String {
+    text.split('\n') // want final newline
+        .filter(|&it| !(it.starts_with("# ") || it == "#"))
+        .map(|it| format!("{}\n", it))
+        .collect()
+}
+
+fn reveal_hash_comments(text: &str) -> String {
+    text.split('\n') // want final newline
+        .map(|it| {
+            if it.starts_with("# ") {
+                &it[2..]
+            } else if it == "#" {
+                ""
+            } else {
+                it
+            }
+        })
+        .map(|it| format!("{}\n", it))
+        .collect()
+}
+
 fn collect_assists() -> Result<Vec<Assist>> {
     let mut res = Vec::new();
     for entry in fs::read_dir(project_root().join(codegen::ASSISTS_DIR))? {
@@ -91,13 +113,14 @@ fn doctest_{}() {{
     check(
         "{}",
 r#####"
-{}
-"#####, r#####"
-{}
-"#####)
+{}"#####, r#####"
+{}"#####)
 }}
 "######,
-            assist.id, assist.id, assist.before, assist.after
+            assist.id,
+            assist.id,
+            reveal_hash_comments(&assist.before),
+            reveal_hash_comments(&assist.after)
         );
 
         buf.push_str(&test)
@@ -123,12 +146,13 @@ fn generate_docs(assists: &[Assist], mode: Mode) -> Result<()> {
 ```rust
 // BEFORE
 {}
-
 // AFTER
-{}
-```
+{}```
 ",
-            assist.id, assist.doc, before, after
+            assist.id,
+            assist.doc,
+            hide_hash_comments(&before),
+            hide_hash_comments(&after)
         );
         buf.push_str(&docs);
     }