aboutsummaryrefslogtreecommitdiff
path: root/xtask
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-06-04 17:35:19 +0100
committerLukas Wirth <[email protected]>2021-06-04 17:35:19 +0100
commit343df88ac7579316a5500fa7f4a07602809af669 (patch)
tree09db0d170e3fa31c118e6d0dce62af98f51ac5e0 /xtask
parent5d17b6a6873d530eda89d271807dcb70a811a200 (diff)
Generate default lint completions
Diffstat (limited to 'xtask')
-rw-r--r--xtask/src/codegen/gen_lint_completions.rs52
1 files changed, 41 insertions, 11 deletions
diff --git a/xtask/src/codegen/gen_lint_completions.rs b/xtask/src/codegen/gen_lint_completions.rs
index f5736d1b5..d56b23218 100644
--- a/xtask/src/codegen/gen_lint_completions.rs
+++ b/xtask/src/codegen/gen_lint_completions.rs
@@ -12,23 +12,54 @@ pub(crate) fn generate_lint_completions() -> Result<()> {
12 cmd!("git clone --depth=1 https://github.com/rust-lang/rust ./target/rust").run()?; 12 cmd!("git clone --depth=1 https://github.com/rust-lang/rust ./target/rust").run()?;
13 } 13 }
14 14
15 let mut contents = String::from("use crate::completions::attribute::LintCompletion;\n\n"); 15 let mut contents = String::from(
16 generate_descriptor(&mut contents, "./target/rust/src/doc/unstable-book/src".into())?; 16 r#"pub struct Lint {
17 pub label: &'static str,
18 pub description: &'static str,
19}
20
21"#,
22 );
23 generate_lint_descriptor(&mut contents)?;
24 contents.push('\n');
25
26 generate_feature_descriptor(&mut contents, "./target/rust/src/doc/unstable-book/src".into())?;
17 contents.push('\n'); 27 contents.push('\n');
18 28
19 cmd!("curl http://rust-lang.github.io/rust-clippy/master/lints.json --output ./target/clippy_lints.json").run()?; 29 cmd!("curl http://rust-lang.github.io/rust-clippy/master/lints.json --output ./target/clippy_lints.json").run()?;
20 generate_descriptor_clippy(&mut contents, &Path::new("./target/clippy_lints.json"))?; 30 generate_descriptor_clippy(&mut contents, &Path::new("./target/clippy_lints.json"))?;
21 let contents = reformat(&contents)?; 31 let contents = reformat(&contents)?;
22 32
23 let destination = 33 let destination = project_root().join("crates/ide_db/src/helpers/generated_lints.rs");
24 project_root().join("crates/ide_completion/src/generated_lint_completions.rs");
25 ensure_file_contents(destination.as_path(), &contents)?; 34 ensure_file_contents(destination.as_path(), &contents)?;
26 35
27 Ok(()) 36 Ok(())
28} 37}
29 38
30fn generate_descriptor(buf: &mut String, src_dir: PathBuf) -> Result<()> { 39fn generate_lint_descriptor(buf: &mut String) -> Result<()> {
31 buf.push_str(r#"pub const FEATURES: &[LintCompletion] = &["#); 40 let stdout = cmd!("rustc -W help").read()?;
41 let start = stdout.find("---- ------- -------").ok_or_else(|| anyhow::format_err!(""))?;
42 let end =
43 stdout.rfind("Lint groups provided by rustc:").ok_or_else(|| anyhow::format_err!(""))?;
44 buf.push_str(r#"pub const DEFAULT_LINTS: &[Lint] = &["#);
45 buf.push('\n');
46 let mut lints = stdout[start..end]
47 .lines()
48 .filter(|l| !l.is_empty())
49 .flat_map(|line| {
50 let (name, rest) = line.trim().split_once(char::is_whitespace)?;
51 let (_default_level, description) = rest.trim().split_once(char::is_whitespace)?;
52 Some((name.trim(), description.trim()))
53 })
54 .collect::<Vec<_>>();
55 lints.sort_by(|(ident, _), (ident2, _)| ident.cmp(ident2));
56 lints.into_iter().for_each(|(name, description)| push_lint_completion(buf, name, description));
57 buf.push_str("];\n");
58 Ok(())
59}
60
61fn generate_feature_descriptor(buf: &mut String, src_dir: PathBuf) -> Result<()> {
62 buf.push_str(r#"pub const FEATURES: &[Lint] = &["#);
32 buf.push('\n'); 63 buf.push('\n');
33 let mut vec = ["language-features", "library-features"] 64 let mut vec = ["language-features", "library-features"]
34 .iter() 65 .iter()
@@ -46,9 +77,8 @@ fn generate_descriptor(buf: &mut String, src_dir: PathBuf) -> Result<()> {
46 }) 77 })
47 .collect::<Vec<_>>(); 78 .collect::<Vec<_>>();
48 vec.sort_by(|(feature_ident, _), (feature_ident2, _)| feature_ident.cmp(feature_ident2)); 79 vec.sort_by(|(feature_ident, _), (feature_ident2, _)| feature_ident.cmp(feature_ident2));
49 vec.into_iter().for_each(|(feature_ident, doc)| { 80 vec.into_iter()
50 push_lint_completion(buf, &feature_ident, &doc); 81 .for_each(|(feature_ident, doc)| push_lint_completion(buf, &feature_ident, &doc));
51 });
52 buf.push_str("];\n"); 82 buf.push_str("];\n");
53 Ok(()) 83 Ok(())
54} 84}
@@ -90,7 +120,7 @@ fn generate_descriptor_clippy(buf: &mut String, path: &Path) -> Result<()> {
90 } 120 }
91 } 121 }
92 clippy_lints.sort_by(|lint, lint2| lint.id.cmp(&lint2.id)); 122 clippy_lints.sort_by(|lint, lint2| lint.id.cmp(&lint2.id));
93 buf.push_str(r#"pub const CLIPPY_LINTS: &[LintCompletion] = &["#); 123 buf.push_str(r#"pub const CLIPPY_LINTS: &[Lint] = &["#);
94 buf.push('\n'); 124 buf.push('\n');
95 clippy_lints.into_iter().for_each(|clippy_lint| { 125 clippy_lints.into_iter().for_each(|clippy_lint| {
96 let lint_ident = format!("clippy::{}", clippy_lint.id); 126 let lint_ident = format!("clippy::{}", clippy_lint.id);
@@ -106,7 +136,7 @@ fn generate_descriptor_clippy(buf: &mut String, path: &Path) -> Result<()> {
106fn push_lint_completion(buf: &mut String, label: &str, description: &str) { 136fn push_lint_completion(buf: &mut String, label: &str, description: &str) {
107 writeln!( 137 writeln!(
108 buf, 138 buf,
109 r###" LintCompletion {{ 139 r###" Lint {{
110 label: "{}", 140 label: "{}",
111 description: r##"{}"## 141 description: r##"{}"##
112 }},"###, 142 }},"###,