From 0fb01367f59b374ddcb0053e26184d7bd5643224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Sat, 27 Feb 2021 16:25:06 +0200 Subject: Format generated features manually instead of relying on rustfmt --- xtask/src/codegen/gen_lint_completions.rs | 66 ++++++++++++++++--------------- 1 file changed, 34 insertions(+), 32 deletions(-) (limited to 'xtask/src') diff --git a/xtask/src/codegen/gen_lint_completions.rs b/xtask/src/codegen/gen_lint_completions.rs index 25f770eaf..8c51d35c7 100644 --- a/xtask/src/codegen/gen_lint_completions.rs +++ b/xtask/src/codegen/gen_lint_completions.rs @@ -1,7 +1,7 @@ //! Generates descriptors structure for unstable feature from Unstable Book +use std::fmt::Write; use std::path::{Path, PathBuf}; -use quote::quote; use walkdir::WalkDir; use xshell::{cmd, read_file}; @@ -15,16 +15,13 @@ pub fn generate_lint_completions(mode: Mode) -> Result<()> { cmd!("git clone --depth=1 https://github.com/rust-lang/rust ./target/rust").run()?; } - let ts_features = generate_descriptor("./target/rust/src/doc/unstable-book/src".into())?; - cmd!("curl http://rust-lang.github.io/rust-clippy/master/lints.json --output ./target/clippy_lints.json").run()?; + let mut contents = String::from("use crate::completions::attribute::LintCompletion;\n\n"); + generate_descriptor(&mut contents, "./target/rust/src/doc/unstable-book/src".into())?; + contents.push('\n'); - let ts_clippy = generate_descriptor_clippy(&Path::new("./target/clippy_lints.json"))?; - let ts = quote! { - use crate::completions::attribute::LintCompletion; - #ts_features - #ts_clippy - }; - let contents = reformat(ts.to_string().as_str())?; + cmd!("curl http://rust-lang.github.io/rust-clippy/master/lints.json --output ./target/clippy_lints.json").run()?; + generate_descriptor_clippy(&mut contents, &Path::new("./target/clippy_lints.json"))?; + let contents = reformat(&contents)?; let destination = project_root().join("crates/ide_completion/src/generated_lint_completions.rs"); @@ -34,8 +31,10 @@ pub fn generate_lint_completions(mode: Mode) -> Result<()> { Ok(()) } -fn generate_descriptor(src_dir: PathBuf) -> Result { - let definitions = ["language-features", "library-features"] +fn generate_descriptor(buf: &mut String, src_dir: PathBuf) -> Result<()> { + buf.push_str(r#"pub(super) const FEATURES: &[LintCompletion] = &["#); + buf.push('\n'); + ["language-features", "library-features"] .iter() .flat_map(|it| WalkDir::new(src_dir.join(it))) .filter_map(|e| e.ok()) @@ -43,21 +42,15 @@ fn generate_descriptor(src_dir: PathBuf) -> Result { // Get all `.md ` files entry.file_type().is_file() && entry.path().extension().unwrap_or_default() == "md" }) - .map(|entry| { + .for_each(|entry| { let path = entry.path(); let feature_ident = path.file_stem().unwrap().to_str().unwrap().replace("-", "_"); let doc = read_file(path).unwrap(); - quote! { LintCompletion { label: #feature_ident, description: #doc } } + push_lint_completion(buf, &feature_ident, &doc); }); - - let ts = quote! { - pub(super) const FEATURES: &[LintCompletion] = &[ - #(#definitions),* - ]; - }; - - Ok(ts) + buf.push_str("];\n"); + Ok(()) } #[derive(Default)] @@ -66,7 +59,7 @@ struct ClippyLint { id: String, } -fn generate_descriptor_clippy(path: &Path) -> Result { +fn generate_descriptor_clippy(buf: &mut String, path: &Path) -> Result<()> { let file_content = read_file(path)?; let mut clippy_lints: Vec = vec![]; @@ -97,18 +90,27 @@ fn generate_descriptor_clippy(path: &Path) -> Result { } } - let definitions = clippy_lints.into_iter().map(|clippy_lint| { + buf.push_str(r#"pub(super) const CLIPPY_LINTS: &[LintCompletion] = &["#); + buf.push('\n'); + clippy_lints.into_iter().for_each(|clippy_lint| { let lint_ident = format!("clippy::{}", clippy_lint.id); let doc = clippy_lint.help; - - quote! { LintCompletion { label: #lint_ident, description: #doc } } + push_lint_completion(buf, &lint_ident, &doc); }); - let ts = quote! { - pub(super) const CLIPPY_LINTS: &[LintCompletion] = &[ - #(#definitions),* - ]; - }; + buf.push_str("];\n"); + + Ok(()) +} - Ok(ts) +fn push_lint_completion(buf: &mut String, label: &str, description: &str) { + writeln!( + buf, + r###" LintCompletion {{ + label: "{}", + description: r##"{}"## + }},"###, + label, description + ) + .unwrap(); } -- cgit v1.2.3 From 351670f6201d7ceabaec3e4b79e5dcf4c5048e12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Sat, 27 Feb 2021 16:26:02 +0200 Subject: Remove dead gen_features code --- xtask/src/codegen/gen_features.rs | 48 --------------------------------------- 1 file changed, 48 deletions(-) delete mode 100644 xtask/src/codegen/gen_features.rs (limited to 'xtask/src') diff --git a/xtask/src/codegen/gen_features.rs b/xtask/src/codegen/gen_features.rs deleted file mode 100644 index 3cf15ce02..000000000 --- a/xtask/src/codegen/gen_features.rs +++ /dev/null @@ -1,48 +0,0 @@ -//! Generates descriptors structure for unstable feature from Unstable Book -use std::path::{Path, PathBuf}; - -use quote::quote; -use walkdir::WalkDir; -use xshell::{cmd, read_file}; - -use crate::codegen::{project_root, reformat, update, Mode, Result}; - -pub fn generate_features(mode: Mode) -> Result<()> { - if !Path::new("./target/rust").exists() { - cmd!("git clone https://github.com/rust-lang/rust ./target/rust").run()?; - } - - let contents = generate_descriptor("./target/rust/src/doc/unstable-book/src".into())?; - - let destination = project_root().join("crates/ide/src/completion/generated_features.rs"); - update(destination.as_path(), &contents, mode)?; - - Ok(()) -} - -fn generate_descriptor(src_dir: PathBuf) -> Result { - let definitions = ["language-features", "library-features"] - .iter() - .flat_map(|it| WalkDir::new(src_dir.join(it))) - .filter_map(|e| e.ok()) - .filter(|entry| { - // Get all `.md ` files - entry.file_type().is_file() && entry.path().extension().unwrap_or_default() == "md" - }) - .map(|entry| { - let path = entry.path(); - let feature_ident = path.file_stem().unwrap().to_str().unwrap().replace("-", "_"); - let doc = read_file(path).unwrap(); - - quote! { LintCompletion { label: #feature_ident, description: #doc } } - }); - - let ts = quote! { - use crate::completion::complete_attribute::LintCompletion; - - pub(super) const FEATURES: &[LintCompletion] = &[ - #(#definitions),* - ]; - }; - reformat(&ts.to_string()) -} -- cgit v1.2.3