Skip to content

Commit

Permalink
go/doc: add // while wrapping a line comment in ToText
Browse files Browse the repository at this point in the history
Currently, lineWrapper does not detect if it is printing a line comment or not.
Hence, while wrapping a comment, the new line does not get prefixed with a //.

We add logic to lineWrapper to detect this case and add // accordingly. Block
comments do not need any such handling.

Added tests for both cases.

Fixes golang#20929

Change-Id: I656037c2d865f31dd853cf9195f43ab7c6e6fc53
Reviewed-on: https://go-review.googlesource.com/c/go/+/163578
Run-TryBot: Robert Griesemer <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Robert Griesemer <[email protected]>
  • Loading branch information
agnivade authored and griesemer committed Mar 5, 2019
1 parent 5c22842 commit e90f572
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/go/doc/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,24 +464,32 @@ type lineWrapper struct {

var nl = []byte("\n")
var space = []byte(" ")
var prefix = []byte("// ")

func (l *lineWrapper) write(text string) {
if l.n == 0 && l.printed {
l.out.Write(nl) // blank line before new paragraph
}
l.printed = true

needsPrefix := false
isComment := strings.HasPrefix(text, "//")
for _, f := range strings.Fields(text) {
w := utf8.RuneCountInString(f)
// wrap if line is too long
if l.n > 0 && l.n+l.pendSpace+w > l.width {
l.out.Write(nl)
l.n = 0
l.pendSpace = 0
needsPrefix = isComment
}
if l.n == 0 {
l.out.Write([]byte(l.indent))
}
if needsPrefix {
l.out.Write(prefix)
needsPrefix = false
}
l.out.Write(space[:l.pendSpace])
l.out.Write([]byte(f))
l.n += l.pendSpace + w
Expand Down
20 changes: 20 additions & 0 deletions src/go/doc/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,26 @@ $ pre2
},
text: ". Para.\n\n$ should not be ``escaped''",
},
{
in: "// A very long line of 46 char for line wrapping.",
out: []block{
{opPara, []string{"// A very long line of 46 char for line wrapping."}},
},
text: `. // A very long line of 46 char for line
. // wrapping.
`,
},
{
in: `/* A very long line of 46 char for line wrapping.
A very long line of 46 char for line wrapping. */`,
out: []block{
{opPara, []string{"/* A very long line of 46 char for line wrapping.\n", "A very long line of 46 char for line wrapping. */"}},
},
text: `. /* A very long line of 46 char for line
. wrapping. A very long line of 46 char
. for line wrapping. */
`,
},
}

func TestBlocks(t *testing.T) {
Expand Down

0 comments on commit e90f572

Please sign in to comment.