Skip to content

Commit

Permalink
Reflow long lines which don't end with period
Browse files Browse the repository at this point in the history
	$ 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.
  • Loading branch information
shivansh committed Mar 19, 2022
1 parent e6e2cf0 commit c4a54ae
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
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
34 changes: 25 additions & 9 deletions shortener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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):], " ")
Expand Down Expand Up @@ -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"))
Expand Down

0 comments on commit c4a54ae

Please sign in to comment.