I have a situation where I end up coding a lot of LUA inside of XML strings,
and it looks quite ugly, to be honest.
It would be extremely nice to be able to have syntax highlighting for the LUA, since that's the main focus of the file; I'm fairly sure that this is supported (apparently SQL is highlighted inside of PHP in some cases), but I have looked for how to edit the syntax files and I'm honestly just stumped. Some of the solutions even seem to suggest I need to edit a .tmLanguage file, but I can't find any of those files and none of the sublime-syntax files look like the same kind of format.
If it makes the situation any easier, the strings containing the LUA always begin with "%function(self) and they always end with end", and I only really want this highlighting functionality in XML.
The first step is to open the XML syntax definition for editing. I will explain how to do that. Anything referring to a tmLanguage (XML format) file is out-of-date, Sublime Text 3 now uses sublime-syntax (YAML format) files for all it's default syntax highlighting.
I would recommend cloning the official git repo https://github.com/sublimehq/Packages (please follow the instructions in the repo's readme, which details how to clone a package in such a way that Sublime Text picks it up) so that you will be able to include any future changes made to the official version into your version using git (because we are overriding the default one that would get upgraded automatically with ST, with this customized version). (Basically, you want the XML folder from the repo in your Sublime Packages folder, which is accessible from ST by navigating to the Preferences -> Browse Packages menu item.)
The file we are interested in is XML/XML.sublime-syntax. As you can see, it is full of regular expressions. The one we are most interested in is where it parses XML attributes.
Search for double-quoted-string:. Immediately under this line, paste in the following:
- match: '"(?=%function\(self\))'
scope: string.quoted.double.xml punctuation.definition.string.begin.xml
push:
- clear_scopes: true
- match: '"'
scope: string.quoted.double.xml punctuation.definition.string.end.xml
pop: true
- include: scope:source.lua
Note that the first character (the - before match) should line up with the original - match: '"' that was there.
Save it. And that's it!
Basically, what we are doing, is:
"), it will look for a double quote followed by the literal string %function(self). It does this using a lookahead, so that it can then include the source.lua scope, and have that %function(self) text/code highlighted properly as LUA (along with the rest of the LUA code).clear_scopes: true to ensure that the LUA code doesn't look like XML text/attributes.") and "pops" the context off the stack, back to being inside a plain XML tag. (I chose to look for the " without end before it, so that end would still be picked up by the LUA syntax definition. Otherwise we would have to duplicate the match regex and scope for it in the XML syntax, which isn't ideal.)(You might want to create a new branch in your local repo, and commit these changes, to make it easier to merge future official changes in.)

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With