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

Support complex index expressions #365

Merged
merged 7 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 2 additions & 0 deletions decoder/expr_any_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,7 @@ func (a Any) completeNonComplexExprAtPos(ctx context.Context, pos hcl.Pos) []lan
}
candidates = append(candidates, lt.CompletionAtPos(ctx, pos)...)

candidates = append(candidates, a.complexIndex(ctx, pos)...)
dbanck marked this conversation as resolved.
Show resolved Hide resolved
dbanck marked this conversation as resolved.
Show resolved Hide resolved

return candidates
}
91 changes: 90 additions & 1 deletion decoder/expr_any_completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ func TestCompletionAtPos_exprAny_functions(t *testing.T) {
Start: hcl.Pos{Line: 2, Column: 5, Byte: 27},
End: hcl.Pos{Line: 2, Column: 15, Byte: 37},
},
Type: cty.List(cty.String),
Type: cty.Map(cty.String),
dbanck marked this conversation as resolved.
Show resolved Hide resolved
NestedTargets: reference.Targets{
{
Addr: lang.Address{
Expand Down Expand Up @@ -560,6 +560,95 @@ func TestCompletionAtPos_exprAny_functions(t *testing.T) {
Snippet: `var.map["foo"]`,
},
},
{
Label: `var.map`,
dbanck marked this conversation as resolved.
Show resolved Hide resolved
Detail: "map of string",
Kind: lang.ReferenceCandidateKind,
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 22, Byte: 21},
End: hcl.Pos{Line: 1, Column: 22, Byte: 21},
},
NewText: `var.map`,
Snippet: `var.map`,
},
},
{
Label: "element",
Detail: "element(list dynamic, index number) dynamic",
Description: lang.Markdown("`element` retrieves a single element from a list."),
Kind: lang.FunctionCandidateKind,
TextEdit: lang.TextEdit{
NewText: "element()",
Snippet: "element(${0})",
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 22, Byte: 21},
End: hcl.Pos{Line: 1, Column: 22, Byte: 21},
},
},
},
{
Label: "join",
Detail: "join(separator string, …lists list of string) string",
Description: lang.Markdown("`join` produces a string by concatenating together all elements of a given list of strings with the given delimiter."),
Kind: lang.FunctionCandidateKind,
TextEdit: lang.TextEdit{
NewText: "join()",
Snippet: "join(${0})",
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 22, Byte: 21},
End: hcl.Pos{Line: 1, Column: 22, Byte: 21},
},
},
},
{
Label: "keys",
Detail: "keys(inputMap dynamic) dynamic",
Description: lang.Markdown("`keys` takes a map and returns a list containing the keys from that map."),
Kind: lang.FunctionCandidateKind,
TextEdit: lang.TextEdit{
NewText: "keys()",
Snippet: "keys(${0})",
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 22, Byte: 21},
End: hcl.Pos{Line: 1, Column: 22, Byte: 21},
},
},
},
{
Label: "log",
Detail: "log(num number, base number) number",
Description: lang.Markdown("`log` returns the logarithm of a given number in a given base."),
Kind: lang.FunctionCandidateKind,
TextEdit: lang.TextEdit{
NewText: "log()",
Snippet: "log(${0})",
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 22, Byte: 21},
End: hcl.Pos{Line: 1, Column: 22, Byte: 21},
},
},
},
{
Label: "lower",
Detail: "lower(str string) string",
Description: lang.Markdown("`lower` converts all cased letters in the given string to lowercase."),
Kind: lang.FunctionCandidateKind,
TextEdit: lang.TextEdit{
NewText: "lower()",
Snippet: "lower(${0})",
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 22, Byte: 21},
End: hcl.Pos{Line: 1, Column: 22, Byte: 21},
},
},
},
}),
},
{
Expand Down
4 changes: 4 additions & 0 deletions decoder/expr_any_hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func (a Any) hoverNonComplexExprAtPos(ctx context.Context, pos hcl.Pos) *lang.Ho
return hoverData
}

if hoverData, ok := a.hoverIndexExprAtPos(ctx, pos); ok {
dbanck marked this conversation as resolved.
Show resolved Hide resolved
return hoverData
}

ref := Reference{
expr: a.expr,
cons: schema.Reference{OfType: a.cons.OfType},
Expand Down
77 changes: 77 additions & 0 deletions decoder/expr_any_index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

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"
"github.com/zclconf/go-cty/cty"
)

func (a Any) complexIndex(ctx context.Context, pos hcl.Pos) []lang.Candidate {
dbanck marked this conversation as resolved.
Show resolved Hide resolved
var candidates []lang.Candidate

cons := schema.AnyExpression{
OfType: cty.String, // TODO improve type (could be int)
dbanck marked this conversation as resolved.
Show resolved Hide resolved
}

switch eType := a.expr.(type) {
// An empty expression, e.g. `tags[]`, is a scope traversal expression
// with an empty index step.
case *hclsyntax.ScopeTraversalExpr:
if len(eType.Traversal) < 2 {
return candidates
}
// If the last part of the traversal is an index step,
// we start a new completion to enable completion of
// references and functions.
lastTraversal := eType.Traversal[len(eType.Traversal)-1]
if _, ok := lastTraversal.(hcl.TraverseIndex); ok {
editRange := hcl.Range{
Filename: eType.SrcRange.Filename,
Start: pos,
End: pos,
}
expr := &hclsyntax.LiteralValueExpr{
SrcRange: editRange,
Val: cty.UnknownVal(cty.DynamicPseudoType),
}
dbanck marked this conversation as resolved.
Show resolved Hide resolved
return newExpression(a.pathCtx, expr, cons).CompletionAtPos(ctx, pos)
}
// If there is a prefix or valid expression within the index step,
// we're dealing an index expression and can defer completion for the key.
dbanck marked this conversation as resolved.
Show resolved Hide resolved
case *hclsyntax.IndexExpr:
return newExpression(a.pathCtx, eType.Key, cons).CompletionAtPos(ctx, pos)
}

return candidates
}

func (a Any) hoverIndexExprAtPos(ctx context.Context, pos hcl.Pos) (*lang.HoverData, bool) {
if eType, ok := a.expr.(*hclsyntax.IndexExpr); ok {
if eType.Key.Range().ContainsPos(pos) {
cons := schema.AnyExpression{
OfType: cty.String, // TODO improve type (could be int)
}
return newExpression(a.pathCtx, eType.Key, cons).HoverAtPos(ctx, pos), true
}
}

return nil, false
}

func (a Any) semanticTokensForIndexExpr(ctx context.Context) ([]lang.SemanticToken, bool) {
if eType, ok := a.expr.(*hclsyntax.IndexExpr); ok {
cons := schema.AnyExpression{
OfType: cty.String, // TODO improve type (could be int)
}
return newExpression(a.pathCtx, eType.Key, cons).SemanticTokens(ctx), true
}

return nil, false
}
Loading