From c4a54aefd98b506bf3a70c21768a1f378917335c Mon Sep 17 00:00:00 2001 From: Shivansh Rai Date: Sat, 19 Mar 2022 13:22:51 +0530 Subject: [PATCH] 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. --- _fixtures/comments.go | 9 +++++++-- _fixtures/comments__exp.go | 9 +++++++-- shortener.go | 34 +++++++++++++++++++++++++--------- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/_fixtures/comments.go b/_fixtures/comments.go index 041fa18..875cadd 100644 --- a/_fixtures/comments.go +++ b/_fixtures/comments.go @@ -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 /* @@ -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 diff --git a/_fixtures/comments__exp.go b/_fixtures/comments__exp.go index 1e00983..b4f87cc 100644 --- a/_fixtures/comments__exp.go +++ b/_fixtures/comments__exp.go @@ -5,7 +5,7 @@ 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 // @@ -13,6 +13,11 @@ import "fmt" // 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 /* @@ -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 diff --git a/shortener.go b/shortener.go index 144d78f..b136d76 100644 --- a/shortener.go +++ b/shortener.go @@ -276,12 +276,13 @@ 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 line prefix := "" lines := strings.Split(string(contents), "\n") for _, line := range lines { if s.isComment(line) && !IsAnnotation(line) && !s.isGoDirective(line) && - s.lineLen(line) > s.config.MaxLen { + prevLineLen+s.lineLen(line) > s.config.MaxLen { start := strings.Index(line, "//") prefix = line[0:(start + 2)] trimmedLine := strings.Trim(line[(start+2):], " ") @@ -309,18 +310,33 @@ 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 s.isComment(line) && !IsAnnotation(line) && + !s.isGoDirective(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. + start := strings.Index(line, "//") + prefix = line[0:(start + 2)] + trimmedLine := strings.Trim(line[(start+2):], " ") + prevLineLen = currLineLen + len(trimmedLine) + words = append(currLineWords, strings.Split(trimmedLine, " ")...) + 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"))