Skip to content

Commit b1a4452

Browse files
committed
feedback
1 parent a4b842d commit b1a4452

File tree

8 files changed

+57
-41
lines changed

8 files changed

+57
-41
lines changed

context/context.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,16 @@ package context
22

33
import (
44
"context"
5-
6-
"github.com/hashicorp/hcl-lang/schema"
75
)
86

97
type bodyExtCtxKey struct{}
108

119
type bodyActiveCountCtxKey struct{}
1210

13-
func WithExtensions(ctx context.Context, ext *schema.BodyExtensions) context.Context {
14-
return context.WithValue(ctx, bodyExtCtxKey{}, ext)
15-
}
16-
1711
func WithActiveCount(ctx context.Context) context.Context {
1812
return context.WithValue(ctx, bodyActiveCountCtxKey{}, true)
1913
}
2014

21-
func ExtensionsFromContext(ctx context.Context) (*schema.BodyExtensions, bool) {
22-
ext, ok := ctx.Value(bodyExtCtxKey{}).(*schema.BodyExtensions)
23-
return ext, ok
24-
}
25-
2615
func ActiveCountFromContext(ctx context.Context) bool {
2716
return ctx.Value(bodyActiveCountCtxKey{}) != nil
2817
}

decoder/body_extensions_test.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,20 +198,23 @@ func TestCompletionAtPos_BodySchema_Extensions(t *testing.T) {
198198
Kind: lang.PlainTextKind,
199199
},
200200
Detail: "number",
201-
TextEdit: lang.TextEdit{Range: hcl.Range{
202-
Filename: "test.tf",
203-
Start: hcl.Pos{
204-
Line: 3,
205-
Column: 13,
206-
Byte: 55,
207-
},
208-
End: hcl.Pos{
209-
Line: 3,
210-
Column: 13,
211-
Byte: 55,
201+
TextEdit: lang.TextEdit{
202+
Range: hcl.Range{
203+
Filename: "test.tf",
204+
Start: hcl.Pos{
205+
Line: 3,
206+
Column: 13,
207+
Byte: 55,
208+
},
209+
End: hcl.Pos{
210+
Line: 3,
211+
Column: 13,
212+
Byte: 55,
213+
},
212214
},
215+
NewText: "count.index",
216+
Snippet: "count.index",
213217
},
214-
NewText: "count.index", Snippet: "count.index"},
215218
Kind: lang.TraversalCandidateKind,
216219
},
217220
}),

