Skip to content

Commit

Permalink
Added local vars completion
Browse files Browse the repository at this point in the history
  • Loading branch information
juliosueiras committed Jul 6, 2019
1 parent a6e1c96 commit 8c4728a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
27 changes: 27 additions & 0 deletions hclstructs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,33 @@ func TraverseIndex() reflect.Type {
return GetType(hcl.TraverseIndex{})
}

func GetExprStringType(origType reflect.Type) string {

switch origType {
// May need recursion
case BinaryOpExpr():
return "binary operation"
case FunctionCallExpr():
return "function call"
case ScopeTraversalExpr():
return "scoped expression"
case LiteralValueExpr():
return "literal value"
case ForExpr():
return "for loop"
case TupleConsExpr():
return "array"
case TemplateWrapExpr():
return "string interpolation"
case ObjectConsExpr():
return "object"
default:
return "undefined"
}

return "undefined"
}

func GetExprVariables(origType reflect.Type, expr hcl.Expression, posHCL hcl.Pos) []hcl.Traversal {

switch origType {
Expand Down
1 change: 1 addition & 0 deletions tfstructs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func GetDataSourceSchema(dataSourceType string, config hcl.Body, targetDir strin
"data": cty.DynamicVal,
"var": cty.DynamicVal, // Need to check for undefined vars
"module": cty.DynamicVal,
"local": cty.DynamicVal,
},
Functions: scope.Functions(),
})
Expand Down
65 changes: 63 additions & 2 deletions tfstructs/vars.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package tfstructs

import (
"fmt"
"github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/hcl2/hcl/hclsyntax"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/configs"
"github.com/juliosueiras/terraform-lsp/hclstructs"
"github.com/juliosueiras/terraform-lsp/helper"
"github.com/sourcegraph/go-lsp"
"reflect"
)

type GetVarAttributeRequest struct {
Expand All @@ -17,12 +21,69 @@ type GetVarAttributeRequest struct {
}

func GetVarAttributeCompletion(request GetVarAttributeRequest) []lsp.CompletionItem {
helper.DumpLog("hi")
helper.DumpLog(request.Variables)
if request.Variables.RootName() == "var" {
vars := request.Variables

request.Result = helper.ParseVariables(vars[1:], request.Files.Variables, request.Result)
} else if request.Variables.RootName() == "local" {
if len(request.Variables) > 1 {
var found *configs.Local
for _, v := range request.Files.Locals {
if v.Name == request.Variables[1].(hcl.TraverseAttr).Name {
found = v
break
}
}

origType := reflect.TypeOf(found.Expr)

if origType == hclstructs.ObjectConsExpr() {
items := found.Expr.(*hclsyntax.ObjectConsExpr).Items
for _, v := range request.Variables[2:] {
for _, l := range items {
if v.(hcl.TraverseAttr).Name == l.KeyExpr.(*hclsyntax.ObjectConsKeyExpr).Wrapped.(*hclsyntax.ScopeTraversalExpr).AsTraversal().RootName() {
origType2 := reflect.TypeOf(l.ValueExpr)

if origType2 == hclstructs.ObjectConsExpr() {
items = l.ValueExpr.(*hclsyntax.ObjectConsExpr).Items
}
}
}
}

for _, v := range items {
origType2 := reflect.TypeOf(v.ValueExpr)
helper.DumpLog(v.KeyExpr.(*hclsyntax.ObjectConsKeyExpr).Wrapped.(*hclsyntax.ScopeTraversalExpr).AsTraversal().RootName())
request.Result = append(request.Result, lsp.CompletionItem{
Label: v.KeyExpr.(*hclsyntax.ObjectConsKeyExpr).Wrapped.(*hclsyntax.ScopeTraversalExpr).AsTraversal().RootName(),
Detail: fmt.Sprintf(" %s", hclstructs.GetExprStringType(origType2)),
})
}
}

return request.Result

} else if len(request.Variables) == 1 {
for _, v := range request.Files.Locals {
origType := reflect.TypeOf(v.Expr)
request.Result = append(request.Result, lsp.CompletionItem{
Label: v.Name,
Detail: fmt.Sprintf(" local value(%s)", hclstructs.GetExprStringType(origType)),
})
}

return request.Result
} else {
for _, v := range request.Files.Locals {
origType := reflect.TypeOf(v.Expr)
request.Result = append(request.Result, lsp.CompletionItem{
Label: v.Name,
Detail: fmt.Sprintf(" local value(%s)", hclstructs.GetExprStringType(origType)),
})
}

return request.Result
}
} else if request.Variables.RootName() == "data" {
// Need refactoring
if len(request.Variables) > 2 {
Expand Down

0 comments on commit 8c4728a

Please sign in to comment.