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 AnyExpression as Constraint #232

Merged
merged 37 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
69814df
lang: Add new FunctionCandidateKind
dbanck Mar 15, 2023
443b6bb
decoder: Implement completion for Any
dbanck Mar 15, 2023
8a69f38
decoder: Implement hover for Any
dbanck Mar 15, 2023
a2863b9
decoder: Implement semantic tokens for Any
dbanck Mar 15, 2023
8ce9322
decoder: Implement reference origins for Any
dbanck Mar 15, 2023
a2ddf05
decoder: Implement reference targets for Any
dbanck Mar 15, 2023
15c2651
decoder: Test completion for Any
dbanck Mar 15, 2023
35b12df
decoder: Test hover for Any
dbanck Mar 15, 2023
3254aaf
decoder: Implement functionExpr (completion+hover+semtok+origins+targ…
radeksimko Apr 3, 2023
24300d4
reference: Refactor TraversalToLocalOrigin to accomodate AnyExpression
radeksimko Apr 3, 2023
090284b
decoder: Implement completion for AnyExpression
radeksimko Apr 3, 2023
721fa94
decoder: Implement hover for AnyExpression
radeksimko Apr 3, 2023
e00d6f8
decoder: Implement semantic tokens for AnyExpression
radeksimko Apr 3, 2023
fe14f25
decoder: Implement reference origins for AnyExpression
radeksimko Apr 3, 2023
5e67d86
decoder: Implement reference targets for AnyExpression
radeksimko Apr 3, 2023
0503b17
decoder: Account for incomplete expressions in functionExpr args
radeksimko Apr 3, 2023
4f0a6fd
decoder: Fix return type comparison in functionExpr
radeksimko Apr 3, 2023
70e9187
decoder: Add partial arg test for functionExpr
radeksimko Apr 3, 2023
63ba8d3
decoder: Add comment to clarify reason for condition
radeksimko Apr 3, 2023
2fb6cc9
decoder: Implement InferType() for Any
radeksimko Apr 3, 2023
9c744a1
decoder: Report function names with appropriate token type
radeksimko Apr 4, 2023
2e12db4
decoder: Account for traversal trailing dot in functionExpr argument
radeksimko Apr 4, 2023
73a54e1
decoder: Fix reporting of semantic tokens for functionExpr
radeksimko Apr 4, 2023
9f763e5
decoder: Test semantic tokens for functions
dbanck Apr 4, 2023
e7a6667
decoder: Extend hover tests for functions
dbanck Apr 4, 2023
b80119d
decoder: Remove TODOs (follow-up issues will be filed)
radeksimko Apr 4, 2023
c82e3fa
decoder: add test for mismatching argument in semtok (Any)
radeksimko Apr 4, 2023
5feeaa6
decoder: add test for mismatching argument in hover (Any)
radeksimko Apr 4, 2023
250321c
decoder: Add remaining tests for Any
radeksimko Apr 4, 2023
352c5e1
decoder: Add literal type hover tests for Any
radeksimko Apr 4, 2023
fbb9361
decoder: Add reference hover tests for Any
dbanck Apr 4, 2023
1266df0
decoder: Add semantic token tests for Any
dbanck Apr 4, 2023
6f40469
decoder: Return early for function calls without references
dbanck Apr 4, 2023
4dd2044
decoder: Test collection for reference targets for Any
dbanck Apr 4, 2023
84d0789
decoder: Test collection for reference origins for Any
dbanck Apr 4, 2023
9ed053d
decoder: Fix AnyExpression Map completion tests
dbanck Apr 6, 2023
f2c8ee0
Fix duplicate completion entries for complex types
dbanck Apr 6, 2023
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
38 changes: 13 additions & 25 deletions decoder/expr_any.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
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"
"github.com/zclconf/go-cty/cty"
)

