diff options
Diffstat (limited to 'xtask/src/codegen/gen_lint_completions.rs')
-rw-r--r-- | xtask/src/codegen/gen_lint_completions.rs | 71 |
1 files changed, 37 insertions, 34 deletions
diff --git a/xtask/src/codegen/gen_lint_completions.rs b/xtask/src/codegen/gen_lint_completions.rs index b97421217..b1c057037 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 |
2 | use std::fmt::Write; | ||
2 | use std::path::{Path, PathBuf}; | 3 | use std::path::{Path, PathBuf}; |
3 | 4 | ||
4 | use quote::quote; | ||
5 | use walkdir::WalkDir; | 5 | use walkdir::WalkDir; |
6 | use xshell::{cmd, read_file}; | 6 | use xshell::{cmd, read_file}; |
7 | 7 | ||
@@ -10,31 +10,31 @@ use crate::{ | |||
10 | run_rustfmt, | 10 | run_rustfmt, |
11 | }; | 11 | }; |
12 | 12 | ||
13 | pub fn generate_lint_completions(mode: Mode) -> Result<()> { | 13 | pub(crate) fn generate_lint_completions(mode: Mode) -> Result<()> { |
14 | if !Path::new("./target/rust").exists() { | 14 | if !Path::new("./target/rust").exists() { |
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 = project_root().join("crates/completion/src/generated_lint_completions.rs"); | 26 | let destination = |
27 | project_root().join("crates/ide_completion/src/generated_lint_completions.rs"); | ||
30 | update(destination.as_path(), &contents, mode)?; | 28 | update(destination.as_path(), &contents, mode)?; |
31 | run_rustfmt(mode)?; | 29 | run_rustfmt(mode)?; |
32 | 30 | ||
33 | Ok(()) | 31 | Ok(()) |
34 | } | 32 | } |
35 | 33 | ||
36 | fn generate_descriptor(src_dir: PathBuf) -> Result<proc_macro2::TokenStream> { | 34 | fn generate_descriptor(buf: &mut String, src_dir: PathBuf) -> Result<()> { |
37 | 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"] | ||
38 | .iter() | 38 | .iter() |
39 | .flat_map(|it| WalkDir::new(src_dir.join(it))) | 39 | .flat_map(|it| WalkDir::new(src_dir.join(it))) |
40 | .filter_map(|e| e.ok()) | 40 | .filter_map(|e| e.ok()) |
@@ -42,21 +42,15 @@ fn generate_descriptor(src_dir: PathBuf) -> Result<proc_macro2::TokenStream> { | |||
42 | // Get all `.md ` files | 42 | // Get all `.md ` files |
43 | 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" |
44 | }) | 44 | }) |
45 | .map(|entry| { | 45 | .for_each(|entry| { |
46 | let path = entry.path(); | 46 | let path = entry.path(); |
47 | let feature_ident = path.file_stem().unwrap().to_str().unwrap().replace("-", "_"); | 47 | let feature_ident = path.file_stem().unwrap().to_str().unwrap().replace("-", "_"); |
48 | let doc = read_file(path).unwrap(); | 48 | let doc = read_file(path).unwrap(); |
49 | 49 | ||
50 | quote! { LintCompletion { label: #feature_ident, description: #doc } } | 50 | push_lint_completion(buf, &feature_ident, &doc); |
51 | }); | 51 | }); |
52 | 52 | buf.push_str("];\n"); | |
53 | let ts = quote! { | 53 | Ok(()) |
54 | pub(super) const FEATURES: &[LintCompletion] = &[ | ||
55 | #(#definitions),* | ||
56 | ]; | ||
57 | }; | ||
58 | |||
59 | Ok(ts) | ||
60 | } | 54 | } |
61 | 55 | ||
62 | #[derive(Default)] | 56 | #[derive(Default)] |
@@ -65,7 +59,7 @@ struct ClippyLint { | |||
65 | id: String, | 59 | id: String, |
66 | } | 60 | } |
67 | 61 | ||
68 | fn generate_descriptor_clippy(path: &Path) -> Result<proc_macro2::TokenStream> { | 62 | fn generate_descriptor_clippy(buf: &mut String, path: &Path) -> Result<()> { |
69 | let file_content = read_file(path)?; | 63 | let file_content = read_file(path)?; |
70 | let mut clippy_lints: Vec<ClippyLint> = vec![]; | 64 | let mut clippy_lints: Vec<ClippyLint> = vec![]; |
71 | 65 | ||
@@ -96,18 +90,27 @@ fn generate_descriptor_clippy(path: &Path) -> Result<proc_macro2::TokenStream> { | |||
96 | } | 90 | } |
97 | } | 91 | } |
98 | 92 | ||
99 | 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| { | ||
100 | let lint_ident = format!("clippy::{}", clippy_lint.id); | 96 | let lint_ident = format!("clippy::{}", clippy_lint.id); |
101 | let doc = clippy_lint.help; | 97 | let doc = clippy_lint.help; |
102 | 98 | push_lint_completion(buf, &lint_ident, &doc); | |
103 | quote! { LintCompletion { label: #lint_ident, description: #doc } } | ||
104 | }); | 99 | }); |
105 | 100 | ||
106 | let ts = quote! { | 101 | buf.push_str("];\n"); |
107 | pub(super) const CLIPPY_LINTS: &[LintCompletion] = &[ | 102 | |
108 | #(#definitions),* | 103 | Ok(()) |
109 | ]; | 104 | } |
110 | }; | ||
111 | 105 | ||
112 | Ok(ts) | 106 | fn 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(); | ||
113 | } | 116 | } |