-
Notifications
You must be signed in to change notification settings - Fork 29
Adding user-defined group delimiters #104
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
Open
drubery
wants to merge
1
commit into
google:main
Choose a base branch
from
drubery:push-tlymwmkvwxvu
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| Sort by consecutive newlines: | ||
| // keep-sorted-test start group_delimiter_regexes=['^$'] | ||
| b-block | ||
| attached line 1 | ||
| attached line 2 | ||
|
|
||
| a-block | ||
| attached line 3 | ||
| attached line 4 | ||
| // keep-sorted-test end | ||
|
|
||
| Sort by semicolon: | ||
| // keep-sorted-test start group_delimiter_regexes=[';$'] | ||
| XXX a semicolon ; in the middle is ignored | ||
| But at the end it terminates the group ; | ||
|
|
||
| ZZZ This group will come after YYY; | ||
|
|
||
| YYY will be sorted up; | ||
| // keep-sorted-test end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| Sort by consecutive newlines: | ||
| // keep-sorted-test start group_delimiter_regexes=['^$'] | ||
| a-block | ||
| attached line 3 | ||
| attached line 4 | ||
| b-block | ||
| attached line 1 | ||
| attached line 2 | ||
|
|
||
| // keep-sorted-test end | ||
|
|
||
| Sort by semicolon: | ||
| // keep-sorted-test start group_delimiter_regexes=[';$'] | ||
| XXX a semicolon ; in the middle is ignored | ||
| But at the end it terminates the group ; | ||
|
|
||
| YYY will be sorted up; | ||
|
|
||
| ZZZ This group will come after YYY; | ||
| // keep-sorted-test end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,7 +35,7 @@ import ( | |
| // true is unmarshaled as 1, false as 0. | ||
| type IntOrBool int | ||
|
|
||
| type ByRegexOption struct { | ||
| type RegexOption struct { | ||
| Pattern *regexp.Regexp | ||
| Template *string | ||
| } | ||
|
|
@@ -67,7 +67,7 @@ func (opts BlockOptions) String() string { | |
| // - []string: key=a,b,c,d | ||
| // - map[string]bool: key=a,b,c,d | ||
| // - int: key=123 | ||
| // - ByRegexOptions key=a,b,c,d, key=[yaml_list] | ||
| // - []RegexOptions: key=a,b,c,d, key=[yaml_list] | ||
| type blockOptions struct { | ||
| // AllowYAMLLists determines whether list.set valued options are allowed to be specified by YAML. | ||
| AllowYAMLLists bool `key:"allow_yaml_lists"` | ||
|
|
@@ -88,6 +88,8 @@ type blockOptions struct { | |
| StickyComments bool `key:"sticky_comments"` | ||
| // StickyPrefixes tells us about other types of lines that should behave as sticky comments. | ||
| StickyPrefixes map[string]bool `key:"sticky_prefixes"` | ||
| // GroupDelimiterRegexes tells us if a line is allowed to end a group. | ||
| GroupDelimiterRegexes []RegexOption `key:"group_delimiter_regexes"` | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if someone provides a replacement template with this option? Is that even valid? I wonder if it should actually be separate from the ByRegexpOption |
||
|
|
||
| /////////////////////// | ||
| // Sorting options // | ||
|
|
@@ -102,7 +104,7 @@ type blockOptions struct { | |
| // IgnorePrefixes is a slice of prefixes that we do not consider when sorting lines. | ||
| IgnorePrefixes []string `key:"ignore_prefixes"` | ||
| // ByRegex is a slice of regexes that are used to extract the pieces of the line group that keep-sorted should sort by. | ||
| ByRegex []ByRegexOption `key:"by_regex"` | ||
| ByRegex []RegexOption `key:"by_regex"` | ||
|
|
||
| //////////////////////////// | ||
| // Post-sorting options // | ||
|
|
@@ -210,8 +212,8 @@ func formatValue(val reflect.Value) (string, error) { | |
| return strconv.Itoa(int(val.Int())), nil | ||
| case reflect.TypeFor[int](): | ||
| return strconv.Itoa(int(val.Int())), nil | ||
| case reflect.TypeFor[[]ByRegexOption](): | ||
| opts := val.Interface().([]ByRegexOption) | ||
| case reflect.TypeFor[[]RegexOption](): | ||
| opts := val.Interface().([]RegexOption) | ||
| vals := make([]string, 0, len(opts)) | ||
| seenTemplate := false | ||
| for _, opt := range opts { | ||
|
|
@@ -390,20 +392,20 @@ func (opts blockOptions) trimIgnorePrefix(s string) string { | |
| return s | ||
| } | ||
|
|
||
| // matchRegexes applies ByRegex to s. | ||
| // If ByRegex is empty, returns a slice that contains just s. | ||
| // matchRegexes applies regexes to s. | ||
| // If regexes is empty, returns a slice that contains just s. | ||
| // Otherwise, applies each regex to s in sequence: | ||
| // If a regex has capturing groups, the capturing groups will be added to the | ||
| // resulting slice. | ||
| // If a regex does not have capturing groups, all matched text will be added to | ||
| // the resulting slice. | ||
| func (opts blockOptions) matchRegexes(s string) []regexMatch { | ||
| if len(opts.ByRegex) == 0 { | ||
| func (opts blockOptions) matchRegexes(s string, regexes []RegexOption) []regexMatch { | ||
| if len(regexes) == 0 { | ||
| return []regexMatch{{s}} | ||
| } | ||
|
|
||
| var ret []regexMatch | ||
| for _, p := range opts.ByRegex { | ||
| for _, p := range regexes { | ||
| regex := p.Pattern | ||
|
|
||
| if p.Template != nil { | ||
|
|
@@ -421,6 +423,7 @@ func (opts blockOptions) matchRegexes(s string) []regexMatch { | |
| } | ||
|
|
||
| m := regex.FindStringSubmatch(s) | ||
|
|
||
| if m == nil { | ||
| ret = append(ret, regexDidNotMatch) | ||
| continue | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind adding a golden test or two for this new option?