Sublime Forum

Switch/Case block indentation

#1

I’m working in a C++ file and I’m working with a switch/case block and Sublime Text formats it like this:

[code] switch (thing) {
case type1:
doType1();
break;

  case type2:
  doType2();
  break;

}[/code]

That is to say, that’s how it formats it for me as I’m working on it, and if I highlight the block and choose Reindent, that’s how it formats it for me.

What I want is this:

[code] switch (thing) {
case type1:
doType1();
break;

  case type2:
     doType2();
     break;

}[/code]

That is to say, I want the body of the case blocks to be indented from the left of the case block itself. I don’t see any way of doing this from Sublime Text itself or any of the plugins that I’ve found. I can use SublimeAStyleFormatter to select this block and format it and it will, but when I have to add case blocks, for instance, Sublime Text will left-align the body of the case block with the case statement itself.

Is there any way to fix this via built-in Sublime Text settings or an existing plugin?

Thanks!

0 Likes

Switch/Case block indentation in PHP
#2

You can set indentation rules in the packages tmPreferences file

0 Likes

#3

I guess you are interested in similar things
https://forum.sublimetext.com/t/how-do-i-change-the-indentation-rules/8756/1

I’m looking forward to seeing any related solution…

0 Likes

#4

In the section for increaseIndentPattern, add the following line:

	|   ^ \s* case.*: $

This will indent as you wish. Nb. the new rule only takes effect upon creation/opening of a new file after saving the indentation rule file.

You’ll want to modify the file “Indentation Rules.tmPreferences” for C++/C/JS or whatever language where you want the case: indenting.

I’ve been trying to figure how to get Sublime to automatically unindent the line following a “break;” (or anything else for that matter) but I’ve had no luck so far. Using key “decreaseIndentPattern” also unindents the line with the word “break” on it which is undesirable. What we really need is the opposite of “indentNextLinePattern” but that seems not to exist. I wonder if Jon, or anyone else, could shed any light here?

0 Likes

#5

Wow, I had no idea you guys replied to my message. Thank you so much! Does anyone know how to get email notifications when people reply to forum messages?

Anyway, I’m looking in ~/Library/Application Support/Sublime Text 2/Packages/ and I’m surprised to not see anything for C or Objective-C or Objective-C++. All I see is C++/Indentation Rules.tmPreferences. Oh, but based on the scope string of source.c, source.c++, source.objc, source.objc++, I guess that means this will be used for all of these? Hrm, okay, I can live with that.

Thank you so much for the help, guys! And I’d also be very interested in hearing if there’s any way to achieve an decreaseNextLinePattern or something like it.

0 Likes

#6

That worked great but can’t figure out the decreaseIndentPattern… been trying something like

(?x) ^ (.*/)? \s } .* $|<?(php)?\s+(else(if)?|end(if|for(each)?|while|break;))

and a few other break; deintendations but I can’t break; to deindent

0 Likes

#7

van can you post your deindent because my break is not deindenting and that’s the result I would like.

0 Likes