Sublime Forum

ST2 wrong reindent PHP

#1

Hi, I had some problem to indent php code.
I figured it out from php indentationRules was wrong, and fixed them.

Reindent command didn’t work proper like this.

<span class="syntaxhtml"><br />     <span class="syntaxdefault"><?php<br />     $game_list </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> array</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"callofduty"</span><span class="syntaxkeyword">=></span><span class="syntaxstring">"cod"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault">            </span><span class="syntaxstring">"destiny"</span><span class="syntaxkeyword">=></span><span class="syntaxstring">"des"<br /></span><span class="syntaxdefault">            </span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">            ?><br /></span></span>

Open Packages > PHP > indentation Rules.tmPreferences files. And 11~12 lines.

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

Fix to

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

And now indent command work like this.

<span class="syntaxhtml"><br />     <span class="syntaxdefault"><?php<br />     $game_list </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> array</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"callofduty"</span><span class="syntaxkeyword">=></span><span class="syntaxstring">"cod"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault">          </span><span class="syntaxstring">"destiny"</span><span class="syntaxkeyword">=></span><span class="syntaxstring">"des"<br /></span><span class="syntaxdefault">     </span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">     ?><br /></span></span>

I had another problem that indentation work duplicated both PHP>Indentation Rules.tmP… and HTML>Miscellaneous.temP… files.
In HTML>Miscellaneouse.tmPreferences 18 line.

     |&lt;\?(php)?\s+(else(if)?|end(if|for(each)?|while)) 

It is may caused to this.

<span class="syntaxhtml"><br />     <span class="syntaxdefault"><?php foreach </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$game_list as $key </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> $value</span><span class="syntaxkeyword">):</span><span class="syntaxdefault"> ?><br /></span>     <option value="<span class="syntaxdefault"><?=$key?></span>"><span class="syntaxdefault"><?=$value?></span></option><br /><span class="syntaxdefault"><?php endforeach ?><br /></span></span>

I removed HTML>Miscellaneouse.tmPreferences 18 line.
And it’s work fine now.

I hope resolve this to next version.

I’m not good at english indeed. :frowning:

0 Likes

#2

Updated :
PHP > Indentation Rules.tmPreferences file.

		<key>settings</key>
		<dict>
			<key>decreaseIndentPattern</key>
			<string>(?x)
				^ (.*\*/)? \s* (\}|\)(?!,)!^;]*  ) .* $
				|&lt;\?(php)?\s+(else(if)?|end(if|for(each)?|while))
			</string>

			<key>increaseIndentPattern</key>
			<string>(?x)
				^\s*
				&lt;(?!\?|area|base|br|col|frame|hr|html|img|input|link|meta|param|^&gt;]*/&gt;)
				([A-Za-z0-9]+)(?=\s|&gt;)\b^&gt;]*&gt;(?!.*&lt;/\1&gt;)
				|^.*\b(array)\b.*(\)|,) $
				|&lt;!--(?!.*--&gt;)
				|^.*((\{|\()^})]*)$
			</string>
			
			<key>indentNextLinePattern</key>
			<string>^(?!.*(#|//|\*/|&lt;\?))(?!.*};:]\s*(//|/\*.*\*/\s*$)).*^\s;:{}]\s*$|&lt;\?php.+?\b(if|else(?:if)?|for(?:each)?|while)\b.*:(?!.*end\1)</string>

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

		</dict>

I don’t know why have to insert HTML Indentaion Rules increaseIndentPattern to PHP Indentation Rules.
But decreaseIndentPattern is not.

0 Likes