Commit e90f572c authored by Agniva De Sarker's avatar Agniva De Sarker Committed by Robert Griesemer

go/doc: add // while wrapping a line comment in ToText

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 #20929

Change-Id: I656037c2d865f31dd853cf9195f43ab7c6e6fc53
Reviewed-on: https://go-review.googlesource.com/c/go/+/163578
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
parent 5c22842c
...@@ -464,6 +464,7 @@ type lineWrapper struct { ...@@ -464,6 +464,7 @@ type lineWrapper struct {
var nl = []byte("\n") var nl = []byte("\n")
var space = []byte(" ") var space = []byte(" ")
var prefix = []byte("// ")
func (l *lineWrapper) write(text string) { func (l *lineWrapper) write(text string) {
if l.n == 0 && l.printed { if l.n == 0 && l.printed {
...@@ -471,6 +472,8 @@ func (l *lineWrapper) write(text string) { ...@@ -471,6 +472,8 @@ func (l *lineWrapper) write(text string) {
} }
l.printed = true l.printed = true
needsPrefix := false
isComment := strings.HasPrefix(text, "//")
for _, f := range strings.Fields(text) { for _, f := range strings.Fields(text) {
w := utf8.RuneCountInString(f) w := utf8.RuneCountInString(f)
// wrap if line is too long // wrap if line is too long
...@@ -478,10 +481,15 @@ func (l *lineWrapper) write(text string) { ...@@ -478,10 +481,15 @@ func (l *lineWrapper) write(text string) {
l.out.Write(nl) l.out.Write(nl)
l.n = 0 l.n = 0
l.pendSpace = 0 l.pendSpace = 0
needsPrefix = isComment
} }
if l.n == 0 { if l.n == 0 {
l.out.Write([]byte(l.indent)) l.out.Write([]byte(l.indent))
} }
if needsPrefix {
l.out.Write(prefix)
needsPrefix = false
}
l.out.Write(space[:l.pendSpace]) l.out.Write(space[:l.pendSpace])
l.out.Write([]byte(f)) l.out.Write([]byte(f))
l.n += l.pendSpace + w l.n += l.pendSpace + w
......
...@@ -134,6 +134,26 @@ $ pre2 ...@@ -134,6 +134,26 @@ $ pre2
}, },
text: ". Para.\n\n$ should not be ``escaped''", 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) { func TestBlocks(t *testing.T) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment