Skip to content

Commit

Permalink
Add support for inline Twig comments
Browse files Browse the repository at this point in the history
Co-authored-by: Wouter de Jong <[email protected]>
  • Loading branch information
javiereguiluz and wouterj committed Sep 29, 2024
1 parent b4a1031 commit 126cfbf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
18 changes: 15 additions & 3 deletions src/Templates/highlight.php/twig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,22 @@
"begin": "\\{%",
"end": "%}",
"contains": [
{
"className": "comment",
"begin": "#",
"end": "$",
"endsWithParent": true
},
{
"className": "name",
"begin": "\\w+",
"keywords": "apply autoescape block deprecated do embed extends filter flush for from if import include macro sandbox set use verbatim with endapply endautoescape endblock enddeprecated enddo endembed endextends endfilter endflush endfor endfrom endif endimport endinclude endmacro endsandbox endset enduse endverbatim endwith",
"starts": {
"endsWithParent": true,
"contains": [
{
"$ref": "#contains.1.contains.0"
},
{
"begin": "\\|[A-Za-z_]+:?",
"keywords": "abs batch capitalize column convert_encoding date date_modify default escape filter first format inky_to_html inline_css join json_encode keys last length lower map markdown merge nl2br number_format raw reduce replace reverse round slice sort spaceless split striptags title trim upper url_encode",
Expand All @@ -53,7 +62,7 @@
]
},
{
"$ref": "#contains.1.contains.0.starts.contains.0.contains.0"
"$ref": "#contains.1.contains.1.starts.contains.1.contains.0"
}
],
"relevance": 0
Expand All @@ -68,10 +77,13 @@
"contains": [
"self",
{
"$ref": "#contains.1.contains.0.starts.contains.0"
"$ref": "#contains.1.contains.0"
},
{
"$ref": "#contains.1.contains.1.starts.contains.1"
},
{
"$ref": "#contains.1.contains.0.starts.contains.0.contains.0"
"$ref": "#contains.1.contains.1.starts.contains.1.contains.0"
}
]
}
Expand Down
30 changes: 27 additions & 3 deletions tests/fixtures/expected/blocks/code-blocks/twig.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
<div translate="no" data-loc="1" class="notranslate codeblock codeblock-length-sm codeblock-twig">
<div translate="no" data-loc="9" class="notranslate codeblock codeblock-length-sm codeblock-twig">
<div class="codeblock-scroll">
<pre class="codeblock-lines">1</pre>
<pre class="codeblock-code"><code><span class="hljs-comment">{# some code #}</span></code></pre>
<pre class="codeblock-lines">1
2
3
4
5
6
7
8
9</pre>
<pre class="codeblock-code">
<code>
<span class="hljs-comment">{# some code #}</span>
<span class="xml"></span>
<span class="hljs-template-tag">
{%
<span class="hljs-name"><span class="hljs-keyword">set</span></span> some_var = 'some value' <span class="hljs-comment"># some inline comment</span>
%}
</span>
<span class="xml"></span>
<span class="hljs-template-variable">{{
<span class="hljs-comment"># another inline comment</span>
'Lorem Ipsum'|uppercase
<span class="hljs-comment"># final inline comment</span>
}}</span>
</code>
</pre>
</div>
</div>
10 changes: 9 additions & 1 deletion tests/fixtures/source/blocks/code-blocks/twig.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@

.. code-block:: twig
{# some code #}
{%
set some_var = 'some value' # some inline comment
%}
{{
# another inline comment
'Lorem Ipsum'|uppercase
# final inline comment
}}

0 comments on commit 126cfbf

Please sign in to comment.