Home Download Buy Blog Forum Support

Switch/Case block indentation

Switch/Case block indentation

Postby vanRijn on Tue Feb 05, 2013 10:24 pm

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

Code: Select all
   switch (thing) {
      case type1:
      doType1();
      break;

      case type2:
      doType2();
      break;
   }



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: Select all
   switch (thing) {
      case type1:
         doType1();
         break;

      case type2:
         doType2();
         break;
   }


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!
vanRijn
 
Posts: 9
Joined: Tue Feb 05, 2013 6:55 pm

Re: Switch/Case block indentation

Postby atomi on Tue Feb 05, 2013 10:57 pm

You can set indentation rules in the packages tmPreferences file
Last edited by atomi on Wed Feb 06, 2013 3:31 am, edited 1 time in total.
atomi
 
Posts: 342
Joined: Thu Jan 20, 2011 5:06 pm
Location: Los Angeles CA US

Re: Switch/Case block indentation

Postby prmaple on Wed Feb 06, 2013 2:24 am

I guess you are interested in similar things
http://www.sublimetext.com/forum/viewtopic.php?f=2&t=10978

I'm looking forward to seeing any related solution...
Find me
HTML5 Games: SNAKE! Tetris!
prmaple
 
Posts: 80
Joined: Sun Dec 25, 2011 11:06 am
Location: Melbourne

Re: Switch/Case block indentation

Postby qgates on Thu Feb 07, 2013 4:35 am

In the <string> section for <key>increaseIndentPattern</key>, add the following line:
Code: Select all
   |   ^ \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?
qgates
 
Posts: 212
Joined: Mon Nov 05, 2012 4:33 pm

Re: Switch/Case block indentation

Postby vanRijn on Sun Mar 10, 2013 1:10 am

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 <string>source.c, source.c++, source.objc, source.objc++</string>, 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.
vanRijn
 
Posts: 9
Joined: Tue Feb 05, 2013 6:55 pm

Re: Switch/Case block indentation

Postby raptor235 on Tue Dec 10, 2013 7:47 pm

That worked great but can't figure out the decreaseIndentPattern... been trying something like

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

and a few other break; deintendations but I can't break; to deindent
raptor235
 
Posts: 36
Joined: Mon Jan 16, 2012 7:13 pm

Re: Switch/Case block indentation

Postby raptor235 on Tue Dec 10, 2013 7:50 pm

van can you post your deindent because my break is not deindenting and that's the result I would like.
raptor235
 
Posts: 36
Joined: Mon Jan 16, 2012 7:13 pm


Return to General Discussion

Who is online

Users browsing this forum: Google [Bot] and 21 guests