Sublime Forum

Nested code syntax highlighting

#1

Hi,

Is it possible to get nested syntax highlitghting working with sublime text?
I have Perl code with HTML in a string. Also there are delimiters ‘<% and %>’ that are embedded in the html that perl code will reside in.
So it’s sortof Perl inside HTML inside Perl

Below is a code snippet with:
Line 1 - Perl
Lines 2-3 - HTML
Lines 4-8 - Perl
Lines 9-10 - HTML
Line 11 - Perl

[code]my $htmlTemplateList = "

<% foreach my $s (0 .. $#scriptResourceList) { } %> "; [/code] Is it possible to get Sublime Text to highlight this properly and detect each of the languages?

Thanks

0 Likes

#2

I would also like to know this.

I have found some limited support in the form of “include” syntax directives (here shown in YAML):

sql-string-double-quoted:
	begin: '"\s*(?=(SELECT|INSERT|UPDATE|DELETE|CREATE|REPLACE|ALTER)\b)'
	end: '"'
	...
	patterns:
	- ...
	- include: source.sql

Unfortunately, there are a few restrictions:

  • Begin and end markers seem to be restricted to single-line regexes
    , probably for performance reasons;

  • I can’t seem to find a way to choose the included syntax based on the block contents, except for a switch on the single-line begin marker;

  • I couldn’t find a way to tell ST something like: this is a generic block, up to this end marker, please make a polite guess as to the language it may contain.

A few syntax files make such a switch based on the single-line marker, for example PHP highlighs single- or double-quoted strings as SQL if the first line contains SELECT (uppercase).

0 Likes

#3

Yes, it is possible. You may find an example at https://github.com/yrammos/LyTeXTools/blob/lytextools/LyTeX.JSON-tmLanguage, my syntax definition for LaTeX files including snippets of LilyPond code. As you suggest, unless the “nested” language is unambiguously delimited from the “main” language, you will need to use begin/end markers.

0 Likes