aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/codegen
diff options
context:
space:
mode:
Diffstat (limited to 'xtask/src/codegen')
-rw-r--r--xtask/src/codegen/gen_lint_completions.rs66
1 files changed, 34 insertions, 32 deletions
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 @@
1//! Generates descriptors structure for unstable feature from Unstable Book 1//! Generates descriptors structure for unstable feature from Unstable Book
2use std::fmt::Write;
2use std::path::{Path, PathBuf}; 3use std::path::{Path, PathBuf};
3 4
4use quote::quote;
5use walkdir::WalkDir; 5use walkdir::WalkDir;
6use xshell::{cmd, read_file}; 6use xshell::{cmd, read_file};
7 7
@@ -15,16 +15,13 @@ pub fn generate_lint_completions(mode: Mode) -> Result<()> {
15 cmd!("git clone --depth=1 https://github.com/rust-lang/rust ./target/rust").run()?; 15 cmd!("git clone --depth=1 https://github.com/rust-lang/rust ./target/rust").run()?;
16 } 16 }
17 17
18 let ts_features = generate_descriptor("./target/rust/src/doc/unstable-book/src".into())?; 18 let mut contents = String::from("use crate::completions::attribute::LintCompletion;\n\n");
19 cmd!("curl http://rust-lang.github.io/rust-clippy/master/lints.json --output ./target/clippy_lints.json").run()?; 19 generate_descriptor(&mut contents, "./target/rust/src/doc/unstable-book/src".into())?;
20 contents.push('\n');
20 21
21 let ts_clippy = generate_descriptor_clippy(&Path::new("./target/clippy_lints.json"))?; 22 cmd!("curl http://rust-lang.github.io/rust-clippy/master/lints.json --output ./target/clippy_lints.json").run()?;
22 let ts = quote! { 23 generate_descriptor_clippy(&mut contents, &Path::new("./target/clippy_lints.json"))?;
23 use crate::completions::attribute::LintCompletion; 24 let contents = reformat(&contents)?;
24 #ts_features
25 #ts_clippy
26 };
27 let contents = reformat(ts.to_string().as_str())?;
28 25
29 let destination = 26 let destination =
30 project_root().join("crates/ide_completion/src/generated_lint_completions.rs"); 27 project_root().join("crates/ide_completion/src/generated_lint_completions.rs");
@@ -34,8 +31,10 @@ pub fn generate_lint_completions(mode: Mode) -> Result<()> {
34 Ok(()) 31 Ok(())
35} 32}
36 33
37fn generate_descriptor(src_dir: PathBuf) -> Result<proc_macro2::TokenStream> { 34fn generate_descriptor(buf: &mut String, src_dir: PathBuf) -> Result<()> {
38 let definitions = ["language-features", "library-features"] 35 buf.push_str(r#"pub(super) const FEATURES: &[LintCompletion] = &["#);
36 buf.push('\n');
37 ["language-features", "library-features"]
39 .iter() 38 .iter()
40 .flat_map(|it| WalkDir::new(src_dir.join(it))) 39 .flat_map(|it| WalkDir::new(src_dir.join(it)))
41 .filter_map(|e| e.ok()) 40 .filter_map(|e| e.ok())
@@ -43,21 +42,15 @@ fn generate_descriptor(src_dir: PathBuf) -> Result<proc_macro2::TokenStream> {
43 // Get all `.md ` files 42 // Get all `.md ` files
44 entry.file_type().is_file() && entry.path().extension().unwrap_or_default() == "md" 43 entry.file_type().is_file() && entry.path().extension().unwrap_or_default() == "md"
45 }) 44 })
46 .map(|entry| { 45 .for_each(|entry| {
47 let path = entry.path(); 46 let path = entry.path();
48 let feature_ident = path.file_stem().unwrap().to_str().unwrap().replace("-", "_"); 47 let feature_ident = path.file_stem().unwrap().to_str().unwrap().replace("-", "_");
49 let doc = read_file(path).unwrap(); 48 let doc = read_file(path).unwrap();
50 49
51 quote! { LintCompletion { label: #feature_ident, description: #doc } } 50 push_lint_completion(buf, &feature_ident, &doc);
52 }); 51 });
53 52 buf.push_str("];\n");
54 let ts = quote! { 53 Ok(())
55 pub(super) const FEATURES: &[LintCompletion] = &[
56 #(#definitions),*
57 ];
58 };
59
60 Ok(ts)
61} 54}
62 55
63#[derive(Default)] 56#[derive(Default)]
@@ -66,7 +59,7 @@ struct ClippyLint {
66 id: String, 59 id: String,
67} 60}
68 61
69fn generate_descriptor_clippy(path: &Path) -> Result<proc_macro2::TokenStream> { 62fn generate_descriptor_clippy(buf: &mut String, path: &Path) -> Result<()> {
70 let file_content = read_file(path)?; 63 let file_content = read_file(path)?;
71 let mut clippy_lints: Vec<ClippyLint> = vec![]; 64 let mut clippy_lints: Vec<ClippyLint> = vec![];
72 65
@@ -97,18 +90,27 @@ fn generate_descriptor_clippy(path: &Path) -> Result<proc_macro2::TokenStream> {
97 } 90 }
98 } 91 }
99 92
100 let definitions = clippy_lints.into_iter().map(|clippy_lint| { 93 buf.push_str(r#"pub(super) const CLIPPY_LINTS: &[LintCompletion] = &["#);
94 buf.push('\n');
95 clippy_lints.into_iter().for_each(|clippy_lint| {
101 let lint_ident = format!("clippy::{}", clippy_lint.id); 96 let lint_ident = format!("clippy::{}", clippy_lint.id);
102 let doc = clippy_lint.help; 97 let doc = clippy_lint.help;
103 98 push_lint_completion(buf, &lint_ident, &doc);
104 quote! { LintCompletion { label: #lint_ident, description: #doc } }
105 }); 99 });
106 100
107 let ts = quote! { 101 buf.push_str("];\n");
108 pub(super) const CLIPPY_LINTS: &[LintCompletion] = &[ 102
109 #(#definitions),* 103 Ok(())
110 ]; 104}
111 };
112 105
113 Ok(ts) 106fn push_lint_completion(buf: &mut String, label: &str, description: &str) {
107 writeln!(
108 buf,
109 r###" LintCompletion {{
110 label: "{}",
111 description: r##"{}"##
112 }},"###,
113 label, description
114 )
115 .unwrap();
114} 116}