nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: alecthomas/kong
Pull request #274 (2022-02-20 18:03:31)

Support listing enums in specified order
Hi, I saw the issue and wanted to contribute. I am new to this codebase, I am sorry if I missed something.

Closes https://github.com/alecthomas/kong/issues/230


alecthomas: I don't think this will solve #230. The enum values are stored in a map, so this will just result in whatever the map order happens to be. I think it would be better to just store them in a slice instead, and not sort them by default, as the ordering is just a side-effect of being in a map.
alecthomas: Nice!

Patch:
diff --git a/context.go b/context.go
index 1e26ff5..6d48f5f 100644
--- a/context.go
+++ b/context.go
@@ -891,16 +891,15 @@ func checkEnum(value *Value, target reflect.Value) error {
 		return errors.Errorf("enum can only be applied to a slice or value")
 
 	default:
-		enumMap := value.EnumMap()
+		enumSlice := value.EnumSlice()
 		v := fmt.Sprintf("%v", target)
-		if enumMap[v] {
-			return nil
-		}
 		enums := []string{}
-		for enum := range enumMap {
+		for _, enum := range enumSlice {
+			if enum == v {
+				return nil
+			}
 			enums = append(enums, fmt.Sprintf("%q", enum))
 		}
-		sort.Strings(enums)
 		return fmt.Errorf("%s must be one of %s but got %q", value.ShortSummary(), strings.Join(enums, ","), target.Interface())
 	}
 }
diff --git a/model.go b/model.go
index 1013d93..d20d864 100644
--- a/model.go
+++ b/model.go
@@ -257,6 +257,16 @@ func (v *Value) EnumMap() map[string]bool {
 	return out
 }
 
