Home Download Buy Blog Forum Support

PHP Comment Indenting

PHP Comment Indenting

Postby thaddeus on Thu Aug 07, 2014 12:42 am

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 ( http://www.sublimetext.com/forum/viewtopic.php?f=3&t=14172). 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:
Code: Select all
// inline comment
/**
* multi-line comments
*/


Oddly enough, there is one type of comment that will indent correctly:
Code: Select all
/* 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?
thaddeus
 
Posts: 2
Joined: Wed Aug 06, 2014 8:59 pm

Re: PHP Comment Indenting

Postby thaddeus on Thu Aug 07, 2014 12:45 am

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

"Indentation Rules.tmPreferences"
Code: Select all
<?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"
Code: Select all
<?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>
thaddeus
 
Posts: 2
Joined: Wed Aug 06, 2014 8:59 pm

Re: PHP Comment Indenting

Postby gerry on Tue Nov 11, 2014 5:31 pm

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

https://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.
gerry
 
Posts: 8
Joined: Tue Nov 11, 2014 2:22 pm


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 14 guests