From 5d17b6a6873d530eda89d271807dcb70a811a200 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 4 Jun 2021 17:03:18 +0200 Subject: Implement hover for lints --- xtask/src/codegen/gen_lint_completions.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'xtask/src/codegen/gen_lint_completions.rs') diff --git a/xtask/src/codegen/gen_lint_completions.rs b/xtask/src/codegen/gen_lint_completions.rs index 24dbc6a39..f5736d1b5 100644 --- a/xtask/src/codegen/gen_lint_completions.rs +++ b/xtask/src/codegen/gen_lint_completions.rs @@ -28,9 +28,9 @@ pub(crate) fn generate_lint_completions() -> Result<()> { } fn generate_descriptor(buf: &mut String, src_dir: PathBuf) -> Result<()> { - buf.push_str(r#"pub(super) const FEATURES: &[LintCompletion] = &["#); + buf.push_str(r#"pub const FEATURES: &[LintCompletion] = &["#); buf.push('\n'); - ["language-features", "library-features"] + let mut vec = ["language-features", "library-features"] .iter() .flat_map(|it| WalkDir::new(src_dir.join(it))) .filter_map(|e| e.ok()) @@ -38,13 +38,17 @@ fn generate_descriptor(buf: &mut String, src_dir: PathBuf) -> Result<()> { // Get all `.md ` files entry.file_type().is_file() && entry.path().extension().unwrap_or_default() == "md" }) - .for_each(|entry| { + .map(|entry| { let path = entry.path(); let feature_ident = path.file_stem().unwrap().to_str().unwrap().replace("-", "_"); let doc = read_file(path).unwrap(); - - push_lint_completion(buf, &feature_ident, &doc); - }); + (feature_ident, doc) + }) + .collect::>(); + vec.sort_by(|(feature_ident, _), (feature_ident2, _)| feature_ident.cmp(feature_ident2)); + vec.into_iter().for_each(|(feature_ident, doc)| { + push_lint_completion(buf, &feature_ident, &doc); + }); buf.push_str("];\n"); Ok(()) } @@ -85,8 +89,8 @@ fn generate_descriptor_clippy(buf: &mut String, path: &Path) -> Result<()> { .into(); } } - - buf.push_str(r#"pub(super) const CLIPPY_LINTS: &[LintCompletion] = &["#); + clippy_lints.sort_by(|lint, lint2| lint.id.cmp(&lint2.id)); + buf.push_str(r#"pub const CLIPPY_LINTS: &[LintCompletion] = &["#); buf.push('\n'); clippy_lints.into_iter().for_each(|clippy_lint| { let lint_ident = format!("clippy::{}", clippy_lint.id); -- cgit v1.2.3 From 343df88ac7579316a5500fa7f4a07602809af669 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 4 Jun 2021 18:35:19 +0200 Subject: Generate default lint completions --- xtask/src/codegen/gen_lint_completions.rs | 52 ++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 11 deletions(-) (limited to 'xtask/src/codegen/gen_lint_completions.rs') 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<()> { cmd!("git clone --depth=1 https://github.com/rust-lang/rust ./target/rust").run()?; } - let mut contents = String::from("use crate::completions::attribute::LintCompletion;\n\n"); - generate_descriptor(&mut contents, "./target/rust/src/doc/unstable-book/src".into())?; + let mut contents = String::from( + r#"pub struct Lint { + pub label: &'static str, + pub description: &'static str, +} + +"#, + ); + generate_lint_descriptor(&mut contents)?; + contents.push('\n'); + + generate_feature_descriptor(&mut contents, "./target/rust/src/doc/unstable-book/src".into())?; contents.push('\n'); cmd!("curl http://rust-lang.github.io/rust-clippy/master/lints.json --output ./target/clippy_lints.json").run()?; generate_descriptor_clippy(&mut contents, &Path::new("./target/clippy_lints.json"))?; let contents = reformat(&contents)?; - let destination = - project_root().join("crates/ide_completion/src/generated_lint_completions.rs"); + let destination = project_root().join("crates/ide_db/src/helpers/generated_lints.rs"); ensure_file_contents(destination.as_path(), &contents)?; Ok(()) } -fn generate_descriptor(buf: &mut String, src_dir: PathBuf) -> Result<()> { - buf.push_str(r#"pub const FEATURES: &[LintCompletion] = &["#); +fn generate_lint_descriptor(buf: &mut String) -> Result<()> { + let stdout = cmd!("rustc -W help").read()?; + let start = stdout.find("---- ------- -------").ok_or_else(|| anyhow::format_err!(""))?; + let end = + stdout.rfind("Lint groups provided by rustc:").ok_or_else(|| anyhow::format_err!(""))?; + buf.push_str(r#"pub const DEFAULT_LINTS: &[Lint] = &["#); + buf.push('\n'); + let mut lints = stdout[start..end] + .lines() + .filter(|l| !l.is_empty()) + .flat_map(|line| { + let (name, rest) = line.trim().split_once(char::is_whitespace)?; + let (_default_level, description) = rest.trim().split_once(char::is_whitespace)?; + Some((name.trim(), description.trim())) + }) + .collect::>(); + lints.sort_by(|(ident, _), (ident2, _)| ident.cmp(ident2)); + lints.into_iter().for_each(|(name, description)| push_lint_completion(buf, name, description)); + buf.push_str("];\n"); + Ok(()) +} + +fn generate_feature_descriptor(buf: &mut String, src_dir: PathBuf) -> Result<()> { + buf.push_str(r#"pub const FEATURES: &[Lint] = &["#); buf.push('\n'); let mut vec = ["language-features", "library-features"] .iter() @@ -46,9 +77,8 @@ fn generate_descriptor(buf: &mut String, src_dir: PathBuf) -> Result<()> { }) .collect::>(); vec.sort_by(|(feature_ident, _), (feature_ident2, _)| feature_ident.cmp(feature_ident2)); - vec.into_iter().for_each(|(feature_ident, doc)| { - push_lint_completion(buf, &feature_ident, &doc); - }); + vec.into_iter() + .for_each(|(feature_ident, doc)| push_lint_completion(buf, &feature_ident, &doc)); buf.push_str("];\n"); Ok(()) } @@ -90,7 +120,7 @@ fn generate_descriptor_clippy(buf: &mut String, path: &Path) -> Result<()> { } } clippy_lints.sort_by(|lint, lint2| lint.id.cmp(&lint2.id)); - buf.push_str(r#"pub const CLIPPY_LINTS: &[LintCompletion] = &["#); + buf.push_str(r#"pub const CLIPPY_LINTS: &[Lint] = &["#); buf.push('\n'); clippy_lints.into_iter().for_each(|clippy_lint| { let lint_ident = format!("clippy::{}", clippy_lint.id); @@ -106,7 +136,7 @@ fn generate_descriptor_clippy(buf: &mut String, path: &Path) -> Result<()> { fn push_lint_completion(buf: &mut String, label: &str, description: &str) { writeln!( buf, - r###" LintCompletion {{ + r###" Lint {{ label: "{}", description: r##"{}"## }},"###, -- cgit v1.2.3 From 0b9ba4977ef5b81747c8e5c623009497f9d7c99a Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 4 Jun 2021 18:55:08 +0200 Subject: Generate default lint groups --- xtask/src/codegen/gen_lint_completions.rs | 33 ++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) (limited to 'xtask/src/codegen/gen_lint_completions.rs') diff --git a/xtask/src/codegen/gen_lint_completions.rs b/xtask/src/codegen/gen_lint_completions.rs index d56b23218..b797067cd 100644 --- a/xtask/src/codegen/gen_lint_completions.rs +++ b/xtask/src/codegen/gen_lint_completions.rs @@ -1,4 +1,5 @@ //! Generates descriptors structure for unstable feature from Unstable Book +use std::borrow::Cow; use std::fmt::Write; use std::path::{Path, PathBuf}; @@ -38,22 +39,36 @@ pub(crate) fn generate_lint_completions() -> Result<()> { fn generate_lint_descriptor(buf: &mut String) -> Result<()> { let stdout = cmd!("rustc -W help").read()?; - let start = stdout.find("---- ------- -------").ok_or_else(|| anyhow::format_err!(""))?; - let end = - stdout.rfind("Lint groups provided by rustc:").ok_or_else(|| anyhow::format_err!(""))?; + let start_lints = + stdout.find("---- ------- -------").ok_or_else(|| anyhow::format_err!(""))?; + let start_lint_groups = + stdout.find("---- ---------").ok_or_else(|| anyhow::format_err!(""))?; + let end_lints = + stdout.find("Lint groups provided by rustc:").ok_or_else(|| anyhow::format_err!(""))?; + let end_lint_groups = stdout + .find("Lint tools like Clippy can provide additional lints and lint groups.") + .ok_or_else(|| anyhow::format_err!(""))?; buf.push_str(r#"pub const DEFAULT_LINTS: &[Lint] = &["#); buf.push('\n'); - let mut lints = stdout[start..end] + let mut lints = stdout[start_lints..end_lints] .lines() .filter(|l| !l.is_empty()) - .flat_map(|line| { - let (name, rest) = line.trim().split_once(char::is_whitespace)?; - let (_default_level, description) = rest.trim().split_once(char::is_whitespace)?; - Some((name.trim(), description.trim())) + .map(|line| { + let (name, rest) = line.trim().split_once(char::is_whitespace).unwrap(); + let (_default_level, description) = + rest.trim().split_once(char::is_whitespace).unwrap(); + (name.trim(), Cow::Borrowed(description.trim())) }) .collect::>(); + lints.extend(stdout[start_lint_groups..end_lint_groups].lines().filter(|l| !l.is_empty()).map( + |line| { + let (name, lints) = line.trim().split_once(char::is_whitespace).unwrap(); + (name.trim(), format!("lint group for: {}", lints.trim()).into()) + }, + )); + lints.sort_by(|(ident, _), (ident2, _)| ident.cmp(ident2)); - lints.into_iter().for_each(|(name, description)| push_lint_completion(buf, name, description)); + lints.into_iter().for_each(|(name, description)| push_lint_completion(buf, name, &description)); buf.push_str("];\n"); Ok(()) } -- cgit v1.2.3 From 0c89f38378c2110cf6c080a5dc837bf7731fef5c Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 4 Jun 2021 19:03:45 +0200 Subject: Replace `-` with `_` in generated lint names --- xtask/src/codegen/gen_lint_completions.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'xtask/src/codegen/gen_lint_completions.rs') diff --git a/xtask/src/codegen/gen_lint_completions.rs b/xtask/src/codegen/gen_lint_completions.rs index b797067cd..a28b6cb59 100644 --- a/xtask/src/codegen/gen_lint_completions.rs +++ b/xtask/src/codegen/gen_lint_completions.rs @@ -52,6 +52,7 @@ fn generate_lint_descriptor(buf: &mut String) -> Result<()> { buf.push('\n'); let mut lints = stdout[start_lints..end_lints] .lines() + .skip(1) .filter(|l| !l.is_empty()) .map(|line| { let (name, rest) = line.trim().split_once(char::is_whitespace).unwrap(); @@ -60,15 +61,19 @@ fn generate_lint_descriptor(buf: &mut String) -> Result<()> { (name.trim(), Cow::Borrowed(description.trim())) }) .collect::>(); - lints.extend(stdout[start_lint_groups..end_lint_groups].lines().filter(|l| !l.is_empty()).map( - |line| { - let (name, lints) = line.trim().split_once(char::is_whitespace).unwrap(); - (name.trim(), format!("lint group for: {}", lints.trim()).into()) - }, - )); + lints.extend( + stdout[start_lint_groups..end_lint_groups].lines().skip(1).filter(|l| !l.is_empty()).map( + |line| { + let (name, lints) = line.trim().split_once(char::is_whitespace).unwrap(); + (name.trim(), format!("lint group for: {}", lints.trim()).into()) + }, + ), + ); lints.sort_by(|(ident, _), (ident2, _)| ident.cmp(ident2)); - lints.into_iter().for_each(|(name, description)| push_lint_completion(buf, name, &description)); + lints.into_iter().for_each(|(name, description)| { + push_lint_completion(buf, &name.replace("-", "_"), &description) + }); buf.push_str("];\n"); Ok(()) } -- cgit v1.2.3