|
| 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