aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/codegen/gen_features.rs
diff options
context:
space:
mode:
Diffstat (limited to 'xtask/src/codegen/gen_features.rs')
-rw-r--r--xtask/src/codegen/gen_features.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/xtask/src/codegen/gen_features.rs b/xtask/src/codegen/gen_features.rs
new file mode 100644
index 000000000..78268308b
--- /dev/null
+++ b/xtask/src/codegen/gen_features.rs
@@ -0,0 +1,50 @@
1//! Generates descriptors structure for unstable feature from Unstable Book
2use std::path::{Path, PathBuf};
3
4use quote::quote;
5use walkdir::WalkDir;
6
7use crate::{
8 codegen::{project_root, reformat, update, Mode, Result},
9 not_bash::{fs2, run},
10};
11
12pub fn generate_features(mode: Mode) -> Result<()> {
13 if !Path::new("./target/rust").exists() {
14 run!("git clone https://github.com/rust-lang/rust ./target/rust")?;
15 }
16
17 let contents = generate_descriptor("./target/rust/src/doc/unstable-book/src".into())?;
18
19 let destination = project_root().join("crates/ide/src/completion/generated_features.rs");
20 update(destination.as_path(), &contents, mode)?;
21
22 Ok(())
23}
24
25fn generate_descriptor(src_dir: PathBuf) -> Result<String> {
26 let definitions = ["language-features", "library-features"]
27 .iter()
28 .flat_map(|it| WalkDir::new(src_dir.join(it)))
29 .filter_map(|e| e.ok())
30 .filter(|entry| {
31 // Get all `.md ` files
32 entry.file_type().is_file() && entry.path().extension().unwrap_or_default() == "md"
33 })
34 .map(|entry| {
35 let path = entry.path();
36 let feature_ident = path.file_stem().unwrap().to_str().unwrap().replace("-", "_");
37 let doc = fs2::read_to_string(path).unwrap();
38
39 quote! { LintCompletion { label: #feature_ident, description: #doc } }
40 });
41
42 let ts = quote! {
43 use crate::completion::complete_attribute::LintCompletion;
44
45 pub(super) const FEATURES: &[LintCompletion] = &[
46 #(#definitions),*
47 ];
48 };
49 reformat(ts)
50}