Skip to content

Commit 01843af

Browse files
authored
Support option group documentation (#7593)
1 parent 2ecf597 commit 01843af

File tree

5 files changed

+62
-3
lines changed

5 files changed

+62
-3
lines changed

crates/ruff_dev/src/generate_options.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ pub(crate) fn generate() -> String {
1616
fn generate_set(output: &mut String, set: &Set) {
1717
writeln!(output, "### {title}\n", title = set.title()).unwrap();
1818

19+
if let Some(documentation) = set.metadata().documentation() {
20+
output.push_str(documentation);
21+
output.push('\n');
22+
output.push('\n');
23+
}
24+
1925
let mut visitor = CollectOptionsVisitor::default();
2026
set.metadata().record(&mut visitor);
2127

crates/ruff_macros/src/config.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ use syn::{
1010
};
1111

1212
pub(crate) fn derive_impl(input: DeriveInput) -> syn::Result<proc_macro2::TokenStream> {
13-
let DeriveInput { ident, data, .. } = input;
13+
let DeriveInput {
14+
ident,
15+
data,
16+
attrs: struct_attributes,
17+
..
18+
} = input;
1419

1520
match data {
1621
Data::Struct(DataStruct {
@@ -50,12 +55,39 @@ pub(crate) fn derive_impl(input: DeriveInput) -> syn::Result<proc_macro2::TokenS
5055
};
5156
}
5257

53-
Ok(quote! {
58+
let docs: Vec<&Attribute> = struct_attributes
59+
.iter()
60+
.filter(|attr| attr.path().is_ident("doc"))
61+
.collect();
62+
63+
// Convert the list of `doc` attributes into a single string.
64+
let doc = dedent(
65+
&docs
66+
.into_iter()
67+
.map(parse_doc)
68+
.collect::<syn::Result<Vec<_>>>()?
69+
.join("\n"),
70+
)
71+
.trim_matches('\n')
72+
.to_string();
73+
74+
let documentation = if doc.is_empty() {
75+
None
76+
} else {
77+
Some(quote!(
78+
fn documentation() -> Option<&'static str> {
79+
Some(&#doc)
80+
}
81+
))
82+
};
5483

84+
Ok(quote! {
5585
impl crate::options_base::OptionsMetadata for #ident {
5686
fn record(visit: &mut dyn crate::options_base::Visit) {
5787
#(#output);*
5888
}
89+
90+
#documentation
5991
}
6092
})
6193
}

crates/ruff_workspace/src/options.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,8 +2412,15 @@ impl OptionsMetadata for FormatOrOutputFormat {
24122412
fn record(visit: &mut dyn Visit) {
24132413
FormatOptions::record(visit);
24142414
}
2415+
2416+
fn documentation() -> Option<&'static str> {
2417+
FormatOptions::documentation()
2418+
}
24152419
}
24162420

2421+
/// Experimental: Configures how `ruff format` formats your code.
2422+
///
2423+
/// Please provide feedback in [this discussion](https://github.com/astral-sh/ruff/discussions/7310).
24172424
#[derive(
24182425
Debug, PartialEq, Eq, Default, Serialize, Deserialize, ConfigurationOptions, CombineOptions,
24192426
)]

crates/ruff_workspace/src/options_base.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ pub trait OptionsMetadata {
1616
/// Visits the options metadata of this object by calling `visit` for each option.
1717
fn record(visit: &mut dyn Visit);
1818

19+
fn documentation() -> Option<&'static str> {
20+
None
21+
}
22+
1923
/// Returns the extracted metadata.
2024
fn metadata() -> OptionSet
2125
where
@@ -51,14 +55,18 @@ impl Display for OptionEntry {
5155
#[derive(Copy, Clone, Eq, PartialEq)]
5256
pub struct OptionSet {
5357
record: fn(&mut dyn Visit),
58+
doc: fn() -> Option<&'static str>,
5459
}
5560

5661
impl OptionSet {
5762
pub fn of<T>() -> Self
5863
where
5964
T: OptionsMetadata + 'static,
6065
{
61-
Self { record: T::record }
66+
Self {
67+
record: T::record,
68+
doc: T::documentation,
69+
}
6270
}
6371

6472
/// Visits the options in this set by calling `visit` for each option.
@@ -67,6 +75,11 @@ impl OptionSet {
6775
record(visit);
6876
}
6977

78+
pub fn documentation(&self) -> Option<&'static str> {
79+
let documentation = self.doc;
80+
documentation()
81+
}
82+
7083
/// Returns `true` if this set has an option that resolves to `name`.
7184
///
7285
/// The name can be separated by `.` to find a nested option.

ruff.schema.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)