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

Remove schema.TupleConsExpr #175

Merged
merged 4 commits into from
Dec 13, 2022
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
112 changes: 40 additions & 72 deletions decoder/expression_candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,6 @@ func constraintsAtPos(expr hcl.Expression, constraints ExprConstraints, pos hcl.
Filename: rng.Filename,
}

tc, ok := constraints.TupleConsExpr()
if ok && len(eType.Exprs) == 0 && tupleConsBody.ContainsPos(pos) {
return ExprConstraints(tc.AnyElem), hcl.Range{
Start: pos,
End: pos,
Filename: eType.Range().Filename,
}
}

se, ok := constraints.SetExpr()
if ok && len(eType.Exprs) == 0 && tupleConsBody.ContainsPos(pos) {
return ExprConstraints(se.Elem), hcl.Range{
Expand Down Expand Up @@ -329,22 +320,9 @@ func (d *PathDecoder) constraintToCandidates(ctx context.Context, constraint sch
})
case schema.TraversalExpr:
candidates = append(candidates, d.candidatesForTraversalConstraint(ctx, c, outerBodyRng, prefixRng, editRng)...)
case schema.TupleConsExpr:
candidates = append(candidates, lang.Candidate{
Label: fmt.Sprintf(`[%s]`, labelForConstraints(c.AnyElem)),
Detail: c.Name,
Description: c.Description,
Kind: lang.TupleCandidateKind,
TextEdit: lang.TextEdit{
NewText: `[ ]`,
Snippet: `[ ${0} ]`,
Range: editRng,
},
TriggerSuggest: triggerSuggestForExprConstraints(c.AnyElem),
})
case schema.ListExpr:
candidates = append(candidates, lang.Candidate{
Label: fmt.Sprintf(`[%s]`, labelForConstraints(c.Elem)),
Label: fmt.Sprintf(`[ %s ]`, labelsForConstraints(c.Elem)),
Detail: c.FriendlyName(),
Description: c.Description,
Kind: lang.ListCandidateKind,
Expand All @@ -357,7 +335,7 @@ func (d *PathDecoder) constraintToCandidates(ctx context.Context, constraint sch
})
case schema.SetExpr:
candidates = append(candidates, lang.Candidate{
Label: fmt.Sprintf(`[%s]`, labelForConstraints(c.Elem)),
Label: fmt.Sprintf(`[ %s ]`, labelsForConstraints(c.Elem)),
Detail: c.FriendlyName(),
Description: c.Description,
Kind: lang.SetCandidateKind,
Expand All @@ -374,7 +352,7 @@ func (d *PathDecoder) constraintToCandidates(ctx context.Context, constraint sch
triggerSuggest = triggerSuggestForExprConstraints(c.Elems[0])
}
candidates = append(candidates, lang.Candidate{
Label: fmt.Sprintf(`[%s]`, labelForConstraints(c.Elems[0])),
Label: fmt.Sprintf(`[ %s ]`, labelsForConstraints(c.Elems[0])),
Detail: c.FriendlyName(),
Description: c.Description,
Kind: lang.TupleCandidateKind,
Expand All @@ -387,7 +365,7 @@ func (d *PathDecoder) constraintToCandidates(ctx context.Context, constraint sch
})
case schema.MapExpr:
candidates = append(candidates, lang.Candidate{
Label: fmt.Sprintf(`{ key =%s}`, labelForConstraints(c.Elem)),
Label: fmt.Sprintf(`{ key = %s }`, labelsForConstraints(c.Elem)),
Detail: c.FriendlyName(),
Description: c.Description,
Kind: lang.MapCandidateKind,
Expand Down Expand Up @@ -507,11 +485,6 @@ func newTextForConstraints(cons schema.ExprConstraints, isNested bool) string {
return newTextForLiteralValue(c.Val)
case schema.KeywordExpr:
return c.Keyword
case schema.TupleConsExpr:
if isNested {
return "[ ]"
}
return fmt.Sprintf("[\n %s\n]", newTextForConstraints(c.AnyElem, true))
case schema.ListExpr:
if isNested {
return "[ ]"
Expand Down Expand Up @@ -562,11 +535,6 @@ func snippetForConstraints(placeholder uint, cons schema.ExprConstraints, isNest
return snippetForLiteralValue(placeholder, c.Val)
case schema.KeywordExpr:
return fmt.Sprintf("${%d:%s}", placeholder, c.Keyword)
case schema.TupleConsExpr:
if isNested {
return fmt.Sprintf("[ ${%d} ]", placeholder+1)
}
return fmt.Sprintf("[\n %s\n]", snippetForConstraints(placeholder+1, c.AnyElem, true))
case schema.ListExpr:
if isNested {
return fmt.Sprintf("[ ${%d} ]", placeholder+1)
Expand All @@ -591,40 +559,55 @@ func snippetForConstraints(placeholder uint, cons schema.ExprConstraints, isNest
return ""
}

func labelForConstraints(cons schema.ExprConstraints) string {
labels := " "
labelsAdded := 0
for _, constraint := range cons {
if len(labels) > 10 {
labels += "…"
break
}
if labelsAdded > 0 {
labels += "| "
type labelSet []string

func (ls labelSet) String() string {
if len(ls) > 10 {
return strings.Join(ls[:9], " | ") + " …"
}

return strings.Join(ls, " | ")
}

func (ls labelSet) AddLabelIfNotPresent(label string) labelSet {
if !ls.HasLabel(label) {
return append(ls, label)
}
return ls
}

func (ls labelSet) HasLabel(label string) bool {
for _, l := range ls {
if l == label {
return true
}
}
return false
}

func labelsForConstraints(cons schema.ExprConstraints) labelSet {
ls := make(labelSet, 0)

for _, constraint := range cons {
switch c := constraint.(type) {
case schema.LiteralTypeExpr:
labels += labelForLiteralType(c.Type)
ls = ls.AddLabelIfNotPresent(labelForLiteralType(c.Type))
case schema.LiteralValue:
continue
case schema.KeywordExpr:
labels += c.FriendlyName()
ls = ls.AddLabelIfNotPresent(c.FriendlyName())
case schema.TraversalExpr:
labels += c.FriendlyName()
case schema.TupleConsExpr:
labels += fmt.Sprintf("[%s]", labelForConstraints(c.AnyElem))
ls = ls.AddLabelIfNotPresent(c.FriendlyName())
case schema.ListExpr:
labels += fmt.Sprintf("[%s]", labelForConstraints(c.Elem))
ls = ls.AddLabelIfNotPresent(fmt.Sprintf("[ %s ]", labelsForConstraints(c.Elem)))
case schema.SetExpr:
labels += fmt.Sprintf("[%s]", labelForConstraints(c.Elem))
ls = ls.AddLabelIfNotPresent(fmt.Sprintf("[ %s ]", labelsForConstraints(c.Elem)))
case schema.TupleExpr:
labels += fmt.Sprintf("[%s]", labelForConstraints(c.Elems[0]))
ls = ls.AddLabelIfNotPresent(fmt.Sprintf("[ %s ]", labelsForConstraints(c.Elems[0])))
}
labelsAdded++
}
labels += " "

return labels
return ls
}

func typeToCandidates(ofType cty.Type, editRng hcl.Range) []lang.Candidate {
Expand Down Expand Up @@ -746,8 +729,6 @@ func triggerSuggestForExprConstraints(ec schema.ExprConstraints) bool {
return true
case schema.TraversalExpr:
return true
case schema.TupleConsExpr:
return triggerSuggestForExprConstraints(et.AnyElem)
case schema.ListExpr:
return triggerSuggestForExprConstraints(et.Elem)
case schema.SetExpr:
Expand Down Expand Up @@ -779,12 +760,6 @@ func snippetForExprContraints(placeholder uint, ec schema.ExprConstraints) strin
return snippetForLiteralValue(placeholder, et.Val)
}
return ""
case schema.TupleConsExpr:
ec := ExprConstraints(et.AnyElem)
if ec.HasKeywordsOnly() {
return "[ ${0} ]"
}
return "[\n ${0}\n]"
case schema.ListExpr:
ec := ExprConstraints(et.Elem)
if ec.HasKeywordsOnly() {
Expand Down Expand Up @@ -828,13 +803,6 @@ func snippetForExprContraint(placeholder uint, ec schema.ExprConstraints) string
// return snippetForLiteralValue(placeholder, et.Val)
// }
// return ""
if t, ok := e.TupleConsExpr(); ok {
ec := ExprConstraints(t.AnyElem)
if ec.HasKeywordsOnly() {
return "[ ${0} ]"
}
return "[\n ${0}\n]"
}
if t, ok := e.ListExpr(); ok {
ec := ExprConstraints(t.Elem)
if ec.HasKeywordsOnly() {
Expand Down
Loading