rust-lang / rust-lang/rustfmt

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

Open
#5,254 2 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-macros C-feature-request
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

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,
            }
        }
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the related discussions in issues #8, #3445, and #2905, then examine the proposed lib.rs attribute or rustfmt.toml configuration for the widget! example. Done would be an agreed design and implementation for opting in to formatting named macro usage like a standard module, including the configuration syntax and macro matching behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.