@@ -13,6 +13,7 @@ import (
13
13
"go/ast"
14
14
"go/token"
15
15
"go/types"
16
+ "slices"
16
17
"sort"
17
18
"strings"
18
19
@@ -104,21 +105,21 @@ func joinLines(fset *token.FileSet, start, end token.Pos, src []byte, file *ast.
104
105
// processLines is the common operation for both split and join lines because this split/join operation is
105
106
// essentially a transformation of the separating whitespace.
106
107
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 )
109
109
110
- // box *ast.CommentGroup to * ast.Node for easier processing later.
110
+ // box *ast.CommentGroup to ast.Node for easier processing later.
111
111
for _ , cg := range comments {
112
112
nodes = append (nodes , cg )
113
113
}
114
114
115
+ // Sort to interleave comments and nodes.
115
116
sort .Slice (nodes , func (i , j int ) bool {
116
117
return nodes [i ].Pos () < nodes [j ].Pos ()
117
118
})
118
119
119
120
edits := []analysis.TextEdit {
120
121
{
121
- Pos : braceOpen + 1 ,
122
+ Pos : token . Pos ( int ( braceOpen ) + len ( "{" )) ,
122
123
End : nodes [0 ].Pos (),
123
124
NewText : []byte (prefix + indent ),
124
125
},
@@ -143,6 +144,8 @@ func processLines(fset *token.FileSet, items []ast.Node, comments []*ast.Comment
143
144
posOffset := safetoken .EndPosition (fset , pos ).Offset
144
145
endOffset := safetoken .StartPosition (fset , end ).Offset
145
146
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.
146
149
continue
147
150
}
148
151
0 commit comments