Skip to content

Commit bb36847

Browse files
committed
Address comment
1 parent dff8ef3 commit bb36847

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

gopls/internal/golang/lines.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"go/ast"
1414
"go/token"
1515
"go/types"
16+
"slices"
1617
"sort"
1718
"strings"
1819

@@ -104,21 +105,21 @@ func joinLines(fset *token.FileSet, start, end token.Pos, src []byte, file *ast.
104105
// processLines is the common operation for both split and join lines because this split/join operation is
105106
// essentially a transformation of the separating whitespace.
106107
func processLines(fset *token.FileSet, items []ast.Node, comments []*ast.CommentGroup, src []byte, braceOpen, braceClose token.Pos, sep, prefix, suffix, indent string) *analysis.SuggestedFix {
107-
var nodes []ast.Node
108-
nodes = append(nodes, items...)
108+
nodes := slices.Clone(items)
109109

110-
// box *ast.CommentGroup to *ast.Node for easier processing later.
110+
// box *ast.CommentGroup to ast.Node for easier processing later.
111111
for _, cg := range comments {
112112
nodes = append(nodes, cg)
113113
}
114114

115+
// Sort to interleave comments and nodes.
115116
sort.Slice(nodes, func(i, j int) bool {
116117
return nodes[i].Pos() < nodes[j].Pos()
117118
})
118119

119120
edits := []analysis.TextEdit{
120121
{
121-
Pos: braceOpen + 1,
122+
Pos: token.Pos(int(braceOpen) + len("{")),
122123
End: nodes[0].Pos(),
123124
NewText: []byte(prefix + indent),
124125
},
@@ -143,6 +144,8 @@ func processLines(fset *token.FileSet, items []ast.Node, comments []*ast.Comment
143144
posOffset := safetoken.EndPosition(fset, pos).Offset
144145
endOffset := safetoken.StartPosition(fset, end).Offset
145146
if bytes.IndexByte(src[posOffset:endOffset], ',') == -1 {
147+
// nodes[i] or nodes[i-1] is a comment hence no delimiter in between
148+
// in such case, do nothing.
146149
continue
147150
}
148151

0 commit comments

Comments
 (0)