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 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
3 changes: 2 additions & 1 deletion decoder/expr_any_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func (a Any) completeNonComplexExprAtPos(ctx context.Context, pos hcl.Pos) []lan

// 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 complex index expressions https://github.com/hashicorp/terraform-ls/issues/531
// TODO: Support relative traversals https://github.com/hashicorp/terraform-ls/issues/532

opCandidates, ok := a.completeOperatorExprAtPos(ctx, pos)
Expand Down Expand Up @@ -143,5 +142,7 @@ func (a Any) completeNonComplexExprAtPos(ctx context.Context, pos hcl.Pos) []lan
}
candidates = append(candidates, lt.CompletionAtPos(ctx, pos)...)

candidates = append(candidates, a.completeIndexExprAtPos(ctx, pos)...)

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
5 changes: 4 additions & 1 deletion decoder/expr_any_hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ func (a Any) HoverAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverData {
func (a Any) hoverNonComplexExprAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverData {
// 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 complex index expressions https://github.com/hashicorp/terraform-ls/issues/531
// TODO: Support relative traversals https://github.com/hashicorp/terraform-ls/issues/532

if hoverData, ok := a.hoverOperatorExprAtPos(ctx, pos); ok {
Expand All @@ -111,6 +110,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
73 changes: 73 additions & 0 deletions decoder/expr_any_index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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) completeIndexExprAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate {
var candidates []lang.Candidate

cons := schema.AnyExpression{
// TODO we could improve this by looking up the type of the
// referenced collection. For example, if it's a list, we
// could use a number type. But since number and strings
// are convertible both ways, it shouldn't matter too much.
OfType: cty.String,
}

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 {
expr := newEmptyExpressionAtPos(eType.Range().Filename, pos)
return newExpression(a.pathCtx, expr, cons).CompletionAtPos(ctx, pos)
}
// If there is a prefix or valid expression within the index step,
// we're dealing with an index expression and can defer completion for the key.
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 (see above)
}
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 (see above)
}
return newExpression(a.pathCtx, eType.Key, cons).SemanticTokens(ctx), true
}

return nil, false
}
Loading