-
Notifications
You must be signed in to change notification settings - Fork 24
/
expr_any_conditional.go
133 lines (113 loc) · 4.37 KB
/
expr_any_conditional.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
// 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/reference"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
)
func (a Any) completeConditionalExprAtPos(ctx context.Context, pos hcl.Pos) ([]lang.Candidate, bool) {
candidates := make([]lang.Candidate, 0)
switch eType := a.expr.(type) {
case *hclsyntax.ConditionalExpr:
if eType.Condition.Range().ContainsPos(pos) || eType.Condition.Range().End.Byte == pos.Byte {
cons := schema.AnyExpression{
OfType: cty.Bool,
}
return newExpression(a.pathCtx, eType.Condition, cons).CompletionAtPos(ctx, pos), true
}
if eType.TrueResult.Range().ContainsPos(pos) || eType.TrueResult.Range().End.Byte == pos.Byte {
cons := schema.AnyExpression{
OfType: cty.DynamicPseudoType,
}
return newExpression(a.pathCtx, eType.TrueResult, cons).CompletionAtPos(ctx, pos), true
}
if eType.FalseResult.Range().ContainsPos(pos) || eType.FalseResult.Range().End.Byte == pos.Byte {
cons := schema.AnyExpression{
OfType: cty.DynamicPseudoType,
}
return newExpression(a.pathCtx, eType.FalseResult, cons).CompletionAtPos(ctx, pos), true
}
return candidates, false
}
return candidates, true
}
func (a Any) hoverConditionalExprAtPos(ctx context.Context, pos hcl.Pos) (*lang.HoverData, bool) {
switch eType := a.expr.(type) {
case *hclsyntax.ConditionalExpr:
if eType.Condition.Range().ContainsPos(pos) {
cons := schema.AnyExpression{
OfType: cty.Bool,
}
return newExpression(a.pathCtx, eType.Condition, cons).HoverAtPos(ctx, pos), true
}
if eType.TrueResult.Range().ContainsPos(pos) {
cons := schema.AnyExpression{
OfType: cty.DynamicPseudoType,
}
return newExpression(a.pathCtx, eType.TrueResult, cons).HoverAtPos(ctx, pos), true
}
if eType.FalseResult.Range().ContainsPos(pos) {
cons := schema.AnyExpression{
OfType: cty.DynamicPseudoType,
}
return newExpression(a.pathCtx, eType.FalseResult, cons).HoverAtPos(ctx, pos), true
}
}
return nil, false
}
func (a Any) refOriginsForConditionalExpr(ctx context.Context, allowSelfRefs bool) (reference.Origins, bool) {
origins := make(reference.Origins, 0)
// There is currently no way of decoding conditional expressions in JSON
// so we just collect them using the fallback logic assuming "any"
// constraint and focus on collecting expressions in HCL with more
// accurate constraints below.
switch eType := a.expr.(type) {
case *hclsyntax.ConditionalExpr:
condExpr := newExpression(a.pathCtx, eType.Condition, schema.AnyExpression{
OfType: cty.Bool,
})
if expr, ok := condExpr.(ReferenceOriginsExpression); ok {
origins = append(origins, expr.ReferenceOrigins(ctx, allowSelfRefs)...)
}
trueExpr := newExpression(a.pathCtx, eType.TrueResult, schema.AnyExpression{
OfType: a.cons.OfType,
SkipLiteralComplexTypes: a.cons.SkipLiteralComplexTypes,
})
if expr, ok := trueExpr.(ReferenceOriginsExpression); ok {
origins = append(origins, expr.ReferenceOrigins(ctx, allowSelfRefs)...)
}
falseExpr := newExpression(a.pathCtx, eType.FalseResult, schema.AnyExpression{
OfType: a.cons.OfType,
SkipLiteralComplexTypes: a.cons.SkipLiteralComplexTypes,
})
if expr, ok := falseExpr.(ReferenceOriginsExpression); ok {
origins = append(origins, expr.ReferenceOrigins(ctx, allowSelfRefs)...)
}
return origins, true
}
return origins, false
}
func (a Any) semanticTokensForConditionalExpr(ctx context.Context) ([]lang.SemanticToken, bool) {
tokens := make([]lang.SemanticToken, 0)
switch eType := a.expr.(type) {
case *hclsyntax.ConditionalExpr:
tokens = append(tokens, newExpression(a.pathCtx, eType.Condition, schema.AnyExpression{
OfType: cty.Bool,
}).SemanticTokens(ctx)...)
tokens = append(tokens, newExpression(a.pathCtx, eType.TrueResult, schema.AnyExpression{
OfType: a.cons.OfType,
SkipLiteralComplexTypes: a.cons.SkipLiteralComplexTypes,
}).SemanticTokens(ctx)...)
tokens = append(tokens, newExpression(a.pathCtx, eType.FalseResult, schema.AnyExpression{
OfType: a.cons.OfType,
SkipLiteralComplexTypes: a.cons.SkipLiteralComplexTypes,
}).SemanticTokens(ctx)...)
return tokens, true
}
return tokens, false
}