Skip to content

Option: format named macro usage with standard module/function/... formatter #5254

Open
@dhardy

Description

@dhardy

So, #8 says "format macros" (hard in general); #3445 suggests formatting macros using { .. } braces as a Block; #2905 proposes formatting expression-like macros...

... general solutions are hard and probably require matching the macro by path (including crate of origin) ...

... can we get half-way there with per-project configuration?

Configuration

So: could we opt-in to macro formatting by adding this to lib.rs?

// example: widget! macro (used below)
#![rustfmt::fmt_as_module(widget)]

(Or to the rustfmt.toml?)

Example: widget

KAS (master) has a widget! macro which can be formatted like a module:

widget! {
    /// A frame around content
    ///
    /// This widget provides a simple abstraction: drawing a frame around its
    /// contents.
    #[autoimpl(Deref, DerefMut on self.inner)]
    #[autoimpl(class_traits where W: trait on self.inner)]
    #[derive(Clone, Debug, Default)]
    #[handler(msg = <W as Handler>::Msg)]
    #[widget{
        layout = frame(self.inner, kas::theme::FrameStyle::Frame);
    }]
    pub struct Frame<W: Widget> {
        #[widget_core]
        core: CoreData,
        #[widget]
        pub inner: W,
    }

    // NOTE: `impl Self` is not valid Rust syntax, but rustfmt handles it fine
    impl Self {
        /// Construct a frame
        #[inline]
        pub fn new(inner: W) -> Self {
            Frame {
                core: Default::default(),
                inner,
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions