phirschybar phirschybar - 2 years ago 115
PHP Question

Edit Color Scheme for php in Sublime Text 2

I would like to have the $ (dollar sign) which indicates a php variable to appear as it's own color (#ff0000) in ST2. I have been editing a custom color scheme file but cannot seem to get a special color for just this symbol.

Answer Source

Important Addendum Prefixed To The Answer

The following explanation assumes that one is writing the syntax definition in JSON and then building the Plist xml file with the PackageDev plugin (as described in the unofficial sublime docs linked below). It is from the latter files that ST2 reads syntax parsing instructions.

= = = =

I'm reporting to you from my own first foray into this territory, so I may be mistaken on a point or two in what follows.

Unless your syntax parser defines $ as having its own scope selector, you won't be able to set the color for that lone sign through the color scheme files. As you may already know, you can check the scope selector of an element by selecting it and pressing option+command+p (alt+super+p on not-macs): it reports the information in the status bar.

I just checked the php syntax parser on my own software, and there's no differentiation between the scope of the $ and the string to which it is prefixed. Thus, assuming your setup is like my own, you'll need to write your own syntax definition. This can be done either in the existing php syntax file, or (surely a safer option) in a new definition document which you'll use as your primary one for editing.

You can find a tutorial covering the basic steps of writing your own syntax definitions in the unofficial sublime text docs. In fact, a section therein, entitled "Fine Tuning Matches", explains how to define a scope selector especially for prefixed dolla' signs by using "captures". You can refer to the documentation for the specifics on creating syntax definition files and all that, but, for the sake of solidifying my own understanding of the process and to make the answer readily available here, I'll do my best to explain the JSON determination of the selector.

This is the regex pattern you'll need to use:

{ "match": "\\$([A-Za-z][A-Za-z0-9_]+)",
  "name": "keyword.syntaxTypeAbbreviation",
  "captures": {
    "1": { "name": "moreSpecific.selectorName.syntaxTypeAbbreviation " }
  "comment": "Variables like $PARAM1, $TM_SELECTION..."

Analysis of the meaning of the above:

"match" gives the regex which identifies the string pattern to be selected. Unless I'm mistaken, it reads, "any string beginning with '$' ([followed by one uppercase or lowercase letter] and [at least one additional letter or numeral] plus any additional characters)". (Sublime Text uses the oniguruma regex specifications.)

"name" provides the string by which the the scope is to be selected in the color scheme document. In the scheme xml, this corresponds to the <key>name</key> <string>specific.selector.syntaxTypeAbbreviation</string>.

"captures" (and this is the critical element for your aim) specifies the subset of the pattern which is to be given its own, more specific, scope selector. Each set of regex terms enclosed within parenthesis occupies one place, and the integer ("1", here) specifies which of the places is to be "captured". In our case, of course, there is only the one capturable group.

"name" does the same as before, but as a subspecies of the previous designation and, "comment" offers the option for you to comment on the significance of the definition.

Finally, it is important to note that the first scope selector will set the color of the whole string and the second overrides its predecessor to give a different color to the following letters and numbers. That is to say, the first selector will let you color the $ and the second will be the color of the appended string.

We can therefore translate the pattern thus:

"Any string beginning with '$' ([followed by one uppercase or lowercase letter] and [at least one additional letter or numeral] plus any additional characters) is to be named NAME, except for the part which is enclosed in the first set of parenthesis, which is to be named OTHERNAME instead.

I hope that this explanation will be at lest half as useful for you to read as it has been for me to write. Also, I'd appreciate folks correcting whatever mistakes I surely did make.



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download