Skip to content

Commit b9b845e

Browse files
committed
internal/lsp: fix folding range for block comments
Block comments are a single comment that stretches across multiple lines. The folding range code made assumptions that each comment was a single line. This change fixes the folding range logic to check for multiline comments and adjust the folding range to start at the end of the first line. Fixes golang/go#46253 Fixes golang/vscode-go#1511 Change-Id: I902f6cda7547cb1f8b4cd447152c3cf29a691d3b Reviewed-on: https://go-review.googlesource.com/c/tools/+/325071 Trust: Suzy Mueller <[email protected]> Run-TryBot: Suzy Mueller <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent 1225b6f commit b9b845e

File tree

3 files changed

+134
-33
lines changed

3 files changed

+134
-33
lines changed

internal/lsp/source/folding_range.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"go/ast"
1010
"go/token"
1111
"sort"
12+
"strings"
1213

1314
"golang.org/x/tools/internal/lsp/protocol"
1415
)
@@ -151,17 +152,27 @@ func validLineFoldingRange(fset *token.FileSet, open, close, start, end token.Po
151152
}
152153

153154
// commentsFoldingRange returns the folding ranges for all comment blocks in file.
154-
// The folding range starts at the end of the first comment, and ends at the end of the
155+
// The folding range starts at the end of the first line of the comment block, and ends at the end of the
155156
// comment block and has kind protocol.Comment.
156157
func commentsFoldingRange(fset *token.FileSet, m *protocol.ColumnMapper, file *ast.File) (comments []*FoldingRangeInfo) {
157158
for _, commentGrp := range file.Comments {
158-
// Don't fold single comments.
159-
if len(commentGrp.List) <= 1 {
159+
startGrp, endGrp := fset.Position(commentGrp.Pos()), fset.Position(commentGrp.End())
160+
if startGrp.Line == endGrp.Line {
161+
// Don't fold single line comments.
160162
continue
161163
}
164+
165+
firstComment := commentGrp.List[0]
166+
startPos, endLinePos := firstComment.Pos(), firstComment.End()
167+
startCmmnt, endCmmnt := fset.Position(startPos), fset.Position(endLinePos)
168+
if startCmmnt.Line != endCmmnt.Line {
169+
// If the first comment spans multiple lines, then we want to have the
170+
// folding range start at the end of the first line.
171+
endLinePos = token.Pos(int(startPos) + len(strings.Split(firstComment.Text, "\n")[0]))
172+
}
162173
comments = append(comments, &FoldingRangeInfo{
163174
// Fold from the end of the first line comment to the end of the comment block.
164-
MappedRange: NewMappedRange(fset, m, commentGrp.List[0].End(), commentGrp.End()),
175+
MappedRange: NewMappedRange(fset, m, endLinePos, commentGrp.End()),
165176
Kind: protocol.Comment,
166177
})
167178
}

internal/lsp/testdata/folding/a.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import _ "os"
1010
// bar is a function.
1111
// With a multiline doc comment.
1212
func bar() string {
13+
/* This is a single line comment */
1314
switch {
1415
case true:
1516
if true {
@@ -22,6 +23,14 @@ func bar() string {
2223
default:
2324
fmt.Println("default")
2425
}
26+
/* This is a multiline
27+
block
28+
comment */
29+
30+
/* This is a multiline
31+
block
32+
comment */
33+
// Followed by another comment.
2534
_ = []int{
2635
1,
2736
2,

internal/lsp/testdata/folding/a.go.golden

Lines changed: 110 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ import _ "os"
2121
// bar is a function.
2222
// With a multiline doc comment.
2323
func bar() string {
24+
/* This is a single line comment */
2425
switch {<>}
26+
/* This is a multiline<>
27+
28+
/* This is a multiline<>
2529
_ = []int{<>}
2630
_ = [2]string{<>}
2731
_ = map[string]int{<>}
@@ -48,11 +52,20 @@ import _ "os"
4852
// bar is a function.
4953
// With a multiline doc comment.
5054
func bar() string {
55+
/* This is a single line comment */
5156
switch {
5257
case true:<>
5358
case false:<>
5459
default:<>
5560
}
61+
/* This is a multiline
62+
block
63+
comment */
64+
65+
/* This is a multiline
66+
block
67+
comment */
68+
// Followed by another comment.
5669
_ = []int{
5770
1,
5871
2,
@@ -102,6 +115,7 @@ import _ "os"
102115
// bar is a function.
103116
// With a multiline doc comment.
104117
func bar() string {
118+
/* This is a single line comment */
105119
switch {
106120
case true:
107121
if true {<>} else {<>}
@@ -110,6 +124,14 @@ func bar() string {
110124
default:
111125
fmt.Println(<>)
112126
}
127+
/* This is a multiline
128+
block
129+
comment */
130+
131+
/* This is a multiline
132+
block
133+
comment */
134+
// Followed by another comment.
113135
_ = []int{
114136
1,
115137
2,
@@ -162,6 +184,7 @@ import _ "os"
162184
// bar is a function.
163185
// With a multiline doc comment.
164186
func bar() string {
187+
/* This is a single line comment */
165188
switch {
166189
case true:
167190
if true {
@@ -174,6 +197,14 @@ func bar() string {
174197
default:
175198
fmt.Println("default")
176199
}
200+
/* This is a multiline
201+
block
202+
comment */
203+
204+
/* This is a multiline
205+
block
206+
comment */
207+
// Followed by another comment.
177208
_ = []int{
178209
1,
179210
2,
@@ -221,35 +252,37 @@ is not indented`
221252
3:9-6:0
222253
10:22-11:32
223254
12:10-12:9
224-
12:20-66:0
225-
13:10-24:1
226-
14:12-19:3
227-
15:12-17:2
228-
16:16-16:21
229-
17:11-19:2
230-
18:16-18:22
231-
20:13-21:22
232-
21:15-21:21
233-
22:10-23:24
234-
23:15-23:23
235-
25:12-29:1
236-
30:16-32:1
237-
33:21-37:1
238-
38:17-42:1
239-
43:8-47:1
240-
48:15-48:23
241-
48:32-48:40
242-
49:10-60:1
243-
50:18-55:3
244-
51:11-53:2
245-
52:16-52:28
246-
53:11-55:2
247-
54:16-54:29
248-
56:11-57:18
249-
57:15-57:17
250-
58:10-59:24
251-
59:15-59:23
252-
61:32-62:30
255+
12:20-75:0
256+
14:10-25:1
257+
15:12-20:3
258+
16:12-18:2
259+
17:16-17:21
260+
18:11-20:2
261+
19:16-19:22
262+
21:13-22:22
263+
22:15-22:21
264+
23:10-24:24
265+
24:15-24:23
266+
26:24-28:11
267+
30:24-33:32
268+
34:12-38:1
269+
39:16-41:1
270+
42:21-46:1
271+
47:17-51:1
272+
52:8-56:1
273+
57:15-57:23
274+
57:32-57:40
275+
58:10-69:1
276+
59:18-64:3
277+
60:11-62:2
278+
61:16-61:28
279+
62:11-64:2
280+
63:16-63:29
281+
65:11-66:18
282+
66:15-66:17
283+
67:10-68:24
284+
68:15-68:23
285+
70:32-71:30
253286

254287
-- foldingRange-comment-0 --
255288
package folding //@fold("package")
@@ -263,6 +296,7 @@ import _ "os"
263296

264297
// bar is a function.<>
265298
func bar() string {
299+
/* This is a single line comment */
266300
switch {
267301
case true:
268302
if true {
@@ -275,6 +309,9 @@ func bar() string {
275309
default:
276310
fmt.Println("default")
277311
}
312+
/* This is a multiline<>
313+
314+
/* This is a multiline<>
278315
_ = []int{
279316
1,
280317
2,
@@ -327,6 +364,7 @@ import _ "os"
327364
// bar is a function.
328365
// With a multiline doc comment.
329366
func bar() string {
367+
/* This is a single line comment */
330368
switch {
331369
case true:
332370
if true {
@@ -339,6 +377,14 @@ func bar() string {
339377
default:
340378
fmt.Println("default")
341379
}
380+
/* This is a multiline
381+
block
382+
comment */
383+
384+
/* This is a multiline
385+
block
386+
comment */
387+
// Followed by another comment.
342388
_ = []int{
343389
1,
344390
2,
@@ -407,8 +453,12 @@ import _ "os"
407453
// bar is a function.
408454
// With a multiline doc comment.
409455
func bar() string {
456+
/* This is a single line comment */
410457
switch {<>
411458
}
459+
/* This is a multiline<>
460+
461+
/* This is a multiline<>
412462
_ = []int{<>,
413463
}
414464
_ = [2]string{"d",
@@ -442,11 +492,20 @@ import _ "os"
442492
// bar is a function.
443493
// With a multiline doc comment.
444494
func bar() string {
495+
/* This is a single line comment */
445496
switch {
446497
case true:<>
447498
case false:<>
448499
default:<>
449500
}
501+
/* This is a multiline
502+
block
503+
comment */
504+
505+
/* This is a multiline
506+
block
507+
comment */
508+
// Followed by another comment.
450509
_ = []int{
451510
1,
452511
2,
@@ -496,6 +555,7 @@ import _ "os"
496555
// bar is a function.
497556
// With a multiline doc comment.
498557
func bar() string {
558+
/* This is a single line comment */
499559
switch {
500560
case true:
501561
if true {<>
@@ -506,6 +566,14 @@ func bar() string {
506566
default:
507567
fmt.Println("default")
508568
}
569+
/* This is a multiline
570+
block
571+
comment */
572+
573+
/* This is a multiline
574+
block
575+
comment */
576+
// Followed by another comment.
509577
_ = []int{
510578
1,
511579
2,
@@ -559,6 +627,7 @@ import _ "os"
559627

560628
// bar is a function.<>
561629
func bar() string {
630+
/* This is a single line comment */
562631
switch {
563632
case true:
564633
if true {
@@ -571,6 +640,9 @@ func bar() string {
571640
default:
572641
fmt.Println("default")
573642
}
643+
/* This is a multiline<>
644+
645+
/* This is a multiline<>
574646
_ = []int{
575647
1,
576648
2,
@@ -624,6 +696,7 @@ import _ "os"
624696
// bar is a function.
625697
// With a multiline doc comment.
626698
func bar() string {
699+
/* This is a single line comment */
627700
switch {
628701
case true:
629702
if true {
@@ -636,6 +709,14 @@ func bar() string {
636709
default:
637710
fmt.Println("default")
638711
}
712+
/* This is a multiline
713+
block
714+
comment */
715+
716+
/* This is a multiline
717+
block
718+
comment */
719+
// Followed by another comment.
639720
_ = []int{
640721
1,
641722
2,

0 commit comments

Comments
 (0)