Sublime Forum

String interpolation for ST3 .sublime-syntax format

#1

I’m making a new ST3 format .sublime-syntax file, and I can’t work out how to do string interpolation. Any clues would be great.

Here’s a string:

 "this is a string"

and here’s another one:

"""this is also a string"""

(Both can be multiline.)

If there’s a dollar sign:

"this is a $foo string" 

then the value of the variable foo is replaced. Otherwise you have to do this:

"this \$ is a dollar sign"

And you can also supply expressions in parentheses:

"this is a $(foo = cos(2 * pi)) string"

I can’t work out how to start trying to add this to a syntax format… :slight_smile:

0 Likes

#2

I adapted a bit of my Scala syntax to give you an idea:

main:
- match: '"'
  scope: punctuation.definition.string.interpolated.begin.scala
  push: string

string:
  - meta_include_prototype: false
  - meta_scope: string.interpolated.scala
  - match: '(\$)\('
    scope: punctuation.definition.string.interpolated.element.begin.scala
    push:
      - meta_scope: source.scala
      - match: '\)'
        scope: punctuation.definition.string.interpolated.element.end.scala
        pop: true
      - include: 'main'
  - match: '(\$){{name}}'
    captures:
      1: punctuation.definition.string.interpolated.element.scala
      2: source.scala variable.other.scala
  - match: '"'
    scope: punctuation.definition.string.interpolated.end.scala
    pop: true
  - include: escaped
0 Likes

#3

Thanks very much for this! With your help I’ve managed to make some progress (the beginning of an interpolation is recognised, but I’ve yet to switch back when its over).

0 Likes

#4

It’s the pop: true that does this. Did you read the doc?

0 Likes

#5

Hi, thanks! Yes, I have spent a bit too long staring at the documentation and various examples (including yours). I finally managed to get the interpolated strings working eventually (it was probably pop, as you suggested). Well, it works for about 95% of the time, which is probably good enough for now.

0 Likes

#6

The most probable reason for the scope not popping, is that another regex is matching the " preventing the context to be popped.

The order in which you put your rules is important.

0 Likes