I noticed sublime text automatically recognises certain heredoc tags and highlights the syntax accordingly.
Here's a simple example where the first heredoc knows to highlight the syntax according to SQL, but the second one doesn't, even though its contents is identical. So it can be deduced that sublime 'knows' the first one is SQL because of the heredoc tag being SQL
).
Question: what is the full list of available heredoc tags?
Note: this was in a ruby heredoc, but I guess it might also work for other languages, although I can't confirm.
This is a function of the syntax definition in use (in your case, the Ruby syntax). Or if you will, this isn't something that just works everywhere, a language has to specifically support it.
So, the list of such heredocs is indeed constrained to a known list, and in order to see that you need to look at the syntax definition for the language in question.
In the case of Ruby (as of build 3211) that list is HTML
, SQL
, CSS
, JS
(or JAVASCRIPT
), and RUBY
. In build 4092, the syntax also includes SH
(or SHELL
) as well. All others (such as your DOC
above, are "plain").
If you'd like more insight into how it works, using View Package File
from the command palette, you can open Ruby/Ruby.sublime-syntax
to see the rules that match the language. If you search for heredoc
, you can see a context
that includes all of the rules that match this, which generally look like this (in build 3211; it is more advanced in build 4092):
# heredoc with embedded SQL and indented terminator
- match: '(<<[-~]"?((?:[_\w]+_|)SQL)\b"?)'
scope: punctuation.definition.string.begin.ruby
push: [heredoc-sql, trailing-heredoc-start]
In this case, the rule says that the heredoc specifier followed by SQL should push
a context named heredoc-sql
, which is just below:
heredoc-sql:
- meta_scope: string.unquoted.embedded.sql.ruby
- meta_content_scope: text.sql.embedded.ruby
- match: ^\s*\2$
scope: punctuation.definition.string.end.ruby
pop: true
- include: scope:source.sql
- include: interpolated-ruby
- include: escaped-char
Here the include: scope:source.sql
is the magic sauce that says that the language should switch into SQL mode. The rule above it with the pop
is what causes it to "exit" the heredoc and go back to Ruby.
Using this formula, a syntax modification can be done to include other languages supported by Sublime as well. This is potentially more easily accomplished in the Sublime Text "4" builds, where one of the syntax system enhancements is the ability to extend a syntax definition.