+// EnumSlice returns a slice of the enums in this value.
+func (v *Value) EnumSlice() []string {
+	parts := strings.Split(v.Enum, ",")
+	out := make([]string, len(parts))
+	for i, part := range parts {
+		out[i] = strings.TrimSpace(part)
+	}
+	return out
+}
+
 // ShortSummary returns a human-readable summary of the value, not including any placeholders/defaults.
 func (v *Value) ShortSummary() string {
 	if v.Flag != nil {


Test patch:
diff --git a/kong_test.go b/kong_test.go
index e7568b2..6e52f5a 100644
--- a/kong_test.go
+++ b/kong_test.go
@@ -663,6 +663,7 @@ func TestInterpolationIntoModel(t *testing.T) {
 	require.Equal(t, "Some default value.", flag.Default)
 	require.Equal(t, "Help, I need chickens!", flag.Help)
 	require.Equal(t, map[string]bool{"a": true, "b": true, "c": true, "d": true}, flag.EnumMap())
+	require.Equal(t, []string{"a", "b", "c", "d"}, flag.EnumSlice())
 	require.Equal(t, "One of a,b", flag2.Help)
 	require.Equal(t, "SAVE_THE_QUEEN", flag3.Env)
 	require.Equal(t, "God SAVE_THE_QUEEN", flag3.Help)
@@ -787,6 +788,14 @@ func TestEnum(t *testing.T) {
 	require.EqualError(t, err, "--flag must be one of \"a\",\"b\",\"c\" but got \"d\"")
 }
 
+func TestEnumMeaningfulOrder(t *testing.T) {
+	var cli struct {
+		Flag string `enum:"first,second,third,fourth,fifth" required:""`
+	}
+	_, err := mustNew(t, &cli).Parse([]string{"--flag", "sixth"})
+	require.EqualError(t, err, "--flag must be one of \"first\",\"second\",\"third\",\"fourth\",\"fifth\" but got \"sixth\"")
+}
+
 type commandWithHook struct {
 	value string
 }
Repository: alecthomas/kong
Pull request #321 (2022-07-28 01:15:32)

fix: change VersionFlag to a BeforeReset hook
Similar to how `--help` was updated in https://github.com/alecthomas/kong/pull/298, `--version` changed to a BeforeReset hook so that it will always work.

alecthomas: Great idea, thanks :)

Patch:
diff --git a/util.go b/util.go
index 9468ef5..50b1dfe 100644
--- a/util.go
+++ b/util.go
@@ -29,8 +29,8 @@ func (c ConfigFlag) BeforeResolve(kong *Kong, ctx *Context, trace *Path) error {
 // VersionFlag is a flag type that can be used to display a version number, stored in the "version" variable.
 type VersionFlag bool
 
-// BeforeApply writes the version variable and terminates with a 0 exit status.
-func (v VersionFlag) BeforeApply(app *Kong, vars Vars) error {
+// BeforeReset writes the version variable and terminates with a 0 exit status.
+func (v VersionFlag) BeforeReset(app *Kong, vars Vars) error {
 	fmt.Fprintln(app.Stdout, vars["version"])
 	app.Exit(0)
 	return nil


Test patch:
diff --git a/kong_test.go b/kong_test.go
index d66cbd6..18ac9f1 100644
--- a/kong_test.go
+++ b/kong_test.go
@@ -1544,7 +1544,8 @@ func TestPassthroughCmdOnlyStringArgs(t *testing.T) {
 
 func TestHelpShouldStillWork(t *testing.T) {
 	type CLI struct {
-		Dir string `type:"existingdir" default:"missing-dir"`
+		Dir  string `type:"existingdir" default:"missing-dir"`
+		File string `type:"existingfile" default:"testdata/missing.txt"`
 	}
 	var cli CLI
 	w := &strings.Builder{}
@@ -1563,6 +1564,29 @@ func TestHelpShouldStillWork(t *testing.T) {
 	assert.Error(t, err)
 }
 
+func TestVersionFlagShouldStillWork(t *testing.T) {
+	type CLI struct {
+		Dir     string `type:"existingdir" default:"missing-dir"`
+		File    string `type:"existingfile" default:"testdata/missing.txt"`
+		Version kong.VersionFlag
+	}
+	var cli CLI
+	w := &strings.Builder{}
+	k := mustNew(t, &cli, kong.Writers(w, w))
+	rc := -1 // init nonzero to help assert help hook was called
+	k.Exit = func(i int) {
+		rc = i
+	}
+	_, err := k.Parse([]string{"--version"})
+	t.Log(w.String())
+	// checking return code validates the help hook was called
+	assert.Zero(t, rc)
+	// allow for error propagation from other validation (only for the
+	// sake of this test, due to the exit function not actually exiting the
+	// program; errors will not propagate in the real world).
+	assert.Error(t, err)
+}
+
 func TestSliceDecoderHelpfulErrorMsg(t *testing.T) {
 	tests := []struct {
 		name string
Repository: alecthomas/kong
Pull request #334 (2022-09-21 23:00:17)

Fix loading large integers from JSON files
`%v` uses E notation to format floats with more than 6 digits. Since JSON numbers are always floats, Kong breaks when loading large integers from JSON files.


Patch:
diff --git a/mapper.go b/mapper.go
index e94ae76..6d36b15 100644
--- a/mapper.go
+++ b/mapper.go
@@ -363,9 +363,12 @@ func intDecoder(bits int) MapperFunc { // nolint: dupl
 		case string:
 			sv = v
 
-		case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
+		case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
 			sv = fmt.Sprintf("%v", v)
 
+		case float32, float64:
+			sv = fmt.Sprintf("%0.f", v)
+
 		default:
 			return fmt.Errorf("expected an int but got %q (%T)", t, t.Value)
 		}


Test patch:
diff --git a/mapper_test.go b/mapper_test.go
index 4909758..804ac39 100644
--- a/mapper_test.go
+++ b/mapper_test.go
@@ -400,6 +400,21 @@ func TestNumbers(t *testing.T) {
 	})
 }
 
+func TestJSONLargeNumber(t *testing.T) {
+	// https://github.com/alecthomas/kong/pull/334
+	const n = 1000000
+	var cli struct {
+		N int64
+	}
+	json := fmt.Sprintf(`{"n": %d}`, n)
+	r, err := kong.JSON(strings.NewReader(json))
+	assert.NoError(t, err)
+	parser := mustNew(t, &cli, kong.Resolvers(r))
+	_, err = parser.Parse([]string{})
+	assert.NoError(t, err)
+	assert.Equal(t, n, cli.N)
+}
+
 func TestFileMapper(t *testing.T) {
 	type CLI struct {
 		File *os.File `arg:""`
Repository: alecthomas/kong
Pull request #419 (2024-03-14 17:12:19)

Aliases can be duplicated between different sub commands
Make sure we remove all the aliases from the seenFlags tracker just like we do with other flags/short flags


Patch:
diff --git a/build.go b/build.go
index a52d90f..1cfe1c3 100644
--- a/build.go
+++ b/build.go
@@ -170,6 +170,9 @@ MAIN:
 		if flag.Short != 0 {
 			delete(seenFlags, "-"+string(flag.Short))
 		}
+		for _, aflag := range flag.Aliases {
+			delete(seenFlags, "--"+aflag)
+		}
 	}
 
 	if err := validatePositionalArguments(node); err != nil {


Test patch:
diff --git a/kong_test.go b/kong_test.go
index bd1ecfa..0e9fa9a 100644
--- a/kong_test.go
+++ b/kong_test.go
@@ -1340,6 +1340,20 @@ func TestDuplicateAliases(t *testing.T) {
 	assert.EqualError(t, err, "<anonymous struct>.Flag2: duplicate flag --flag")
 }
 
+func TestSubCommandAliases(t *testing.T) {
+	type SubC struct {
+		Flag1 string `aliases:"flag"`
+	}
+
+	cli1 := struct {
+		Sub1 SubC `cmd:"sub1"`
+		Sub2 SubC `cmd:"sub2"`
+	}{}
+
+	_, err := kong.New(&cli1)
+	assert.NoError(t, err, "dupe aliases shouldn't error if they're in separate sub commands")
+}
+
 func TestDuplicateAliasLong(t *testing.T) {
 	cli2 := struct {
 		Flag  string ``
Repository: alecthomas/kong
Pull request #510 (2025-03-12 23:07:08)

feat: Placeholder string interpolation.
Add support string interpolation in placeholder values.

Some tools like to place the supported enum values in the placeholder string. So, it's a tad annoying being able fill in the `enum` value from vars but not the `placeholder` value.


Patch:
diff --git a/README.md b/README.md
index 3b62344..9e1d6cc 100644
--- a/README.md
+++ b/README.md
@@ -620,8 +620,8 @@ also supports dynamically adding commands via `kong.DynamicCommand()`.
 
 ## Variable interpolation
 
-Kong supports limited variable interpolation into help strings, enum lists and
-default values.
+Kong supports limited variable interpolation into help strings, placeholder strings,
+enum lists and default values.
 
 Variables are in the form:
 
diff --git a/kong.go b/kong.go
index 4f6be87..a09b711 100644
--- a/kong.go
+++ b/kong.go
@@ -270,6 +270,11 @@ func (k *Kong) interpolateValue(value *Value, vars Vars) (err error) {
 		if len(value.Flag.Envs) != 0 {
 			updatedVars["env"] = value.Flag.Envs[0]
 		}
+
+		value.Flag.PlaceHolder, err = interpolate(value.Flag.PlaceHolder, vars, updatedVars)
+		if err != nil {
+			return fmt.Errorf("placeholder value for %s: %s", value.Summary(), err)
+		}
 	}
 	value.Help, err = interpolate(value.Help, vars, updatedVars)
 	if err != nil {


Test patch:
diff --git a/kong_test.go b/kong_test.go
index 4edda4f..3ea6b5f 100644
--- a/kong_test.go
+++ b/kong_test.go
@@ -766,8 +766,8 @@ func TestPassesThroughOriginalCommandError(t *testing.T) {
 
 func TestInterpolationIntoModel(t *testing.T) {
 	var cli struct {
-		Flag    string `default:"${default_value}" help:"Help, I need ${somebody}" enum:"${enum}"`
-		EnumRef string `enum:"a,b" required:"" help:"One of ${enum}"`
+		Flag    string `default:"${default_value}" help:"Help, I need ${somebody}" enum:"${enum}" placeholder:"${enum}"`
+		EnumRef string `enum:"a,b" required:"" help:"One of ${enum}" placeholder:"${enum}"`
 		EnvRef  string `env:"${env}" help:"God ${env}"`
 	}
 	_, err := kong.New(&cli)
@@ -787,7 +787,9 @@ func TestInterpolationIntoModel(t *testing.T) {
 	assert.Equal(t, "Help, I need chickens!", flag.Help)
 	assert.Equal(t, map[string]bool{"a": true, "b": true, "c": true, "d": true}, flag.EnumMap())
 	assert.Equal(t, []string{"a", "b", "c", "d"}, flag.EnumSlice())
+	assert.Equal(t, "a,b,c,d", flag.PlaceHolder)
 	assert.Equal(t, "One of a,b", flag2.Help)
+	assert.Equal(t, "a,b", flag2.PlaceHolder)
 	assert.Equal(t, []string{"SAVE_THE_QUEEN"}, flag3.Envs)
 	assert.Equal(t, "God SAVE_THE_QUEEN", flag3.Help)
 }
Repository: alecthomas/chroma
Pull request #231 (2019-02-08 22:54:12)

Improve numbers parsing in Kotlin


Patch:
diff --git a/lexers/k/kotlin.go b/lexers/k/kotlin.go
index bf6fd31..56b7966 100644
--- a/lexers/k/kotlin.go
+++ b/lexers/k/kotlin.go
@@ -30,7 +30,7 @@ var Kotlin = internal.Register(MustNewLexer(
 			{`"""[^"]*"""`, LiteralString, nil},
 			{`"(\\\\|\\"|[^"\n])*["\n]`, LiteralString, nil},
 			{`'\\.'|'[^\\]'`, LiteralStringChar, nil},
-			{`[0-9](\.[0-9]*)?([eE][+-][0-9]+)?[flFL]?|0[xX][0-9a-fA-F]+[Ll]?`, LiteralNumber, nil},
+			{`0[xX][0-9a-fA-F]+[Uu]?[Ll]?|[0-9]+(\.[0-9]*)?([eE][+-][0-9]+)?[fF]?[Uu]?[Ll]?`, LiteralNumber, nil},
 			{`(companion)(\s+)(object)`, ByGroups(Keyword, Text, Keyword), nil},
 			{`(class|interface|object)(\s+)`, ByGroups(Keyword, Text), Push("class")},
 			{`(package|import)(\s+)`, ByGroups(Keyword, Text), Push("package")},


Test patch:
diff --git a/lexers/testdata/kotlin.actual b/lexers/testdata/kotlin.actual
index 12904de..7dbd8ea 100644
--- a/lexers/testdata/kotlin.actual
+++ b/lexers/testdata/kotlin.actual
@@ -10,6 +10,7 @@ fun main(args: Array<String>) {
   StringBuilder().apply {
     append(multiline)
   }
+  val unsigned = 0x00UL + 123u + 76.54
 }
 /*
 */
diff --git a/lexers/testdata/kotlin.expected b/lexers/testdata/kotlin.expected
index 09efbdf..cd7574b 100644
--- a/lexers/testdata/kotlin.expected
+++ b/lexers/testdata/kotlin.expected
@@ -53,6 +53,22 @@
   {"type":"Punctuation","value":")"},
   {"type":"Text","value":"\n  "},
   {"type":"Punctuation","value":"}"},
+  {"type":"Text","value":"\n  "},
+  {"type":"Keyword","value":"val"},
+  {"type":"Text","value":" "},
+  {"type":"NameProperty","value":"unsigned"},
+  {"type":"Text","value":" "},
+  {"type":"Punctuation","value":"="},
+  {"type":"Text","value":" "},
+  {"type":"LiteralNumber","value":"0x00UL"},
+  {"type":"Text","value":" "},
+  {"type":"Punctuation","value":"+"},
+  {"type":"Text","value":" "},
+  {"type":"LiteralNumber","value":"123u"},
+  {"type":"Text","value":" "},
+  {"type":"Punctuation","value":"+"},
+  {"type":"Text","value":" "},
+  {"type":"LiteralNumber","value":"76.54"},
   {"type":"Text","value":"\n"},
   {"type":"Punctuation","value":"}"},
   {"type":"Text","value":"\n"},
Repository: alecthomas/chroma
Pull request #237 (2019-03-04 21:14:49)

Support SystemVerilog DPI and DPI-C import statements
Ref: Section "35.5.4 Import declarations" from SystemVerilog standard
IEEE 1800-2017.

alecthomas: Thanks!

Patch:
diff --git a/lexers/s/systemverilog.go b/lexers/s/systemverilog.go
index 21021a2..85c459f 100644
--- a/lexers/s/systemverilog.go
+++ b/lexers/s/systemverilog.go
@@ -18,6 +18,7 @@ var Systemverilog = internal.Register(MustNewLexer(
 		"root": {
 			{"^\\s*`define", CommentPreproc, Push("macro")},
 			{`^(\s*)(package)(\s+)`, ByGroups(Text, KeywordNamespace, Text), nil},
+			{`^(\s*)(import)(\s+)("DPI(?:-C)?")(\s+)`, ByGroups(Text, KeywordNamespace, Text, LiteralString, Text), nil},
 			{`^(\s*)(import)(\s+)`, ByGroups(Text, KeywordNamespace, Text), Push("import")},
 			{`\n`, Text, nil},
 			{`\s+`, Text, nil},


Test patch:
diff --git a/lexers/testdata/systemverilog.actual b/lexers/testdata/systemverilog.actual
new file mode 100644
index 0000000..1f7fcc5
--- /dev/null
+++ b/lexers/testdata/systemverilog.actual
@@ -0,0 +1,11 @@
+// Comment
+
+program top;
+
+  import "DPI-C" hello=task hello();
+
+  initial begin
+    hello();
+  end
+
+endprogram : top
diff --git a/lexers/testdata/systemverilog.expected b/lexers/testdata/systemverilog.expected
new file mode 100644
index 0000000..6b55663
--- /dev/null
+++ b/lexers/testdata/systemverilog.expected
@@ -0,0 +1,35 @@
+[
+  {"type":"CommentSingle","value":"// Comment\n"},
+  {"type":"Text","value":"\n"},
+  {"type":"Keyword","value":"program"},
+  {"type":"Text","value":" "},
+  {"type":"Name","value":"top"},
+  {"type":"Punctuation","value":";"},
+  {"type":"Text","value":"\n\n  "},
+  {"type":"KeywordNamespace","value":"import"},
+  {"type":"Text","value":" "},
+  {"type":"LiteralString","value":"\"DPI-C\""},
+  {"type":"Text","value":" "},
+  {"type":"Name","value":"hello"},
+  {"type":"Operator","value":"="},
+  {"type":"Keyword","value":"task"},
+  {"type":"Text","value":" "},
+  {"type":"Name","value":"hello"},
+  {"type":"Punctuation","value":"();"},
+  {"type":"Text","value":"\n\n  "},
+  {"type":"Keyword","value":"initial"},
+  {"type":"Text","value":" "},
+  {"type":"Keyword","value":"begin"},
+  {"type":"Text","value":"\n    "},
+  {"type":"Name","value":"hello"},
+  {"type":"Punctuation","value":"();"},
+  {"type":"Text","value":"\n  "},
+  {"type":"Keyword","value":"end"},
+  {"type":"Text","value":"\n\n"},
+  {"type":"Keyword","value":"endprogram"},
+  {"type":"Text","value":" "},
+  {"type":"Operator","value":":"},
+  {"type":"Text","value":" "},
+  {"type":"Name","value":"top"},
+  {"type":"Text","value":"\n"}
+]
Repository: alecthomas/chroma
Pull request #358 (2020-05-09 12:55:03)

Fix invalid css when all styles is used
The `--all-styles` option currently produces invalid css for the `Text` token, because it doesn't have an associated class name.

The invalid css is `/* Text */ .chroma . {  }` and can be reproduced by running `chroma --html-styles --html-all-styles`.

More discussion about this issue at https://github.com/gohugoio/hugo/issues/7207


Patch:
diff --git a/formatters/html/html.go b/formatters/html/html.go
index 0bdc1e5..ad48da1 100644
--- a/formatters/html/html.go
+++ b/formatters/html/html.go
@@ -366,8 +366,12 @@ func (f *Formatter) WriteCSS(w io.Writer, style *chroma.Style) error {
 		if tt == chroma.Background {
 			continue
 		}
+		class := f.class(tt)
+		if class == "" {
+			continue
+		}
 		styles := css[tt]
-		if _, err := fmt.Fprintf(w, "/* %s */ .%schroma .%s { %s }\n", tt, f.prefix, f.class(tt), styles); err != nil {
+		if _, err := fmt.Fprintf(w, "/* %s */ .%schroma .%s { %s }\n", tt, f.prefix, class, styles); err != nil {
 			return err
 		}
 	}


Test patch:
diff --git a/formatters/html/html_test.go b/formatters/html/html_test.go
index cd9ea98..b056f19 100644
--- a/formatters/html/html_test.go
+++ b/formatters/html/html_test.go
@@ -222,3 +222,14 @@ func TestReconfigureOptions(t *testing.T) {
 	assert.NoError(t, err)
 	assert.Equal(t, `<pre class="chroma"><span class="nb">echo</span> FOO</pre>`, buf.String())
 }
+
+func TestWriteCssWithAllClasses(t *testing.T) {
+	formatter := New()
+	formatter.allClasses = true
+
+	var buf bytes.Buffer
+	err := formatter.WriteCSS(&buf, styles.Fallback)
+
+	assert.NoError(t, err)
+	assert.NotContains(t, buf.String(), ".chroma . {", "Generated css doesn't contain invalid css")
+}
Repository: alecthomas/chroma
Pull request #359 (2020-05-09 13:12:13)

Bash: Parse single comment mark as comment
Fixes https://github.com/alecthomas/chroma/issues/318

| Before | After |
| -- | -- |
| <img width="174" alt="Screen Shot 2020-05-09 at 15 11 37" src="https://user-images.githubusercontent.com/329222/81474682-6d09b300-9207-11ea-9b00-656bf9a96f00.png"> | <img width="160" alt="Screen Shot 2020-05-09 at 15 11 15" src="https://user-images.githubusercontent.com/329222/81474687-6ed37680-9207-11ea-8fb2-74f5fa0a35ef.png"> |




Patch:
diff --git a/lexers/b/bash.go b/lexers/b/bash.go
index 38b3f22..33f6cd8 100644
--- a/lexers/b/bash.go
+++ b/lexers/b/bash.go
@@ -36,7 +36,7 @@ var Bash = internal.Register(MustNewLexer(
 			{`\b(if|fi|else|while|do|done|for|then|return|function|case|select|continue|until|esac|elif)(\s*)\b`, ByGroups(Keyword, Text), nil},
 			{"\\b(alias|bg|bind|break|builtin|caller|cd|command|compgen|complete|declare|dirs|disown|echo|enable|eval|exec|exit|export|false|fc|fg|getopts|hash|help|history|jobs|kill|let|local|logout|popd|printf|pushd|pwd|read|readonly|set|shift|shopt|source|suspend|test|time|times|trap|true|type|typeset|ulimit|umask|unalias|unset|wait)(?=[\\s)`])", NameBuiltin, nil},
 			{`\A#!.+\n`, CommentPreproc, nil},
-			{`#.*\S`, CommentSingle, nil},
+			{`#.*(\S|$)`, CommentSingle, nil},
 			{`\\[\w\W]`, LiteralStringEscape, nil},
 			{`(\b\w+)(\s*)(\+?=)`, ByGroups(NameVariable, Text, Operator), nil},
 			{`[\[\]{}()=]`, Operator, nil},


Test patch:
diff --git a/lexers/testdata/bash.actual b/lexers/testdata/bash.actual
index fe54c5f..a094d87 100644
--- a/lexers/testdata/bash.actual
+++ b/lexers/testdata/bash.actual
@@ -43,4 +43,7 @@ tiger_em () {
         get_em $directory "ftp://ftp2.census.gov/geo/tiger/TIGER2010/"$1
         shift
     done
-}
\ No newline at end of file
+}
+
+# bare comment marker
+#
diff --git a/lexers/testdata/bash.expected b/lexers/testdata/bash.expected
index 75b068f..0137bc0 100644
--- a/lexers/testdata/bash.expected
+++ b/lexers/testdata/bash.expected
@@ -130,5 +130,10 @@
   {"type":"Text","value":"\n    "},
   {"type":"Keyword","value":"done"},
   {"type":"Text","value":"\n"},
-  {"type":"Operator","value":"}"}
+  {"type":"Operator","value":"}"},
+  {"type":"Text","value":"\n\n"},
+  {"type":"CommentSingle","value":"# bare comment marker"},
+  {"type":"Text","value":"\n"},
+  {"type":"CommentSingle","value":"#"},
+  {"type":"Text","value":"\n"}
 ]
Repository: alecthomas/chroma
Pull request #402 (2020-09-23 03:41:06)

Support comma in go template (e.g. for range)
In 

```
{{ range $idx, $value := $variable }}
Hello {{ $idx }}
{{ end }}
```

the comma was an `Error`!


Patch:
diff --git a/lexers/g/go.go b/lexers/g/go.go
index ef400b7..8eced07 100644
--- a/lexers/g/go.go
+++ b/lexers/g/go.go
@@ -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},


Test patch:
diff --git a/lexers/testdata/go-text-template.actual b/lexers/testdata/go-text-template.actual
index 7761c3d..a014cc1 100644
--- a/lexers/testdata/go-text-template.actual
+++ b/lexers/testdata/go-text-template.actual
@@ -3,4 +3,8 @@
 */}}
 
 {{ $myVar := 2 }}
-{{ $myVar = 4 }}
\ No newline at end of file
+{{ $myVar = 4 }}
+
+{{ range $idx, $value := $variable }}
+Hello {{ $idx }}
+{{ end }}
\ No newline at end of file
diff --git a/lexers/testdata/go-text-template.expected b/lexers/testdata/go-text-template.expected
index f774669..651c727 100644
--- a/lexers/testdata/go-text-template.expected
+++ b/lexers/testdata/go-text-template.expected
@@ -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":"}}"}
 ]