csharpier icon indicating copy to clipboard operation
csharpier copied to clipboard

Weird indentation on patterns in switch

Open belav opened this issue 2 years ago • 1 comments

The current behavior looks kinda wrong. But indenting a single time like 2 looks worse. I think going with 3 and 4 would make sense, I believe we have other instances where extra indentation is added.

switch (currentDoc)
{
    // 1. current formatting
    case LeadingComment
    or TrailingComment:
        if (output.Length > 0 && currentMode is not PrintMode.ForceFlat)
            returnFalseIfMoreStringsFound = true;
        break;
    // 2. indenting a single time lines it up with the body
    case LeadingComment
        or TrailingComment:
        if (output.Length > 0 && currentMode is not PrintMode.ForceFlat)
            returnFalseIfMoreStringsFound = true;
        break;
    // 3. should probably not break for this case
    case LeadingComment or TrailingComment:
        if (output.Length > 0 && currentMode is not PrintMode.ForceFlat)
            returnFalseIfMoreStringsFound = true;
        break;
    // 4. but if it gets long enough to break, maybe a double indent?
    case LeadingComment
            or TrailingComment:
        if (output.Length > 0 && currentMode is not PrintMode.ForceFlat)
            returnFalseIfMoreStringsFound = true;
        break;
}


belav avatar Dec 21 '23 17:12 belav

I noticed this today. My patterns are really short [] or null. I feel like or should not be a forced line wrap, only a soft one (if line is too long).

Expected:

int Count(List<object> list)
{
  switch (list)
  {
    case [] or null: 
        return 0;
    case [_]: 
        return 1;
    default:
        return list.Count;
  };
}

Output of 0.27.2:

int Count(List<object> list)
{
    switch (list)
    {
        case []
        or null:
            return 0;
        case [_]:
            return 1;
        default:
            return list.Count;
    }
}

If it has to wrap, both options (indented or not) look a little weird. Personally I think I prefer without indentation (aligned with case), so that it's not aligned with the case statement. Likewise in switch expressions. Although when there are braces (infrequent?), indented looks good 🤔

// When it has to wrap:
switch (x) 
{
  case 1
  or 2:
  case 3:
    DoSomething();
    break;
}

int i = x switch
{
  []
  or null 
    => 0,
  default => 1,    
};

// Although this looks good
switch (x) 
{
  case 1
    or 2:
  case 3
    or 4:
  {
    DoSomething();
    break;
  }
}

jods4 avatar Feb 08 '24 16:02 jods4