Skip to content

Commit cb15abe

Browse files
authored
chore: split shortener.go
1 parent 5dcab47 commit cb15abe

File tree

4 files changed

+468
-439
lines changed

4 files changed

+468
-439
lines changed

shorten/annotations.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package shorten
2+
3+
import (
4+
"strings"
5+
6+
"github.com/golangci/golines/shorten/internal/annotation"
7+
)
8+
9+
// annotateLongLines adds specially formatted comments to all eligible lines that
10+
// are longer than the configured target length.
11+
// If a line already has one of these comments from a previous shortening round,
12+
// then the comment contents are updated.
13+
func (s *Shortener) annotateLongLines(lines []string) ([]string, int) {
14+
var annotatedLines []string
15+
16+
linesToShorten := 0
17+
prevLen := -1
18+
19+
for _, line := range lines {
20+
length := s.lineLen(line)
21+
22+
if prevLen > -1 {
23+
if length <= s.config.MaxLen {
24+
// Shortening successful, remove previous annotation
25+
annotatedLines = annotatedLines[:len(annotatedLines)-1]
26+
} else if length < prevLen {
27+
// Replace annotation with a new length
28+
annotatedLines[len(annotatedLines)-1] = annotation.Create(length)
29+
linesToShorten++
30+
}
31+
} else if !isComment(line) && length > s.config.MaxLen {
32+
annotatedLines = append(
33+
annotatedLines,
34+
annotation.Create(length),
35+
)
36+
linesToShorten++
37+
}
38+
39+
annotatedLines = append(annotatedLines, line)
40+
prevLen = annotation.Parse(line)
41+
}
42+
43+
return annotatedLines, linesToShorten
44+
}
45+
46+
// removeAnnotations removes all comments added by the annotateLongLines function above.
47+
func removeAnnotations(content []byte) []byte {
48+
var cleanedLines []string
49+
50+
lines := strings.SplitSeq(string(content), "\n")
51+
52+
for line := range lines {
53+
if !annotation.Is(line) {
54+
cleanedLines = append(cleanedLines, line)
55+
}
56+
}
57+
58+
return []byte(strings.Join(cleanedLines, "\n"))
59+
}

shorten/comments.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package shorten
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"strings"
7+
8+
"github.com/golangci/golines/shorten/internal/annotation"
9+
)
10+
11+
// Go directive (should be ignored).
12+
// https://go.dev/doc/comment#syntax
13+
var directivePattern = regexp.MustCompile(`\s*//(line |extern |export |[a-z0-9]+:[a-z0-9])`)
14+
15+
// shortenCommentsFunc attempts to shorten long comments in the provided source.
16+
//
17+
// As noted in the repo README,
18+
// this functionality has some quirks and is disabled by default.
19+
func (s *Shortener) shortenCommentsFunc(content []byte) []byte {
20+
var cleanedLines []string
21+
22+
var words []string // all words in a contiguous sequence of long comments
23+
24+
prefix := ""
25+
26+
lines := strings.SplitSeq(string(content), "\n")
27+
for line := range lines {
28+
if isComment(line) && !annotation.Is(line) &&
29+
!isDirective(line) &&
30+
s.lineLen(line) > s.config.MaxLen {
31+
start := strings.Index(line, "//")
32+
prefix = line[0:(start + 2)]
33+
trimmedLine := strings.Trim(line[(start+2):], " ")
34+
currLineWords := strings.Split(trimmedLine, " ")
35+
words = append(words, currLineWords...)
36+
} else {
37+
// Reflow the accumulated `words` before appending the unprocessed `line`.
38+
currLineLen := 0
39+
40+
var currLineWords []string
41+
42+
maxCommentLen := s.config.MaxLen - s.lineLen(prefix)
43+
for _, word := range words {
44+
if currLineLen > 0 && currLineLen+1+len(word) > maxCommentLen {
45+
cleanedLines = append(
46+
cleanedLines,
47+
fmt.Sprintf(
48+
"%s %s",
49+
prefix,
50+
strings.Join(currLineWords, " "),
51+
),
52+
)
53+
currLineWords = []string{}
54+
currLineLen = 0
55+
}
56+
57+
currLineWords = append(currLineWords, word)
58+
currLineLen += 1 + len(word)
59+
}
60+
61+
if currLineLen > 0 {
62+
cleanedLines = append(
63+
cleanedLines,
64+
fmt.Sprintf(
65+
"%s %s",
66+
prefix,
67+
strings.Join(currLineWords, " "),
68+
),
69+
)
70+
}
71+
72+
words = []string{}
73+
74+
cleanedLines = append(cleanedLines, line)
75+
}
76+
}
77+
78+
return []byte(strings.Join(cleanedLines, "\n"))
79+
}
80+
81+
// isDirective determines whether the provided line is a directive, e.g., for `go:generate`.
82+
func isDirective(line string) bool {
83+
return directivePattern.MatchString(line)
84+
}
85+
86+
// isComment determines whether the provided line is a non-block comment.
87+
func isComment(line string) bool {
88+
return strings.HasPrefix(strings.Trim(line, " \t"), "//")
89+
}

0 commit comments

Comments
 (0)