textmate/php.tmbundle

switch statement not auto-indented correctly

ZevEisenberg opened this issue · 18 comments

When you declare a switch statement with switch[tab], the template looks like this:

switch (variable) {
    case 'value':
        # code...
        break;

    default:
        # code...
        break;
}

The code below the case and default is indented. However, when I try to add a new case by pressing return at the end of the first break;, everything stays indented one more to the right than the first case:

switch (variable) {
    case 'value':
        echo 'hello';
        break;
        case 2:
        echo 'hello';
        break;

    default:
        echo 'hello';
        break;
}

When I use Command-Option-[ to Indent Line / Section, most of the statement gets flattened and doesn't match the template:

switch (variable) {
    case 'value':
    echo 'hello';
    break;
    case 2:
    echo 'hello';
    break;

    default:
    echo 'hello';
    break;
}

I would like the auto-indent to make it look like this:

switch (variable) {
    case 'value':
        echo 'hello';
        break;
    case 2:
        echo 'hello';
        break;

    default:
        echo 'hello';
        break;
}

Brace-less case statements are mentioned (as not possible) in 7e58242.

Ah, I had forgotten about brace-ful case statements. So if I want indentation, I should modify my switch snippet to contain braces?

That should work.

It's a decent enough workaround, though I would prefer auto-indentation of brace-less switch statements as in Xcode.

Did you ever rewrite it? I am having the same problem and it's a pain in the ass. Could you post it up if you did?

I looked at it briefly, but I didn't take the time to figure out how to do it. I've since switched my PHP project over to python because of http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/

I hadn't seen that. Sounds pretty reasonable, but he admits that it's inconsistent and verbose. I had gotten used to those things, and I presume he has as well (given that his site is about PHP). I'm happy that PHP works for him. It would work for me, too, if I were forced to use it.

I'm going to use Python on this particular project because it's a hobby project, it's small, and I've been hearing good things about Python. After converting most of my code over, it's looking like the Python version is less verbose and easier to work on, but that may just be that I'm a better programmer than I was when I started the PHP project a year ago.

This problem persists to TextMate 2
Maybe there's a way to solve it now?
Is there a current issue open about this?

@aaronbauman No there haven't been any additions to TextMate to allow this to work.

I'm curious why very similar (it seems) indenting works just fine in yaml.tmbundle, but can't work on brace-less case statements. I currently get glassy-eyed quickly looking through the tmPreferences plists, but as I've been switching between editing YAML files and writing PHP switch statements on some values within those YAML files ... it just jumped out at me that they seemed pretty similar.

I'm obviously missing something, but thought I'd mention this in case those of you who are more familiar with the way indentation preferences work in TM2 might get an idea from this. Hope-hope. 🙏

YAML is a very different language, indentation based. We disable indentation corrections for the entire language for that reason. For PHP we want corrections and they work well in most all cases, this is the one case where they get in the way.

Thanks for that explanation, @infininight. I knew it had to be something like that. And I take it that we're not able to disable indentation corrections inside of a case?

I'd like to reopen this bug on account of the fact that it's been 4 years since it was reported, and my suggestion is that it can be fixed by having an option to disable auto-corrected indenting. Or we can share mods to the auto-correction for people who want to change it.

I'm told that the original submitter's example is how indent is set up for Swift. Apparently any reasons for not doing this correctly before are no longer true. So can we get C's switch statement indentation to behave the same as Swift?

Swift decreases indent for the case labels, so this is how OP’s example will indent using Swift’s rules:

switch (variable) {
case 'value':
    # code...
    break;

default:
    # code...
    break;
}

We do this for Swift because it seems to be Apple’s recommended style, and in Swift braces are never used with switch/case-statements. In PHP, were we to adopt the above style, it would be an issue when using braces with the switch/case-statements.

Well, my problem is that TextMate keeps fighting against having any kind of sensible indenting style in C/C++. I'm not sure how you want to handle PHP, but it's really broken for C languages. I would LOVE it if the C/C++ rules were to behave like Swift. I don't care if it indents case and the code further or keeps case aligned with switch. Either one would be joyous to me.

How TextMate indents your code is based on settings.

For C/C++ you can reach the settings via the bundle editor, select C →
Settings → Indentation Rules.

I created these rules to match my style, so while you might consider
them broken, you will have to define exactly what your problem is,
though you’re currently commenting on a closed issue belonging to the
PHP bundle (i.e. it has nothing to do with C/C++).

If you are unable to setup TextMate to indent as you want, the mailing
list or IRC channel is a better place to ask for help.

On 13 Jan 2016, at 20:53, theosib wrote:

Well, my problem is that TextMate keeps fighting against having any
kind of sensible indenting style in C/C++. I'm not sure how you want
to handle PHP, but it's really broken for C languages. I would LOVE
it if the C/C++ rules were to behave like Swift. I don't care if it
indents case and the code further or keeps case aligned with switch.
Either one would be joyous to me.


Reply to this email directly or view it on GitHub:
#19 (comment)