c.tmbundle icon indicating copy to clipboard operation
c.tmbundle copied to clipboard

Requesting update to C/C++ indentation rules for more common switch statement formatting

Open theosib opened this issue 2 years ago • 0 comments

TextMate auto-indents C and C++ in an unexpected way. To be specific, I would like to get:

This (1):

switch (x) {
    case 3:
        sdrhit;
    case 4:
        dfsdkfsd;
}

Or this (2):

switch (x) {
case 3:
    sdrhit;
case 4:
    dfsdkfsd;
}

Unfortunately, instead, I get this:

switch (x) {
    case 3:
    sdrhit;
    case 4:
    dfsdkfsd;
}

I figured out how to do this, and here are the necessary edits to the C indent rules:

{   increaseIndentPattern = '(?x)
        ^ .* \{ [^}"'']* $
    |   ^ \s* (public|private|protected): \s* $
    |   ^ \s* @(public|private|protected) \s* $
    |   ^ \s* \{ \} $
    |   ^ \s* (case\b .+ |default):     # NEW!
    ';
    decreaseIndentPattern = '(?x)
        ^ \s* ( (?! \S.* /[*] ) .* [*]/ \s* )? \}
    |   ^ \s* (public|private|protected): \s* $
    |   ^ \s* @(public|private|protected) \s* $
    |   (case\b .+ |default): \s* (/[/*] .*)? $     # NEW!
    ';
    indentNextLinePattern = '(?x)^
        (?! .* [;:{},] \s*                  # do not indent when line ends with ;, :, {, }, or comma
            ( // .* | /[*] .* [*]/ \s* )? $ #  …account for potential trailing comment
        |   @(public|private|protected)     # do not indent after obj-c data access keywords
        )
        .                                  # the negative look-ahead above means we don’t care about what we match here
    ';
    unIndentedLinePattern = '^\s*((/\*|\*/|//|template\b.*?>(?!\(.*\))|@protocol|@optional|@interface(?!.*\{)|@implementation|@end).*)?$';
    zeroIndentPattern = '^\s*#';
}

Even if the devs don't want to make this the default, anyone else encountering this problem could refer to this bug report if they want to change it themselves.

theosib avatar Oct 25 '23 23:10 theosib