diff --git a/internal/features/stacks/decoder/path_reader.go b/internal/features/stacks/decoder/path_reader.go index 4056fc8d1..6395f077b 100644 --- a/internal/features/stacks/decoder/path_reader.go +++ b/internal/features/stacks/decoder/path_reader.go @@ -5,14 +5,14 @@ package decoder import ( "context" + "fmt" "github.com/hashicorp/hcl-lang/decoder" "github.com/hashicorp/hcl-lang/lang" "github.com/hashicorp/hcl-lang/reference" - "github.com/hashicorp/hcl-lang/schema" "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-ls/internal/features/stacks/state" - "github.com/hashicorp/terraform-ls/internal/lsp" + ilsp "github.com/hashicorp/terraform-ls/internal/lsp" stackschema "github.com/hashicorp/terraform-schema/schema" ) @@ -25,51 +25,64 @@ type StateReader interface { StackRecordByPath(modPath string) (*state.StackRecord, error) } -// PathContext returns a Stacks PathContext for the given path +// PathContext returns a PathContext for the given path based on the language ID func (pr *PathReader) PathContext(path lang.Path) (*decoder.PathContext, error) { record, err := pr.StateReader.StackRecordByPath(path.Path) if err != nil { return nil, err } - // get terrafom version from statereader and use that to get the schema + switch path.LanguageID { + case ilsp.Stacks.String(): + return stackPathContext(record) + case ilsp.Deploy.String(): + return deployPathContext(record) + } + + return nil, fmt.Errorf("unknown language ID: %q", path.LanguageID) +} - // TODO: This only provides tfstacks schema. There is also tfdeploy schema +func stackPathContext(record *state.StackRecord) (*decoder.PathContext, error) { + // TODO: get Terraform version from record and use that to get the schema // TODO: this should only work for terraform 1.8 and above - var schema *schema.BodySchema - switch path.LanguageID { - case lsp.Stacks.String(): - schema, _ = stackschema.CoreStackSchemaForVersion(stackschema.LatestAvailableVersion) - case lsp.Deploy.String(): - schema, _ = stackschema.CoreStackSchemaForVersion(stackschema.LatestAvailableVersion) + schema, err := stackschema.CoreStackSchemaForVersion(stackschema.LatestAvailableVersion) + if err != nil { + return nil, err } pathCtx := &decoder.PathContext{ - // Path: path, Schema: schema, ReferenceOrigins: make(reference.Origins, 0), ReferenceTargets: make(reference.Targets, 0), Files: make(map[string]*hcl.File, 0), } - // TODO: Add reference origins and targets - // for _, origin := range record.RefOrigins { - // if ast.IsStacksFilename(origin.OriginRange().Filename) { - // pathCtx.ReferenceOrigins = append(pathCtx.ReferenceOrigins, origin) - // } - // } - // for _, target := range record.RefTargets { - // if target.RangePtr != nil && ast.IsStacksFilename(target.RangePtr.Filename) { - // pathCtx.ReferenceTargets = append(pathCtx.ReferenceTargets, target) - // } else if target.RangePtr == nil { - // pathCtx.ReferenceTargets = append(pathCtx.ReferenceTargets, target) - // } - // } + // TODO: Add reference origins and targets if needed for name, f := range record.ParsedStackFiles { pathCtx.Files[name.String()] = f } + return pathCtx, nil +} + +func deployPathContext(record *state.StackRecord) (*decoder.PathContext, error) { + // TODO: get Terraform version from record and use that to get the schema + // TODO: this should only work for terraform 1.8 and above + schema, err := stackschema.CoreDeploySchemaForVersion(stackschema.LatestAvailableVersion) + if err != nil { + return nil, err + } + + pathCtx := &decoder.PathContext{ + Schema: schema, + ReferenceOrigins: make(reference.Origins, 0), + ReferenceTargets: make(reference.Targets, 0), + Files: make(map[string]*hcl.File, 0), + } + + // TODO: Add reference origins and targets if needed + for name, f := range record.ParsedDeployFiles { pathCtx.Files[name.String()] = f } @@ -86,10 +99,18 @@ func (pr *PathReader) Paths(ctx context.Context) []lang.Path { } for _, record := range stackRecords { - paths = append(paths, lang.Path{ - Path: record.Path(), - LanguageID: lsp.Stacks.String(), - }) + if len(record.ParsedStackFiles) > 0 { + paths = append(paths, lang.Path{ + Path: record.Path(), + LanguageID: ilsp.Stacks.String(), + }) + } + if len(record.ParsedDeployFiles) > 0 { + paths = append(paths, lang.Path{ + Path: record.Path(), + LanguageID: ilsp.Deploy.String(), + }) + } } return paths