Skip to content

Commit

Permalink
[Go] Fix multi-line string interpolation in YAML templates
Browse files Browse the repository at this point in the history
This commit...

1. adds a special `match-block-string-templates` with out closing `}}` checks,
   to support interpolation within multi-line strings (for use in YAML).

2. adds tests for multi-line comments in HTML and Markdown templates.
  • Loading branch information
deathaxe committed Jul 24, 2024
1 parent 26052a1 commit 3b7f2b7
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 25 deletions.
16 changes: 16 additions & 0 deletions Go/Go.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -747,8 +747,24 @@ contexts:
- match: \%(?:\[\d+\])?[ .\d*#+-]*[A-Za-z]
scope: constant.other.placeholder.go

match-block-string-templates:
# This context is used for interpolation within quoted or unquoted multiline strings.
# Doesn't check for closing `}}` as it might be located on a sub-sequent line.
# It clears the `string` scope of the owning context.
# Note: used by YAML (Go).sublime-syntax
- match: ({{)(-?)
captures:
1: punctuation.section.interpolation.begin.go
2: keyword.operator.template.trim.left.go
push:
- pop-now-clear-scope
- match-template-inner
- match-template-function
- match-template-keyword

match-string-templates:
# This context is used for interpolation within quoted or unquoted strings.
# Matchs only if closing `}}` is found on same line to avoid false positives.
# It clears the `string` scope of the owning context.
- match: ({{)(-?)(?=.*?}})
captures:
Expand Down
10 changes: 5 additions & 5 deletions Go/YAML (Go).sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ contexts:

block-scalar-body:
- meta_prepend: true
- include: scope:source.go#match-string-templates
- include: scope:source.go#match-block-string-templates

flow-scalar-plain-in-body:
- meta_prepend: true
- include: scope:source.go#match-string-templates
- include: scope:source.go#match-block-string-templates

flow-scalar-plain-out-body:
- meta_prepend: true
- include: scope:source.go#match-string-templates
- include: scope:source.go#match-block-string-templates

flow-scalar-double-quoted-body:
- meta_prepend: true
- include: scope:source.go#match-string-templates
- include: scope:source.go#match-block-string-templates

flow-scalar-single-quoted-body:
- meta_prepend: true
- include: scope:source.go#match-string-templates
- include: scope:source.go#match-block-string-templates

flow-mapping:
- match: \{(?![{%#])
Expand Down
12 changes: 12 additions & 0 deletions Go/tests/syntax_test_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@
</head>

<body>

{{/*
| ^ - meta.interpolation
| ^^ meta.interpolation.go punctuation.section.interpolation.begin.go
| ^^ meta.interpolation.go source.go.template comment.block.go punctuation.definition.comment.begin.go
Spread a key-value map into the "env" list
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.interpolation.go source.go.template comment.block.go
*/}}
| ^^ meta.interpolation.go source.go.template comment.block.go punctuation.definition.comment.end.go
| ^^ meta.interpolation.go punctuation.section.interpolation.end.go
| ^ - meta.interpolation

{{ block "main" . }}
| ^^^^^^^^^^^^^^^^^^^^ meta.interpolation.go
| ^^ punctuation.section.interpolation.begin.go
Expand Down
10 changes: 10 additions & 0 deletions Go/tests/syntax_test_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
| ^ keyword.operator.template.trim.right.go
| ^^ punctuation.section.interpolation.end.go - source.go

{{/*
|^ meta.paragraph.markdown - meta.interpolation
| ^^ meta.paragraph.markdown meta.interpolation.go punctuation.section.interpolation.begin.go
| ^^ meta.paragraph.markdown meta.interpolation.go source.go.template comment.block.go punctuation.definition.comment.begin.go
Spread a key-value map into the "env" list
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.paragraph.markdown meta.interpolation.go source.go.template comment.block.go
*/}}
| ^^ meta.paragraph.markdown meta.interpolation.go source.go.template comment.block.go punctuation.definition.comment.end.go
| ^^ meta.paragraph.markdown meta.interpolation.go punctuation.section.interpolation.end.go
| ^ meta.paragraph.markdown - meta.interpolation

# My {{ .Site.Title }} homepage
| <- markup.heading.1.markdown punctuation.definition.heading.begin.markdown
Expand Down
66 changes: 46 additions & 20 deletions Go/tests/syntax_test_template.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# SYNTAX TEST "Packages/Go/YAML (Go).sublime-syntax"

{{/*
#^ - meta.string - meta.interpolation
# ^^ meta.string.yaml meta.interpolation.go punctuation.section.interpolation.begin.go
# ^^ meta.string.yaml meta.interpolation.go source.go.template comment.block.go punctuation.definition.comment.begin.go
Spread a key-value map into the "env" list
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.yaml meta.interpolation.go source.go.template comment.block.go
*/}}
# <- meta.string.yaml meta.interpolation.go source.go.template comment.block.go
#^ meta.string.yaml meta.interpolation.go source.go.template comment.block.go - punctuation
# ^^ meta.string.yaml meta.interpolation.go source.go.template comment.block.go punctuation.definition.comment.end.go
# ^^ meta.string.yaml meta.interpolation.go punctuation.section.interpolation.end.go
# ^ - meta.string - meta.interpolation

key: {{ /*comment*/ }}
# ^^^^^^^^^^^^^^^^^ meta.string.yaml meta.interpolation.go - string
# ^^ punctuation.section.interpolation.begin.go
Expand Down Expand Up @@ -36,33 +49,46 @@ key: {{ /*comment*/ }}
# ^^^^^^^^^^ source.go.template
# ^^ punctuation.section.interpolation.end.go
# ^ punctuation.separator.key-value.mapping.yaml
- Val{{ $ue }}
- Multi{{ printf
# ^ punctuation.definition.block.sequence.item.yaml
# ^^^ meta.string.yaml string.unquoted.plain.out.yaml
# ^^^^^^^^^ meta.template.go meta.string.yaml meta.interpolation.go
# ^^ punctuation.section.interpolation.begin.go
# ^^^ variable.other.template.go
# ^^ punctuation.section.interpolation.end.go
# ^^^^^ meta.string.yaml string.unquoted.plain.out.yaml
# ^^^^^^^^^^ meta.string.yaml meta.interpolation.go - string
# ^^ punctuation.section.interpolation.begin.go
# ^^^^^^ support.function.builtin.go
$line }} string
# ^^^^^^^^^ meta.string.yaml meta.interpolation.go - string
# ^^^^^ variable.other.template.go
# ^^ punctuation.section.interpolation.end.go
# ^^^^^^^ meta.string.yaml string.unquoted.plain.out.yaml - meta.interpolation

- 'Val{{ $ue }}'
- 'Multi{{ printf
# ^ punctuation.definition.block.sequence.item.yaml
# ^^^^ meta.template.go meta.string.yaml string.quoted.single.yaml
# ^^^^^^ meta.string.yaml string.quoted.single.yaml
# ^ punctuation.definition.string.begin.yaml
# ^^^^^^^^^ meta.template.go meta.string.yaml meta.interpolation.go
# ^^ punctuation.section.interpolation.begin.go
# ^^^ variable.other.template.go
# ^^ punctuation.section.interpolation.end.go
# ^ punctuation.definition.string.end.yaml
# ^^^^^^^^^^ meta.string.yaml meta.interpolation.go - string
# ^^ punctuation.section.interpolation.begin.go
# ^^^^^^ support.function.builtin.go
$line }} string'
# ^^^^^^^^^ meta.string.yaml meta.interpolation.go - string
# ^^^^^ variable.other.template.go
# ^^ punctuation.section.interpolation.end.go
# ^^^^^^^^ meta.string.yaml string.quoted.single.yaml - meta.interpolation
# ^ punctuation.definition.string.end.yaml

- "Val{{ $ue }}"
- "Multi{{ printf
# ^ punctuation.definition.block.sequence.item.yaml
# ^^^^ meta.template.go meta.string.yaml string.quoted.double.yaml
# ^^^^^^ meta.string.yaml string.quoted.double.yaml
# ^ punctuation.definition.string.begin.yaml
# ^^^^^^^^^ meta.template.go meta.string.yaml meta.interpolation.go
# ^^ punctuation.section.interpolation.begin.go
# ^^^ variable.other.template.go
# ^^ punctuation.section.interpolation.end.go
# ^ punctuation.definition.string.end.yaml
# ^^^^^^^^^^ meta.string.yaml meta.interpolation.go - string
# ^^ punctuation.section.interpolation.begin.go
# ^^^^^^ support.function.builtin.go
$line }} string"
# ^^^^^^^^^ meta.string.yaml meta.interpolation.go - string
# ^^^^^ variable.other.template.go
# ^^ punctuation.section.interpolation.end.go
# ^^^^^^^^ meta.string.yaml string.quoted.double.yaml - meta.interpolation
# ^ punctuation.definition.string.end.yaml

{{ end }}
# <- meta.string.yaml meta.interpolation.go punctuation.section.interpolation.begin.go
Expand Down

0 comments on commit 3b7f2b7

Please sign in to comment.