Skip to content

Don't format fn keywords when not selected with --file-lines#6886

Open
randomPoison wants to merge 8 commits intorust-lang:mainfrom
randomPoison:issue-6868-before-fn
Open

Don't format fn keywords when not selected with --file-lines#6886
randomPoison wants to merge 8 commits intorust-lang:mainfrom
randomPoison:issue-6868-before-fn

Conversation

@randomPoison
Copy link
Copy Markdown
Contributor

Allow --file-lines to skip formatting the leading keywords for a function definition (pub, unsafe, extern, etc.) when not selected. Part of #6868.

The behavior I chose is to treat all of the keywords before fn as a single unit: We either format all of them, or we format none of them. This seemed like the most reasonable approach because rustfmt will always put all of the keywords on one line, so it didn't make sense to try to support formatting only a couple of the keywords. Treating them all as an indivisible block also keeps the logic simpler here, since there's only one span we have to check for.

I also made a minor tweak to span_before_last to allow it to handle multi-character needles. Previously it was only used to find single character needles, and was hardcoding the length of the needle as 1. I wanted to use it to find the span before the fn keyword, so I've updated it to take the actual length of the needle into account.

@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label May 7, 2026
@ytmimi ytmimi added the S-on-hold Status: PR/issue is on hold (due to bandwidth/prioritization/need discussions etc.). label May 8, 2026
@ytmimi
Copy link
Copy Markdown
Contributor

ytmimi commented May 8, 2026

Going to mark this as on-hold for now. There's some ongoing work when it comes to formatting these "headers" (visibility, constness, asyncness, etc.) and I'd want us to apply that to formatting fns before we try to move forward with these changes.

@randomPoison
Copy link
Copy Markdown
Contributor Author

Hm, I would think that these changes are pretty orthogonal to any decisions about how exactly to format the function keywords, since this is just determining if those keywords are selected with --file-lines without altering how they're actually laid out. But I see how decisions there could impact what exactly we want to do here, e.g. if we end up wanting more fine-grained formatting of the header keywords.

Can you point me to where the relevant work/discussion is happening? I'd like to follow along with it so I can figure out how it'll interact with --file-lines.

Also, do you know if there are any related discussions that would impact incorporating --file-lines into the rest of the function signature? I'd like to continue implementing that, but if there's pending design/discussion happening then I can hold off on doing that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-on-hold Status: PR/issue is on hold (due to bandwidth/prioritization/need discussions etc.). S-waiting-on-review Status: awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants