Sublime Forum

Extending language syntaxes

#1

Hello all -

My company just bought three licenses, we’re in the process of converting the entire dev team over. Love it!

I’m trying to get a few extra things working for color-coding PHP, some of which are specific to our company (our language translation indexes, for example) and some things that aren’t colored the way we’d like (variables inside double-quoted strings).

It seemed logical to create a new tmLanguage file that would import the existing source.php - but I’m having some troubles here. If I do not import the PHP syntax, my definitions work. However, upon importing them, my defs stop working - they don’t even show up when I do the ctrl+shift+alt+p thing. Another issue is that some plugins, notably phpcs, use the syntax of the current file to determine if the view is PHP or not.

I can post my code tomorrow for people to look at if anyone is interested. In the meantime, is there a better way to add just one or two rules? A co-worker made a plugin to add a scope to regions, and it sorta worked, but it applies all the styles as background, even if the colors are specified with foreground as the key. This happens even if we re-use an existing color definition. Very strange, and doesn’t seem like it should behave that way.

Thanks for reading
MPEDrummer

0 Likes

#2

This is probably a silly thing to ask but did you make sure to add your include definition at the end of your custom syntax definitions’ tmLanguage file?

eg

...
	<key>fileTypes</key>
	<array>
		<string>php</string>
	</array>
...
	<key>name</key>
	<string>PHP (custom)</string>
<key>patterns</key>
<array>
                ...define strings with nested variables here so the match takes precedence...


		<dict>
			<key>match</key>
			<string>(custom|stuff|regex)</string>
			<key>name</key>
			<string>customscope.blah.foo.php</string>
		</dict>

		<dict>
			<key>include</key>
			<string>text.html.basic</string>
		</dict>

		<dict>
			<key>include</key>
			<string>source.php</string>
		</dict>
</array>
<key>repository</key>
<dict>
.... blah blah if needed
</dict>

Also note you might want to add text.html.basic if you need it.
Also make sure the selected syntax is set to your custom php syntax name (in this case PHP (custom)). If it needs to be PHP for your plugins to work you could try naming the syntax PHP and removing the old PHP but that is completely dependent on the way that plugin decides the view’s syntax. You’ll need to look at the code in this case.

As for using a plugin to change region scopes by hooking into the api, well, I would see if it’s possible to use just the tmLanguage for that. If it isn’t possible for whatever reason you might want to post that code so we can get a better idea of what you are trying to do and why it isn’t working.

EDIT: I just wanted to add that if you want nested variables in html attribute strings or php function strings that are nested in code blocks you will have to copy over those rules to your tmLanguage file and edit them as appropriate for your context. GL. HTH.

0 Likes

Python syntax - import/from
#3

Tried it in both places (top and bottom) with the same result.

So, just to clarify - you’re saying that since the string I wish to highlight is nested inside a quoted string in PHP already, I have to modify the “quoted string” rule and match the scope exactly? As in, they don’t nest without using the whole “captures” stuff? If so…bummer, dude. :smile:

Thanks for the help.
MPEDrummer

0 Likes

#4

[quote=“mpedrummer”]Tried it in both places (top and bottom) with the same result.

So, just to clarify - you’re saying that since the string I wish to highlight is nested inside a quoted string in PHP already, I have to modify the “quoted string” rule and match the scope exactly? As in, they don’t nest without using the whole “captures” stuff? If so…bummer, dude. :smile:

Thanks for the help.
MPEDrummer[/quote]

Yeah you’ll need to move the whole captures to your custom grammar if the quoted string is nested in another rule. It’s not so bad just copy and paste to your grammar file before source.php include. That way your rule with the custom quoted string will match first. It just seems like a lot of work but a grammar file is pretty easy to develop.

Regarding the scoping, you can change the scope to whatever you’d like in your own grammar file. Although I would recommend using the default TextMate scoping which you can find at
manual.macromates.com/en/language_grammars

0 Likes