Wednesday, 21 August 2013

Apply different scopes to the same character in a Sublime Text syntax definition file

Apply different scopes to the same character in a Sublime Text syntax
definition file

I'm trying to create a new .tmLanguage file for Sublime Text 2 that
defines special fenced code blocks for knitr and Markdown. These blocks
take this form:
```{r example_chunk, echo=true}
x <- rnorm(100)
y <- rnorm(100)
plot(y ~ x, pch=20)
```
There are two sections: (1) the parameters ({r ...}) and (2) the actual
embedded code (between the closing } and the ``` at the end). There are
four scopes that need to be applied to delineate the two sections:
punctuation.definition.parameters.begin.knitr
punctuation.definition.parameters.end.knitr
punctuation.section.embedded.begin.knitr
punctuation.section.embedded.end.knitr
Using regular expressions to peg these scopes to parts of code is easy
enough (code available here). However, two of these scopes need to be
applied to the same single character: the final } in the parameters
section, which ends the parameters and signifies the beginning of the
fenced/embedded code.

However, it appears to be impossible to assign two scopes to the same
character in a .tmLanguage file. It's not possible to end the parameters
section and begin the embedded section. The first scope defined takes
precedence, thus breaking the syntax highlighting.
Is there a way to use a .tmLanguage syntax definition to apply two
different scopes to the same character in Sublime Text? If not, is there
some way I can peg punctuation.definition.parameters.end.knitr and
punctuation.section.embedded.begin.knitr to two different somethings
instead of the single {? (Keeping in mind that I can't add additional
characters to the code block.)

No comments:

Post a Comment