From: Agniva De Sarker Date: Wed, 20 Feb 2019 08:03:30 +0000 (+0530) Subject: go/doc: add // while wrapping a line comment in ToText X-Git-Tag: go1.13beta1~1201 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=e90f572cd68adf11337faf30d1db0231d0c9d870;p=gostls13.git 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 TryBot-Result: Gobot Gobot Reviewed-by: Robert Griesemer --- diff --git a/src/go/doc/comment.go b/src/go/doc/comment.go index 31ee93e44f..88be45bb8f 100644 --- a/src/go/doc/comment.go +++ b/src/go/doc/comment.go @@ -464,6 +464,7 @@ 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 { @@ -471,6 +472,8 @@ func (l *lineWrapper) write(text string) { } 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 @@ -478,10 +481,15 @@ func (l *lineWrapper) write(text string) { 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 diff --git a/src/go/doc/comment_test.go b/src/go/doc/comment_test.go index 0687f3a62b..101f446287 100644 --- a/src/go/doc/comment_test.go +++ b/src/go/doc/comment_test.go @@ -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) {