Open mesqueeb opened 8 months ago
@nicklockwood I would like to try my hand at contributing this rule.
questions:
--indentationstyle prettier
rule or be more specifically aimed at function parameters.
indentationstyle
approach to the rule is better, I can start by making sure it works well for the function parameters, open a draft PR, keep on building upon that for the other indentation parts.--print-width
option which is pretty important to decide if it should format the function params like case 1 or case 2 above. Since Prettier has less freedom, the existence of a max-width is important for prettier to decide wether to put stuff on one line or forcing multiple lines. Do you have any advice on implementing this as well, or shall I perhaps start my implementation with a fixed 100
character limit per line, and we can see in the future how to make this a user-settable option.Thanks a lot! I look forward to start contributing the strict implementation similar to prettier. 🎉
@mesqueeb in general I'm not opposed to making the function wrapping rule stricter, or at least supporting a mode of operation where it behaves more strictly. I think some of the behavior you want is actually already implemented, it's just behind options that are turned off by default.
The rule that handles wrapping function parameters is called wrapArguments
and it has a lot of configuration options, which you can see here: https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#wrapArguments
Setting the --wrapparameters before-first
option (the default is preserve
) handles your Case 1 already.
What it won't do is unwrap function arguments again if they would fit on one line (your Case 2). That should possibly be implemented by a separate rule called unwrapArguments
. The rules wouldn't conflict because only one case would ever apply to a given function, depending on the specified max line length.
- Prettier has a --print-width option
In SwiftFormat this option is called --maxwidth
. It's listed undet the options for the wrap
rule: https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#wrap
(The wrap rule internally invokes the wrapArguments
rule for any line that exceeds the max width)
I'm hoping to find a way to have SwiftFormat be more like Prettier and give way less freedom than it currently does.
There's many cases in SwiftFormat where depending on where you put a line break it will allow a whole variety of formatting methods. Eg. putting a line break before or after any single function param will make SwiftFormat auto format those params in different ways. With prettier there's only 2 ways, all params on one line if it fits, or all params with linebreaks and a single indentation.
What's the best way for me to achieve this? Is there some sort of plugin functionality for SwiftFormat so we could have a more restrictive rule set like Prettier? Ps: I use
SwiftFormat for Xcode.app
for my formatting currently.Case 1
before format:
current after format:
desired after format:
Case 2
before format:
current after format:
desired after format: