@@ -81,27 +81,105 @@ func (a *Adapter) parseWithScopeTracking(fset *token.FileSet, f *ast.File, fileP
8181
8282 // Now iterate in order
8383 for _ , cg := range f .Comments {
84- for _ , c := range cg .List {
85- symbol := commentSymbols [c ]
86- if symbol == "" {
87- symbol = "package." + f .Name .Name // Default to package level
88- }
89- comments = append (comments , a .createComment (fset , c , filePath , symbol ))
84+ if len (cg .List ) == 0 {
85+ continue
86+ }
87+
88+ // Use the symbol of the first comment in the group
89+ symbol := commentSymbols [cg .List [0 ]]
90+ if symbol == "" {
91+ symbol = "package." + f .Name .Name // Default to package level
9092 }
93+
94+ // Split group into subgroups and create comments
95+ subComments := a .processCommentGroup (fset , cg , filePath , symbol )
96+ comments = append (comments , subComments ... )
9197 }
9298
9399 return comments , nil
94100}
95101
96- func (a * Adapter ) createComment (fset * token.FileSet , c * ast.Comment , file , symbol string ) * domain.Comment {
102+ // processCommentGroup handles the logic of merging or splitting comments within a group.
103+ // It ensures that only consecutive Line comments (//) are merged.
104+ // Block comments (/* */) are kept separate, even if consecutive or adjacent to line comments.
105+ func (a * Adapter ) processCommentGroup (fset * token.FileSet , cg * ast.CommentGroup , file , symbol string ) []* domain.Comment {
106+ var results []* domain.Comment
107+ if len (cg .List ) == 0 {
108+ return results
109+ }
110+
111+ // Buffer for consecutive line comments
112+ var lineBuffer []* ast.Comment
113+
114+ flushLineBuffer := func () {
115+ if len (lineBuffer ) == 0 {
116+ return
117+ }
118+ results = append (results , a .createMergedComment (fset , lineBuffer , file , symbol ))
119+ lineBuffer = nil // Clear buffer
120+ }
121+
122+ for _ , c := range cg .List {
123+ if strings .HasPrefix (c .Text , "//" ) {
124+ // It's a line comment, add to buffer
125+ lineBuffer = append (lineBuffer , c )
126+ } else {
127+ // It's a block comment (/* ... */)
128+ // 1. Flush any existing line comments
129+ flushLineBuffer ()
130+
131+ // 2. Add this block comment individually
132+ results = append (results , a .createSingleComment (fset , c , file , symbol ))
133+ }
134+ }
135+ // Flush remaining line comments at the end
136+ flushLineBuffer ()
137+
138+ return results
139+ }
140+
141+ // createMergedComment creates a single comment from a list of Line comments
142+ func (a * Adapter ) createMergedComment (fset * token.FileSet , comments []* ast.Comment , file , symbol string ) * domain.Comment {
143+ if len (comments ) == 0 {
144+ return nil
145+ }
146+
147+ first := comments [0 ]
148+ last := comments [len (comments )- 1 ]
149+
150+ pos := fset .Position (first .Pos ())
151+ end := fset .Position (last .End ())
152+
153+ var sb strings.Builder
154+ for i , c := range comments {
155+ if i > 0 {
156+ sb .WriteString ("\n " )
157+ }
158+ sb .WriteString (c .Text )
159+ }
160+
161+ return & domain.Comment {
162+ File : file ,
163+ Language : "go" ,
164+ Symbol : symbol ,
165+ Range : domain.TextRange {
166+ StartLine : pos .Line ,
167+ StartCol : pos .Column ,
168+ EndLine : end .Line ,
169+ EndCol : end .Column ,
170+ },
171+ SourceText : sb .String (),
172+ Type : domain .CommentTypeLine ,
173+ }
174+ }
175+
176+ // createSingleComment creates a comment from a single AST comment (usually Block)
177+ func (a * Adapter ) createSingleComment (fset * token.FileSet , c * ast.Comment , file , symbol string ) * domain.Comment {
97178 pos := fset .Position (c .Pos ())
98179 end := fset .Position (c .End ())
99180
100- // Clean the text: remove // or /* */
101- text := c .Text
102- // Determine type
103181 cType := domain .CommentTypeLine
104- if strings .HasPrefix (text , "/*" ) {
182+ if strings .HasPrefix (c . Text , "/*" ) {
105183 cType = domain .CommentTypeBlock
106184 }
107185
@@ -115,7 +193,17 @@ func (a *Adapter) createComment(fset *token.FileSet, c *ast.Comment, file, symbo
115193 EndLine : end .Line ,
116194 EndCol : end .Column ,
117195 },
118- SourceText : text ,
196+ SourceText : c . Text ,
119197 Type : cType ,
120198 }
121199}
200+
201+ // createCommentFromGroup is deprecated
202+ func (a * Adapter ) createCommentFromGroup (fset * token.FileSet , cg * ast.CommentGroup , file , symbol string ) * domain.Comment {
203+ return nil
204+ }
205+
206+ // createComment is deprecated
207+ func (a * Adapter ) createComment (fset * token.FileSet , c * ast.Comment , file , symbol string ) * domain.Comment {
208+ return nil
209+ }
0 commit comments