belav / csharpier

CSharpier is an opinionated code formatter for c#.
https://csharpier.com
MIT License
1.41k stars 98 forks source link

Switch expressions should be broken if one line is too long #1302

Open Nixxen opened 4 months ago

Nixxen commented 4 months ago

Switch expressions (as with functions) should be broken if one line is too long to maintain readability. This is an example of a switch expression return. We have the line width set to 120 characters.

Input:

            return operation switch
            {
                GenericOperationEnum.OperationA => await GetOperationATimelineAsync(timelineRequest),
                GenericOperationEnum.OperationB => await GetOperationBTimelineAsync(timelineRequest),
                GenericOperationEnum.OperationC => await GetOperationCTimelineAsync(timelineRequest),
                GenericOperationEnum.OperationDLongerNameHere => await GetOperationDLongerNameHereTimelineAsync(timelineRequest),
                GenericOperationEnum.OperationELongerNameHere => await GetOperationELongerNameHereTimelineAsync(timelineRequest),
                GenericOperationEnum.OperationFLongerNameHere => await GetOperationFLongerNameHereTimelineAsync(timelineRequest),
                GenericOperationEnum.OperationG => await GetOperationGTimelineAsync(timelineRequest),
                _ => new() { await GetGenericTimelineDataSeriesAsync(timelineRequest) },
            };

Output:

            return operation switch
            {
                GenericOperationEnum.OperationA => await GetOperationATimelineAsync(timelineRequest),
                GenericOperationEnum.OperationB => await GetOperationBTimelineAsync(timelineRequest),
                GenericOperationEnum.OperationC => await GetOperationCTimelineAsync(timelineRequest),
                GenericOperationEnum.OperationDLongerNameHere
                    => await GetOperationDLongerNameHereTimelineAsync(timelineRequest),
                GenericOperationEnum.OperationELongerNameHere
                    => await GetOperationELongerNameHereTimelineAsync(timelineRequest),
                GenericOperationEnum.OperationFLongerNameHere
                    => await GetOperationFLongerNameHereTimelineAsync(timelineRequest),
                GenericOperationEnum.OperationG => await GetOperationGTimelineAsync(timelineRequest),
                _ => new() { await GetGenericTimelineDataSeriesAsync(timelineRequest) },
            };

Expected behavior:

            return operation switch
            {
                GenericOperationEnum.OperationA
                    => await GetOperationATimelineAsync(timelineRequest),
                GenericOperationEnum.OperationB
                    => await GetOperationBTimelineAsync(timelineRequest),
                GenericOperationEnum.OperationC
                    => await GetOperationCTimelineAsync(timelineRequest),
                GenericOperationEnum.OperationDLongerNameHere
                    => await GetOperationDLongerNameHereTimelineAsync(timelineRequest),
                GenericOperationEnum.OperationELongerNameHere
                    => await GetOperationELongerNameHereTimelineAsync(timelineRequest),
                GenericOperationEnum.OperationFLongerNameHere
                    => await GetOperationFLongerNameHereTimelineAsync(timelineRequest),
                GenericOperationEnum.OperationG
                    => await GetOperationGTimelineAsync(timelineRequest),
                _ 
                    => new() { await GetGenericTimelineDataSeriesAsync(timelineRequest) },
            };