-
Notifications
You must be signed in to change notification settings - Fork 24
/
targets.go
165 lines (132 loc) · 3.39 KB
/
targets.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package reference
import (
"errors"
"strings"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
)
type Targets []Target
func (refs Targets) Copy() Targets {
if refs == nil {
return nil
}
newRefs := make(Targets, len(refs))
for i, ref := range refs {
newRefs[i] = ref.Copy()
}
return newRefs
}
func (r Targets) Len() int {
return len(r)
}
func (r Targets) Less(i, j int) bool {
return r[i].Addr.String() < r[j].Addr.String()
}
func (r Targets) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
type TargetWalkFunc func(Target) error
var stopWalking error = errors.New("stop walking")
const InfiniteDepth = -1
func (refs Targets) deepWalk(f TargetWalkFunc, depth int) {
w := refTargetDeepWalker{
WalkFunc: f,
Depth: depth,
}
w.walk(refs)
}
type refTargetDeepWalker struct {
WalkFunc TargetWalkFunc
Depth int
currentDepth int
}
func (w refTargetDeepWalker) walk(refTargets Targets) {
for _, ref := range refTargets {
err := w.WalkFunc(ref)
if err == stopWalking {
return
}
if len(ref.NestedTargets) > 0 && (w.Depth == InfiniteDepth || w.Depth > w.currentDepth) {
w.currentDepth++
w.walk(ref.NestedTargets)
w.currentDepth--
}
}
}
func (refs Targets) MatchWalk(te schema.TraversalExpr, prefix string, f TargetWalkFunc) {
for _, ref := range refs {
if strings.HasPrefix(ref.Addr.String(), string(prefix)) {
nestedMatches := ref.NestedTargets.containsMatch(te, prefix)
if ref.MatchesConstraint(te) || nestedMatches {
f(ref)
continue
}
}
ref.NestedTargets.MatchWalk(te, prefix, f)
}
}
func (refs Targets) containsMatch(te schema.TraversalExpr, prefix string) bool {
for _, ref := range refs {
if strings.HasPrefix(ref.Addr.String(), string(prefix)) &&
ref.MatchesConstraint(te) {
return true
}
if len(ref.NestedTargets) > 0 {
if match := ref.NestedTargets.containsMatch(te, prefix); match {
return true
}
}
}
return false
}
func (refs Targets) Match(addr lang.Address, cons OriginConstraints) (Targets, bool) {
matchingReferences := make(Targets, 0)
refs.deepWalk(func(ref Target) error {
if ref.Matches(addr, cons) {
matchingReferences = append(matchingReferences, ref)
}
return nil
}, InfiniteDepth)
return matchingReferences, len(matchingReferences) > 0
}
func (refs Targets) OutermostInFile(file string) Targets {
targets := make(Targets, 0)
for _, target := range refs {
if target.RangePtr == nil {
continue
}
if target.RangePtr.Filename == file {
targets = append(targets, target)
}
}
return targets
}
func (refs Targets) InnermostAtPos(file string, pos hcl.Pos) (Targets, bool) {
matchingTargets := make(Targets, 0)
for _, target := range refs {
if target.RangePtr == nil {
continue
}
if target.RangePtr.Filename == file && target.RangePtr.ContainsPos(pos) {
matchingTargets = append(matchingTargets, target)
}
}
innermostTargets := make(Targets, 0)
for _, target := range matchingTargets {
if target.DefRangePtr != nil {
if target.DefRangePtr.Filename == file &&
target.DefRangePtr.ContainsPos(pos) {
innermostTargets = append(innermostTargets, target)
continue
}
}
nestedTargets, ok := target.NestedTargets.InnermostAtPos(file, pos)
if ok {
innermostTargets = nestedTargets
continue
}
innermostTargets = append(innermostTargets, target)
}
return innermostTargets, len(innermostTargets) > 0
}