Skip to content

Commit 170b94a

Browse files
committed
chore: simplify and explain end of the rounds
1 parent a490368 commit 170b94a

File tree

4 files changed

+25
-23
lines changed

4 files changed

+25
-23
lines changed

shorten/annotations.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import (
1313
// If a line already has one of these comments from a previous shortening round,
1414
// then the comment contents are updated.
1515
func (s *Shortener) annotateLongLines(lines []string) ([]string, int) {
16-
var annotatedLines []string
16+
var (
17+
annotatedLines []string
18+
nbLinesToShorten int
19+
)
1720

18-
linesToShorten := 0
1921
prevLen := -1
2022

2123
for _, line := range lines {
@@ -28,21 +30,23 @@ func (s *Shortener) annotateLongLines(lines []string) ([]string, int) {
2830
} else if length < prevLen {
2931
// Replace annotation with a new length
3032
annotatedLines[len(annotatedLines)-1] = annotation.Create(length)
31-
linesToShorten++
33+
34+
nbLinesToShorten++
3235
}
3336
} else if !comments.Is(line) && length > s.config.MaxLen {
3437
annotatedLines = append(
3538
annotatedLines,
3639
annotation.Create(length),
3740
)
38-
linesToShorten++
41+
42+
nbLinesToShorten++
3943
}
4044

4145
annotatedLines = append(annotatedLines, line)
4246
prevLen = annotation.Parse(line)
4347
}
4448

45-
return annotatedLines, linesToShorten
49+
return annotatedLines, nbLinesToShorten
4650
}
4751

4852
// removeAnnotations removes all comments added by the annotateLongLines function above.

shorten/internal/tags/tags.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414

1515
var structTagRegexp = regexp.MustCompile("`([ ]*[a-zA-Z0-9_-]+:\".*\"[ ]*){2,}`")
1616

17-
// HasMultipleTags returns whether the given lines have a multikey struct line.
17+
// HasMultipleEntries returns whether the given lines have a multi-entries struct line.
1818
// It's used as an optimization step to avoid unnecessary shortening rounds.
19-
func HasMultipleTags(lines []string) bool {
19+
func HasMultipleEntries(lines []string) bool {
2020
return slices.ContainsFunc(lines, structTagRegexp.MatchString)
2121
}
2222

shorten/internal/tags/tags_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/stretchr/testify/assert"
88
)
99

10-
func TestHasMultipleTags(t *testing.T) {
10+
func TestHasMultipleEntries(t *testing.T) {
1111
testCases := []struct {
1212
desc string
1313
lines []string
@@ -55,7 +55,7 @@ func TestHasMultipleTags(t *testing.T) {
5555
t.Run(test.desc, func(t *testing.T) {
5656
t.Parallel()
5757

58-
test.assert(t, HasMultipleTags(test.lines))
58+
test.assert(t, HasMultipleEntries(test.lines))
5959
})
6060
}
6161
}

shorten/shortener.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,9 @@ func (s *Shortener) Process(content []byte) ([]byte, error) {
120120

121121
// Annotate all long lines
122122
lines := strings.Split(string(content), "\n")
123-
annotatedLines, linesToShorten := s.annotateLongLines(lines)
123+
annotatedLines, nbLinesToShorten := s.annotateLongLines(lines)
124124

125-
var stop bool
126-
127-
if linesToShorten == 0 {
128-
if round == 0 {
129-
if !s.config.ReformatTags || !tags.HasMultipleTags(lines) {
130-
stop = true
131-
}
132-
} else {
133-
stop = true
134-
}
135-
}
136-
137-
if stop {
125+
if !s.shouldContinue(nbLinesToShorten, round, lines) {
138126
s.logger.Debug("nothing more to shorten or reformat, stopping")
139127

140128
break
@@ -194,6 +182,16 @@ func (s *Shortener) Process(content []byte) ([]byte, error) {
194182
return content, nil
195183
}
196184

185+
// shouldContinue returns true:
186+
// if there are lines to shorten,
187+
// or if this is the first round (0),
188+
// and the option to reformat struct tags is enabled,
189+
// and there are struct tags with multiple entries.
190+
func (s *Shortener) shouldContinue(nbLinesToShorten, round int, lines []string) bool {
191+
return nbLinesToShorten > 0 ||
192+
round == 0 && s.config.ReformatTags && tags.HasMultipleEntries(lines)
193+
}
194+
197195
func (s *Shortener) createDot(result dst.Node) error {
198196
dotFile, err := os.Create(s.config.DotFile)
199197
if err != nil {

0 commit comments

Comments
 (0)