Skip to content

Commit

Permalink
Parse *.tf.json for references and symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Oct 14, 2021
1 parent 5b1d9f4 commit dbb155e
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 10 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/hashicorp/go-memdb v1.3.2
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-version v1.3.0
github.com/hashicorp/hcl-lang v0.0.0-20211007132635-f22d3c2adf6c
github.com/hashicorp/hcl-lang v0.0.0-20211014152429-0bfbdcca0902
github.com/hashicorp/hcl/v2 v2.10.1
github.com/hashicorp/terraform-exec v0.15.0
github.com/hashicorp/terraform-json v0.13.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uG
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl-lang v0.0.0-20210803155453-7c098e4940bc/go.mod h1:xzXU6Fn+TWVaZUFxV8CyAsObi2oMgSEFAmLvCx2ArzM=
github.com/hashicorp/hcl-lang v0.0.0-20211007132635-f22d3c2adf6c h1:98h0kdFx2qqS8lMAbGxR+b/HO3d52NdJsIgzG6Ikucg=
github.com/hashicorp/hcl-lang v0.0.0-20211007132635-f22d3c2adf6c/go.mod h1:D7lBT7dekCcgbxzIHHBFvaRm42u5jY0pDoiC2J6A2KM=
github.com/hashicorp/hcl-lang v0.0.0-20211014152429-0bfbdcca0902 h1:FxmNMZrjISkvGoXdoySct4dnk40KwhspPj9LvAaPPJo=
github.com/hashicorp/hcl-lang v0.0.0-20211014152429-0bfbdcca0902/go.mod h1:D7lBT7dekCcgbxzIHHBFvaRm42u5jY0pDoiC2J6A2KM=
github.com/hashicorp/hcl/v2 v2.10.1 h1:h4Xx4fsrRE26ohAk/1iGF/JBqRQbyUqu5Lvj60U54ys=
github.com/hashicorp/hcl/v2 v2.10.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
Expand Down
2 changes: 1 addition & 1 deletion internal/langserver/handlers/code_lens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ output "test" {
"arguments": [
{
"line": 0,
"character": 8
"character": 7
},
{
"includeDeclaration": false
Expand Down
7 changes: 5 additions & 2 deletions internal/langserver/handlers/workspace_symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func (h *logHandler) WorkspaceSymbol(ctx context.Context, params lsp.WorkspaceSymbolParams) ([]lsp.SymbolInformation, error) {
var symbols []lsp.SymbolInformation

mm, err := lsctx.ModuleFinder(ctx)
mf, err := lsctx.ModuleFinder(ctx)
if err != nil {
return symbols, err
}
Expand All @@ -22,7 +22,7 @@ func (h *logHandler) WorkspaceSymbol(ctx context.Context, params lsp.WorkspaceSy
return nil, err
}

modules, err := mm.ListModules()
modules, err := mf.ListModules()
if err != nil {
return nil, err
}
Expand All @@ -33,6 +33,9 @@ func (h *logHandler) WorkspaceSymbol(ctx context.Context, params lsp.WorkspaceSy
return symbols, err
}

schema, _ := mf.SchemaForModule(mod.Path)
d.SetSchema(schema)

modSymbols, err := d.Symbols(params.Query)
if err != nil {
continue
Expand Down
8 changes: 7 additions & 1 deletion internal/terraform/ast/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ func (mf ModFilename) String() string {
return string(mf)
}

func (mf ModFilename) IsJSON() bool {
return strings.HasSuffix(string(mf), ".json")
}

func IsModuleFilename(name string) bool {
return strings.HasSuffix(name, ".tf") && !isIgnoredFile(name)
return (strings.HasSuffix(name, ".tf") ||
strings.HasSuffix(name, ".tf.json")) &&
!isIgnoredFile(name)
}

type ModFiles map[ModFilename]*hcl.File
Expand Down
6 changes: 3 additions & 3 deletions internal/terraform/parser/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package parser
import (
"path/filepath"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/terraform-ls/internal/terraform/ast"
)

Expand Down Expand Up @@ -37,8 +35,10 @@ func ParseModuleFiles(fs FS, modPath string) (ast.ModFiles, ast.ModDiags, error)
return nil, nil, err
}

f, pDiags := hclsyntax.ParseConfig(src, name, hcl.InitialPos)
filename := ast.ModFilename(name)

f, pDiags := parseFile(src, filename)

diags[filename] = pDiags
if f != nil {
files[filename] = f
Expand Down
16 changes: 16 additions & 0 deletions internal/terraform/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,26 @@ package parser

import (
"io/fs"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/json"
)

type FS interface {
fs.FS
ReadDir(name string) ([]fs.DirEntry, error)
ReadFile(name string) ([]byte, error)
}

type filename interface {
IsJSON() bool
String() string
}

func parseFile(src []byte, filename filename) (*hcl.File, hcl.Diagnostics) {
if filename.IsJSON() {
return json.Parse(src, filename.String())
}
return hclsyntax.ParseConfig(src, filename.String(), hcl.InitialPos)
}

0 comments on commit dbb155e

Please sign in to comment.