SwiftFormat icon indicating copy to clipboard operation
SwiftFormat copied to clipboard

SwiftUI code format question

Open hapton opened this issue 2 years ago • 14 comments

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 avatar Apr 28 '23 12:04 hapton

@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 avatar May 01 '23 06:05 nicklockwood

@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 avatar May 03 '23 07:05 Parabak

@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.

nicklockwood avatar May 03 '23 07:05 nicklockwood

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 avatar May 03 '23 07:05 Parabak

@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 avatar May 03 '23 08:05 nicklockwood

@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 avatar May 03 '23 09:05 Parabak

@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.

nicklockwood avatar May 03 '23 10:05 nicklockwood

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 avatar May 04 '23 07:05 facumenzella

@facumenzella 👍 The thing is that we wanted to apply this only to SwiftUI files.

Parabak avatar May 04 '23 07:05 Parabak

@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.

nicklockwood avatar May 04 '23 08:05 nicklockwood

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 avatar May 04 '23 08:05 Parabak

@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

nicklockwood avatar May 04 '23 08:05 nicklockwood

Hey, is this ready to use?

berkaytheunicorn avatar Nov 26 '23 18:11 berkaytheunicorn

I haven't proceed with this, @facumenzella did you?

Parabak avatar Nov 27 '23 10:11 Parabak