diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs index 78a84f68d0..98acd7fa67 100644 --- a/xtask/src/codegen.rs +++ b/xtask/src/codegen.rs @@ -15,7 +15,11 @@ use std::{ path::{Path, PathBuf}, }; -use crate::{not_bash::fs2, project_root, Result}; +use crate::{ + ensure_rustfmt, + not_bash::{fs2, pushenv, run}, + project_root, Result, +}; pub use self::{ gen_assists_docs::{generate_assists_docs, generate_assists_tests}, @@ -62,6 +66,18 @@ fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> { } } +const PREAMBLE: &str = "Generated file, do not edit by hand, see `xtask/src/codegen`"; + +fn reformat(text: impl std::fmt::Display) -> Result { + let _e = pushenv("RUSTUP_TOOLCHAIN", "stable"); + ensure_rustfmt()?; + let stdout = run!( + "rustfmt --config-path {} --config fn_single_line=true", project_root().join("rustfmt.toml").display(); + Vec> { do_extract_comment_blocks(text, false).into_iter().map(|(_line, block)| block).collect() } diff --git a/xtask/src/codegen/gen_assists_docs.rs b/xtask/src/codegen/gen_assists_docs.rs index 526941f73a..4f49685944 100644 --- a/xtask/src/codegen/gen_assists_docs.rs +++ b/xtask/src/codegen/gen_assists_docs.rs @@ -3,7 +3,7 @@ use std::{fmt, fs, path::Path}; use crate::{ - codegen::{self, extract_comment_blocks_with_empty_lines, Location, Mode}, + codegen::{self, extract_comment_blocks_with_empty_lines, reformat, Location, Mode, PREAMBLE}, project_root, rust_files, Result, }; @@ -15,7 +15,7 @@ pub fn generate_assists_tests(mode: Mode) -> Result<()> { pub fn generate_assists_docs(mode: Mode) -> Result<()> { let assists = Assist::collect()?; let contents = assists.into_iter().map(|it| it.to_string()).collect::>().join("\n\n"); - let contents = contents.trim().to_string() + "\n"; + let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim()); let dst = project_root().join("docs/user/generated_assists.adoc"); codegen::update(&dst, &contents, mode) } @@ -134,7 +134,7 @@ r#####" buf.push_str(&test) } - let buf = crate::reformat(buf)?; + let buf = reformat(buf)?; codegen::update(&project_root().join(codegen::ASSISTS_TESTS), &buf, mode) } diff --git a/xtask/src/codegen/gen_feature_docs.rs b/xtask/src/codegen/gen_feature_docs.rs index 31bc3839d0..3f0013e82a 100644 --- a/xtask/src/codegen/gen_feature_docs.rs +++ b/xtask/src/codegen/gen_feature_docs.rs @@ -3,14 +3,14 @@ use std::{fmt, fs, path::PathBuf}; use crate::{ - codegen::{self, extract_comment_blocks_with_empty_lines, Location, Mode}, + codegen::{self, extract_comment_blocks_with_empty_lines, Location, Mode, PREAMBLE}, project_root, rust_files, Result, }; pub fn generate_feature_docs(mode: Mode) -> Result<()> { let features = Feature::collect()?; let contents = features.into_iter().map(|it| it.to_string()).collect::>().join("\n\n"); - let contents = contents.trim().to_string() + "\n"; + let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim()); let dst = project_root().join("docs/user/generated_features.adoc"); codegen::update(&dst, &contents, mode)?; Ok(()) diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index dd1f4d6a2c..df3ec22c8d 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -14,7 +14,7 @@ use ungrammar::{rust_grammar, Grammar, Rule}; use crate::{ ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Cardinality, Field, KindsSrc, KINDS_SRC}, - codegen::{self, update, Mode}, + codegen::{self, reformat, update, Mode}, project_root, Result, }; @@ -61,7 +61,7 @@ fn generate_tokens(grammar: &AstSrc) -> Result { } }); - let pretty = crate::reformat(quote! { + let pretty = reformat(quote! { use crate::{SyntaxKind::{self, *}, SyntaxToken, ast::AstToken}; #(#tokens)* })? @@ -261,7 +261,7 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> Result { } } - let pretty = crate::reformat(res)?; + let pretty = reformat(res)?; Ok(pretty) } @@ -383,7 +383,7 @@ fn generate_syntax_kinds(grammar: KindsSrc<'_>) -> Result { } }; - crate::reformat(ast) + reformat(ast) } fn to_upper_snake_case(s: &str) -> String { diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index 807ef587ce..f3ad81ba72 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs @@ -62,17 +62,6 @@ pub fn run_rustfmt(mode: Mode) -> Result<()> { Ok(()) } -fn reformat(text: impl std::fmt::Display) -> Result { - let _e = pushenv("RUSTUP_TOOLCHAIN", "stable"); - ensure_rustfmt()?; - let stdout = run!( - "rustfmt --config-path {} --config fn_single_line=true", project_root().join("rustfmt.toml").display(); - Result<()> { let out = run!("rustfmt --version")?; if !out.contains("stable") {