Skip to content

Commit

Permalink
gopls/internal/golang: fix folding range for function calls
Browse files Browse the repository at this point in the history
Make folding ranges for function calls consistent with folding
ranges for function bodies and composite literals:

- keep the closing parenthesis visible
- do not generate a folding range if either the first or last
  argument is on the same line as the opening or closing parenthesis

Fixes golang/go#70467
  • Loading branch information
ShoshinNikita committed Nov 20, 2024
1 parent e751756 commit 54fe53c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
7 changes: 6 additions & 1 deletion gopls/internal/golang/folding_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,13 @@ func foldingRangeFunc(pgf *parsego.File, n ast.Node, lineFoldingOnly bool) *Fold
// Fold from position of ":" to end.
start, end = n.Colon+1, n.End()
case *ast.CallExpr:
// Fold from position of "(" to position of ")".
// Fold between positions of or lines between "(" and ")".
start, end = n.Lparen+1, n.Rparen
if len(n.Args) > 0 {
argsStart := n.Args[0].End()
argsEnd := n.Args[len(n.Args)-1].End()
start, end = validLineFoldingRange(pgf.Tok, n.Lparen, n.Rparen, argsStart, argsEnd, lineFoldingOnly)
}
case *ast.FieldList:
// Fold between positions of or lines between opening parenthesis/brace and closing parenthesis/brace.
var startList, endList token.Pos
Expand Down
70 changes: 69 additions & 1 deletion gopls/internal/test/marker/testdata/foldingrange/a_lineonly.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package folding //@foldingrange(raw)
import (
"fmt"
_ "log"
"sort"
)

import _ "os"
Expand Down Expand Up @@ -85,12 +86,46 @@ func bar() string {
this string
is not indented`
}

func _() {
slice := []int{1, 2, 3}
sort.Slice(slice, func(i, j int) bool {
a, b := slice[i], slice[j]
return a < b
})

sort.Slice(slice, func(i, j int) bool { return slice[i] < slice[j] })

sort.Slice(
slice,
func(i, j int) bool {
return slice[i] < slice[j]
},
)

fmt.Println(
1, 2, 3,
4,
)

fmt.Println(1, 2, 3,
4, 5, 6,
7, 8, 9,
10)
}

func _(
a int, b int,
c int,
) {
}
-- @raw --
package folding //@foldingrange(raw)

import (<0 kind="imports">
"fmt"
_ "log"</0>
_ "log"
"sort"</0>
)

import _ "os"
Expand Down Expand Up @@ -161,3 +196,36 @@ func bar() string {<2 kind="">
this string
is not indented`</2></22>
}

func _() {<23 kind="">
slice := []int{1, 2, 3}
sort.Slice(slice, func(i, j int) bool {<24 kind="">
a, b := slice[i], slice[j]
return a < b</24>
})

sort.Slice(slice, func(i, j int) bool { return slice[i] < slice[j] })

sort.Slice(<25 kind="">
slice,
func(i, j int) bool {<26 kind="">
return slice[i] < slice[j]</26>
}</25>,
)

fmt.Println(<27 kind="">
1, 2, 3,
4</27>,
)

fmt.Println(1, 2, 3,
4, 5, 6,
7, 8, 9,
10)</23>
}

func _(<28 kind="">
a int, b int,
c int</28>,
) {
}

0 comments on commit 54fe53c

Please sign in to comment.