type Any struct {
Expand All @@ -18,27 +15,18 @@ type Any struct {
pathCtx *PathContext
}

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

func (a Any) HoverAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverData {
// TODO
return nil
}
func (a Any) InferType() (cty.Type, bool) {
consType, ok := a.cons.ConstraintType()
if !ok {
return consType, false
}

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

func (a Any) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference.Origins {
// TODO
return nil
}
if consType == cty.DynamicPseudoType && !isEmptyExpression(a.expr) {
val, diags := a.expr.Value(nil)
if !diags.HasErrors() {
consType = val.Type()
}
}

func (a Any) ReferenceTargets(ctx context.Context, targetCtx *TargetContext) reference.Targets {
// TODO
return nil
return consType, true
}
129 changes: 129 additions & 0 deletions decoder/expr_any_completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package decoder

import (
"context"

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

func (a Any) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
typ := a.cons.OfType

if !a.cons.SkipLiteralComplexTypes && typ.IsListType() {
expr, ok := a.expr.(*hclsyntax.TupleConsExpr)
if !ok {
return a.completeNonComplexExprAtPos(ctx, pos)
}

cons := schema.List{
Elem: schema.AnyExpression{
OfType: typ.ElementType(),
},
}

return newExpression(a.pathCtx, expr, cons).CompletionAtPos(ctx, pos)
}

if !a.cons.SkipLiteralComplexTypes && typ.IsSetType() {
expr, ok := a.expr.(*hclsyntax.TupleConsExpr)
if !ok {
return a.completeNonComplexExprAtPos(ctx, pos)
}

cons := schema.Set{
Elem: schema.AnyExpression{
OfType: typ.ElementType(),
},
}

return newExpression(a.pathCtx, expr, cons).CompletionAtPos(ctx, pos)
}

if !a.cons.SkipLiteralComplexTypes && typ.IsTupleType() {
expr, ok := a.expr.(*hclsyntax.TupleConsExpr)
if !ok {
return a.completeNonComplexExprAtPos(ctx, pos)
}

elemTypes := typ.TupleElementTypes()
cons := schema.Tuple{
Elems: make([]schema.Constraint, len(elemTypes)),
}
for i, elemType := range elemTypes {
cons.Elems[i] = schema.LiteralType{
Type: elemType,
}
}

return newExpression(a.pathCtx, expr, cons).CompletionAtPos(ctx, pos)
}

if !a.cons.SkipLiteralComplexTypes && typ.IsMapType() {
expr, ok := a.expr.(*hclsyntax.ObjectConsExpr)
if !ok {
return a.completeNonComplexExprAtPos(ctx, pos)
}

cons := schema.Map{
Elem: schema.AnyExpression{
OfType: typ.ElementType(),
},
}
return newExpression(a.pathCtx, expr, cons).CompletionAtPos(ctx, pos)
}

if !a.cons.SkipLiteralComplexTypes && typ.IsObjectType() {
expr, ok := a.expr.(*hclsyntax.ObjectConsExpr)
if !ok {
return a.completeNonComplexExprAtPos(ctx, pos)
}

cons := schema.Object{
Attributes: ctyObjectToObjectAttributes(typ),
}
return newExpression(a.pathCtx, expr, cons).CompletionAtPos(ctx, pos)
}

return a.completeNonComplexExprAtPos(ctx, pos)
}

func (a Any) completeNonComplexExprAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
candidates := make([]lang.Candidate, 0)

// TODO: Support TemplateExpr https://github.com/hashicorp/terraform-ls/issues/522
// TODO: Support splat expression https://github.com/hashicorp/terraform-ls/issues/526
// TODO: Support for-in-if expression https://github.com/hashicorp/terraform-ls/issues/527
// TODO: Support conditional expression https://github.com/hashicorp/terraform-ls/issues/528
// TODO: Support operator expresssions https://github.com/hashicorp/terraform-ls/issues/529
// TODO: Support complex index expressions https://github.com/hashicorp/terraform-ls/issues/531
// TODO: Support relative traversals https://github.com/hashicorp/terraform-ls/issues/532

ref := Reference{
expr: a.expr,
cons: schema.Reference{OfType: a.cons.OfType},
pathCtx: a.pathCtx,
}
candidates = append(candidates, ref.CompletionAtPos(ctx, pos)...)

fe := functionExpr{
expr: a.expr,
returnType: a.cons.OfType,
pathCtx: a.pathCtx,
}
candidates = append(candidates, fe.CompletionAtPos(ctx, pos)...)

lt := LiteralType{
expr: a.expr,
cons: schema.LiteralType{
Type: a.cons.OfType,
SkipComplexTypes: a.cons.SkipLiteralComplexTypes,
},
pathCtx: a.pathCtx,
}
candidates = append(candidates, lt.CompletionAtPos(ctx, pos)...)

return candidates
}
Loading