Skip to content

Commit b664517

Browse files
dbanckradeksimko
andauthored
Expose completion path keys for testing (#127)
* Expose completion path keys for testing Co-authored-by: Radek Simko <[email protected]>
1 parent 058bfb1 commit b664517

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

decoder/expression_candidates.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,27 +173,51 @@ func constraintsAtPos(expr hcl.Expression, constraints ExprConstraints, pos hcl.
173173

174174
type pathKey struct{}
175175

176+
// WithPath is not intended to be used outside this package
177+
// except for testing hooks downstream.
178+
func WithPath(ctx context.Context, path lang.Path) context.Context {
179+
return context.WithValue(ctx, pathKey{}, path)
180+
}
181+
176182
func PathFromContext(ctx context.Context) (lang.Path, bool) {
177183
p, ok := ctx.Value(pathKey{}).(lang.Path)
178184
return p, ok
179185
}
180186

181187
type posKey struct{}
182188

189+
// WithPos is not intended to be used outside this package
190+
// except for testing hooks downstream.
191+
func WithPos(ctx context.Context, pos hcl.Pos) context.Context {
192+
return context.WithValue(ctx, posKey{}, pos)
193+
}
194+
183195
func PosFromContext(ctx context.Context) (hcl.Pos, bool) {
184196
p, ok := ctx.Value(posKey{}).(hcl.Pos)
185197
return p, ok
186198
}
187199

188200
type filenameKey struct{}
189201

202+
// WithFilename is not intended to be used outside this package
203+
// except for testing hooks downstream.
204+
func WithFilename(ctx context.Context, filename string) context.Context {
205+
return context.WithValue(ctx, filenameKey{}, filename)
206+
}
207+
190208
func FilenameFromContext(ctx context.Context) (string, bool) {
191209
f, ok := ctx.Value(filenameKey{}).(string)
192210
return f, ok
193211
}
194212

195213
type maxCandidatesKey struct{}
196214

215+
// WithMaxCandidates is not intended to be used outside this package
216+
// except for testing hooks downstream.
217+
func WithMaxCandidates(ctx context.Context, maxCandidates uint) context.Context {
218+
return context.WithValue(ctx, maxCandidatesKey{}, maxCandidates)
219+
}
220+
197221
func MaxCandidatesFromContext(ctx context.Context) (uint, bool) {
198222
mc, ok := ctx.Value(maxCandidatesKey{}).(uint)
199223
return mc, ok
@@ -243,10 +267,10 @@ func (d *PathDecoder) candidatesFromHooks(ctx context.Context, attr *hclsyntax.A
243267
prefix := string(prefixBytes)
244268
prefix = strings.TrimLeft(prefix, `"`)
245269

246-
ctx = context.WithValue(ctx, pathKey{}, d.path)
247-
ctx = context.WithValue(ctx, filenameKey{}, attr.Expr.Range().Filename)
248-
ctx = context.WithValue(ctx, posKey{}, pos)
249-
ctx = context.WithValue(ctx, maxCandidatesKey{}, d.maxCandidates)
270+
ctx = WithPath(ctx, d.path)
271+
ctx = WithFilename(ctx, attr.Expr.Range().Filename)
272+
ctx = WithPos(ctx, pos)
273+
ctx = WithMaxCandidates(ctx, d.maxCandidates)
250274

251275
count := 0
252276
for _, hook := range schema.CompletionHooks {

0 commit comments

Comments
 (0)