Skip to content

Commit

Permalink
decoder: Account for attribute (Def)Ranges in Object/Map
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Mar 22, 2023
1 parent 71ae3ab commit 9ce64af
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
5 changes: 5 additions & 0 deletions decoder/expr_map_ref_targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
)
Expand Down Expand Up @@ -39,6 +40,10 @@ func (m Map) ReferenceTargets(ctx context.Context, targetCtx *TargetContext) ref
}

elemCtx := targetCtx.Copy()

elemCtx.ParentDefRangePtr = item.KeyExpr.Range().Ptr()
elemCtx.ParentRangePtr = hcl.RangeBetween(item.KeyExpr.Range(), item.ValueExpr.Range()).Ptr()

elemCtx.ParentAddress = append(elemCtx.ParentAddress, lang.IndexStep{
Key: cty.StringVal(keyName),
})
Expand Down
19 changes: 15 additions & 4 deletions decoder/expr_object_ref_targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func (obj Object) ReferenceTargets(ctx context.Context, targetCtx *TargetContext
attrNames := sortedAttributeNames(obj.cons.Attributes)
for _, name := range attrNames {
var valueExpr hcl.Expression
item, ok := declaredAttributes[name]
if ok {
item, attrDeclared := declaredAttributes[name]
if attrDeclared {
valueExpr = item.ValueExpr
} else {
valueExpr = newEmptyExpressionAtPos(eType.Range().Filename, eType.Range().Start)
Expand All @@ -61,6 +61,11 @@ func (obj Object) ReferenceTargets(ctx context.Context, targetCtx *TargetContext

elemCtx := targetCtx.Copy()

if attrDeclared {
elemCtx.ParentDefRangePtr = item.KeyExpr.Range().Ptr()
elemCtx.ParentRangePtr = hcl.RangeBetween(item.KeyExpr.Range(), item.ValueExpr.Range()).Ptr()
}

if hclsyntax.ValidIdentifier(name) {
// Prefer simpler syntax - e.g. myobj.attribute if possible
elemCtx.ParentAddress = append(elemCtx.ParentAddress, lang.AttrStep{
Expand Down Expand Up @@ -93,6 +98,10 @@ func (obj Object) ReferenceTargets(ctx context.Context, targetCtx *TargetContext

if targetCtx != nil {
// collect target for the whole object
rangePtr := obj.expr.Range().Ptr()
if targetCtx.ParentRangePtr != nil {
rangePtr = targetCtx.ParentRangePtr
}

// type-aware
if targetCtx.AsExprType {
Expand All @@ -103,7 +112,8 @@ func (obj Object) ReferenceTargets(ctx context.Context, targetCtx *TargetContext
Name: targetCtx.FriendlyName,
Type: objType,
ScopeId: targetCtx.ScopeId,
RangePtr: obj.expr.Range().Ptr(),
DefRangePtr: targetCtx.ParentDefRangePtr,
RangePtr: rangePtr,
NestedTargets: attrTargets,
LocalAddr: targetCtx.ParentLocalAddress,
TargetableFromRangePtr: targetCtx.TargetableFromRangePtr,
Expand All @@ -117,7 +127,8 @@ func (obj Object) ReferenceTargets(ctx context.Context, targetCtx *TargetContext
Addr: targetCtx.ParentAddress,
Name: targetCtx.FriendlyName,
ScopeId: targetCtx.ScopeId,
RangePtr: obj.expr.Range().Ptr(),
DefRangePtr: targetCtx.ParentDefRangePtr,
RangePtr: rangePtr,
NestedTargets: attrTargets,
LocalAddr: targetCtx.ParentLocalAddress,
TargetableFromRangePtr: targetCtx.TargetableFromRangePtr,
Expand Down
5 changes: 5 additions & 0 deletions decoder/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ type TargetContext struct {
// TargetableFromRangePtr defines where the target is locally targetable
// from via the ParentLocalAddress.
TargetableFromRangePtr *hcl.Range

// TODO: Comment
ParentRangePtr *hcl.Range
// TODO: Comment
ParentDefRangePtr *hcl.Range
}

func (tctx *TargetContext) Copy() *TargetContext {
Expand Down
12 changes: 7 additions & 5 deletions decoder/reference_targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,13 @@ func (d *PathDecoder) decodeReferenceTargetsForAttribute(attr *hcl.Attribute, at
attrAddr, ok := resolveAttributeAddress(attr, attrSchema.Address.Steps)
if ok {
targetCtx = &TargetContext{
FriendlyName: attrSchema.Address.FriendlyName,
ScopeId: attrSchema.Address.ScopeId,
AsExprType: attrSchema.Address.AsExprType,
AsReference: attrSchema.Address.AsReference,
ParentAddress: attrAddr,
FriendlyName: attrSchema.Address.FriendlyName,
ScopeId: attrSchema.Address.ScopeId,
AsExprType: attrSchema.Address.AsExprType,
AsReference: attrSchema.Address.AsReference,
ParentAddress: attrAddr,
ParentRangePtr: attr.Range.Ptr(),
ParentDefRangePtr: attr.NameRange.Ptr(),
}
}
}
Expand Down

0 comments on commit 9ce64af

Please sign in to comment.