diff --git a/README.md b/README.md index 8ac7cd9..21ffbaf 100644 --- a/README.md +++ b/README.md @@ -179,8 +179,10 @@ allows for sorting data such as Go structs and JSON objects. #### Comments Comments embedded within the sorted block are made to stick with their -successor. The comment lines must start with the same token as the -keep-sorted instruction itself (e.g. `#` in the case below). +successor. The comment lines must start with the same comment marker as the +keep-sorted instruction itself (e.g. `#` in the case below). keep-sorted +will recognize `//`, `/*`, `#`, `--`, `;`, and ` +2 +1 + +3 +`, + + want: ` +// some prefix (normally this is go/) keep-sorted-test start +1 +2 +// comment on 3 +3 +// keep-sorted-test end + +# some prefix (normally this is go/) keep-sorted-test start +1 +2 +# comment on 3 +3 +# keep-sorted-test end + +/* some prefix (normally this is go/) keep-sorted-test start */ +1 +2 +/* comment on 3 */ +3 +/* keep-sorted-test end */ + +-- some prefix (normally this is go/) keep-sorted-test start +1 +2 +-- comment on 3 +3 +-- keep-sorted-test end + +; some prefix (normally this is go/) keep-sorted-test start +1 +2 +; comment on 3 +3 +; keep-sorted-test end + + +1 +2 + +3 +`, + }, } { t.Run(tc.name, func(t *testing.T) { got, gotAlreadyFixed := New("keep-sorted-test").Fix(tc.in, nil) diff --git a/keepsorted/options.go b/keepsorted/options.go index 0e98156..3aeb08a 100644 --- a/keepsorted/options.go +++ b/keepsorted/options.go @@ -180,8 +180,21 @@ func parseValueWithDefault(f reflect.StructField, key, val string, defaultFn fun } func (f *Fixer) guessCommentMarker(startLine string) string { - sp := strings.SplitN(startLine, f.ID, 2) - return strings.TrimSpace(sp[0]) + startLine = strings.TrimSpace(startLine) + if strings.HasPrefix(startLine, "//") { + return "//" + } else if strings.HasPrefix(startLine, "#") { + return "#" + } else if strings.HasPrefix(startLine, "/*") { + return "/*" + } else if strings.HasPrefix(startLine, "--") { + return "--" + } else if strings.HasPrefix(startLine, ";") { + return ";" + } else if strings.HasPrefix(startLine, "