aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/codegen.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.rs
parent518f99e16b993e3414a81181c8bad7a89e590ece (diff)
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 bf3a90119..44729cd57 100644
--- a/xtask/src/codegen.rs
+++ b/xtask/src/codegen.rs
@@ -7,12 +7,22 @@
7 7
8mod gen_syntax; 8mod gen_syntax;
9mod gen_parser_tests; 9mod gen_parser_tests;
10mod gen_assists_docs;
10 11
11use std::{fs, mem, path::Path}; 12use std::{
13 fs,
14 io::Write,
15 mem,
16 path::Path,
17 process::{Command, Stdio},
18};
12 19
13use crate::Result; 20use crate::{project_root, Result};
14 21
15pub use self::{gen_parser_tests::generate_parser_tests, gen_syntax::generate_syntax}; 22pub use self::{
23 gen_assists_docs::generate_assists_docs, gen_parser_tests::generate_parser_tests,
24 gen_syntax::generate_syntax,
25};
16 26
17pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron"; 27pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron";
18const GRAMMAR_DIR: &str = "crates/ra_parser/src/grammar"; 28const 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
22pub const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs"; 32pub const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs";
23pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs"; 33pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs";
24 34
35const ASSISTS_DIR: &str = "crates/ra_assists/src/assists";
36const ASSISTS_TESTS: &str = "crates/ra_assists/src/doc_tests/generated.rs";
37const ASSISTS_DOCS: &str = "docs/user/assists.md";
38
25#[derive(Debug, PartialEq, Eq, Clone, Copy)] 39#[derive(Debug, PartialEq, Eq, Clone, Copy)]
26pub enum Mode { 40pub enum Mode {
27 Overwrite, 41 Overwrite,
@@ -30,7 +44,7 @@ pub enum Mode {
30 44
31/// A helper to update file on disk if it has changed. 45/// A helper to update file on disk if it has changed.
32/// With verify = false, 46/// With verify = false,
33pub fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> { 47fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> {
34 match fs::read_to_string(path) { 48 match fs::read_to_string(path) {
35 Ok(ref old_contents) if old_contents == contents => { 49 Ok(ref old_contents) if old_contents == contents => {
36 return Ok(()); 50 return Ok(());
@@ -45,6 +59,20 @@ pub fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> {
45 Ok(()) 59 Ok(())
46} 60}
47 61
62fn reformat(text: impl std::fmt::Display) -> Result<String> {
63 let mut rustfmt = Command::new("rustfmt")
64 .arg("--config-path")
65 .arg(project_root().join("rustfmt.toml"))
66 .stdin(Stdio::piped())
67 .stdout(Stdio::piped())
68 .spawn()?;
69 write!(rustfmt.stdin.take().unwrap(), "{}", text)?;
70 let output = rustfmt.wait_with_output()?;
71 let stdout = String::from_utf8(output.stdout)?;
72 let preamble = "Generated file, do not edit by hand, see `crate/ra_tools/src/codegen`";
73 Ok(format!("//! {}\n\n{}", preamble, stdout))
74}
75
48fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> { 76fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> {
49 let mut res = Vec::new(); 77 let mut res = Vec::new();
50 78