Skip to content

Commit c4a54ae

Browse files
committed
Reflow long lines which don't end with period
$ cat x.go package main // 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. $ golines --shorten-comments x.go package main // 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. The modified behavior is to reflow long lines which don't end with period with the consecutive lines, irrespective of the latter being long or not. $ golines --shorten-comments x.go package main // 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.
1 parent e6e2cf0 commit c4a54ae

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

_fixtures/comments.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ package fixtures
33
import "fmt"
44

55
// Short prefix
6-
// 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!!
6+
// 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.
77
// Short suffix
88
//
99

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

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

1520
/*
@@ -28,7 +33,7 @@ func testFunc() {
2833

2934
// These are comments like the ones in https://github.com/segmentio/golines/issues/9
3035
//
31-
// Documentation: https://swagger.io/docs/specification/authentication/bearer-authentication/more/more/more/more/more
36+
// Documentation: https://swagger.io/docs/specification/authentication/bearer-authentication/more/more/more/more/more.
3237
//
3338
// More documentation:
3439
// https://swagger.io/docs/specification/authentication/bearer-authentication/more/more/more/more/more

_fixtures/comments__exp.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@ import "fmt"
55
// Short prefix
66
// This is a really, really long comment on a single line. We should try to break it up if possible
77
// because it's longer than 100 chars. In fact, it's so long that it should probably be on three
8-
// lines instead of two. Wow, so long!!
8+
// lines instead of two. Wow, so long.
99
// Short suffix
1010
//
1111

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

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

1823
/*
@@ -33,7 +38,7 @@ func testFunc() {
3338
// These are comments like the ones in https://github.com/segmentio/golines/issues/9
3439
//
3540
// Documentation:
36-
// https://swagger.io/docs/specification/authentication/bearer-authentication/more/more/more/more/more
41+
// https://swagger.io/docs/specification/authentication/bearer-authentication/more/more/more/more/more.
3742
//
3843
// More documentation:
3944
// https://swagger.io/docs/specification/authentication/bearer-authentication/more/more/more/more/more

shortener.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,13 @@ func (s *Shortener) removeAnnotations(contents []byte) []byte {
276276
func (s *Shortener) shortenCommentsFunc(contents []byte) []byte {
277277
cleanedLines := []string{}
278278
words := []string{} // all words in a contiguous sequence of long comments
279+
prevLineLen := 0 // length of reflown words from previous long line
279280
prefix := ""
280281
lines := strings.Split(string(contents), "\n")
281282
for _, line := range lines {
282283
if s.isComment(line) && !IsAnnotation(line) &&
283284
!s.isGoDirective(line) &&
284-
s.lineLen(line) > s.config.MaxLen {
285+
prevLineLen+s.lineLen(line) > s.config.MaxLen {
285286
start := strings.Index(line, "//")
286287
prefix = line[0:(start + 2)]
287288
trimmedLine := strings.Trim(line[(start+2):], " ")
@@ -309,18 +310,33 @@ func (s *Shortener) shortenCommentsFunc(contents []byte) []byte {
309310
currLineLen += 1 + len(word)
310311
}
311312
if currLineLen > 0 {
312-
cleanedLines = append(
313-
cleanedLines,
314-
fmt.Sprintf(
315-
"%s %s",
316-
prefix,
317-
strings.Join(currLineWords, " "),
318-
),
319-
)
313+
lastWord := currLineWords[len(currLineWords)-1]
314+
if s.isComment(line) && !IsAnnotation(line) &&
315+
!s.isGoDirective(line) &&
316+
!strings.HasSuffix(lastWord, ".") {
317+
// The previous long line didn't end with a period, and the current
318+
// line is a comment. Hence they are reflown.
319+
start := strings.Index(line, "//")
320+
prefix = line[0:(start + 2)]
321+
trimmedLine := strings.Trim(line[(start+2):], " ")
322+
prevLineLen = currLineLen + len(trimmedLine)
323+
words = append(currLineWords, strings.Split(trimmedLine, " ")...)
324+
continue // skip reprocessing `line` later
325+
} else {
326+
cleanedLines = append(
327+
cleanedLines,
328+
fmt.Sprintf(
329+
"%s %s",
330+
prefix,
331+
strings.Join(currLineWords, " "),
332+
),
333+
)
334+
}
320335
}
321336
words = []string{}
322337

323338
cleanedLines = append(cleanedLines, line)
339+
prevLineLen = 0
324340
}
325341
}
326342
return []byte(strings.Join(cleanedLines, "\n"))

0 commit comments

Comments
 (0)