Skip to content

Commit

Permalink
fix: Sort nested keep-sorted blocks a bit more naturally. (#47)
Browse files Browse the repository at this point in the history
Right now, we're including the keep-sorted directive as a line to sort against. It should instead be a comment if possible.

See git history of this PR to see how group.out has changed.
  • Loading branch information
JeffFaer authored Oct 25, 2024
1 parent 3436ef5 commit 3180202
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
18 changes: 18 additions & 0 deletions goldens/group.in
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,21 @@ Sorting a switch statement with consecutive cases using nested keep-sorted block
// keep-sorted-test end
return 10
// keep-sorted-test end

Sorting a switch statement with consecutive cases using nested keep-sorted block
where the nested keep-sorted block shouldn't be the first element.
switch (foo) {
// keep-sorted-test start
case 5:
return 5;
// keep-sorted-test start
case 2:
case 6:
case 4:
// keep-sorted-test end
return 10;
case 3:
return 3;
case 1:
return 1;
// keep-sorted-test end
18 changes: 18 additions & 0 deletions goldens/group.out
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,21 @@ Sorting a switch statement with consecutive cases using nested keep-sorted block
case 6:
return 6;
// keep-sorted-test end

Sorting a switch statement with consecutive cases using nested keep-sorted block
where the nested keep-sorted block shouldn't be the first element.
switch (foo) {
// keep-sorted-test start
case 1:
return 1;
// keep-sorted-test start
case 2:
case 4:
case 6:
// keep-sorted-test end
return 10;
case 3:
return 3;
case 5:
return 5;
// keep-sorted-test end
4 changes: 2 additions & 2 deletions keepsorted/keep_sorted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1335,17 +1335,17 @@ func TestLineGrouping(t *testing.T) {
want: []lineGroup{
{[]string{
"// def",
}, []string{
"// keep-sorted-test start",
}, []string{
"3",
"1",
"2",
"// keep-sorted-test end",
}},
{[]string{
"// abc",
}, []string{
"// keep-sorted-test start",
}, []string{
"b",
"c",
"a",
Expand Down
21 changes: 15 additions & 6 deletions keepsorted/line_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,21 @@ func groupLines(lines []string, metadata blockMetadata) []lineGroup {
indents = calculateIndents(lines)
}

countStartDirectives := func(l string) {
if strings.Contains(l, metadata.startDirective) {
numUnmatchedStartDirectives++
} else if strings.Contains(l, metadata.endDirective) {
numUnmatchedStartDirectives--
}
}

// append a line to both lineRange, and block, if necessary.
appendLine := func(i int, l string) {
lineRange.append(i)
if metadata.opts.Block {
block.append(l, metadata.opts)
} else if metadata.opts.Group {
if strings.Contains(l, metadata.startDirective) {
numUnmatchedStartDirectives++
} else if strings.Contains(l, metadata.endDirective) {
numUnmatchedStartDirectives--
}
countStartDirectives(l)
}

if metadata.opts.Group && initialIndent == nil {
Expand Down Expand Up @@ -100,7 +104,12 @@ func groupLines(lines []string, metadata blockMetadata) []lineGroup {
if !metadata.opts.Block && metadata.opts.Group && strings.Contains(l, metadata.startDirective) {
// We don't need to check for end directives here because this makes
// numUnmatchedStartDirectives > 0, so we'll take the code path above through appendLine.
appendLine(i, l)
if lineRange.empty() {
commentRange.append(i)
countStartDirectives(l)
} else {
appendLine(i, l)
}
} else {
commentRange.append(i)
}
Expand Down

0 comments on commit 3180202

Please sign in to comment.