Skip to content

Commit a4b842d

Browse files
committed
Extracted count candidate and hover data
1 parent e29b159 commit a4b842d

File tree

4 files changed

+28
-39
lines changed

4 files changed

+28
-39
lines changed

decoder/body_candidates.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,3 @@ func isBlockDeclarable(body *hclsyntax.Body, blockType string, bSchema *schema.B
146146
}
147147
return true
148148
}
149-
150-
func countAttributeCandidate(editRng hcl.Range) lang.Candidate {
151-
return lang.Candidate{
152-
Label: "count",
153-
Detail: "optional, number",
154-
Description: lang.PlainText("The distinct index number (starting with 0) corresponding to the instance"),
155-
Kind: lang.AttributeCandidateKind,
156-
TextEdit: lang.TextEdit{
157-
NewText: "count",
158-
Snippet: "count = ${1:1}",
159-
Range: editRng,
160-
},
161-
}
162-
}

decoder/decoder.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package decoder
33
import (
44
"fmt"
55

6+
"github.com/hashicorp/hcl-lang/lang"
67
"github.com/hashicorp/hcl-lang/schema"
78
"github.com/hashicorp/hcl/v2"
89
"github.com/hashicorp/hcl/v2/hclsyntax"
@@ -157,3 +158,27 @@ func decodeBody(body hcl.Body, bodySchema *schema.BodySchema) bodyContent {
157158
func stringPos(pos hcl.Pos) string {
158159
return fmt.Sprintf("%d,%d", pos.Line, pos.Column)
159160
}
161+
162+
func countAttributeCandidate(editRng hcl.Range) lang.Candidate {
163+
return lang.Candidate{
164+
Label: "count",
165+
Detail: "optional, number",
166+
Description: lang.PlainText("The distinct index number (starting with 0) corresponding to the instance"),
167+
Kind: lang.AttributeCandidateKind,
168+
TextEdit: lang.TextEdit{
169+
NewText: "count",
170+
Snippet: "count = ${1:1}",
171+
Range: editRng,
172+
},
173+
}
174+
}
175+
176+
func countAttributeHoverData(editRng hcl.Range) *lang.HoverData {
177+
return &lang.HoverData{
178+
Content: lang.MarkupContent{
179+
Kind: lang.MarkdownKind,
180+
Value: "**count** _optional, number_\n\nThe distinct index number (starting with 0) corresponding to the instance",
181+
},
182+
Range: editRng,
183+
}
184+
}

decoder/expression_candidates.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -464,17 +464,7 @@ func (d *PathDecoder) candidatesForTraversalConstraint(ctx context.Context, tc s
464464

465465
ext, ok := icontext.ExtensionsFromContext(ctx)
466466
if ok && ext.Count && icontext.ActiveCountFromContext(ctx) {
467-
candidates = append(candidates, lang.Candidate{
468-
Label: "count.index",
469-
Detail: "number",
470-
Description: lang.PlainText("The distinct index number (starting with 0) corresponding to the instance"),
471-
Kind: lang.TraversalCandidateKind,
472-
TextEdit: lang.TextEdit{
473-
NewText: "count.index",
474-
Snippet: "count.index",
475-
Range: editRng,
476-
},
477-
})
467+
candidates = append(candidates, countAttributeCandidate(editRng))
478468
}
479469

480470
if d.pathCtx.ReferenceTargets == nil {

decoder/hover.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,7 @@ func (d *PathDecoder) hoverAtPos(ctx context.Context, body *hclsyntax.Body, body
6161

6262
if bodySchema.Extensions != nil {
6363
if name == "count" && bodySchema.Extensions.Count {
64-
return &lang.HoverData{
65-
Content: lang.MarkupContent{
66-
Kind: lang.MarkdownKind,
67-
Value: "**count** _optional, number_\n\nThe distinct index number (starting with 0) corresponding to the instance",
68-
},
69-
Range: attr.Range(),
70-
}, nil
64+
return countAttributeHoverData(attr.Range()), nil
7165
}
7266
}
7367

@@ -267,13 +261,7 @@ func (d *PathDecoder) hoverDataForExpr(ctx context.Context, expr hcl.Expression,
267261
Name: "index",
268262
},
269263
}) && icontext.ActiveCountFromContext(ctx) {
270-
return &lang.HoverData{
271-
Content: lang.MarkupContent{
272-
Kind: lang.MarkdownKind,
273-
Value: "**count** _optional, number_\n\nThe distinct index number (starting with 0) corresponding to the instance",
274-
},
275-
Range: expr.Range(),
276-
}, nil
264+
return countAttributeHoverData(expr.Range()), nil
277265
}
278266

279267
tes, ok := constraints.TraversalExprs()

0 commit comments

Comments
 (0)