diff options
Diffstat (limited to 'xtask/src/codegen.rs')
-rw-r--r-- | xtask/src/codegen.rs | 36 |
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 | ||
8 | mod gen_syntax; | 8 | mod gen_syntax; |
9 | mod gen_parser_tests; | 9 | mod gen_parser_tests; |
10 | mod gen_assists_docs; | ||
10 | 11 | ||
11 | use std::{fs, mem, path::Path}; | 12 | use std::{ |
13 | fs, | ||
14 | io::Write, | ||
15 | mem, | ||
16 | path::Path, | ||
17 | process::{Command, Stdio}, | ||
18 | }; | ||
12 | 19 | ||
13 | use crate::Result; | 20 | use crate::{project_root, Result}; |
14 | 21 | ||
15 | pub use self::{gen_parser_tests::generate_parser_tests, gen_syntax::generate_syntax}; | 22 | pub use self::{ |
23 | gen_assists_docs::generate_assists_docs, gen_parser_tests::generate_parser_tests, | ||
24 | gen_syntax::generate_syntax, | ||
25 | }; | ||
16 | 26 | ||
17 | pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron"; | 27 | pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron"; |
18 | const GRAMMAR_DIR: &str = "crates/ra_parser/src/grammar"; | 28 | 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 | |||
22 | pub const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs"; | 32 | pub const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs"; |
23 | pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs"; | 33 | pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs"; |
24 | 34 | ||
35 | const ASSISTS_DIR: &str = "crates/ra_assists/src/assists"; | ||
36 | const ASSISTS_TESTS: &str = "crates/ra_assists/src/doc_tests/generated.rs"; | ||
37 | const ASSISTS_DOCS: &str = "docs/user/assists.md"; | ||
38 | |||
25 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | 39 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] |
26 | pub enum Mode { | 40 | pub 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, |
33 | pub fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> { | 47 | fn 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 | ||
62 | fn 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 | |||
48 | fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> { | 76 | fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> { |
49 | let mut res = Vec::new(); | 77 | let mut res = Vec::new(); |
50 | 78 | ||