mirror of https://github.com/golang/go.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 <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
5c22842cf2
commit
e90f572cd6
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue