Skip to content

Commit f7e7785

Browse files
committed
glib-macros: add # Notify to the property documentation headings
This is similar to `# Getter` and `# Setter` but it documents the notification function.
1 parent 3581e84 commit f7e7785

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

examples/object_subclass/author.rs

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ mod imp {
2828
/// # Setter
2929
///
3030
/// You can change the surname of the author too if you want.
31+
///
32+
/// # Notify
33+
///
34+
/// Explicitly send a notification that the surname has changed.
3135
#[property(get, set)]
3236
surname: RefCell<String>,
3337
}

glib-macros/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,8 @@ pub fn cstr_bytes(item: TokenStream) -> TokenStream {
13481348
///
13491349
/// Doc comments preceding a `#[property]` attribute will be copied to the generated getter and setter methods. You can specify different comments by the getter and setter by using `# Getter` and `# Setter` headings. The text under the header will be copied to the respective method.
13501350
///
1351+
/// You can also use a `# Notify` heading in order to set the public documentation of the automatically-generated `notify_$property` method.
1352+
///
13511353
/// ## Extension trait
13521354
/// You can choose to move the method definitions to a trait by using `#[properties(wrapper_type = super::MyType, ext_trait = MyTypePropertiesExt)]`.
13531355
/// The trait name is optional, and defaults to `MyTypePropertiesExt`, where `MyType` is extracted from the wrapper type.
@@ -1433,6 +1435,10 @@ pub fn cstr_bytes(item: TokenStream) -> TokenStream {
14331435
/// /// # Setter
14341436
/// ///
14351437
/// /// This is the comment for the setter of the `extra_comments` field.
1438+
/// ///
1439+
/// /// # Notify
1440+
/// ///
1441+
/// /// Sometimes you want to notify explicitly and here's where you explain that.
14361442
/// #[property(get, set)]
14371443
/// extra_comments: RefCell<bool>,
14381444
/// }

glib-macros/src/properties.rs

+29
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,9 @@ fn arrange_property_comments(comments: &[Attribute]) -> (Vec<&Attribute>, Vec<&A
578578
let mut getter = vec![];
579579
let mut setter = vec![];
580580
let mut saw_section = false;
581+
// This one we throw away as we're only looking for getter and setter
582+
// comments here but we don't want to mix them up.
583+
let mut notify = vec![];
581584

582585
// We start with no tags so if the programmer doesn't split the comments we can still arrange them.
583586
let mut current_section = &mut untagged;
@@ -598,6 +601,7 @@ fn arrange_property_comments(comments: &[Attribute]) -> (Vec<&Attribute>, Vec<&A
598601
current_section = &mut setter;
599602
saw_section = true;
600603
}
604+
"# Notify" => current_section = &mut notify,
601605
_ => current_section.push(attr),
602606
}
603607
}
@@ -613,6 +617,29 @@ fn arrange_property_comments(comments: &[Attribute]) -> (Vec<&Attribute>, Vec<&A
613617
(getter, setter)
614618
}
615619

620+
fn extract_notify_docs(comments: &[Attribute]) -> Vec<&Attribute> {
621+
let mut docs = vec![];
622+
let mut in_section = false;
623+
for attr in comments {
624+
if let syn::Meta::NameValue(meta) = &attr.meta {
625+
if let syn::Expr::Lit(expr) = &meta.value {
626+
if let syn::Lit::Str(lit_str) = &expr.lit {
627+
match lit_str.value().trim() {
628+
"# Notify" => {
629+
in_section = true;
630+
}
631+
"# Getter" | "# Setter" => in_section = false,
632+
_ if in_section => docs.push(attr),
633+
_ => {}
634+
}
635+
}
636+
}
637+
}
638+
}
639+
640+
docs
641+
}
642+
616643
fn expand_impl_getset_properties(props: &[PropDesc]) -> Vec<syn::ImplItemFn> {
617644
let crate_ident = crate_ident_new();
618645
let defs = props.iter().filter(|p| !p.is_overriding()).map(|p| {
@@ -695,7 +722,9 @@ fn expand_impl_notify_prop(wrapper_type: &syn::Path, props: &[PropDesc]) -> Vec<
695722
let fn_ident = format_ident!("notify_{}", name_to_ident(&name));
696723
let span = p.attrs_span;
697724
let enum_ident = name_to_enum_ident(name.value());
725+
let docs = extract_notify_docs(&p.comments);
698726
parse_quote_spanned!(span=>
727+
#(#docs)*
699728
#[allow(dead_code)]
700729
pub fn #fn_ident(&self) {
701730
self.notify_by_pspec(

0 commit comments

Comments
 (0)