-
Notifications
You must be signed in to change notification settings - Fork 131
/
decoder.go
105 lines (90 loc) · 3 KB
/
decoder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package decoder
import (
"context"
"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform-ls/internal/codelens"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
"github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/terraform/ast"
tfschema "github.com/hashicorp/terraform-schema/schema"
)
func NewDecoder(ctx context.Context, pathReader decoder.PathReader) *decoder.Decoder {
d := decoder.NewDecoder(pathReader)
d.SetContext(decoderContext(ctx))
return d
}
func modulePathContext(mod *state.Module, schemaReader state.SchemaReader, modReader ModuleReader) (*decoder.PathContext, error) {
schema, err := schemaForModule(mod, schemaReader, modReader)
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),
}
for _, origin := range mod.RefOrigins {
if ast.IsModuleFilename(origin.OriginRange().Filename) {
pathCtx.ReferenceOrigins = append(pathCtx.ReferenceOrigins, origin)
}
}
for _, target := range mod.RefTargets {
if target.RangePtr != nil && ast.IsModuleFilename(target.RangePtr.Filename) {
pathCtx.ReferenceTargets = append(pathCtx.ReferenceTargets, target)
} else if target.RangePtr == nil {
pathCtx.ReferenceTargets = append(pathCtx.ReferenceTargets, target)
}
}
for name, f := range mod.ParsedModuleFiles {
pathCtx.Files[name.String()] = f
}
return pathCtx, nil
}
func varsPathContext(mod *state.Module) (*decoder.PathContext, error) {
schema, err := tfschema.SchemaForVariables(mod.Meta.Variables)
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),
}
for _, origin := range mod.RefOrigins {
if ast.IsVarsFilename(origin.OriginRange().Filename) {
pathCtx.ReferenceOrigins = append(pathCtx.ReferenceOrigins, origin)
}
}
for _, target := range mod.RefTargets {
if target.RangePtr != nil && ast.IsVarsFilename(target.RangePtr.Filename) {
pathCtx.ReferenceTargets = append(pathCtx.ReferenceTargets, target)
}
}
for name, f := range mod.ParsedVarsFiles {
pathCtx.Files[name.String()] = f
}
return pathCtx, nil
}
func decoderContext(ctx context.Context) decoder.DecoderContext {
dCtx := decoder.DecoderContext{
UtmSource: "terraform-ls",
UseUtmContent: true,
}
cc, err := ilsp.ClientCapabilities(ctx)
if err == nil {
cmdId, ok := lsp.ExperimentalClientCapabilities(cc.Experimental).ShowReferencesCommandId()
if ok {
dCtx.CodeLenses = append(dCtx.CodeLenses, codelens.ReferenceCount(cmdId))
}
}
clientName, ok := ilsp.ClientName(ctx)
if ok {
dCtx.UtmMedium = clientName
}
return dCtx
}