Skip to content

Commit

Permalink
glib-macros: add # Notify to the property documentation headings
Browse files Browse the repository at this point in the history
This is similar to `# Getter` and `# Setter` but it documents the notification
function.
  • Loading branch information
carlosmn committed Feb 18, 2025
1 parent 3581e84 commit f7e7785
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/object_subclass/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ mod imp {
/// # Setter
///
/// You can change the surname of the author too if you want.
///
/// # Notify
///
/// Explicitly send a notification that the surname has changed.
#[property(get, set)]
surname: RefCell<String>,
}
Expand Down
6 changes: 6 additions & 0 deletions glib-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,8 @@ pub fn cstr_bytes(item: TokenStream) -> TokenStream {
///
/// 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.
///
/// You can also use a `# Notify` heading in order to set the public documentation of the automatically-generated `notify_$property` method.
///
/// ## Extension trait
/// You can choose to move the method definitions to a trait by using `#[properties(wrapper_type = super::MyType, ext_trait = MyTypePropertiesExt)]`.
/// The trait name is optional, and defaults to `MyTypePropertiesExt`, where `MyType` is extracted from the wrapper type.
Expand Down Expand Up @@ -1433,6 +1435,10 @@ pub fn cstr_bytes(item: TokenStream) -> TokenStream {
/// /// # Setter
/// ///
/// /// This is the comment for the setter of the `extra_comments` field.
/// ///
/// /// # Notify
/// ///
/// /// Sometimes you want to notify explicitly and here's where you explain that.
/// #[property(get, set)]
/// extra_comments: RefCell<bool>,
/// }
Expand Down
29 changes: 29 additions & 0 deletions glib-macros/src/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,9 @@ fn arrange_property_comments(comments: &[Attribute]) -> (Vec<&Attribute>, Vec<&A
let mut getter = vec![];
let mut setter = vec![];
let mut saw_section = false;
// This one we throw away as we're only looking for getter and setter
// comments here but we don't want to mix them up.
let mut notify = vec![];

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

fn extract_notify_docs(comments: &[Attribute]) -> Vec<&Attribute> {
let mut docs = vec![];
let mut in_section = false;
for attr in comments {
if let syn::Meta::NameValue(meta) = &attr.meta {
if let syn::Expr::Lit(expr) = &meta.value {
if let syn::Lit::Str(lit_str) = &expr.lit {
match lit_str.value().trim() {
"# Notify" => {
in_section = true;
}
"# Getter" | "# Setter" => in_section = false,
_ if in_section => docs.push(attr),
_ => {}
}
}
}
}
}

docs
}

fn expand_impl_getset_properties(props: &[PropDesc]) -> Vec<syn::ImplItemFn> {
let crate_ident = crate_ident_new();
let defs = props.iter().filter(|p| !p.is_overriding()).map(|p| {
Expand Down Expand Up @@ -695,7 +722,9 @@ fn expand_impl_notify_prop(wrapper_type: &syn::Path, props: &[PropDesc]) -> Vec<
let fn_ident = format_ident!("notify_{}", name_to_ident(&name));
let span = p.attrs_span;
let enum_ident = name_to_enum_ident(name.value());
let docs = extract_notify_docs(&p.comments);
parse_quote_spanned!(span=>
#(#docs)*
#[allow(dead_code)]
pub fn #fn_ident(&self) {
self.notify_by_pspec(
Expand Down

0 comments on commit f7e7785

Please sign in to comment.