-
Notifications
You must be signed in to change notification settings - Fork 24
/
expr_any_semtok.go
147 lines (120 loc) · 3.19 KB
/
expr_any_semtok.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package decoder
import (
"context"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2/hclsyntax"
)
func (a Any) SemanticTokens(ctx context.Context) []lang.SemanticToken {
typ := a.cons.OfType
if typ.IsListType() {
expr, ok := a.expr.(*hclsyntax.TupleConsExpr)
if !ok {
return a.semanticTokensForNonComplexExpr(ctx)
}
cons := schema.List{
Elem: schema.AnyExpression{
OfType: typ.ElementType(),
},
}
return newExpression(a.pathCtx, expr, cons).SemanticTokens(ctx)
}
if typ.IsSetType() {
expr, ok := a.expr.(*hclsyntax.TupleConsExpr)
if !ok {
return a.semanticTokensForNonComplexExpr(ctx)
}
cons := schema.Set{
Elem: schema.AnyExpression{
OfType: typ.ElementType(),
},
}
return newExpression(a.pathCtx, expr, cons).SemanticTokens(ctx)
}
if typ.IsTupleType() {
expr, ok := a.expr.(*hclsyntax.TupleConsExpr)
if !ok {
return a.semanticTokensForNonComplexExpr(ctx)
}
elemTypes := typ.TupleElementTypes()
cons := schema.Tuple{
Elems: make([]schema.Constraint, len(elemTypes)),
}
for i, elemType := range elemTypes {
cons.Elems[i] = schema.AnyExpression{
OfType: elemType,
}
}
return newExpression(a.pathCtx, expr, cons).SemanticTokens(ctx)
}
if typ.IsMapType() {
expr, ok := a.expr.(*hclsyntax.ObjectConsExpr)
if !ok {
return a.semanticTokensForNonComplexExpr(ctx)
}
cons := schema.Map{
Elem: schema.AnyExpression{
OfType: typ.ElementType(),
},
AllowInterpolatedKeys: true,
}
return newExpression(a.pathCtx, expr, cons).SemanticTokens(ctx)
}
if typ.IsObjectType() {
expr, ok := a.expr.(*hclsyntax.ObjectConsExpr)
if !ok {
return a.semanticTokensForNonComplexExpr(ctx)
}
cons := schema.Object{
Attributes: ctyObjectToObjectAttributes(typ),
AllowInterpolatedKeys: true,
}
return newExpression(a.pathCtx, expr, cons).SemanticTokens(ctx)
}
return a.semanticTokensForNonComplexExpr(ctx)
}
func (a Any) semanticTokensForNonComplexExpr(ctx context.Context) []lang.SemanticToken {
// TODO: Support splat expression https://github.com/hashicorp/terraform-ls/issues/526
// TODO: Support relative traversals https://github.com/hashicorp/terraform-ls/issues/532
if tokens, ok := a.semanticTokensForOperatorExpr(ctx); ok {
return tokens
}
if tokens, ok := a.semanticTokensForTemplateExpr(ctx); ok {
return tokens
}
if tokens, ok := a.semanticTokensForConditionalExpr(ctx); ok {
return tokens
}
if tokens, ok := a.semanticTokensForForExpr(ctx); ok {
return tokens
}
if tokens, ok := a.semanticTokensForIndexExpr(ctx); ok {
return tokens
}
ref := Reference{
expr: a.expr,
cons: schema.Reference{OfType: a.cons.OfType},
pathCtx: a.pathCtx,
}
tokens := ref.SemanticTokens(ctx)
if len(tokens) > 0 {
return tokens
}
fe := functionExpr{
expr: a.expr,
returnType: a.cons.OfType,
pathCtx: a.pathCtx,
}
tokens = fe.SemanticTokens(ctx)
if len(tokens) > 0 {
return tokens
}
lt := LiteralType{
expr: a.expr,
cons: schema.LiteralType{Type: a.cons.OfType},
pathCtx: a.pathCtx,
}
return lt.SemanticTokens(ctx)
}