-
Notifications
You must be signed in to change notification settings - Fork 1
/
posfilter.go
65 lines (51 loc) · 1.37 KB
/
posfilter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package protogetter
import (
"go/token"
)
type PosFilter struct {
positions map[token.Pos]struct{}
alreadyReplaced map[string]map[int][2]int // map[filename][line][start, end]
}
func NewPosFilter() *PosFilter {
return &PosFilter{
positions: make(map[token.Pos]struct{}),
alreadyReplaced: make(map[string]map[int][2]int),
}
}
func (f *PosFilter) IsFiltered(pos token.Pos) bool {
_, ok := f.positions[pos]
return ok
}
func (f *PosFilter) AddPos(pos token.Pos) {
f.positions[pos] = struct{}{}
}
func (f *PosFilter) IsAlreadyReplaced(fset *token.FileSet, pos, end token.Pos) bool {
filePos := fset.Position(pos)
fileEnd := fset.Position(end)
lines, ok := f.alreadyReplaced[filePos.Filename]
if !ok {
return false
}
lineRange, ok := lines[filePos.Line]
if !ok {
return false
}
if lineRange[0] <= filePos.Offset && fileEnd.Offset <= lineRange[1] {
return true
}
return false
}
func (f *PosFilter) AddAlreadyReplaced(fset *token.FileSet, pos, end token.Pos) {
filePos := fset.Position(pos)
fileEnd := fset.Position(end)
lines, ok := f.alreadyReplaced[filePos.Filename]
if !ok {
lines = make(map[int][2]int)
f.alreadyReplaced[filePos.Filename] = lines
}
lineRange, ok := lines[filePos.Line]
if ok && lineRange[0] <= filePos.Offset && fileEnd.Offset <= lineRange[1] {
return
}
lines[filePos.Line] = [2]int{filePos.Offset, fileEnd.Offset}
}