Skip to content

Commit ea0bcc6

Browse files
authored
Add each.* hover data (#148)
This adds hover data support for `each.key` and `each.value`
1 parent b20d628 commit ea0bcc6

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

decoder/decoder.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,17 @@ func foreachEachCandidate(editRng hcl.Range) []lang.Candidate {
248248
},
249249
}
250250
}
251+
252+
func eachKeyHoverData(rng hcl.Range) *lang.HoverData {
253+
return &lang.HoverData{
254+
Content: lang.Markdown("`each.key` _string_\n\nThe map key (or set member) corresponding to this instance"),
255+
Range: rng,
256+
}
257+
}
258+
259+
func eachValueHoverData(rng hcl.Range) *lang.HoverData {
260+
return &lang.HoverData{
261+
Content: lang.Markdown("`each.value` _any type_\n\nThe map value corresponding to this instance. (If a set was provided, this is the same as `each.key`.)"),
262+
Range: rng,
263+
}
264+
}

decoder/hover.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,17 @@ func (d *PathDecoder) hoverDataForExpr(ctx context.Context, expr hcl.Expression,
258258
if err != nil {
259259
return nil, err
260260
}
261-
if address.Equals(lang.Address{
262-
lang.RootStep{
263-
Name: "count",
264-
},
265-
lang.AttrStep{
266-
Name: "index",
267-
},
268-
}) && schema.ActiveCountFromContext(ctx) {
261+
262+
countIndexAddr := lang.Address{lang.RootStep{Name: "count"}, lang.AttrStep{Name: "index"}}
263+
eachKeyAddr := lang.Address{lang.RootStep{Name: "each"}, lang.AttrStep{Name: "key"}}
264+
eachValueAddr := lang.Address{lang.RootStep{Name: "each"}, lang.AttrStep{Name: "value"}}
265+
266+
if address.Equals(countIndexAddr) && schema.ActiveCountFromContext(ctx) {
269267
return countIndexHoverData(expr.Range()), nil
268+
} else if address.Equals(eachKeyAddr) && schema.ActiveForEachFromContext(ctx) {
269+
return eachKeyHoverData(expr.Range()), nil
270+
} else if address.Equals(eachValueAddr) && schema.ActiveForEachFromContext(ctx) {
271+
return eachValueHoverData(expr.Range()), nil
270272
}
271273

272274
tes, ok := constraints.TraversalExprs()

decoder/hover_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,13 +1155,13 @@ func TestDecoder_HoverAtPos_foreach_extension(t *testing.T) {
11551155
}
11561156
}
11571157
`,
1158-
hcl.Pos{Line: 5, Column: 16, Byte: 73},
1158+
hcl.Pos{Line: 2, Column: 8, Byte: 30},
11591159
&lang.HoverData{
1160-
Content: lang.Markdown("_map of dynamic_"),
1160+
Content: lang.Markdown("`each.key` _string_\n\nThe map key (or set member) corresponding to this instance"),
11611161
Range: hcl.Range{
11621162
Filename: "test.tf",
1163-
Start: hcl.Pos{Line: 3, Column: 13, Byte: 50},
1164-
End: hcl.Pos{Line: 6, Column: 3, Byte: 81},
1163+
Start: hcl.Pos{Line: 2, Column: 8, Byte: 29},
1164+
End: hcl.Pos{Line: 2, Column: 16, Byte: 37},
11651165
},
11661166
},
11671167
},
@@ -1200,13 +1200,13 @@ func TestDecoder_HoverAtPos_foreach_extension(t *testing.T) {
12001200
}
12011201
}
12021202
`,
1203-
hcl.Pos{Line: 5, Column: 16, Byte: 73},
1203+
hcl.Pos{Line: 2, Column: 8, Byte: 30},
12041204
&lang.HoverData{
1205-
Content: lang.Markdown("_map of dynamic_"),
1205+
Content: lang.Markdown("`each.value` _any type_\n\nThe map value corresponding to this instance. (If a set was provided, this is the same as `each.key`.)"),
12061206
Range: hcl.Range{
12071207
Filename: "test.tf",
1208-
Start: hcl.Pos{Line: 3, Column: 13, Byte: 52},
1209-
End: hcl.Pos{Line: 6, Column: 3, Byte: 83},
1208+
Start: hcl.Pos{Line: 2, Column: 8, Byte: 29},
1209+
End: hcl.Pos{Line: 2, Column: 18, Byte: 39},
12101210
},
12111211
},
12121212
},

0 commit comments

Comments
 (0)