Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions _fixtures/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ package fixtures
import "fmt"

// Short prefix
// This is a really, really long comment on a single line. We should try to break it up if possible because it's longer than 100 chars. In fact, it's so long that it should probably be on three lines instead of two. Wow, so long!!
// This is a really, really long comment on a single line. We should try to break it up if possible because it's longer than 100 chars. In fact, it's so long that it should probably be on three lines instead of two. Wow, so long.
// Short suffix
//

// This comment contains multiple contiguous lines which are greater than the target maximum line length.
// The expected result is a sequence of shortened (reflown) lines without preserving the position of line breaks.

// Unlike the above comment block, only the current line in this comment block has length greater than the
// target maximum line length
// but since the previous line doesn't end with a period, it should be reflown with this line
// despite both the lines being shorter than the target maximum line length.

// Another comment

/*
Expand All @@ -28,7 +33,7 @@ func testFunc() {

// These are comments like the ones in https://github.com/segmentio/golines/issues/9
//
// Documentation: https://swagger.io/docs/specification/authentication/bearer-authentication/more/more/more/more/more
// Documentation: https://swagger.io/docs/specification/authentication/bearer-authentication/more/more/more/more/more.
//
// More documentation:
// https://swagger.io/docs/specification/authentication/bearer-authentication/more/more/more/more/more
Expand Down
9 changes: 7 additions & 2 deletions _fixtures/comments__exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import "fmt"
// Short prefix
// This is a really, really long comment on a single line. We should try to break it up if possible
// because it's longer than 100 chars. In fact, it's so long that it should probably be on three
// lines instead of two. Wow, so long!!
// lines instead of two. Wow, so long.
// Short suffix
//

// This comment contains multiple contiguous lines which are greater than the target maximum line
// length. The expected result is a sequence of shortened (reflown) lines without preserving the
// position of line breaks.

// Unlike the above comment block, only the current line in this comment block has length greater
// than the target maximum line length but since the previous line doesn't end with a period, it
// should be reflown with this line despite both the lines being shorter than the target maximum
// line length.

// Another comment

/*
Expand All @@ -33,7 +38,7 @@ func testFunc() {
// These are comments like the ones in https://github.com/segmentio/golines/issues/9
//
// Documentation:
// https://swagger.io/docs/specification/authentication/bearer-authentication/more/more/more/more/more
// https://swagger.io/docs/specification/authentication/bearer-authentication/more/more/more/more/more.
//
// More documentation:
// https://swagger.io/docs/specification/authentication/bearer-authentication/more/more/more/more/more
Expand Down
50 changes: 35 additions & 15 deletions shortener.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,25 @@ func (s *Shortener) removeAnnotations(contents []byte) []byte {
func (s *Shortener) shortenCommentsFunc(contents []byte) []byte {
cleanedLines := []string{}
words := []string{} // all words in a contiguous sequence of long comments
prevLineLen := 0 // length of reflown words from previous long comment
prefix := ""
lines := strings.Split(string(contents), "\n")

shorten := func(line string) bool {
return s.isComment(line) && !IsAnnotation(line) && !s.isGoDirective(line)
}

getCurrLineWords := func(line string) ([]string, int) {
start := strings.Index(line, "//")
prefix = line[0:(start + 2)]
trimmedLine := strings.Trim(line[(start+2):], " ")
currLineWords := strings.Split(trimmedLine, " ")
return currLineWords, len(trimmedLine)
}

for _, line := range lines {
if s.isComment(line) && !IsAnnotation(line) &&
!s.isGoDirective(line) &&
s.lineLen(line) > s.config.MaxLen {
start := strings.Index(line, "//")
prefix = line[0:(start + 2)]
trimmedLine := strings.Trim(line[(start+2):], " ")
currLineWords := strings.Split(trimmedLine, " ")
if shorten(line) && prevLineLen+s.lineLen(line) > s.config.MaxLen {
currLineWords, _ := getCurrLineWords(line)
words = append(words, currLineWords...)
} else {
// Reflow the accumulated `words` before appending the unprocessed `line`.
Expand All @@ -309,18 +318,29 @@ func (s *Shortener) shortenCommentsFunc(contents []byte) []byte {
currLineLen += 1 + len(word)
}
if currLineLen > 0 {
cleanedLines = append(
cleanedLines,
fmt.Sprintf(
"%s %s",
prefix,
strings.Join(currLineWords, " "),
),
)
lastWord := currLineWords[len(currLineWords)-1]
if shorten(line) && !strings.HasSuffix(lastWord, ".") {
// The previous long line didn't end with a period, and the current
// line is a comment. Hence they are reflown.
w, l := getCurrLineWords(line)
prevLineLen = currLineLen + l
words = append(currLineWords, w...)
continue // skip reprocessing `line` later
} else {
cleanedLines = append(
cleanedLines,
fmt.Sprintf(
"%s %s",
prefix,
strings.Join(currLineWords, " "),
),
)
}
}
words = []string{}

cleanedLines = append(cleanedLines, line)
prevLineLen = 0
}
}
return []byte(strings.Join(cleanedLines, "\n"))
Expand Down