decoder/candidates.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ func (d *PathDecoder) candidatesAtPos(ctx context.Context, body *hclsyntax.Body,
5050
filename := body.Range().Filename
5151

5252
if bodySchema.Extensions != nil {
53-
ctx = icontext.WithExtensions(ctx, bodySchema.Extensions)
5453
if bodySchema.Extensions.Count {
5554
if _, ok := body.Attributes["count"]; ok {
5655
// append to context we need count completed

decoder/decoder.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ func countAttributeCandidate(editRng hcl.Range) lang.Candidate {
173173
}
174174
}
175175

176+
func countIndexCandidate(editRng hcl.Range) lang.Candidate {
177+
return lang.Candidate{
178+
Label: "count.index",
179+
Detail: "number",
180+
Description: lang.PlainText("The distinct index number (starting with 0) corresponding to the instance"),
181+
Kind: lang.TraversalCandidateKind,
182+
TextEdit: lang.TextEdit{
183+
NewText: "count.index",
184+
Snippet: "count.index",
185+
Range: editRng,
186+
},
187+
}
188+
}
189+
176190
func countAttributeHoverData(editRng hcl.Range) *lang.HoverData {
177191
return &lang.HoverData{
178192
Content: lang.MarkupContent{

decoder/expression_candidates.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,8 @@ func (d *PathDecoder) constraintToCandidates(ctx context.Context, constraint sch
462462
func (d *PathDecoder) candidatesForTraversalConstraint(ctx context.Context, tc schema.TraversalExpr, outerBodyRng, prefixRng, editRng hcl.Range) []lang.Candidate {
463463
candidates := make([]lang.Candidate, 0)
464464

465-
ext, ok := icontext.ExtensionsFromContext(ctx)
466-
if ok && ext.Count && icontext.ActiveCountFromContext(ctx) {
467-
candidates = append(candidates, countAttributeCandidate(editRng))
465+
if icontext.ActiveCountFromContext(ctx) {
466+
candidates = append(candidates, countIndexCandidate(editRng))
468467
}
469468

470469
if d.pathCtx.ReferenceTargets == nil {

decoder/hover.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ func (d *PathDecoder) hoverAtPos(ctx context.Context, body *hclsyntax.Body, body
4747
filename := body.Range().Filename
4848

4949
if bodySchema.Extensions != nil {
50-
ctx = icontext.WithExtensions(ctx, bodySchema.Extensions)
5150
if bodySchema.Extensions.Count {
5251
if _, ok := body.Attributes["count"]; ok {
5352
// append to context we need count provided

decoder/semantic_tokens.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import (
44
"context"
55
"sort"
66

7+
"github.com/zclconf/go-cty/cty"
8+
79
icontext "github.com/hashicorp/hcl-lang/context"
810
"github.com/hashicorp/hcl-lang/lang"
911
"github.com/hashicorp/hcl-lang/reference"
1012
"github.com/hashicorp/hcl-lang/schema"
1113
"github.com/hashicorp/hcl/v2"
1214
"github.com/hashicorp/hcl/v2/hclsyntax"
13-
"github.com/zclconf/go-cty/cty"
1415
)
1516

1617
// SemanticTokensInFile returns a sequence of semantic tokens
@@ -185,7 +186,19 @@ func (d *PathDecoder) tokensForExpression(ctx context.Context, expr hclsyntax.Ex
185186
tokens = append(tokens, lang.SemanticToken{
186187
Type: lang.TokenTraversalStep,
187188
Modifiers: []lang.SemanticTokenModifier{},
188-
Range: traversal[1].SourceRange(),
189+
Range: hcl.Range{
190+
Filename: traversal[1].SourceRange().Filename,
191+
Start: hcl.Pos{
192+
Line: traversal[1].SourceRange().Start.Line,
193+
Column: traversal[1].SourceRange().Start.Column + 1,
194+
Byte: traversal[1].SourceRange().Start.Byte + 1,
195+
},
196+
End: hcl.Pos{
197+
Line: traversal[1].SourceRange().End.Line,
198+
Column: traversal[1].SourceRange().End.Column + 1,
199+
Byte: traversal[1].SourceRange().End.Byte + 1,
200+
},
201+
},
189202
})
190203
return tokens
191204
}

decoder/semantic_tokens_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
"testing"
77

88
"github.com/google/go-cmp/cmp"
9-
"github.com/zclconf/go-cty/cty"
109
"github.com/hashicorp/hcl-lang/lang"
1110
"github.com/hashicorp/hcl-lang/schema"
1211
"github.com/hashicorp/hcl/v2"
1312
"github.com/hashicorp/hcl/v2/hclsyntax"
1413
"github.com/hashicorp/hcl/v2/json"
14+
"github.com/zclconf/go-cty/cty"
1515
)
1616

1717
func TestDecoder_SemanticTokensInFile_emptyBody(t *testing.T) {
@@ -1027,7 +1027,7 @@ resource "aws_instance" "app_server" {
10271027
},
10281028
End: hcl.Pos{
10291029
Line: 4,
1030-
Column: 26,
1030+
Column: 25,
10311031
Byte: 85,
10321032
},
10331033
},
@@ -1039,13 +1039,13 @@ resource "aws_instance" "app_server" {
10391039
Filename: "test.tf",
10401040
Start: hcl.Pos{
10411041
Line: 4,
1042-
Column: 25,
1043-
Byte: 85,
1042+
Column: 26,
1043+
Byte: 86,
10441044
},
10451045
End: hcl.Pos{
10461046
Line: 4,
1047-
Column: 31,
1048-
Byte: 91,
1047+
Column: 32,
1048+
Byte: 92,
10491049
},
10501050
},
10511051
},
@@ -1248,20 +1248,20 @@ resource "aws_instance" "app_server" {
12481248
},
12491249
},
12501250
},
1251-
{ // .index
1251+
{ // index
12521252
Type: lang.TokenTraversalStep,
12531253
Modifiers: lang.SemanticTokenModifiers{},
12541254
Range: hcl.Range{
12551255
Filename: "test.tf",
12561256
Start: hcl.Pos{
12571257
Line: 4,
1258-
Column: 25,
1259-
Byte: 85,
1258+
Column: 26,
1259+
Byte: 86,
12601260
},
12611261
End: hcl.Pos{
12621262
Line: 4,
1263-
Column: 31,
1264-
Byte: 91,
1263+
Column: 32,
1264+
Byte: 92,
12651265
},
12661266
},
12671267
},

0 commit comments

Comments
 (0)