Menu

#597 Preserve one-liners in switch

closed-fixed
None
2025-03-21
2025-03-21
Zenju
No

95% of use cases for "INDENT-OFF" in my code are for switch statements:
Astyle converts this:

std::wstring formatHttpError(int sc)
{
    switch (sc)
    {
        case 300: return L"Multiple choices.";
        case 301: return L"Moved permanently.";
        case 302: return L"Moved temporarily.";
        case 303: return L"See other";
        case 304: return L"Not modified.";
    }

    switch (c)
    {
        case '&': output += "&" ; break;
        case '"': output += """; break;
        case '<': output += "&lt;"  ; break;
        case '>': output += "&gt;"  ; break;
        default: output += c; break;
    }
}

to this:

std::wstring formatHttpError(int sc)
{
    switch (sc)
    {
        case 300:
            return L"Multiple choices.";
        case 301:
            return L"Moved permanently.";
        case 302:
            return L"Moved temporarily.";
        case 303:
            return L"See other";
        case 304:
            return L"Not modified.";
    }

    switch (c)
    {
        case '&':
            output += "&amp;" ;
            break;
        case '"':
            output += "&quot;";
            break;
        case '<':
            output += "&lt;"  ;
            break;
        case '>':
            output += "&gt;"  ;
            break;
        default:
            output += c;
            break;
    }
}

I prefer the former more succinct represenation. It would be great if Astyle had some option like "--preserve-switch-oneliners".

I guess there is no need/want to convert multi-line switch cases into a single line, but if they are already on a single line, they should be kept.

Discussion

  • Zenju

    Zenju - 2025-03-21

    My Astyle config used:

    --style=allman
    --suffix=none
    --keep-one-line-blocks
    --align-pointer=type
    --min-conditional-indent=0
    --max-continuation-indent=80
    --indent-switches
    --indent-col1-comments
    --indent-preproc-block
    --indent-preproc-define
    --pad-header
    --pad-comma
    --formatted
    --lineend=linux
    --indent-preproc-block
    --convert-tabs
    --preserve-ws
    
     
  • Zenju

    Zenju - 2025-03-21

    Sorry, this should have been filed under "Feature Requests", not "Bugs".

     
  • Zenju

    Zenju - 2025-03-21

    I just realized there is "--keep-one-line-statements", and it works for switches, too!!
    Ticket can be closed! :)

     
  • André Simon

    André Simon - 2025-03-21
    • status: open --> closed-fixed
    • assigned_to: André Simon
     

Log in to post a comment.

MongoDB Logo MongoDB