mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
Format generated features manually instead of relying on rustfmt
This commit is contained in:
parent
0892ccd090
commit
0fb01367f5
2 changed files with 6411 additions and 34 deletions
File diff suppressed because one or more lines are too long
|
@ -1,7 +1,7 @@
|
||||||
//! Generates descriptors structure for unstable feature from Unstable Book
|
//! Generates descriptors structure for unstable feature from Unstable Book
|
||||||
|
use std::fmt::Write;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use quote::quote;
|
|
||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
use xshell::{cmd, read_file};
|
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()?;
|
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())?;
|
let mut contents = String::from("use crate::completions::attribute::LintCompletion;\n\n");
|
||||||
cmd!("curl http://rust-lang.github.io/rust-clippy/master/lints.json --output ./target/clippy_lints.json").run()?;
|
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"))?;
|
cmd!("curl http://rust-lang.github.io/rust-clippy/master/lints.json --output ./target/clippy_lints.json").run()?;
|
||||||
let ts = quote! {
|
generate_descriptor_clippy(&mut contents, &Path::new("./target/clippy_lints.json"))?;
|
||||||
use crate::completions::attribute::LintCompletion;
|
let contents = reformat(&contents)?;
|
||||||
#ts_features
|
|
||||||
#ts_clippy
|
|
||||||
};
|
|
||||||
let contents = reformat(ts.to_string().as_str())?;
|
|
||||||
|
|
||||||
let destination =
|
let destination =
|
||||||
project_root().join("crates/ide_completion/src/generated_lint_completions.rs");
|
project_root().join("crates/ide_completion/src/generated_lint_completions.rs");
|
||||||
|
@ -34,8 +31,10 @@ pub fn generate_lint_completions(mode: Mode) -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_descriptor(src_dir: PathBuf) -> Result<proc_macro2::TokenStream> {
|
fn generate_descriptor(buf: &mut String, src_dir: PathBuf) -> Result<()> {
|
||||||
let definitions = ["language-features", "library-features"]
|
buf.push_str(r#"pub(super) const FEATURES: &[LintCompletion] = &["#);
|
||||||
|
buf.push('\n');
|
||||||
|
["language-features", "library-features"]
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|it| WalkDir::new(src_dir.join(it)))
|
.flat_map(|it| WalkDir::new(src_dir.join(it)))
|
||||||
.filter_map(|e| e.ok())
|
.filter_map(|e| e.ok())
|
||||||
|
@ -43,21 +42,15 @@ fn generate_descriptor(src_dir: PathBuf) -> Result<proc_macro2::TokenStream> {
|
||||||
// Get all `.md ` files
|
// Get all `.md ` files
|
||||||
entry.file_type().is_file() && entry.path().extension().unwrap_or_default() == "md"
|
entry.file_type().is_file() && entry.path().extension().unwrap_or_default() == "md"
|
||||||
})
|
})
|
||||||
.map(|entry| {
|
.for_each(|entry| {
|
||||||
let path = entry.path();
|
let path = entry.path();
|
||||||
let feature_ident = path.file_stem().unwrap().to_str().unwrap().replace("-", "_");
|
let feature_ident = path.file_stem().unwrap().to_str().unwrap().replace("-", "_");
|
||||||
let doc = read_file(path).unwrap();
|
let doc = read_file(path).unwrap();
|
||||||
|
|
||||||
quote! { LintCompletion { label: #feature_ident, description: #doc } }
|
push_lint_completion(buf, &feature_ident, &doc);
|
||||||
});
|
});
|
||||||
|
buf.push_str("];\n");
|
||||||
let ts = quote! {
|
Ok(())
|
||||||
pub(super) const FEATURES: &[LintCompletion] = &[
|
|
||||||
#(#definitions),*
|
|
||||||
];
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(ts)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -66,7 +59,7 @@ struct ClippyLint {
|
||||||
id: String,
|
id: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_descriptor_clippy(path: &Path) -> Result<proc_macro2::TokenStream> {
|
fn generate_descriptor_clippy(buf: &mut String, path: &Path) -> Result<()> {
|
||||||
let file_content = read_file(path)?;
|
let file_content = read_file(path)?;
|
||||||
let mut clippy_lints: Vec<ClippyLint> = vec![];
|
let mut clippy_lints: Vec<ClippyLint> = vec![];
|
||||||
|
|
||||||
|
@ -97,18 +90,27 @@ fn generate_descriptor_clippy(path: &Path) -> Result<proc_macro2::TokenStream> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 lint_ident = format!("clippy::{}", clippy_lint.id);
|
||||||
let doc = clippy_lint.help;
|
let doc = clippy_lint.help;
|
||||||
|
push_lint_completion(buf, &lint_ident, &doc);
|
||||||
quote! { LintCompletion { label: #lint_ident, description: #doc } }
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let ts = quote! {
|
buf.push_str("];\n");
|
||||||
pub(super) const CLIPPY_LINTS: &[LintCompletion] = &[
|
|
||||||
#(#definitions),*
|
|
||||||
];
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(ts)
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn push_lint_completion(buf: &mut String, label: &str, description: &str) {
|
||||||
|
writeln!(
|
||||||
|
buf,
|
||||||
|
r###" LintCompletion {{
|
||||||
|
label: "{}",
|
||||||
|
description: r##"{}"##
|
||||||
|
}},"###,
|
||||||
|
label, description
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue