about summary refs log tree commit diff
path: root/xtask/src/codegen.rs
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2019-10-25 14:16:46 +0300
committerAleksey Kladov <aleksey.kladov@gmail.com>2019-10-25 14:47:48 +0300
commit0dd35ff2b2ceffdb926953fdacc7d30e1968047d (patch)
treea8bcab00ef1c434e56845034f22a5595d119dea7 /xtask/src/codegen.rs
parent518f99e16b993e3414a81181c8bad7a89e590ece (diff)
downloadrust-0dd35ff2b2ceffdb926953fdacc7d30e1968047d.tar.gz
rust-0dd35ff2b2ceffdb926953fdacc7d30e1968047d.zip
auto-generate assists docs and tests
Diffstat (limited to 'xtask/src/codegen.rs')
-rw-r--r--xtask/src/codegen.rs36
1 files changed, 32 insertions, 4 deletions
diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs
index bf3a9011950..44729cd57aa 100644
--- a/xtask/src/codegen.rs
+++ b/xtask/src/codegen.rs
@@ -7,12 +7,22 @@
 
 mod gen_syntax;
 mod gen_parser_tests;
+mod gen_assists_docs;
 
-use std::{fs, mem, path::Path};
+use std::{
+    fs,
+    io::Write,
+    mem,
+    path::Path,
+    process::{Command, Stdio},
+};
 
-use crate::Result;
+use crate::{project_root, Result};
 
-pub use self::{gen_parser_tests::generate_parser_tests, gen_syntax::generate_syntax};
+pub use self::{
+    gen_assists_docs::generate_assists_docs, gen_parser_tests::generate_parser_tests,
+    gen_syntax::generate_syntax,
+};
 
 pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron";
 const GRAMMAR_DIR: &str = "crates/ra_parser/src/grammar";
@@ -22,6 +32,10 @@ const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/err
 pub const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs";
 pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs";
 
+const ASSISTS_DIR: &str = "crates/ra_assists/src/assists";
+const ASSISTS_TESTS: &str = "crates/ra_assists/src/doc_tests/generated.rs";
+const ASSISTS_DOCS: &str = "docs/user/assists.md";
+
 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
 pub enum Mode {
     Overwrite,
@@ -30,7 +44,7 @@ pub enum Mode {
 
 /// A helper to update file on disk if it has changed.
 /// With verify = false,
-pub fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> {
+fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> {
     match fs::read_to_string(path) {
         Ok(ref old_contents) if old_contents == contents => {
             return Ok(());
@@ -45,6 +59,20 @@ pub fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> {
     Ok(())
 }
 
+fn reformat(text: impl std::fmt::Display) -> Result<String> {
+    let mut rustfmt = Command::new("rustfmt")
+        .arg("--config-path")
+        .arg(project_root().join("rustfmt.toml"))
+        .stdin(Stdio::piped())
+        .stdout(Stdio::piped())
+        .spawn()?;
+    write!(rustfmt.stdin.take().unwrap(), "{}", text)?;
+    let output = rustfmt.wait_with_output()?;
+    let stdout = String::from_utf8(output.stdout)?;
+    let preamble = "Generated file, do not edit by hand, see `crate/ra_tools/src/codegen`";
+    Ok(format!("//! {}\n\n{}", preamble, stdout))
+}
+
 fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> {
     let mut res = Vec::new();