Skip to content

Commit

Permalink
Create separate path contexts for Stack and Deploy
Browse files Browse the repository at this point in the history
Each context needs its own set of references and files.
  • Loading branch information
dbanck authored and ansgarm committed Jul 4, 2024
1 parent ad0adef commit c4ef3a6
Showing 1 changed file with 50 additions and 29 deletions.
79 changes: 50 additions & 29 deletions internal/features/stacks/decoder/path_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)

Check failure on line 48 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

undefined: stackschema.CoreStackSchemaForVersion

Check failure on line 48 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / test (windows-latest)

undefined: stackschema.CoreStackSchemaForVersion

Check failure on line 48 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

undefined: stackschema.CoreStackSchemaForVersion
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)

Check failure on line 72 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

undefined: stackschema.CoreDeploySchemaForVersion

Check failure on line 72 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / test (windows-latest)

undefined: stackschema.CoreDeploySchemaForVersion

Check failure on line 72 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

undefined: stackschema.CoreDeploySchemaForVersion
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
}
Expand All @@ -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
Expand Down

0 comments on commit c4ef3a6

Please sign in to comment.