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

decoder: Add support for Reference as Constraint #185

Merged
merged 6 commits into from
Mar 24, 2023
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
34 changes: 3 additions & 31 deletions decoder/expr_reference.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,12 @@
package decoder

import (
"context"

"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
)

type Reference struct {
expr hcl.Expression
cons schema.Reference
}

func (ref Reference) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
// TODO
return nil
}

func (ref Reference) HoverAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverData {
// TODO
return nil
}

func (ref Reference) SemanticTokens(ctx context.Context) []lang.SemanticToken {
// TODO
return nil
}

func (ref Reference) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference.Origins {
// TODO
return nil
}

func (ref Reference) ReferenceTargets(ctx context.Context, targetCtx *TargetContext) reference.Targets {
// TODO
return nil
expr hcl.Expression
cons schema.Reference
pathCtx *PathContext
}
99 changes: 99 additions & 0 deletions decoder/expr_reference_completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package decoder

import (
"context"

"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
)

func (ref Reference) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
if ref.cons.Address != nil {
// no candidates if traversal itself is addressable
return []lang.Candidate{}
}

if ref.pathCtx.ReferenceTargets == nil {
return []lang.Candidate{}
}

file := ref.pathCtx.Files[ref.expr.Range().Filename]
rootBody, ok := file.Body.(*hclsyntax.Body)
if !ok {
return []lang.Candidate{}
}

outerBodyRng := rootBody.Range()
// Find outer block body range to allow filtering
// of references pointing back to the same block
outerBlock := rootBody.OutermostBlockAtPos(pos)
if outerBlock != nil {
ob := outerBlock.Body.(*hclsyntax.Body)
outerBodyRng = ob.Range()
}

if isEmptyExpression(ref.expr) {
editRng := hcl.Range{
Filename: ref.expr.Range().Filename,
Start: pos,
End: pos,
}
candidates := make([]lang.Candidate, 0)
ref.pathCtx.ReferenceTargets.MatchWalk(ctx, ref.cons, "", outerBodyRng, editRng, func(target reference.Target) error {
address := target.Address(ctx, editRng.Start).String()

candidates = append(candidates, lang.Candidate{
Label: address,
Detail: target.FriendlyName(),
Description: target.Description,
Kind: lang.TraversalCandidateKind,
TextEdit: lang.TextEdit{
NewText: address,
Snippet: address,
Range: editRng,
},
})
return nil
})
return candidates
}

eType, ok := ref.expr.(*hclsyntax.ScopeTraversalExpr)
if !ok {
return []lang.Candidate{}
}

editRng := eType.Range()
if !editRng.ContainsPos(pos) {
// account for trailing character(s) which doesn't appear in AST
// such as dot, opening bracket etc.
editRng.End = pos
}
prefixRng := hcl.Range{
Filename: eType.Range().Filename,
Start: eType.Range().Start,
End: pos,
}
prefix := string(prefixRng.SliceBytes(file.Bytes))

candidates := make([]lang.Candidate, 0)
ref.pathCtx.ReferenceTargets.MatchWalk(ctx, ref.cons, prefix, outerBodyRng, editRng, func(target reference.Target) error {
address := target.Address(ctx, editRng.Start).String()

candidates = append(candidates, lang.Candidate{
Label: address,
Detail: target.FriendlyName(),
Description: target.Description,
Kind: lang.TraversalCandidateKind,
TextEdit: lang.TextEdit{
NewText: address,
Snippet: address,
Range: editRng,
},
})
return nil
})
return candidates
}
Loading