Sublime Forum

PHP Comment Indenting

#1

After a few hours of browsing for answers, testing plugins, and modifying ST3 language files, I am still stuck on an issue that seems common for others ( https://forum.sublimetext.com/t/line-reindent-does-not-work-for-comments/11460/1). I am working mainly in PHP/HTML on Build 3059. I have read that this problem occurs in other languages – so why not find a way to fix it together?

The ‘Reindent’ command seems to ignore some comments, keeping them on the first column (if starting with an unindented file). Specifically, any comments in this syntax:

// inline comment
/**
 * multi-line comments
 */

Oddly enough, there is one type of comment that will indent correctly:

/* a comment using multi-line syntax on a single line WILL indent correctly */

This may not be a huge deal to anyone else, but it’s a bit of a nuisance to manually indent comments over to the correct column. And, to quote mindlube from the topic linked above:
“Compounding the issue: code lines below the comment get lined up with the comment, when re-indent is applied to them. So the end result is me having to always do manual indent/unindent-ing of comments.”

All-in-all, this leads to messy code, or too much time spent manually indenting comments.

In my quest to fix this problem, I stumbled upon the language files in the application packages (on OSX: Sublime Text.app > Contents > MacOS > Packages > PHP.sublime-package). After unzipping and looking over the contents, I found two files of particular interest:

  • Indentation Rules Annex.tmPreferences
  • Indentation Rules.tmPreferences
    I became increasingly hopeful when I saw the properties ‘decreaseIndentPattern’ and ‘indentNextLinePattern’ in the ‘Indentation Rules.tmPreferences’ file and the property ‘unIndentedLinePattern’ in the ‘Indentation Rules Annex.tmPreferences’ file. Perhaps these properties have some sort of say over what lines are formatted / how to handle indentation.

Unfortunately, I cannot make sense of how these properties should be modified (if they should be at all…).

Has anyone poked through these files to make the sort of change I am looking for? Or am I jumping into too deep a rabbit hole?

0 Likes

#2

For the interested, here are the contents of those two files I’m looking at in the PHP.sublime-package dir:

“Indentation Rules.tmPreferences”

<?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 Rules</string>
	<key>scope</key>
	<string>source.php - comment</string>
	<key>settings</key>
	<dict>
		<key>decreaseIndentPattern</key>
		<string>(?x) ^ (.*\*/)? \s* \} .* $|&lt;\?(php)?\s+(else(if)?|end(if|for(each)?|while))</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>
	<key>uuid</key>
	<string>CA15DF69-E80D-46DA-BD45-E88C68E92117</string>
</dict>
</plist>

“Indentation Rules Annex.tmPreferences”

<?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 Rules Annex</string>
    <key>scope</key>
    <string>source.php</string>
    <key>settings</key>
    <dict>
        <key>unIndentedLinePattern</key>
        <string>^\s*((\*/|#|//| \*).*)?$</string>
    </dict>
</dict>
</plist>
0 Likes

#3

I’ve uploaded a PHP Grammar package to github with a test suite. Pull requests and tickets are welcome.

github.com/gerardroche/sublime-php-grammar

You’ll see that I’ve been trying to put in place a extensive set of indentation tests before we start making big sweeping changes to the indentation rules. Though there are a few bugfixes and a few improvements.

The comment indentation is difficult. So I think lots of tests are needed for it.

0 Likes