nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.92k stars 639 forks source link

Line breaks at generic types #1501

Open mnazirov opened 1 year ago

mnazirov commented 1 year ago

Hello @nicklockwood ,   I have the following case where 0.48.11 SwiftFormat line breaks at generic types. Adding in the symbol exception didn't help   Before formatting

class SectionDatal<Model, Footer: UpdatableHeaderFooterView<Model>>: SectionDataProvider<Model, UpdatableHeaderFooterView<Model>, Footer> {
    ...
}

 

After formatting

class SectionData<Model, Footer: UpdatableHeaderFooterView<Model>>: SectionDataProvider<
    Model,
    UpdatableHeaderFooterView<Model>,
    Footer
> {
    ...
}

 

Before formatting

func createDestinationStep() -> DestinationStep<SomeFactory.ViewController, SomeFactory.Context> {
    ...
}

 

After formatting

func createDestinationStep() -> DestinationStep<
    SomeFactory.ViewController,
    SomeFactory.Context
> {
    ...
}

 

Configurations

# format options
--rules wrapArguments
--wraparguments before-first
--maxwidth 130
--nowrapoperators <
nicklockwood commented 1 year ago

@mnazirov that's a super-old version. Can you check that it's still happening on 0.51.15 before I investigate further? Thanks.

mnazirov commented 1 year ago

@nicklockwood Updated to version 0.51.15, but the issue with line breaks in generic types still persists   Before formatting

func createConnectScreen() -> DestinationStep<NewConnectFactory.ViewController, NewConnectFactory.Context> {
   ...
}

  After formatting

func createConnectScreen() -> DestinationStep<
        NewConnectFactory.ViewController,
        NewConnectFactory.Context
> {
   ...
}
nicklockwood commented 1 year ago

@mnazirov thanks for double checking. The issue is that --nowrapoperators only applies to operators, and < isn't considered an operator by SwiftFormat when it's used as a bracket in a generics list.

It's not especially difficult for me to change that behavior, but I'm not sure if that would really solve your problem in this case, as it would just wrap at the , instead, which presumably isn't what you'd want.

What is the behavior you'd want in this case? Where should that line wrap?

mnazirov commented 1 year ago

@nicklockwood Thank you for the detailed explanation. I've tried to ensure only the method parameters wrap, while the return types of generics remain unchanged.

nicklockwood commented 1 year ago

@mnazirov sure, I understand that. But in the example above there are no method parameters, so is your desired behavior that the line should just not wrap at all, even if it exceeds your specified max line width?

mnazirov commented 1 year ago

@nicklockwood Yep, I want the line breaks to only occur at method arguments. Can you please advise how I can achieve this result?)

Before formatting

func createConnectScreen(argument: Int, argument2: Int) -> DestinationStep<NewConnectFactory.ViewController, NewConnectFactory.Context> {
   ...
}

Expected formatting result

func createConnectScreen(
    argument: Int, 
    argument2: Int
) -> DestinationStep<NewConnectFactory.ViewController, NewConnectFactory.Context> {
   ...
}
nicklockwood commented 1 year ago

@nicklockwood I think there's no way to do this currently other than to disable the wrap rule.

mnazirov commented 1 year ago

Thank you so much for the speedy reply)