I'm trying to create a TextMate grammar for a language that can have interpolated code sections in any string. So everything between <%
and %>
inside any string is "normal" code, which can have strings again.
I want to use the grammar for a VS Code extension.
My multi-line strings are enclosed by <<
->>
.
I think it is not possible to handle this case for arbitrary depth, so I would be happy to get it to work for simple cases with nesting up to some depth.
What would the patterns look like for the following code?
<<
This is some string
<%This is normal code
<<
This is more <%Normal code%>string <%More code%>
>>%>
And of course more string
>>
I tried to use the following myLanguage.tmGrammar.yaml
:
name: myLanguage
scopeName: source.myLanguage
patterns:
- begin: <<
end: ">>"
name: string.quoted.double
patterns:
- begin: <%
end: "%>"
include: source.myLanguage
- begin: <%
end: "%>"
name: variable # <--- What is the name for "default code"?
patterns:
- begin: <<
end: ">>"
include: source.myLanguage
It mostly works, but I need to use "normal" code instead of keyword
highlighting.
I followed the advice and looked at the TestMate grammar of JavaScript and adapted the backtick strings for my language.
Here is the resulting grammar:
name: myLanguage
scopeName: source.myLanguage
patterns:
- include: '#strings'
repository:
strings:
begin: '<<'
end: '>>'
name: string.quoted.double
patterns:
- include: '#interpolation'
interpolation:
begin: '<%'
end: '%>'
contentName: source.myLanguage
name: meta.embedded
patterns:
- include: $self
The normal code part is highlighted as normal text.
Expanding on this I was able to do more highlighting within the "normal code" sections inside <%
-&>
-blocks.