Skip to content

Commit 42e9638

Browse files
cmaster11alecthomas
authored andcommitted
Support comma in go template (e.g. for range)
``` {{ range $idx, $value := $variable }} Hello {{ $idx }} {{ end }} ```
1 parent 6665753 commit 42e9638

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

Diff for: lexers/g/go.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ var goTemplateRules = Rules{
8080
{`\s+`, Whitespace, nil},
8181
{`\(`, Operator, Push("subexpression")},
8282
{`(range|if|else|while|with|template|end|true|false|nil|and|call|html|index|js|len|not|or|print|printf|println|urlquery|eq|ne|lt|le|gt|ge)\b`, Keyword, nil},
83-
{`\||:?=`, Operator, nil},
83+
{`\||:?=|,`, Operator, nil},
8484
{`[$]?[^\W\d]\w*`, NameOther, nil},
8585
{`[$]?\.(?:[^\W\d]\w*)?`, NameAttribute, nil},
8686
{`"(\\\\|\\"|[^"])*"`, LiteralString, nil},

Diff for: lexers/testdata/go-text-template.actual

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33
*/}}
44

55
{{ $myVar := 2 }}
6-
{{ $myVar = 4 }}
6+
{{ $myVar = 4 }}
7+
8+
{{ range $idx, $value := $variable }}
9+
Hello {{ $idx }}
10+
{{ end }}

Diff for: lexers/testdata/go-text-template.expected

+27
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,32 @@
1919
{"type":"TextWhitespace","value":" "},
2020
{"type":"NameOther","value":"4"},
2121
{"type":"TextWhitespace","value":" "},
22+
{"type":"CommentPreproc","value":"}}"},
23+
{"type":"Other","value":"\n\n"},
24+
{"type":"CommentPreproc","value":"{{"},
25+
{"type":"TextWhitespace","value":" "},
26+
{"type":"Keyword","value":"range"},
27+
{"type":"TextWhitespace","value":" "},
28+
{"type":"NameOther","value":"$idx"},
29+
{"type":"Operator","value":","},
30+
{"type":"TextWhitespace","value":" "},
31+
{"type":"NameOther","value":"$value"},
32+
{"type":"TextWhitespace","value":" "},
33+
{"type":"Operator","value":":="},
34+
{"type":"TextWhitespace","value":" "},
35+
{"type":"NameOther","value":"$variable"},
36+
{"type":"TextWhitespace","value":" "},
37+
{"type":"CommentPreproc","value":"}}"},
38+
{"type":"Other","value":"\nHello "},
39+
{"type":"CommentPreproc","value":"{{"},
40+
{"type":"TextWhitespace","value":" "},
41+
{"type":"NameOther","value":"$idx"},
42+
{"type":"TextWhitespace","value":" "},
43+
{"type":"CommentPreproc","value":"}}"},
44+
{"type":"Other","value":"\n"},
45+
{"type":"CommentPreproc","value":"{{"},
46+
{"type":"TextWhitespace","value":" "},
47+
{"type":"Keyword","value":"end"},
48+
{"type":"TextWhitespace","value":" "},
2249
{"type":"CommentPreproc","value":"}}"}
2350
]

0 commit comments

Comments
 (0)