Sublime Forum

Switch/Case block indentation in PHP

#1

Hello,
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" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>name</key>
    <string>Indentation</string>
    <key>scope</key>
    <string>text.php</string>
    <key>settings</key>
    <dict>
      <key>decreaseIndentPattern</key>
      <string>(?x)
      </string>
      <key>increaseIndentPattern</key>
      <string>(?x)
        ^\s* case.*: $

      </string>
    </dict>
    <key>uuid</key>
    <string>E44DC773-540C-4E3E-BF94-9E0C7A0C98DF</string>
  </dict>
</plist>

but that doesn’t work.

I’ve tested with :

        (?:
            ^\s*case.*:\s*$
        )

or

        (?:
            ^\s* case.*: $
        )

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

Thanks in advance

0 Likes

#2


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


use https://packagecontrol.io/packages/OverrideAudit to edit it

2 Likes

#3

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 :

<key>disableIndentNextLinePattern</key>
      <string><![CDATA[
       ^ \s* ((?:case[\s\S]*?)break(\(|\s|;?)*[1-9]*?(\s|\)?)+;\n)|((?:default[\s\S]*?)break(\(|\s|;?)*[1-9]*?(\s|\)?)+;\n) $
       ]]>
      </string>

OR 

<key>disableIndentNextLinePattern</key>
    <string>(?x)
       ^ \s* ((?:case[\s\S]*?)break(\(|\s|;?)*[1-9]*?(\s|\)?)+;\n)|((?:default[\s\S]*?)break(\(|\s|;?)*[1-9]*?(\s|\)?)+;\n) $
    </string>

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 ?

0 Likes

#4

does this answer the question?

Summary

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

0 Likes

#5

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 ?

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

<key>bracketIndentNextLinePattern</key>
		<string>(?x)
		^ \s* \b(if|while|else|elseif|foreach|switch|for)\b [^;]* $
		| ^ \s* \b(for)\b .* [^;]\s*$
		</string>

even if it’s not necessary :slight_smile:

0 Likes

#6

And this regex rules seems to do exactly what I want !! But that doesn’t work in ST…
((?:\bcase\b\s*[\s\S]*?)\Kbreak(\(|\s|;?)[1-9]?(\)?)+;)|((?:\bdefault\b\s*[\s\S]*?)\Kbreak(\(|\s|;?)[1-9]?(\)?)+;)
https://regex101.com/r/NECiyr/1

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

0 Likes