]> Cypherpunks repositories - gostls13.git/commitdiff
go/doc: add // while wrapping a line comment in ToText
authorAgniva De Sarker <agnivade@yahoo.co.in>
Wed, 20 Feb 2019 08:03:30 +0000 (13:33 +0530)
committerRobert Griesemer <gri@golang.org>
Tue, 5 Mar 2019 23:31:27 +0000 (23:31 +0000)
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: Robert Griesemer <gri@golang.org>
src/go/doc/comment.go
src/go/doc/comment_test.go

index 31ee93e44f819ee883d3350855bde3db39d16d9f..88be45bb8fbdc8ef23bd435d181bfb2c21093572 100644 (file)
@@ -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
index 0687f3a62b344222b1aac075b22fc561f2598d99..101f4462878b9d05a8be1dd80e8d861659283c67 100644 (file)
@@ -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) {