Skip to content

Commit

Permalink
fix: Make prefix_order case insensitive when case=no. (#60)
Browse files Browse the repository at this point in the history
See the history of this PR to see how prefixes.out has changed.
  • Loading branch information
JeffFaer authored Jan 7, 2025
1 parent d1e137d commit 1655d56
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 91 deletions.
80 changes: 45 additions & 35 deletions goldens/prefixes.in
Original file line number Diff line number Diff line change
@@ -1,42 +1,52 @@
preferred prefixes putting 'build_target' before 'branch_name':
build_specs {
# keep-sorted-test start prefix_order=build_target,branch_name
build_target: "//foo/baz"
branch_name: "dev"
build_target: "//foo/bar"
branch_name: "prod"
# keep-sorted-test end
}
build_specs {
# keep-sorted-test start prefix_order=build_target,branch_name
build_target: "//foo/baz"
branch_name: "dev"
build_target: "//foo/bar"
branch_name: "prod"
# keep-sorted-test end
}

Put the specified prefixes at the end:
items = [
# keep-sorted-test start prefix_order=,FOO,BAR
FOO,
DING,
BAR
BATS,
# keep-sorted-test end
]
items = [
# keep-sorted-test start prefix_order=,FOO,BAR
FOO,
DING,
BAR
BATS,
# keep-sorted-test end
]

Case insensitive put the specified prefixes at the end:
items = [
# keep-sorted-test start case=no prefix_order=,foo,bar
FOO,
DING,
BAR
BATS,
# keep-sorted-test end
]

Put a group of items in the beginning, another group at the end:
// keep-sorted-test start prefix_order=INIT_,,FINAL_
INIT_FOO,
FINAL_FOO,
INIT_BAR,
FINAL_BAR,
DO_STUFF,
DO_MORE_STUFF,
ZAP_THINGS
// keep-sorted-test end
// keep-sorted-test start prefix_order=INIT_,,FINAL_
INIT_FOO,
FINAL_FOO,
INIT_BAR,
FINAL_BAR,
DO_STUFF,
DO_MORE_STUFF,
ZAP_THINGS
// keep-sorted-test end

preferred prefixes with comments:
build_specs {
# keep-sorted-test start prefix_order=build_target,branch_name
build_target: "//foo/baz"
# required for development.
branch_name: "dev"
# raise the bar!
build_target: "//foo/bar"
branch_name: "prod"
# keep-sorted-test end
}
build_specs {
# keep-sorted-test start prefix_order=build_target,branch_name
build_target: "//foo/baz"
# required for development.
branch_name: "dev"
# raise the bar!
build_target: "//foo/bar"
branch_name: "prod"
# keep-sorted-test end
}
80 changes: 45 additions & 35 deletions goldens/prefixes.out
Original file line number Diff line number Diff line change
@@ -1,42 +1,52 @@
preferred prefixes putting 'build_target' before 'branch_name':
build_specs {
# keep-sorted-test start prefix_order=build_target,branch_name
build_target: "//foo/bar"
build_target: "//foo/baz"
branch_name: "dev"
branch_name: "prod"
# keep-sorted-test end
}
build_specs {
# keep-sorted-test start prefix_order=build_target,branch_name
build_target: "//foo/bar"
build_target: "//foo/baz"
branch_name: "dev"
branch_name: "prod"
# keep-sorted-test end
}

Put the specified prefixes at the end:
items = [
# keep-sorted-test start prefix_order=,FOO,BAR
BATS,
DING,
FOO,
BAR
# keep-sorted-test end
]
items = [
# keep-sorted-test start prefix_order=,FOO,BAR
BATS,
DING,
FOO,
BAR
# keep-sorted-test end
]

Case insensitive put the specified prefixes at the end:
items = [
# keep-sorted-test start case=no prefix_order=,foo,bar
BATS,
DING,
FOO,
BAR
# keep-sorted-test end
]

Put a group of items in the beginning, another group at the end:
// keep-sorted-test start prefix_order=INIT_,,FINAL_
INIT_BAR,
INIT_FOO,
DO_MORE_STUFF,
DO_STUFF,
ZAP_THINGS,
FINAL_BAR,
FINAL_FOO
// keep-sorted-test end
// keep-sorted-test start prefix_order=INIT_,,FINAL_
INIT_BAR,
INIT_FOO,
DO_MORE_STUFF,
DO_STUFF,
ZAP_THINGS,
FINAL_BAR,
FINAL_FOO
// keep-sorted-test end

preferred prefixes with comments:
build_specs {
# keep-sorted-test start prefix_order=build_target,branch_name
# raise the bar!
build_target: "//foo/bar"
build_target: "//foo/baz"
# required for development.
branch_name: "dev"
branch_name: "prod"
# keep-sorted-test end
}
build_specs {
# keep-sorted-test start prefix_order=build_target,branch_name
# raise the bar!
build_target: "//foo/bar"
build_target: "//foo/baz"
# required for development.
branch_name: "dev"
branch_name: "prod"
# keep-sorted-test end
}
20 changes: 8 additions & 12 deletions keepsorted/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,24 +382,20 @@ func (b block) lessFn() cmpFunc[lineGroup] {
//
// An empty prefix can be used to move "non-matching" entries to a position
// between other prefixes.
type prefixWeight struct {
prefix string
weight int
}
var prefixWeights []prefixWeight
prefixWeights := make(map[string]int)
for i, p := range b.metadata.opts.PrefixOrder {
prefixWeights = append(prefixWeights, prefixWeight{p, i - len(b.metadata.opts.PrefixOrder)})
prefixWeights[p] = i - len(b.metadata.opts.PrefixOrder)
}
// Sort prefixes longest -> shortest to find the most appropriate weight.
slices.SortStableFunc(prefixWeights, comparing(func(pw prefixWeight) int { return len(pw.prefix) }).reversed())
longestFirst := comparing(func(s string) int { return len(s) }).reversed()
prefixes := slices.SortedStableFunc(slices.Values(b.metadata.opts.PrefixOrder), longestFirst)

prefixOrder := comparing(func(lg lineGroup) int {
for _, w := range prefixWeights {
if lg.hasPrefix(w.prefix) {
return w.weight
}
p, ok := b.metadata.opts.hasPrefix(lg.joinedLines(), slices.Values(prefixes))
if !ok {
return 0
}
return 0
return prefixWeights[p]
})

// Combinations of switches (for example, case-insensitive and numeric
Expand Down
4 changes: 0 additions & 4 deletions keepsorted/line_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,6 @@ func (lg lineGroup) append(s string) {
lg.lines[len(lg.lines)-1] = lg.lines[len(lg.lines)-1] + s
}

func (lg lineGroup) hasPrefix(s string) bool {
return strings.HasPrefix(lg.joinedLines(), s)
}

func (lg lineGroup) hasSuffix(s string) bool {
return len(lg.lines) > 0 && strings.HasSuffix(lg.lines[len(lg.lines)-1], s)
}
Expand Down
8 changes: 3 additions & 5 deletions keepsorted/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package keepsorted

import (
"cmp"
"errors"
"fmt"
"iter"
Expand Down Expand Up @@ -171,10 +170,9 @@ func parseBlockOptions(commentMarker, options string, defaults blockOptions) (_
if cm := guessCommentMarker(commentMarker); cm != "" {
ret.setCommentMarker(cm)
}
if len(ret.IgnorePrefixes) > 1 {
// Look at longer prefixes first, in case one of these prefixes is a prefix of another.
slices.SortFunc(ret.IgnorePrefixes, func(a string, b string) int { return cmp.Compare(len(b), len(a)) })
}
// Look at longer prefixes first, in case one of these prefixes is a prefix of another.
longestFirst := comparing(func(s string) int { return len(s) }).reversed()
slices.SortFunc(ret.IgnorePrefixes, longestFirst)

if warn := validate(&ret); len(warn) > 0 {
warns = append(warns, warn...)
Expand Down

0 comments on commit 1655d56

Please sign in to comment.