-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor Options
representation
#7591
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,68 @@ | ||
//! Generate a Markdown-compatible listing of configuration options for `pyproject.toml`. | ||
//! | ||
//! Used for <https://docs.astral.sh/ruff/settings/>. | ||
use itertools::Itertools; | ||
use std::fmt::Write; | ||
|
||
use ruff_workspace::options::Options; | ||
use ruff_workspace::options_base::{OptionEntry, OptionField}; | ||
use ruff_workspace::options_base::{OptionField, OptionSet, OptionsMetadata, Visit}; | ||
|
||
pub(crate) fn generate() -> String { | ||
let mut output = String::new(); | ||
generate_set(&mut output, &Set::Toplevel(Options::metadata())); | ||
|
||
output | ||
} | ||
|
||
fn generate_set(output: &mut String, set: &Set) { | ||
writeln!(output, "### {title}\n", title = set.title()).unwrap(); | ||
|
||
let mut visitor = CollectOptionsVisitor::default(); | ||
set.metadata().record(&mut visitor); | ||
|
||
let (mut fields, mut sets) = (visitor.fields, visitor.groups); | ||
|
||
fields.sort_unstable_by(|(name, _), (name2, _)| name.cmp(name2)); | ||
sets.sort_unstable_by(|(name, _), (name2, _)| name.cmp(name2)); | ||
Comment on lines
+24
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know how I feel about sorting options. It disallows grouping options that are semantically related (all resolver settings) |
||
|
||
// Generate the fields. | ||
for (name, field) in &fields { | ||
emit_field(output, name, field, set.name()); | ||
output.push_str("---\n\n"); | ||
} | ||
|
||
// Generate all the sub-sets. | ||
for (set_name, sub_set) in &sets { | ||
generate_set(output, &Set::Named(set_name, *sub_set)); | ||
} | ||
} | ||
|
||
enum Set<'a> { | ||
Toplevel(OptionSet), | ||
Named(&'a str, OptionSet), | ||
} | ||
|
||
impl<'a> Set<'a> { | ||
fn name(&self) -> Option<&'a str> { | ||
match self { | ||
Set::Toplevel(_) => None, | ||
Set::Named(name, _) => Some(name), | ||
} | ||
} | ||
|
||
fn title(&self) -> &'a str { | ||
match self { | ||
Set::Toplevel(_) => "Top-level", | ||
Set::Named(name, _) => name, | ||
} | ||
} | ||
|
||
fn metadata(&self) -> &OptionSet { | ||
match self { | ||
Set::Toplevel(set) => set, | ||
Set::Named(_, set) => set, | ||
} | ||
} | ||
} | ||
|
||
fn emit_field(output: &mut String, name: &str, field: &OptionField, group_name: Option<&str>) { | ||
// if there's a group name, we need to add it to the anchor | ||
|
@@ -37,38 +96,18 @@ fn emit_field(output: &mut String, name: &str, field: &OptionField, group_name: | |
output.push('\n'); | ||
} | ||
|
||
pub(crate) fn generate() -> String { | ||
let mut output: String = "### Top-level\n\n".into(); | ||
|
||
let sorted_options: Vec<_> = Options::metadata() | ||
.into_iter() | ||
.sorted_by_key(|(name, _)| *name) | ||
.collect(); | ||
|
||
// Generate all the top-level fields. | ||
for (name, entry) in &sorted_options { | ||
let OptionEntry::Field(field) = entry else { | ||
continue; | ||
}; | ||
emit_field(&mut output, name, field, None); | ||
output.push_str("---\n\n"); | ||
} | ||
#[derive(Default)] | ||
struct CollectOptionsVisitor { | ||
groups: Vec<(String, OptionSet)>, | ||
fields: Vec<(String, OptionField)>, | ||
} | ||
|
||
// Generate all the sub-groups. | ||
for (group_name, entry) in &sorted_options { | ||
let OptionEntry::Group(fields) = entry else { | ||
continue; | ||
}; | ||
output.push_str(&format!("### {group_name}\n")); | ||
output.push('\n'); | ||
for (name, entry) in fields.iter().sorted_by_key(|(name, _)| name) { | ||
let OptionEntry::Field(field) = entry else { | ||
continue; | ||
}; | ||
emit_field(&mut output, name, field, Some(group_name)); | ||
output.push_str("---\n\n"); | ||
} | ||
impl Visit for CollectOptionsVisitor { | ||
fn record_set(&mut self, name: &str, group: OptionSet) { | ||
self.groups.push((name.to_owned(), group)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The old implementation only supports one level of nesting. This would have become a problem with |
||
|
||
output | ||
fn record_field(&mut self, name: &str, field: OptionField) { | ||
self.fields.push((name.to_owned(), field)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
writeln!
with trailing\n
looks strangeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree but everything else feels verbose just to get two newlines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I typically do two
writeln
for this but don't feel strongly.)