Skip to content

Commit

Permalink
Support comma in go template (e.g. for range)
Browse files Browse the repository at this point in the history
```
{{ range $idx, $value := $variable }}
Hello {{ $idx }}
{{ end }}
```
  • Loading branch information
cmaster11 authored and alecthomas committed Sep 23, 2020
1 parent 6665753 commit 42e9638
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lexers/g/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var goTemplateRules = Rules{
{`\s+`, Whitespace, nil},
{`\(`, Operator, Push("subexpression")},
{`(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},
{`\||:?=`, Operator, nil},
{`\||:?=|,`, Operator, nil},
{`[$]?[^\W\d]\w*`, NameOther, nil},
{`[$]?\.(?:[^\W\d]\w*)?`, NameAttribute, nil},
{`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
Expand Down
6 changes: 5 additions & 1 deletion lexers/testdata/go-text-template.actual
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
*/}}

{{ $myVar := 2 }}
{{ $myVar = 4 }}
{{ $myVar = 4 }}

{{ range $idx, $value := $variable }}
Hello {{ $idx }}
{{ end }}
27 changes: 27 additions & 0 deletions lexers/testdata/go-text-template.expected
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,32 @@
{"type":"TextWhitespace","value":" "},
{"type":"NameOther","value":"4"},
{"type":"TextWhitespace","value":" "},
{"type":"CommentPreproc","value":"}}"},
{"type":"Other","value":"\n\n"},
{"type":"CommentPreproc","value":"{{"},
{"type":"TextWhitespace","value":" "},
{"type":"Keyword","value":"range"},
{"type":"TextWhitespace","value":" "},
{"type":"NameOther","value":"$idx"},
{"type":"Operator","value":","},
{"type":"TextWhitespace","value":" "},
{"type":"NameOther","value":"$value"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":":="},
{"type":"TextWhitespace","value":" "},
{"type":"NameOther","value":"$variable"},
{"type":"TextWhitespace","value":" "},
{"type":"CommentPreproc","value":"}}"},
{"type":"Other","value":"\nHello "},
{"type":"CommentPreproc","value":"{{"},
{"type":"TextWhitespace","value":" "},
{"type":"NameOther","value":"$idx"},
{"type":"TextWhitespace","value":" "},
{"type":"CommentPreproc","value":"}}"},
{"type":"Other","value":"\n"},
{"type":"CommentPreproc","value":"{{"},
{"type":"TextWhitespace","value":" "},
{"type":"Keyword","value":"end"},
{"type":"TextWhitespace","value":" "},
{"type":"CommentPreproc","value":"}}"}
]

0 comments on commit 42e9638

Please sign in to comment.