Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get LUA syntax highlighting inside of XML quotes in Sublime Text 3?

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.

like image 929
Moshe KXN Avatar asked Jan 22 '26 17:01

Moshe KXN


1 Answers

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:

  • before the syntax checks for a plain double quote ("), 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).
  • Note that it uses clear_scopes: true to ensure that the LUA code doesn't look like XML text/attributes.
  • Then, it looks for the closing XML attribute quote (") 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.)

lua inside xml

like image 187
Keith Hall Avatar answered Jan 24 '26 09:01

Keith Hall



Donate For Us

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