From 81ff12004476b39766e8b51fbb5a919d5d5ded25 Mon Sep 17 00:00:00 2001 From: augustelalande Date: Wed, 20 Mar 2024 21:33:20 -0400 Subject: [PATCH 1/3] link inline settings --- crates/ruff_dev/src/generate_docs.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/crates/ruff_dev/src/generate_docs.rs b/crates/ruff_dev/src/generate_docs.rs index 2c3f24975a2a8..5165a9694e80c 100644 --- a/crates/ruff_dev/src/generate_docs.rs +++ b/crates/ruff_dev/src/generate_docs.rs @@ -1,6 +1,7 @@ //! Generate Markdown documentation for applicable rules. #![allow(clippy::print_stdout, clippy::print_stderr)] +use std::collections::HashSet; use std::fs; use std::path::PathBuf; @@ -97,6 +98,7 @@ pub(crate) fn main(args: &Args) -> Result<()> { fn process_documentation(documentation: &str, out: &mut String, rule_name: &str) { let mut in_options = false; let mut after = String::new(); + let mut options = HashSet::new(); // HACK: This is an ugly regex hack that's necessary because mkdocs uses // a non-CommonMark-compliant Markdown parser, which doesn't support code @@ -134,6 +136,7 @@ fn process_documentation(documentation: &str, out: &mut String, rule_name: &str) let anchor = option.replace('.', "_"); out.push_str(&format!("- [`{option}`][{option}]\n")); after.push_str(&format!("[{option}]: ../settings.md#{anchor}\n")); + options.insert(option); continue; } @@ -141,6 +144,27 @@ fn process_documentation(documentation: &str, out: &mut String, rule_name: &str) out.push_str(line); } + + let re = Regex::new(r"\[`([^`]*?)`]\[(.*?)]").unwrap(); + for (_, [option, _]) in re.captures_iter(&documentation).map(|c| c.extract()) { + match Options::metadata().find(option) { + Some(OptionEntry::Field(field)) => { + if !options.contains(option) { + let anchor = option.replace('.', "_"); + after.push_str(&format!("[{option}]: ../settings.md#{anchor}\n")); + options.insert(option); + } + if field.deprecated.is_some() { + eprintln!("Rule {rule_name} references deprecated option {option}."); + } + } + Some(_) => {} + None => { + panic!("Unknown option {option} referenced by rule {rule_name}"); + } + } + } + if !after.is_empty() { out.push('\n'); out.push('\n'); From 352fada8d5e484a32d0d6aec3a7f8fe04863ac9d Mon Sep 17 00:00:00 2001 From: augustelalande Date: Wed, 20 Mar 2024 22:44:09 -0400 Subject: [PATCH 2/3] only deal with matching options --- crates/ruff_dev/src/generate_docs.rs | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/crates/ruff_dev/src/generate_docs.rs b/crates/ruff_dev/src/generate_docs.rs index 5165a9694e80c..5cd73871b05e4 100644 --- a/crates/ruff_dev/src/generate_docs.rs +++ b/crates/ruff_dev/src/generate_docs.rs @@ -147,20 +147,14 @@ fn process_documentation(documentation: &str, out: &mut String, rule_name: &str) let re = Regex::new(r"\[`([^`]*?)`]\[(.*?)]").unwrap(); for (_, [option, _]) in re.captures_iter(&documentation).map(|c| c.extract()) { - match Options::metadata().find(option) { - Some(OptionEntry::Field(field)) => { - if !options.contains(option) { - let anchor = option.replace('.', "_"); - after.push_str(&format!("[{option}]: ../settings.md#{anchor}\n")); - options.insert(option); - } - if field.deprecated.is_some() { - eprintln!("Rule {rule_name} references deprecated option {option}."); - } + if let Some(OptionEntry::Field(field)) = Options::metadata().find(option) { + if !options.contains(option) { + let anchor = option.replace('.', "_"); + after.push_str(&format!("[{option}]: ../settings.md#{anchor}\n")); + options.insert(option); } - Some(_) => {} - None => { - panic!("Unknown option {option} referenced by rule {rule_name}"); + if field.deprecated.is_some() { + eprintln!("Rule {rule_name} references deprecated option {option}."); } } } From 9b6f2d1f7c015ed67490e6171923e2d137232182 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 21 Mar 2024 12:24:28 -0400 Subject: [PATCH 3/3] Tweaks --- crates/ruff_dev/src/generate_docs.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/ruff_dev/src/generate_docs.rs b/crates/ruff_dev/src/generate_docs.rs index 5cd73871b05e4..987b485db94cc 100644 --- a/crates/ruff_dev/src/generate_docs.rs +++ b/crates/ruff_dev/src/generate_docs.rs @@ -98,21 +98,22 @@ pub(crate) fn main(args: &Args) -> Result<()> { fn process_documentation(documentation: &str, out: &mut String, rule_name: &str) { let mut in_options = false; let mut after = String::new(); - let mut options = HashSet::new(); + let mut referenced_options = HashSet::new(); // HACK: This is an ugly regex hack that's necessary because mkdocs uses // a non-CommonMark-compliant Markdown parser, which doesn't support code // tags in link definitions // (see https://github.com/Python-Markdown/markdown/issues/280). - let documentation = Regex::new(r"\[`([^`]*?)`]($|[^\[\(])") - .unwrap() - .replace_all(documentation, |caps: &Captures| { + let documentation = Regex::new(r"\[`([^`]*?)`]($|[^\[(])").unwrap().replace_all( + documentation, + |caps: &Captures| { format!( "[`{option}`][{option}]{sep}", option = &caps[1], sep = &caps[2] ) - }); + }, + ); for line in documentation.split_inclusive('\n') { if line.starts_with("## ") { @@ -136,7 +137,7 @@ fn process_documentation(documentation: &str, out: &mut String, rule_name: &str) let anchor = option.replace('.', "_"); out.push_str(&format!("- [`{option}`][{option}]\n")); after.push_str(&format!("[{option}]: ../settings.md#{anchor}\n")); - options.insert(option); + referenced_options.insert(option); continue; } @@ -148,10 +149,9 @@ fn process_documentation(documentation: &str, out: &mut String, rule_name: &str) let re = Regex::new(r"\[`([^`]*?)`]\[(.*?)]").unwrap(); for (_, [option, _]) in re.captures_iter(&documentation).map(|c| c.extract()) { if let Some(OptionEntry::Field(field)) = Options::metadata().find(option) { - if !options.contains(option) { + if referenced_options.insert(option) { let anchor = option.replace('.', "_"); after.push_str(&format!("[{option}]: ../settings.md#{anchor}\n")); - options.insert(option); } if field.deprecated.is_some() { eprintln!("Rule {rule_name} references deprecated option {option}.");