Skip to content

Commit

Permalink
Add support for guard tag
Browse files Browse the repository at this point in the history
Supported by Twig 3.15+
  • Loading branch information
deathaxe committed Oct 20, 2024
1 parent 3599f7f commit ed7dd46
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 5 deletions.
48 changes: 48 additions & 0 deletions Syntaxes/HTML/syntax_test_scopes.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,54 @@
{# ^^^^^^^^ meta.path.twig variable.other.twig #}
{# ^^ punctuation.section.embedded.end.twig #}
|==============================================================================
| Guard
| https://twig.symfony.com/doc/3.x/tags/guard.html
|==============================================================================
{% guard function importmap %}
{#^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.embedded.statement.twig source.twig.embedded #}
{# ^^ meta.embedded.statement.twig - source #}
{# ^^^^^ keyword.other.twig #}
{# ^^^^^^^^ storage.type.function.twig #}
{# ^^^^^^^^^ meta.function-call.identifier.twig variable.function.twig #}
{# ^^ punctuation.section.embedded.end.twig #}
{{ importmap('app') }}
{# ^^^^^^^^^^^^^^^^^^^^^^ meta.embedded.expression.twig #}
{# ^^ punctuation.section.embedded.begin.twig #}
{# ^^^^^^^^^^^^^^^^^^ source.twig.embedded #}
{# ^^^^^^^^^ meta.function-call.identifier.twig variable.function.twig #}
{# ^^^^^^^ meta.function-call.arguments.twig #}
{# ^ punctuation.section.arguments.begin.twig #}
{# ^^^^^ meta.string.twig string.quoted.single.twig #}
{# ^ punctuation.definition.string.begin.twig #}
{# ^ punctuation.definition.string.end.twig #}
{# ^ punctuation.section.arguments.end.twig #}
{# ^^ punctuation.section.embedded.end.twig #}
{% guard filter callable %}
{#^^^^^^^^^^^^^^^^^^^^^^^ meta.embedded.statement.twig source.twig.embedded #}
{# ^^ meta.embedded.statement.twig - source #}
{# ^^^^^ keyword.other.twig #}
{# ^^^^^^ storage.type.filter.twig #}
{# ^^^^^^^^ meta.function-call.identifier.twig variable.function.filter.twig #}
{# ^^ punctuation.section.embedded.end.twig #}
{% guard test callable %}
{#^^^^^^^^^^^^^^^^^^^^^ meta.embedded.statement.twig source.twig.embedded #}
{# ^^ meta.embedded.statement.twig - source #}
{# ^^^^^ keyword.other.twig #}
{# ^^^^ storage.type.test.twig #}
{# ^^^^^^^^ meta.function-call.identifier.twig variable.function.test.twig #}
{# ^^ punctuation.section.embedded.end.twig #}
{% endguard %}
{#^^^^^^^^^^ meta.embedded.statement.twig source.twig.embedded #}
{# ^^ meta.embedded.statement.twig - source #}
{# ^^^^^^^^ keyword.other.twig #}
{# ^^ punctuation.section.embedded.end.twig #}
|==============================================================================
| If
| https://twig.symfony.com/doc/3.x/tags/if.html
Expand Down
44 changes: 39 additions & 5 deletions Syntaxes/Text/Text (Twig).sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ contexts:
###[ TWIG OTHER STATEMENTS ]###################################################

other-statement:
# other statements
# apply statement
- match: apply\b
scope: keyword.other.twig
set:
Expand All @@ -318,6 +318,15 @@ contexts:
- match: endapply\b
scope: keyword.other.twig
pop: 1
# guard statement
- match: guard\b
scope: keyword.other.twig
set:
- guard-meta
- guard-type
- match: endguard\b
scope: keyword.other.twig
pop: 1
# block tags
- match: (?:end)?(?:autoescape|cache|embed|sandbox|with)\b
scope: keyword.other.twig
Expand All @@ -327,6 +336,24 @@ contexts:
scope: keyword.other.twig
pop: 1

guard-meta:
- meta_scope: meta.statement.guard.twig
- include: block-pop

guard-type:
- match: filter\b
scope: storage.type.filter.twig
set:
- filter-meta
- filter-name
- match: function\b
scope: storage.type.function.twig
set: function-name
- match: test\b
scope: storage.type.test.twig
set: test-expression
- include: else-pop

###[ TWIG FILTERS ]############################################################

filters:
Expand Down Expand Up @@ -356,14 +383,21 @@ contexts:
###[ TWIG FUNCTION CALLS ]#####################################################

functions:
- match: (?={{identifiers}}\()
push:
- function-arguments
- function-name

function-name:
# builtin functions
- match: '{{builtin_functions}}(?=\()'
- match: '{{builtin_functions}}'
scope: meta.function-call.identifier.twig support.function.twig
push: function-arguments
pop: 1
# custom functions
- match: \w+(?=\()
- match: '{{identifiers}}'
scope: meta.function-call.identifier.twig variable.function.twig
push: function-arguments
pop: 1
- include: else-pop

function-arguments:
- match: \(
Expand Down

0 comments on commit ed7dd46

Please sign in to comment.