My switch/case blocks (in Java, for example) normally look something like this:
switch (foo) { case A: bar(); break;
default: baz(); }
Note how the lines after "case" and "default" are indented. (They're not flush with the line above.) I believe this is a pretty standard formatting convention.
However, it seems that TextMate doesn't know about this convention. If, for example, you were to type the above snippet, typing "case A:" and then hitting Return puts the cursor flush with the line above, rather than indented. Likewise, if you were to take the above snippet, copy some random text (that has a different indentation), then paste it between the "case A" and "bar" lines, TextMate's smart- indent feature makes the pasted text flush with "case A", rather than indented.
Considering that TextMate is smart about indentation in many other ways (such as when you type "{" and then hit Return), I was hoping it could be smart about indenting switch/case blocks, too. Is that possible? Perhaps there is something I could add to the language definition? Thanks,
Trevor
On 18/4/2006, at 23:13, Trevor Harmon wrote:
[ switch/case ] I believe this is a pretty standard formatting convention.
Yes, along with putting brackets around the code, keeping the break at same indent as the case, putting the break inside the brackets, etc. :)
[...] I was hoping it could be smart about indenting switch/case blocks, too. Is that possible? Perhaps there is something I could add to the language definition?
While the indenting rules can be controlled [1], it does not have enough flexibility to work with switch/case statements.
[1] http://macromates.com/textmate/manual/appendix#indentation_rules
On Apr 18, 2006, at 6:15 PM, Allan Odgaard wrote:
[ switch/case ] I believe this is a pretty standard formatting convention.
Yes, along with putting brackets around the code,
I don't see why that should be a problem. In Xcode, for instance, if you open a C file and start typing a switch/case block, it will indent after you type the case clause and hit Return. Then, if you type an open brace, it will automatically *unindent* for you. Couldn't TextMate do the same thing?
keeping the break at same indent as the case,
Hmm, I've never seen that one. Do people actually do that? (Xcode doesn't handle it, either.)
putting the break inside the brackets, etc. :)
This is handled correctly by Xcode. It automatically unindents the closing brace when you type it.
[...] I was hoping it could be smart about indenting switch/case blocks, too. Is that possible? Perhaps there is something I could add to the language definition?
While the indenting rules can be controlled [1], it does not have enough flexibility to work with switch/case statements.
[1] http://macromates.com/textmate/manual/appendix#indentation_rules
Hmm, maybe increaseIndentPattern isn't powerful enough to give Xcode- like behavior to switch/case blocks, but I think it can be hacked enough to make me happy. I went to the C bundle's Indentation Rules and appended this to increaseIndentPattern:
|case.*:
That seems to give exactly the behavior I want. (I never need to put braces in my case blocks.) I also added source.java to the Scope Selector so that this would work in Java files.
Trevor
On 19/4/2006, at 5:38, Trevor Harmon wrote:
[...] if you type an open brace, it will automatically *unindent* for you. Couldn't TextMate do the same thing?
I have considered something like that, but rather than hardcode just that single case, I am (generally) preferring the more general systems. For example you could write:
case 10:«return» // this triggers extra indent case 20: // this line should _not_ have been indented
And when we go outside C we get more of these special cases.
keeping the break at same indent as the case,
Hmm, I've never seen that one [...]
It’s in a lot of my code… :)
On Apr 21, 2006, at 5:09 PM, Allan Odgaard wrote:
[...] if you type an open brace, it will automatically *unindent* for you. Couldn't TextMate do the same thing?
I have considered something like that, but rather than hardcode just that single case, I am (generally) preferring the more general systems. For example you could write:
case 10:«return» // this triggers extra indent case 20: // this line should _not_ have been indented
Ah, good point. Looks like Xcode doesn't handle that case well at all.
And when we go outside C we get more of these special cases.
I was thinking this would be specified somehow in the language definitions for the C-like bundles only (Java, C/C++, Obj-C, C#).
Trevor