Skip to content

Commit b8851ad

Browse files
committed
go/doc: fix ToText
Fixes #6769. LGTM=bradfitz R=bgarcia, rsc, bradfitz CC=golang-codereviews https://golang.org/cl/84220044
1 parent c274ff6 commit b8851ad

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

src/pkg/go/doc/comment.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,9 @@ func ToText(w io.Writer, text string, indent, preIndent string, width int) {
392392
case opPre:
393393
w.Write(nl)
394394
for _, line := range b.lines {
395-
if !isBlank(line) {
395+
if isBlank(line) {
396+
w.Write([]byte("\n"))
397+
} else {
396398
w.Write([]byte(preIndent))
397399
w.Write([]byte(line))
398400
}

src/pkg/go/doc/comment_test.go

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ func TestIsHeading(t *testing.T) {
4242
}
4343

4444
var blocksTests = []struct {
45-
in string
46-
out []block
45+
in string
46+
out []block
47+
text string
4748
}{
4849
{
4950
in: `Para 1.
@@ -59,6 +60,22 @@ Para 3.
5960
pre1
6061
6162
Para 4.
63+
64+
pre
65+
pre1
66+
67+
pre2
68+
69+
Para 5.
70+
71+
72+
pre
73+
74+
75+
pre1
76+
pre2
77+
78+
Para 6.
6279
pre
6380
pre2
6481
`,
@@ -69,8 +86,44 @@ Para 4.
6986
{opPara, []string{"Para 3.\n"}},
7087
{opPre, []string{"pre\n", "pre1\n"}},
7188
{opPara, []string{"Para 4.\n"}},
89+
{opPre, []string{"pre\n", "pre1\n", "\n", "pre2\n"}},
90+
{opPara, []string{"Para 5.\n"}},
91+
{opPre, []string{"pre\n", "\n", "\n", "pre1\n", "pre2\n"}},
92+
{opPara, []string{"Para 6.\n"}},
7293
{opPre, []string{"pre\n", "pre2\n"}},
7394
},
95+
text: `. Para 1. Para 1 line 2.
96+
97+
. Para 2.
98+
99+
100+
. Section
101+
102+
. Para 3.
103+
104+
$ pre
105+
$ pre1
106+
107+
. Para 4.
108+
109+
$ pre
110+
$ pre1
111+
112+
$ pre2
113+
114+
. Para 5.
115+
116+
$ pre
117+
118+
119+
$ pre1
120+
$ pre2
121+
122+
. Para 6.
123+
124+
$ pre
125+
$ pre2
126+
`,
74127
},
75128
}
76129

@@ -83,6 +136,17 @@ func TestBlocks(t *testing.T) {
83136
}
84137
}
85138

139+
func TestToText(t *testing.T) {
140+
var buf bytes.Buffer
141+
for i, tt := range blocksTests {
142+
ToText(&buf, tt.in, ". ", "$\t", 40)
143+
if have := buf.String(); have != tt.text {
144+
t.Errorf("#%d: mismatch\nhave: %s\nwant: %s\nhave vs want:\n%q\n%q", i, have, tt.text, have, tt.text)
145+
}
146+
buf.Reset()
147+
}
148+
}
149+
86150
var emphasizeTests = []struct {
87151
in string
88152
out string

0 commit comments

Comments
 (0)