diff --git a/go.mod b/go.mod index 081ddd8e..25d50107 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index ff23cee8..8c6d5d83 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/langserver/handlers/code_lens_test.go b/internal/langserver/handlers/code_lens_test.go index 41269eee..18266d90 100644 --- a/internal/langserver/handlers/code_lens_test.go +++ b/internal/langserver/handlers/code_lens_test.go @@ -184,7 +184,7 @@ output "test" { "arguments": [ { "line": 0, - "character": 8 + "character": 7 }, { "includeDeclaration": false diff --git a/internal/langserver/handlers/workspace_symbol.go b/internal/langserver/handlers/workspace_symbol.go index 9baee5f8..86a9fbfb 100644 --- a/internal/langserver/handlers/workspace_symbol.go +++ b/internal/langserver/handlers/workspace_symbol.go @@ -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 } @@ -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 } @@ -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 diff --git a/internal/terraform/ast/module.go b/internal/terraform/ast/module.go index 5f49b128..3f3814a5 100644 --- a/internal/terraform/ast/module.go +++ b/internal/terraform/ast/module.go @@ -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 diff --git a/internal/terraform/parser/module.go b/internal/terraform/parser/module.go index 4b388ed2..27ff6cda 100644 --- a/internal/terraform/parser/module.go +++ b/internal/terraform/parser/module.go @@ -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" ) @@ -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 diff --git a/internal/terraform/parser/parser.go b/internal/terraform/parser/parser.go index 8be1ca4d..55652314 100644 --- a/internal/terraform/parser/parser.go +++ b/internal/terraform/parser/parser.go @@ -2,6 +2,10 @@ 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 { @@ -9,3 +13,15 @@ type FS interface { 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) +}