Sublime Forum

Switch/Case block indentation in PHP


I’m searching how to indent the code after case :, but currently anything works.
I’ve tried to do the same thing like that (Switch/Case block indentation), but there is no Indentation.tmPreferences for PHP. I’ve created a new file, named Indentationswitchcase.tmPreferences and stored in Packages/User, with this code :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "">
<plist version="1.0">
        ^\s* case.*: $


but that doesn’t work.

I’ve tested with :



            ^\s* case.*: $

but nothing seems to work.
Is there somebody who has already done this ?

Thanks in advance



the problem I see with your tmPreferences file is that you are targeting the wrong scope.

use to edit it



Thanks @kingkeith, it seems to work with OverrideAudit !
I can indent case and default correctly. I am facing the same problems as those presented in the threads you shared above.

To decrease indentation after break, I can do that for all break whitout exception. But if I try to select only those who are after the words “case” or “default”, that doesn’t work.

My regex rule (added after line 22 of indentation Rules.tmPreferences) :

 ^\s* ((?:case[\s\S]*?)break(\(|\s|;?)*[1-9]*?(\s|\)?)+;\n)|((?:default[\s\S]*?)break(\(|\s|;?)*[1-9]*?(\s|\)?)+;\n) $

that selects everything from “case” or “default” to “break”. If I want to decrease just after this expression, I’ve tested that :

       ^ \s* ((?:case[\s\S]*?)break(\(|\s|;?)*[1-9]*?(\s|\)?)+;\n)|((?:default[\s\S]*?)break(\(|\s|;?)*[1-9]*?(\s|\)?)+;\n) $


       ^ \s* ((?:case[\s\S]*?)break(\(|\s|;?)*[1-9]*?(\s|\)?)+;\n)|((?:default[\s\S]*?)break(\(|\s|;?)*[1-9]*?(\s|\)?)+;\n) $

but that doesn’t work properly too.

Currently, I use this solution in decreaseIndentPattern :

^ \s* break(\s|;?)*[1-9]+?(\s?)+;

And for each one of my breaks in a switch, I add a number (generally 1) : break 1;, and it’s works fine.

I don’t understand why the disableIndentNextLinePattern doesn’t work, have you got an idea about this ?



does this answer the question?


increaseIndentPattern affects the next line
decreaseIndentPattern affects current line
bracketIndentNextLinePattern affects the next line
disableIndentNextLinePattern affects the current line if the above line was matched by bracketIndentNextLinePattern



Oh, I see that you are on the question for a long time :slight_smile:
Thanks for your help, I thought disableIndentNextLinePattern affected the next line below, not the current.

because with this regex rule: case[\s\S]*?;(?=[\s\S]*break), I can select all of a case without the break ! So If there is a possibility to deindent the next line, that would be work !

I keep my solution for the moment, even if I must add 1 after my switch breaks, it works fine.

I’ve also tried some other regex rules, like that : (?!=case[\s\S]*?)break(\(|\s|;?)*[1-9]?(\)?)+;\n.
but I’m not very comfortable with regex, and that doesn’t match what I want. It should select only break after case, but it selects all break words. If you know regex better than me, maybe can you find my error ?

And I think It could be useful for everyone to add switch and for to bracketIndentNextLinePattern

		^ \s* \b(if|while|else|elseif|foreach|switch|for)\b [^;]* $
		| ^ \s* \b(for)\b .* [^;]\s*$

even if it’s not necessary :slight_smile:



And this regex rules seems to do exactly what I want !! But that doesn’t work in ST…

Aaah there is no more hair on my skull :smile: