Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decoder/schema: Pass PrefillRequiredFields around via context #230

Merged
merged 6 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions decoder/attribute_candidates.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package decoder

import (
"context"
"fmt"
"sort"
"strings"
Expand All @@ -11,11 +12,11 @@ import (
"github.com/zclconf/go-cty/cty"
)

func attributeSchemaToCandidate(name string, attr *schema.AttributeSchema, rng hcl.Range) lang.Candidate {
func attributeSchemaToCandidate(ctx context.Context, name string, attr *schema.AttributeSchema, rng hcl.Range) lang.Candidate {
var snippet string
var triggerSuggest bool
if attr.Constraint != nil {
cData := attr.Constraint.EmptyCompletionData(1, 0)
cData := attr.Constraint.EmptyCompletionData(ctx, 1, 0)
snippet = fmt.Sprintf("%s = %s", name, cData.Snippet)
triggerSuggest = cData.TriggerSuggest
} else {
Expand Down
11 changes: 6 additions & 5 deletions decoder/body_candidates.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package decoder

import (
"context"
"sort"
"strings"

Expand All @@ -11,7 +12,7 @@ import (
)

// bodySchemaCandidates returns candidates for completion of fields inside a body or block.
func (d *PathDecoder) bodySchemaCandidates(body *hclsyntax.Body, schema *schema.BodySchema, prefixRng, editRng hcl.Range) lang.Candidates {
func (d *PathDecoder) bodySchemaCandidates(ctx context.Context, body *hclsyntax.Body, schema *schema.BodySchema, prefixRng, editRng hcl.Range) lang.Candidates {
prefix, _ := d.bytesFromRange(prefixRng)

candidates := lang.NewCandidates()
Expand All @@ -23,15 +24,15 @@ func (d *PathDecoder) bodySchemaCandidates(body *hclsyntax.Body, schema *schema.
// check if count attribute is already declared, so we don't
// suggest a duplicate
if _, ok := body.Attributes["count"]; !ok {
candidates.List = append(candidates.List, attributeSchemaToCandidate("count", countAttributeSchema(), editRng))
candidates.List = append(candidates.List, attributeSchemaToCandidate(ctx, "count", countAttributeSchema(), editRng))
}
}

if schema.Extensions.ForEach {
// check if for_each attribute is already declared, so we don't
// suggest a duplicate
if _, present := body.Attributes["for_each"]; !present {
candidates.List = append(candidates.List, attributeSchemaToCandidate("for_each", forEachAttributeSchema(), editRng))
candidates.List = append(candidates.List, attributeSchemaToCandidate(ctx, "for_each", forEachAttributeSchema(), editRng))
}
}
}
Expand All @@ -51,15 +52,15 @@ func (d *PathDecoder) bodySchemaCandidates(body *hclsyntax.Body, schema *schema.
return candidates
}

candidates.List = append(candidates.List, attributeSchemaToCandidate(name, attr, editRng))
candidates.List = append(candidates.List, attributeSchemaToCandidate(ctx, name, attr, editRng))
count++
}
} else if attr := schema.AnyAttribute; attr != nil && len(prefix) == 0 {
if uint(count) >= d.maxCandidates {
return candidates
}

candidates.List = append(candidates.List, attributeSchemaToCandidate("name", attr, editRng))
candidates.List = append(candidates.List, attributeSchemaToCandidate(ctx, "name", attr, editRng))
count++
}

Expand Down
8 changes: 5 additions & 3 deletions decoder/candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func (d *PathDecoder) CandidatesAtPos(ctx context.Context, filename string, pos
outerBodyRng = ob.Range()
}

ctx = schema.WithPrefillRequiredFields(ctx, d.PrefillRequiredFields)

return d.candidatesAtPos(ctx, rootBody, outerBodyRng, d.pathCtx.Schema, pos)
}

Expand Down Expand Up @@ -71,7 +73,7 @@ func (d *PathDecoder) candidatesAtPos(ctx context.Context, body *hclsyntax.Body,
if attr.NameRange.ContainsPos(pos) {
prefixRng := attr.NameRange
prefixRng.End = pos
return d.bodySchemaCandidates(body, bodySchema, prefixRng, attr.Range()), nil
return d.bodySchemaCandidates(ctx, body, bodySchema, prefixRng, attr.Range()), nil
}
if attr.EqualsRange.ContainsPos(pos) {
return lang.ZeroCandidates(), nil
Expand All @@ -98,7 +100,7 @@ func (d *PathDecoder) candidatesAtPos(ctx context.Context, body *hclsyntax.Body,
if block.TypeRange.ContainsPos(pos) {
prefixRng := block.TypeRange
prefixRng.End = pos
return d.bodySchemaCandidates(body, bodySchema, prefixRng, block.Range()), nil
return d.bodySchemaCandidates(ctx, body, bodySchema, prefixRng, block.Range()), nil
}

for i, labelRange := range block.LabelRanges {
Expand Down Expand Up @@ -152,7 +154,7 @@ func (d *PathDecoder) candidatesAtPos(ctx context.Context, body *hclsyntax.Body,
rng = tokenRng
}

return d.bodySchemaCandidates(body, bodySchema, rng, rng), nil
return d.bodySchemaCandidates(ctx, body, bodySchema, rng, rng), nil
}

func (d *PathDecoder) isPosInsideAttrExpr(attr *hclsyntax.Attribute, pos hcl.Pos) bool {
Expand Down
2 changes: 1 addition & 1 deletion decoder/expr_list_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (list List) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candid
label = fmt.Sprintf("[ %s ]", list.cons.Elem.FriendlyName())
}

d := list.cons.EmptyCompletionData(1, 0)
d := list.cons.EmptyCompletionData(ctx, 1, 0)

return []lang.Candidate{
{
Expand Down
4 changes: 2 additions & 2 deletions decoder/expr_map_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (m Map) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate
label = fmt.Sprintf(`{ "key" = %s }`, m.cons.Elem.FriendlyName())
}

cData := m.cons.EmptyCompletionData(1, 0)
cData := m.cons.EmptyCompletionData(ctx, 1, 0)

return []lang.Candidate{
{
Expand Down Expand Up @@ -62,7 +62,7 @@ func (m Map) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate
return []lang.Candidate{}
}

cData := m.cons.Elem.EmptyCompletionData(2, 0)
cData := m.cons.Elem.EmptyCompletionData(ctx, 2, 0)
kind := lang.AttributeCandidateKind
// TODO: replace "attribute" kind w/ Elem type

Expand Down
30 changes: 0 additions & 30 deletions decoder/expr_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,3 @@ func (obj Object) ReferenceTargets(ctx context.Context, targetCtx *TargetContext
// TODO
return nil
}

type ObjectAttributes struct {
expr hcl.Expression
cons schema.ObjectAttributes
}

func (oa ObjectAttributes) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
// TODO
return nil
}

func (oa ObjectAttributes) HoverAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverData {
// TODO
return nil
}

func (oa ObjectAttributes) SemanticTokens(ctx context.Context) []lang.SemanticToken {
// TODO
return nil
}

func (oa ObjectAttributes) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference.Origins {
// TODO
return nil
}

func (oa ObjectAttributes) ReferenceTargets(ctx context.Context, targetCtx *TargetContext) reference.Targets {
// TODO
return nil
}
2 changes: 1 addition & 1 deletion decoder/expr_set_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (set Set) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidat
label = fmt.Sprintf("[ %s ]", set.cons.Elem.FriendlyName())
}

d := set.cons.EmptyCompletionData(1, 0)
d := set.cons.EmptyCompletionData(ctx, 1, 0)

return []lang.Candidate{
{
Expand Down
2 changes: 1 addition & 1 deletion decoder/expr_tuple_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (tuple Tuple) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Cand
label = fmt.Sprintf("[ %s ]", elemLabel)
}

d := tuple.cons.EmptyCompletionData(1, 0)
d := tuple.cons.EmptyCompletionData(ctx, 1, 0)

return []lang.Candidate{
{
Expand Down
3 changes: 0 additions & 3 deletions decoder/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var (
_ Expression = LiteralType{}
_ Expression = LiteralValue{}
_ Expression = Map{}
_ Expression = ObjectAttributes{}
_ Expression = Object{}
_ Expression = Set{}
_ Expression = Reference{}
Expand All @@ -29,7 +28,6 @@ var (
_ ReferenceOriginsExpression = List{}
_ ReferenceOriginsExpression = LiteralType{}
_ ReferenceOriginsExpression = Map{}
_ ReferenceOriginsExpression = ObjectAttributes{}
_ ReferenceOriginsExpression = Object{}
_ ReferenceOriginsExpression = Set{}
_ ReferenceOriginsExpression = Reference{}
Expand All @@ -39,7 +37,6 @@ var (
_ ReferenceTargetsExpression = List{}
_ ReferenceTargetsExpression = LiteralType{}
_ ReferenceTargetsExpression = Map{}
_ ReferenceTargetsExpression = ObjectAttributes{}
_ ReferenceTargetsExpression = Object{}
_ ReferenceTargetsExpression = Reference{}
_ ReferenceTargetsExpression = Tuple{}
Expand Down
7 changes: 6 additions & 1 deletion decoder/label_candidates.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package decoder

import (
"context"
"encoding/json"
"fmt"
"sort"
Expand Down Expand Up @@ -164,7 +165,11 @@ func requiredFieldsSnippet(bodySchema *schema.BodySchema, placeholder int, inden

var snippet string
if attr.Constraint != nil {
snippet = attr.Constraint.EmptyCompletionData(placeholder, indentCount).Snippet
// We already know we want to do pre-filling at this point
// We could plumb through the context here, but it saves us
// an argument in multiple functions above.
ctx := schema.WithPrefillRequiredFields(context.Background(), true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we passing true here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment. We could still refactor related functions later and plumb the context all the way through, but I feel it's better not to do it now.

snippet = attr.Constraint.EmptyCompletionData(ctx, placeholder, indentCount).Snippet
} else {
snippet = snippetForExprContraint(uint(placeholder), attr.Expr)
}
Expand Down
4 changes: 3 additions & 1 deletion schema/constraint.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package schema

import (
"context"

"github.com/hashicorp/hcl-lang/lang"
"github.com/zclconf/go-cty/cty"
)
Expand All @@ -16,7 +18,7 @@ type Constraint interface {
// there is no corresponding configuration, such as when the Constraint
// is part of another and it is desirable to complete
// the parent constraint as whole.
EmptyCompletionData(nextPlaceholder int, nestingLevel int) CompletionData
EmptyCompletionData(ctx context.Context, nextPlaceholder int, nestingLevel int) CompletionData
}

type ConstraintWithHoverData interface {
Expand Down
4 changes: 3 additions & 1 deletion schema/constraint_any_expression.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package schema

import (
"context"

"github.com/zclconf/go-cty/cty"
)

Expand Down Expand Up @@ -28,7 +30,7 @@ func (ae AnyExpression) Copy() Constraint {
}
}

func (ae AnyExpression) EmptyCompletionData(nextPlaceholder int, nestingLevel int) CompletionData {
func (ae AnyExpression) EmptyCompletionData(ctx context.Context, nextPlaceholder int, nestingLevel int) CompletionData {
// TODO
return CompletionData{}
}
Expand Down
4 changes: 3 additions & 1 deletion schema/constraint_keyword.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package schema

import (
"context"

"github.com/hashicorp/hcl-lang/lang"
)

Expand Down Expand Up @@ -36,7 +38,7 @@ func (k Keyword) Copy() Constraint {
}
}

func (k Keyword) EmptyCompletionData(nextPlaceholder int, nestingLevel int) CompletionData {
func (k Keyword) EmptyCompletionData(ctx context.Context, nextPlaceholder int, nestingLevel int) CompletionData {
return CompletionData{
TriggerSuggest: true,
NextPlaceholder: nextPlaceholder,
Expand Down
5 changes: 3 additions & 2 deletions schema/constraint_list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package schema

import (
"context"
"fmt"

"github.com/hashicorp/hcl-lang/lang"
Expand Down Expand Up @@ -48,7 +49,7 @@ func (l List) Copy() Constraint {
}
}

func (l List) EmptyCompletionData(nextPlaceholder int, nestingLevel int) CompletionData {
func (l List) EmptyCompletionData(ctx context.Context, nextPlaceholder int, nestingLevel int) CompletionData {
if l.Elem == nil {
return CompletionData{
NewText: "[ ]",
Expand All @@ -57,7 +58,7 @@ func (l List) EmptyCompletionData(nextPlaceholder int, nestingLevel int) Complet
}
}

elemData := l.Elem.EmptyCompletionData(nextPlaceholder, nestingLevel)
elemData := l.Elem.EmptyCompletionData(ctx, nextPlaceholder, nestingLevel)
if elemData.NewText == "" || elemData.Snippet == "" {
return CompletionData{
NewText: "[ ]",
Expand Down
13 changes: 7 additions & 6 deletions schema/constraint_literal_type.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package schema

import (
"context"
"fmt"

"github.com/hashicorp/hcl-lang/lang"
Expand Down Expand Up @@ -36,7 +37,7 @@ func (lt LiteralType) Copy() Constraint {
}
}

func (lt LiteralType) EmptyCompletionData(nextPlaceholder int, nestingLevel int) CompletionData {
func (lt LiteralType) EmptyCompletionData(ctx context.Context, nextPlaceholder int, nestingLevel int) CompletionData {
if lt.Type.IsPrimitiveType() {
var newText, snippet string

Expand Down Expand Up @@ -69,23 +70,23 @@ func (lt LiteralType) EmptyCompletionData(nextPlaceholder int, nestingLevel int)
Type: lt.Type.ElementType(),
},
}
return listCons.EmptyCompletionData(nextPlaceholder, nestingLevel)
return listCons.EmptyCompletionData(ctx, nextPlaceholder, nestingLevel)
}
if lt.Type.IsSetType() {
setCons := Set{
Elem: LiteralType{
Type: lt.Type.ElementType(),
},
}
return setCons.EmptyCompletionData(nextPlaceholder, nestingLevel)
return setCons.EmptyCompletionData(ctx, nextPlaceholder, nestingLevel)
}
if lt.Type.IsMapType() {
mapCons := Map{
Elem: LiteralType{
Type: lt.Type.ElementType(),
},
}
return mapCons.EmptyCompletionData(nextPlaceholder, nestingLevel)
return mapCons.EmptyCompletionData(ctx, nextPlaceholder, nestingLevel)
}
if lt.Type.IsTupleType() {
types := lt.Type.TupleElementTypes()
Expand All @@ -95,7 +96,7 @@ func (lt LiteralType) EmptyCompletionData(nextPlaceholder int, nestingLevel int)
Type: typ,
}
}
return tupleCons.EmptyCompletionData(nextPlaceholder, nestingLevel)
return tupleCons.EmptyCompletionData(ctx, nextPlaceholder, nestingLevel)
}
if lt.Type.IsObjectType() {
attrTypes := lt.Type.AttributeTypes()
Expand All @@ -117,7 +118,7 @@ func (lt LiteralType) EmptyCompletionData(nextPlaceholder int, nestingLevel int)
cons := Object{
Attributes: attrs,
}
return cons.EmptyCompletionData(nextPlaceholder, nestingLevel)
return cons.EmptyCompletionData(ctx, nextPlaceholder, nestingLevel)
}

return CompletionData{
Expand Down
Loading