Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only recognize well-known comment markers #11

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<!--` as comment markers, for
any other kinds of comments, use `sticky_prefixes`.

This special handling can be disabled by specifying the parameter
`sticky_comments=no`:
Expand Down
48 changes: 48 additions & 0 deletions goldens/comments.in
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,51 @@ Only non-sticky comments:
// only
// comments
// keep-sorted-test end

Slash-star-style comments:
/* keep-sorted-test start */
2
1
/* comment on 3 */
3
/* keep-sorted-test end */

Dash-dash-style comments:
-- keep-sorted-test start
2
1
-- comment on 3
3
-- keep-sorted-test end

Semicolon-style comments:
; keep-sorted-test start
2
1
; comment on 3
3
; keep-sorted-test end

HTML-style comments:
<!-- keep-sorted-test start -->
2
1
<!-- comment on 3 -->
3
<!-- keep-sorted-test end -->

Additional prefixes aren't counted as part of the comment:
// some prefix (normally this is go/) keep-sorted-test start
2
1
// comment on 3
3
// keep-sorted-test end

Non-comment prefixes are still sorted:
* keep-sorted-test start
2
1
* not a comment on 3
3
* keep-sorted-test end
48 changes: 48 additions & 0 deletions goldens/comments.out
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,51 @@ Only non-sticky comments:
// comments
// only
// keep-sorted-test end

Slash-star-style comments:
/* keep-sorted-test start */
1
2
/* comment on 3 */
3
/* keep-sorted-test end */

Dash-dash-style comments:
-- keep-sorted-test start
1
2
-- comment on 3
3
-- keep-sorted-test end

Semicolon-style comments:
; keep-sorted-test start
1
2
; comment on 3
3
; keep-sorted-test end

HTML-style comments:
<!-- keep-sorted-test start -->
1
2
<!-- comment on 3 -->
3
<!-- keep-sorted-test end -->

Additional prefixes aren't counted as part of the comment:
// some prefix (normally this is go/) keep-sorted-test start
1
2
// comment on 3
3
// keep-sorted-test end

Non-comment prefixes are still sorted:
* keep-sorted-test start
* not a comment on 3
1
2
3
* keep-sorted-test end
17 changes: 15 additions & 2 deletions keepsorted/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<!--") {
return "<!--"
}
return ""
}

// hasStickyPrefix determines if s has one of the StickyPrefixes.
Expand Down