rustfmt
rustfmt copied to clipboard
Option: format named macro usage with standard module/function/... formatter
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,
}
}
}
}
Thanks for opening this. Something similar has been proposed on at least a couple other threads (though I can't seem to find any them at the moment) and I've been meaning to pull that out into a more focused issue as this/some variant of this does seem like the most viable path forward.
Will dig back into the issue tracker to pull in some of the relevant discussion, but would like to use this issue to drive the solution topic forward :+1:
I have opened a discussion thread in the Rust Internals Forum related to this issue. If there is something constructive you could add to the discussion (criticism, possible solutions, concerns, use cases), please head through the link below. https://internals.rust-lang.org/t/discussion-adding-grammar-information-to-procedural-macros-for-proper-custom-syntax-support-in-the-toolchain/21496