-
Notifications
You must be signed in to change notification settings - Fork 24
/
expr_type_declaration_semtok.go
133 lines (109 loc) · 3 KB
/
expr_type_declaration_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package decoder
import (
"context"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl/v2/hclsyntax"
)
func (td TypeDeclaration) SemanticTokens(ctx context.Context) []lang.SemanticToken {
switch eType := td.expr.(type) {
case *hclsyntax.ScopeTraversalExpr:
if len(eType.Traversal) != 1 {
return []lang.SemanticToken{}
}
if isPrimitiveTypeDeclaration(eType.Traversal.RootName()) {
return []lang.SemanticToken{
{
Type: lang.TokenTypePrimitive,
Modifiers: []lang.SemanticTokenModifier{},
Range: eType.Range(),
},
}
}
case *hclsyntax.FunctionCallExpr:
if isTypeNameWithElementOnly(eType.Name) {
tokens := make([]lang.SemanticToken, 0)
tokens = append(tokens, lang.SemanticToken{
Type: lang.TokenTypeComplex,
Modifiers: []lang.SemanticTokenModifier{},
Range: eType.NameRange,
})
if len(eType.Args) == 0 {
return tokens
}
if len(eType.Args) == 1 {
cons := TypeDeclaration{
expr: eType.Args[0],
pathCtx: td.pathCtx,
}
tokens = append(tokens, cons.SemanticTokens(ctx)...)
return tokens
}
return []lang.SemanticToken{}
}
if eType.Name == "object" {
return td.objectSemanticTokens(ctx, eType)
}
if eType.Name == "tuple" {
return td.tupleSemanticTokens(ctx, eType)
}
}
return nil
}
func (td TypeDeclaration) objectSemanticTokens(ctx context.Context, funcExpr *hclsyntax.FunctionCallExpr) []lang.SemanticToken {
tokens := make([]lang.SemanticToken, 0)
tokens = append(tokens, lang.SemanticToken{
Type: lang.TokenTypeComplex,
Modifiers: []lang.SemanticTokenModifier{},
Range: funcExpr.NameRange,
})
if len(funcExpr.Args) != 1 {
return tokens
}
objExpr, ok := funcExpr.Args[0].(*hclsyntax.ObjectConsExpr)
if !ok {
return []lang.SemanticToken{}
}
for _, item := range objExpr.Items {
_, _, ok := rawObjectKey(item.KeyExpr)
if !ok {
// avoid reporting un-decodable key
return tokens
}
tokens = append(tokens, lang.SemanticToken{
Type: lang.TokenAttrName,
Modifiers: []lang.SemanticTokenModifier{},
Range: item.KeyExpr.Range(),
})
cons := TypeDeclaration{
expr: item.ValueExpr,
pathCtx: td.pathCtx,
}
tokens = append(tokens, cons.SemanticTokens(ctx)...)
}
return tokens
}
func (td TypeDeclaration) tupleSemanticTokens(ctx context.Context, funcExpr *hclsyntax.FunctionCallExpr) []lang.SemanticToken {
tokens := make([]lang.SemanticToken, 0)
tokens = append(tokens, lang.SemanticToken{
Type: lang.TokenTypeComplex,
Modifiers: []lang.SemanticTokenModifier{},
Range: funcExpr.NameRange,
})
if len(funcExpr.Args) != 1 {
return tokens
}
tupleExpr, ok := funcExpr.Args[0].(*hclsyntax.TupleConsExpr)
if !ok {
return []lang.SemanticToken{}
}
for _, expr := range tupleExpr.Exprs {
cons := TypeDeclaration{
expr: expr,
pathCtx: td.pathCtx,
}
tokens = append(tokens, cons.SemanticTokens(ctx)...)
}
return tokens
}