SwiftUI code format question
How can we make swiftFormat formatting
Text("ssssssssssss").background(Color(hex: 0x333333)).font(Font.caption).padding(.horizontal, 8).padding(.trailing, 8)
to
Text("ssssssssssss")
.background(Color(hex: 0x333333))
.font(Font.caption)
.padding(.horizontal, 8)
.padding(.trailing, 8)
This is swiftUI code.Code wrapping looks clearer。
@hapton SwiftFormat doesn't currently have a rule for this I'm afraid (mainly because I'm still not sure what the best way to format SwiftUI is myself!)
@nicklockwood we have the same question. With one more use case. Format this:
Group {
Text("test")
}.padding(.horizontal, 8)
to
Group {
Text("test")
}
.padding(.horizontal, 8)
Will you accept the pull request if it will be done as Opt-in Rule (disabled by default) ?
@Parabak I'm not sure I understand the proposed rule here - you want to indent all chained methods after a } by one additional space?
EDIT: oh, never mind, you updated it. Yes that seems reasonable - just need to think about how the rule/option would be named.
For All SwiftUI modifiers:
- ~~should be preceded with line break~~
- ~~apply indent if modifier is not preceded with
}or)in case of multiline modifier~~
if the only thing on a line is a closing parenthesis (either } or )) next line should start at the same column. Otherwise next line should start with intend.
Where SwiftUI modifier is smth which starts from . followed by name with opening parenthesis (either { or ()
There is one more example desired formatting
Text(
"long-multiline-text"
)
.textLinkAction {
}
@Parabak I think the last one is already covered by the emptyBraces rule (unless you want different behavior when the braces are part of a method chain?)
@nicklockwood with last examples I tried to illustrate the fact that after ) modifier textLinkAction starts from new line and without indent.
regarding naming: maybe --wrapSwiftUIModifiers ? either enabled or disabled.
@Parabak I don't think this has anything to do with SwiftUI per-se, it's just more common to see these method chains in SwiftUI code than other places.
Probably a new rule like wrapChainedMemberAccess or something. I'll think about it.
I've created this issue a while ago. In SwiftLint is called multiline-function-chains.
I was planning to work on this at some point this month =)
@facumenzella 👍 The thing is that we wanted to apply this only to SwiftUI files.
@Parabak it's probably feasible to add a swiftui-only option once the rule is done, but you could also manage that by using a separate .swiftformat file for your Views subdirectory.
Right, the thing is that we have around 30 such subdirectories. Most likely we can use a symlink there, but having this as an option will simplify setup.
@facumenzella, if you need help, please ping me. I can add swiftui-only option by my own, as @nicklockwood suggested.
@Parabak swiftformat supports wildcard/glob syntax for matching files, so you could run the command with a different config for any file matching **/View.swift or **/View/.swift
Hey, is this ready to use?
I haven't proceed with this, @facumenzella did you?