Skip to content

Commit 80d7008

Browse files
committed
update cover
1 parent c14a74f commit 80d7008

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

cover/counter.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,39 @@ func (c *Counter) newCounter(start, end token.Pos, numStmt int) string {
5050
func (c *Counter) offset(pos token.Pos) int {
5151
return c.Fset.Position(pos).Offset
5252
}
53+
54+
type Callbacks struct {
55+
OnBlockFn func(insertPos token.Pos, pos token.Pos, end token.Pos, numStmts int, basicStmts []ast.Stmt)
56+
OnWrapElseFn func(lbrace int, rbrace int)
57+
}
58+
59+
var _ Callback = (*Callbacks)(nil)
60+
61+
func RangeBlocks(fset *token.FileSet, f *ast.File, content []byte, fn func(insertPos token.Pos, pos token.Pos, end token.Pos, numStmts int, basicStmts []ast.Stmt)) {
62+
if fn == nil {
63+
panic(fmt.Errorf("requires fn"))
64+
}
65+
bc := &Callbacks{OnBlockFn: fn}
66+
fvisitor := &File{
67+
fset: fset,
68+
content: content,
69+
callback: bc,
70+
}
71+
ast.Walk(fvisitor, f)
72+
}
73+
74+
// OnBlock implements Callback
75+
func (c *Callbacks) OnBlock(insertPos token.Pos, pos token.Pos, end token.Pos, numStmts int, basicStmts []ast.Stmt) {
76+
if c.OnBlockFn == nil {
77+
return
78+
}
79+
c.OnBlockFn(insertPos, pos, end, numStmts, basicStmts)
80+
}
81+
82+
// OnWrapElse implements Callback
83+
func (c *Callbacks) OnWrapElse(lbrace int, rbrace int) {
84+
if c.OnWrapElseFn != nil {
85+
return
86+
}
87+
c.OnWrapElseFn(lbrace, rbrace)
88+
}

0 commit comments

Comments
 (0)