Skip to content

Commit 5c8c876

Browse files
MrYamousjoshgoebel
andauthored
enh(twig): update keywords list (#3415)
* enh(twig): update keywords list * chore(twig) major twig refatoring, add new functionality (Josh) Co-authored-by: Josh Goebel <me@joshgoebel.com>
1 parent b63b12d commit 5c8c876

10 files changed

+195
-52
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Grammars:
44

5+
- enh(twig) update keywords list (#3415) [Matthieu Lempereur][]
56
- fix(python) def, class keywords detected mid-identifier (#3381) [Josh Goebel][]
67
- fix(python) Fix recognition of numeric literals followed by keywords without whitespace (#2985) [Richard Gibson][]
78
- enh(swift) add SE-0290 unavailability condition (#3382) [Bradley Mackey][]
@@ -34,6 +35,7 @@ Themes:
3435
[Björn Ebbinghaus]: https://github.com/MrEbbinghaus
3536
[Josh Goebel]: https://github.com/joshgoebel
3637
[Samia Ali]: https://github.com/samiaab1990
38+
[Matthieu Lempereur]: https://github.com/MrYamous
3739
[idleberg]: https://github.com/idleberg
3840

3941
## Version 11.3.1

src/languages/twig.js

Lines changed: 180 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,67 +8,208 @@ Category: template
88
*/
99

1010
export default function(hljs) {
11-
var PARAMS = {
12-
className: 'params',
13-
begin: '\\(', end: '\\)'
11+
const regex = hljs.regex;
12+
const FUNCTION_NAMES = [
13+
"attribute",
14+
"block",
15+
"constant",
16+
"country_timezones",
17+
"cycle",
18+
"date",
19+
"dump",
20+
"html_classes",
21+
"include",
22+
"max",
23+
"min",
24+
"parent",
25+
"random",
26+
"range",
27+
"source",
28+
"template_from_string"
29+
];
30+
31+
const FILTERS = [
32+
"abs",
33+
"batch",
34+
"capitalize",
35+
"column",
36+
"convert_encoding",
37+
"country_name",
38+
"currency_name",
39+
"currency_symbol",
40+
"data_uri",
41+
"date",
42+
"date_modify",
43+
"default",
44+
"escape",
45+
"filter",
46+
"first",
47+
"format",
48+
"format_currency",
49+
"format_date",
50+
"format_datetime",
51+
"format_number",
52+
"format_time",
53+
"html_to_markdown",
54+
"inky_to_html",
55+
"inline_css",
56+
"join",
57+
"json_encode",
58+
"keys",
59+
"language_name",
60+
"last",
61+
"length",
62+
"locale_name",
63+
"lower",
64+
"map",
65+
"markdown",
66+
"markdown_to_html",
67+
"merge",
68+
"nl2br",
69+
"number_format",
70+
"raw",
71+
"reduce",
72+
"replace",
73+
"reverse",
74+
"round",
75+
"slice",
76+
"slug",
77+
"sort",
78+
"spaceless",
79+
"split",
80+
"striptags",
81+
"timezone_name",
82+
"title",
83+
"trim",
84+
"u|0",
85+
"upper",
86+
"url_encode"
87+
];
88+
89+
let TAG_NAMES = [
90+
"apply",
91+
"autoescape",
92+
"block",
93+
"cache",
94+
"deprecated",
95+
"do",
96+
"embed",
97+
"extends",
98+
"filter",
99+
"flush",
100+
"for",
101+
"from",
102+
"if",
103+
"import",
104+
"include",
105+
"macro",
106+
"sandbox",
107+
"set",
108+
"use",
109+
"verbatim",
110+
"with"
111+
];
112+
113+
TAG_NAMES = TAG_NAMES.concat(TAG_NAMES.map(t => `end${t}`));
114+
115+
const STRING = {
116+
scope: 'string',
117+
variants: [
118+
{
119+
begin: /'/,
120+
end: /'/
121+
},
122+
{
123+
begin: /"/,
124+
end: /"/
125+
},
126+
]
14127
};
15128

16-
var FUNCTION_NAMES = 'attribute block constant cycle date dump include ' +
17-
'max min parent random range source template_from_string';
129+
const NUMBER = {
130+
scope: "number",
131+
match: /\d+/
132+
};
18133

19-
var FUNCTIONS = {
20-
beginKeywords: FUNCTION_NAMES,
21-
keywords: {name: FUNCTION_NAMES},
22-
relevance: 0,
134+
const PARAMS = {
135+
begin: /\(/,
136+
end: /\)/,
137+
excludeBegin: true,
138+
excludeEnd: true,
23139
contains: [
24-
PARAMS
140+
STRING,
141+
NUMBER
25142
]
26143
};
27144

28-
var FILTER = {
29-
begin: /\|[A-Za-z_]+:?/,
30-
keywords:
31-
'abs batch capitalize column convert_encoding date date_modify default ' +
32-
'escape filter first format inky_to_html inline_css join json_encode keys last ' +
33-
'length lower map markdown merge nl2br number_format raw reduce replace ' +
34-
'reverse round slice sort spaceless split striptags title trim upper url_encode',
145+
146+
const FUNCTIONS = {
147+
beginKeywords: FUNCTION_NAMES.join(" "),
148+
keywords: { name: FUNCTION_NAMES },
149+
relevance: 0,
150+
contains: [ PARAMS ]
151+
};
152+
153+
const FILTER = {
154+
match: /\|(?=[A-Za-z_]+:?)/,
155+
beginScope: "punctuation",
156+
relevance: 0,
35157
contains: [
36-
FUNCTIONS
158+
{
159+
match: /[A-Za-z_]+:?/,
160+
keywords: FILTERS
161+
},
37162
]
38163
};
39164

40-
var TAGS = 'apply autoescape block deprecated do embed extends filter flush for from ' +
41-
'if import include macro sandbox set use verbatim with';
165+
const tagNamed = (tagnames, {relevance}) => {
166+
return {
167+
beginScope: {
168+
1: 'template-tag',
169+
3: 'name'
170+
},
171+
relevance: relevance || 2,
172+
endScope: 'template-tag',
173+
begin: [
174+
/\{%/,
175+
/\s*/,
176+
regex.either(...tagnames)
177+
],
178+
end: /%\}/,
179+
keywords: "in",
180+
contains: [
181+
FILTER,
182+
FUNCTIONS,
183+
STRING,
184+
NUMBER
185+
]
186+
};
187+
};
42188

43-
TAGS = TAGS + ' ' + TAGS.split(' ').map(function(t){return 'end' + t}).join(' ');
189+
const CUSTOM_TAG_RE = /[a-z_]+/;
190+
const TAG = tagNamed(TAG_NAMES, { relevance: 2 });
191+
const CUSTOM_TAG = tagNamed([ CUSTOM_TAG_RE ], { relevance: 1 });
44192

45193
return {
46194
name: 'Twig',
47-
aliases: ['craftcms'],
195+
aliases: [ 'craftcms' ],
48196
case_insensitive: true,
49197
subLanguage: 'xml',
50198
contains: [
51199
hljs.COMMENT(/\{#/, /#\}/),
200+
TAG,
201+
CUSTOM_TAG,
52202
{
53-
className: 'template-tag',
54-
begin: /\{%/, end: /%\}/,
203+
className: 'template-variable',
204+
begin: /\{\{/,
205+
end: /\}\}/,
55206
contains: [
56-
{
57-
className: 'name',
58-
begin: /\w+/,
59-
keywords: TAGS,
60-
starts: {
61-
endsWithParent: true,
62-
contains: [FILTER, FUNCTIONS],
63-
relevance: 0
64-
}
65-
}
207+
'self',
208+
FILTER,
209+
FUNCTIONS,
210+
STRING,
211+
NUMBER
66212
]
67-
},
68-
{
69-
className: 'template-variable',
70-
begin: /\{\{/, end: /\}\}/,
71-
contains: ['self', FILTER, FUNCTIONS]
72213
}
73214
]
74215
};

test/markup/clojure/globals_definition.expect.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ string&quot;</span>
6969

7070
<span class="hljs-comment">;; create a couple shapes and get their area</span>
7171
(<span class="hljs-keyword">def</span> <span class="hljs-title">myCircle</span> (<span class="hljs-name">Circle.</span> <span class="hljs-number">10</span>))
72-
(<span class="hljs-keyword">def</span> <span class="hljs-title">mySquare</span> (<span class="hljs-name">Square.</span> <span class="hljs-number">5</span> <span class="hljs-number">11</span>))
72+
(<span class="hljs-keyword">def</span> <span class="hljs-title">mySquare</span> (<span class="hljs-name">Square.</span> <span class="hljs-number">5</span> <span class="hljs-number">11</span>))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
(<span class="hljs-keyword">def</span> <span class="hljs-title">+x</span> [(<span class="hljs-name">a</span> <span class="hljs-number">1</span>) <span class="hljs-number">+2</span> <span class="hljs-number">-3.0</span> y-5])
22
(<span class="hljs-name">System/getProperty</span> <span class="hljs-string">&quot;java.vm.version&quot;</span>)
33
(<span class="hljs-name">.getEnclosingClass</span> java.util.Map$Entry)
4-
(<span class="hljs-name">java.util.Map$Entry.</span> .getEnclosingClass)
4+
(<span class="hljs-name">java.util.Map$Entry.</span> .getEnclosingClass)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<span class="hljs-keyword">let!</span> (result2 <span class="hljs-operator">:</span> <span class="hljs-type">byte</span>[]) <span class="hljs-operator">=</span> stream.AsyncRead(bufferSize)
1+
<span class="hljs-keyword">let!</span> (result2 <span class="hljs-operator">:</span> <span class="hljs-type">byte</span>[]) <span class="hljs-operator">=</span> stream.AsyncRead(bufferSize)

test/markup/fsharp/computation-expressions.expect.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
<span class="hljs-keyword">return</span> result
2121
}
2222

23-
<span class="hljs-keyword">let</span> result <span class="hljs-operator">=</span> work <span class="hljs-operator">|&gt;</span> Async.RunSynchronously
23+
<span class="hljs-keyword">let</span> result <span class="hljs-operator">=</span> work <span class="hljs-operator">|&gt;</span> Async.RunSynchronously

test/markup/fsharp/types.expect.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,4 @@
140140
<span class="hljs-operator">|</span> CaseK <span class="hljs-keyword">of</span> ``var with spaces``<span class="hljs-operator">:</span> <span class="hljs-type">string</span>
141141
<span class="hljs-operator">|</span> CaseL <span class="hljs-keyword">of</span> ``var with spaces``<span class="hljs-operator">:</span> ``type with spaces``
142142
<span class="hljs-operator">|</span> CaseM <span class="hljs-keyword">of</span> v1 <span class="hljs-operator">:</span> ``type with spaces``
143-
<span class="hljs-operator">|</span> CaseN <span class="hljs-keyword">of</span> ``type with spaces``
143+
<span class="hljs-operator">|</span> CaseN <span class="hljs-keyword">of</span> ``type with spaces``
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<span class="hljs-template-variable">{{ &quot;string with spaces&quot;|<span class="hljs-keyword">url_encode</span> }}</span>
1+
<span class="hljs-template-variable">{{ <span class="hljs-string">&quot;string with spaces&quot;</span><span class="hljs-punctuation">|</span><span class="hljs-keyword">url_encode</span> }}</span>
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<span class="hljs-template-tag">{% <span class="hljs-name"><span class="hljs-keyword">if</span></span> posts|<span class="hljs-keyword">length</span> %}</span><span class="language-xml">
2-
</span><span class="hljs-template-tag">{% <span class="hljs-name"><span class="hljs-keyword">for</span></span> article in articles %}</span><span class="language-xml">
1+
<span class="hljs-template-tag">{%</span> <span class="hljs-name">if</span> posts<span class="hljs-punctuation">|</span><span class="hljs-keyword">length</span> <span class="hljs-template-tag">%}</span><span class="language-xml">
2+
</span><span class="hljs-template-tag">{%</span> <span class="hljs-name">for</span> article <span class="hljs-keyword">in</span> articles <span class="hljs-template-tag">%}</span><span class="language-xml">
33
<span class="hljs-symbol">&amp;lt;</span>div<span class="hljs-symbol">&amp;gt;</span>
4-
</span><span class="hljs-template-variable">{{ article.title|<span class="hljs-keyword">upper</span>() }}</span><span class="language-xml">
4+
</span><span class="hljs-template-variable">{{ article.title<span class="hljs-punctuation">|</span><span class="hljs-keyword">upper</span>() }}</span><span class="language-xml">
55

66
</span><span class="hljs-comment">{# outputs &#x27;WELCOME&#x27; #}</span><span class="language-xml">
77
<span class="hljs-symbol">&amp;lt;</span>/div<span class="hljs-symbol">&amp;gt;</span>
8-
</span><span class="hljs-template-tag">{% <span class="hljs-name"><span class="hljs-keyword">endfor</span></span> %}</span><span class="language-xml">
9-
</span><span class="hljs-template-tag">{% <span class="hljs-name"><span class="hljs-keyword">endif</span></span> %}</span><span class="language-xml">
8+
</span><span class="hljs-template-tag">{%</span> <span class="hljs-name">endfor</span> <span class="hljs-template-tag">%}</span><span class="language-xml">
9+
</span><span class="hljs-template-tag">{%</span> <span class="hljs-name">endif</span> <span class="hljs-template-tag">%}</span><span class="language-xml">
1010

11-
</span><span class="hljs-template-tag">{% <span class="hljs-name"><span class="hljs-keyword">set</span></span> user = json_encode(user) %}</span><span class="language-xml">
11+
</span><span class="hljs-template-tag">{%</span> <span class="hljs-name">set</span> user = json_encode(user) <span class="hljs-template-tag">%}</span><span class="language-xml">
1212
</span>

tools/checkAutoDetect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
'use strict';
33

44
const fs = require('fs');
5-
const hljs = require('../build.js');
5+
const hljs = require('../build/lib/index.js');
66
const path = require('path');
77
const utility = require('../test/utility.js');
88
const Table = require('cli-table');

0 commit comments

Comments
 (0)