Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 830f83f

Browse files
authored
Merge pull request #257 from smowton/smowton/fix/go-mod-comment-group-indices
Extractor: assign unique indices to comment-groups in go.mod files
2 parents 32510eb + 3ab948f commit 830f83f

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

extractor/gomodextractor.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,27 @@ func extractGoMod(path string) error {
5151
return nil
5252
}
5353

54+
type commentGroupIdxAllocator struct {
55+
nextIdx int
56+
}
57+
58+
func (cgIdxAlloc *commentGroupIdxAllocator) nextCgIdx() int {
59+
ret := cgIdxAlloc.nextIdx
60+
cgIdxAlloc.nextIdx++
61+
return ret
62+
}
63+
5464
func extractGoModFile(tw *trap.Writer, file *modfile.FileSyntax) {
65+
cgIdxAlloc := commentGroupIdxAllocator{0}
66+
5567
for idx, stmt := range file.Stmt {
56-
extractGoModExpr(tw, stmt, tw.Labeler.FileLabel(), idx)
68+
extractGoModExpr(tw, stmt, tw.Labeler.FileLabel(), idx, &cgIdxAlloc)
5769
}
5870

59-
extractGoModComments(tw, file, tw.Labeler.FileLabel())
71+
extractGoModComments(tw, file, tw.Labeler.FileLabel(), &cgIdxAlloc)
6072
}
6173

62-
func extractGoModExpr(tw *trap.Writer, expr modfile.Expr, parent trap.Label, idx int) {
74+
func extractGoModExpr(tw *trap.Writer, expr modfile.Expr, parent trap.Label, idx int, cgIdxAlloc *commentGroupIdxAllocator) {
6375
lbl := tw.Labeler.LocalID(expr)
6476

6577
var kind int
@@ -80,18 +92,18 @@ func extractGoModExpr(tw *trap.Writer, expr modfile.Expr, parent trap.Label, idx
8092
for idx, tok := range expr.Token {
8193
dbscheme.ModTokensTable.Emit(tw, tok, lbl, idx)
8294
}
83-
extractGoModExpr(tw, &expr.LParen, lbl, 0)
95+
extractGoModExpr(tw, &expr.LParen, lbl, 0, cgIdxAlloc)
8496
for idx, line := range expr.Line {
85-
extractGoModExpr(tw, line, lbl, idx+1)
97+
extractGoModExpr(tw, line, lbl, idx+1, cgIdxAlloc)
8698
}
87-
extractGoModExpr(tw, &expr.RParen, lbl, len(expr.Line)+1)
99+
extractGoModExpr(tw, &expr.RParen, lbl, len(expr.Line)+1, cgIdxAlloc)
88100
default:
89101
log.Fatalf("unknown go.mod expression of type %T", expr)
90102
}
91103

92104
dbscheme.ModExprsTable.Emit(tw, lbl, kind, parent, idx)
93105

94-
extractGoModComments(tw, expr, lbl)
106+
extractGoModComments(tw, expr, lbl, cgIdxAlloc)
95107

96108
start, end := expr.Span()
97109
extractLocation(tw, lbl, start.Line, start.LineRune, end.Line, end.LineRune)
@@ -135,7 +147,7 @@ func lexMax(a1 int, a2 int, b1 int, b2 int) (int, int) {
135147
}
136148
}
137149

138-
func extractGoModComments(tw *trap.Writer, expr modfile.Expr, exprlbl trap.Label) {
150+
func extractGoModComments(tw *trap.Writer, expr modfile.Expr, exprlbl trap.Label, cgIdxAlloc *commentGroupIdxAllocator) {
139151
comments := expr.Comment()
140152

141153
if len(comments.Before) == 0 && len(comments.Suffix) == 0 && len(comments.After) == 0 {
@@ -144,7 +156,7 @@ func extractGoModComments(tw *trap.Writer, expr modfile.Expr, exprlbl trap.Label
144156

145157
// extract a pseudo `@commentgroup` for each expr that contains their associated comments
146158
grouplbl := tw.Labeler.LocalID(GoModExprCommentWrapper{expr})
147-
dbscheme.CommentGroupsTable.Emit(tw, grouplbl, tw.Labeler.FileLabel(), 0)
159+
dbscheme.CommentGroupsTable.Emit(tw, grouplbl, tw.Labeler.FileLabel(), cgIdxAlloc.nextCgIdx())
148160
dbscheme.DocCommentsTable.Emit(tw, exprlbl, grouplbl)
149161

150162
var allComments []modfile.Comment
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
| go.mod:1:1:2:32 | comment group |
2-
| go.mod:3:1:3:11 | comment group |
3-
| go.mod:5:1:5:13 | comment group |
1+
| go.mod:0:0:0:0 | go.mod | 0 | go.mod:1:1:2:32 | comment group |
2+
| go.mod:0:0:0:0 | go.mod | 1 | go.mod:3:1:3:11 | comment group |
3+
| go.mod:0:0:0:0 | go.mod | 2 | go.mod:5:1:5:13 | comment group |
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import go
22

3-
from CommentGroup cg
4-
select cg
3+
from File f, CommentGroup cg, int idx
4+
where cg = f.getCommentGroup(idx)
5+
select f, idx, cg

0 commit comments

Comments
 (0)