-
Notifications
You must be signed in to change notification settings - Fork 8
/
body_candidates.go
266 lines (251 loc) · 7.93 KB
/
body_candidates.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package tfschema
import (
"fmt"
"strings"
"github.com/Azure/azapi-lsp/internal/azure"
"github.com/Azure/azapi-lsp/internal/azure/types"
"github.com/Azure/azapi-lsp/internal/langserver/schema"
ilsp "github.com/Azure/azapi-lsp/internal/lsp"
"github.com/Azure/azapi-lsp/internal/parser"
lsp "github.com/Azure/azapi-lsp/internal/protocol"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
)
func bodyCandidates(data []byte, filename string, block *hclsyntax.Block, attribute *hclsyntax.Attribute, pos hcl.Pos, property *Property) []lsp.CompletionItem {
if attribute.Expr != nil {
if _, ok := attribute.Expr.(*hclsyntax.LiteralValueExpr); ok && parser.ToLiteral(attribute.Expr) == nil {
if property != nil {
return property.ValueCandidatesFunc(nil, editRangeFromExprRange(attribute.Expr, pos))
}
}
}
typeValue := parser.ExtractAzureResourceType(block)
if typeValue == nil {
return nil
}
var bodyDef types.TypeBase
def, err := azure.GetResourceDefinitionByResourceType(*typeValue)
if err != nil || def == nil {
return nil
}
bodyDef = def
if len(block.Labels) >= 2 && block.Labels[0] == "azapi_resource_action" {
parts := strings.Split(*typeValue, "@")
if len(parts) != 2 {
return nil
}
actionName := parser.ExtractAction(block)
if actionName != nil && len(*actionName) != 0 {
resourceFuncDef, err := azure.GetResourceFunction(parts[0], parts[1], *actionName)
if err != nil || resourceFuncDef == nil {
return nil
}
bodyDef = resourceFuncDef
}
}
return buildCandidates(data, filename, attribute, pos, bodyDef)
}
func buildCandidates(data []byte, filename string, attribute *hclsyntax.Attribute, pos hcl.Pos, def types.TypeBase) []lsp.CompletionItem {
candidateList := make([]lsp.CompletionItem, 0)
hclNode := parser.JsonEncodeExpressionToHclNode(data, attribute.Expr)
if hclNode == nil {
return nil
}
hclNodes := parser.HclNodeArraysOfPos(hclNode, pos)
if len(hclNodes) == 0 {
return nil
}
lastHclNode := hclNodes[len(hclNodes)-1]
switch {
case parser.ContainsPos(lastHclNode.KeyRange, pos):
// input a property with a prefix
hclNodes := hclNodes[0 : len(hclNodes)-1]
defs := schema.GetDef(def.AsTypeBase(), hclNodes, 0)
keys := make([]schema.Property, 0)
for _, def := range defs {
keys = append(keys, schema.GetAllowedProperties(def)...)
}
if len(hclNodes) == 1 {
keys = ignorePulledOutProperties(keys)
}
editRange := ilsp.HCLRangeToLSP(lastHclNode.KeyRange)
candidateList = keyCandidates(keys, editRange)
case !lastHclNode.KeyRange.Empty() && !lastHclNode.EqualRange.Empty() && lastHclNode.Children == nil:
// input property =
defs := schema.GetDef(def.AsTypeBase(), hclNodes, 0)
values := make([]string, 0)
for _, def := range defs {
values = append(values, schema.GetAllowedValues(def)...)
}
editRange := lastHclNode.ValueRange
if lastHclNode.Value == nil {
editRange.End = pos
}
candidateList = valueCandidates(values, ilsp.HCLRangeToLSP(editRange), false)
case parser.ContainsPos(lastHclNode.ValueRange, pos):
// input a property
defs := schema.GetDef(def.AsTypeBase(), hclNodes, 0)
keys := make([]schema.Property, 0)
for _, def := range defs {
keys = append(keys, schema.GetAllowedProperties(def)...)
}
if len(hclNodes) == 1 {
keys = ignorePulledOutProperties(keys)
}
editRange := ilsp.HCLRangeToLSP(hcl.Range{Start: pos, End: pos, Filename: filename})
candidateList = keyCandidates(keys, editRange)
if len(lastHclNode.Children) == 0 {
propertySets := make([]schema.PropertySet, 0)
for _, def := range defs {
propertySets = append(propertySets, schema.GetRequiredPropertySet(def)...)
}
if len(hclNodes) == 1 {
for i, ps := range propertySets {
propertySets[i].Name = ""
propertySets[i].Properties = ignorePulledOutPropertiesFromPropertySet(ps.Properties)
}
}
candidateList = append(candidateList, requiredPropertiesCandidates(propertySets, editRange)...)
}
}
return candidateList
}
func editRangeFromExprRange(expression hclsyntax.Expression, pos hcl.Pos) lsp.Range {
expRange := expression.Range()
if expRange.Start.Line != expRange.End.Line && expRange.End.Column == 1 && expRange.End.Line-1 == pos.Line {
expRange.End = pos
}
return ilsp.HCLRangeToLSP(expRange)
}
func ignorePulledOutProperties(input []schema.Property) []schema.Property {
res := make([]schema.Property, 0)
// ignore properties pulled out from body
for _, p := range input {
if !isPropertyPulledOut(p) {
res = append(res, p)
}
}
return res
}
func ignorePulledOutPropertiesFromPropertySet(properties map[string]schema.Property) map[string]schema.Property {
res := make(map[string]schema.Property)
// ignore properties pulled out from body
for _, p := range properties {
if !isPropertyPulledOut(p) {
res[p.Name] = p
}
}
return res
}
func isPropertyPulledOut(p schema.Property) bool {
return p.Name == "name" || p.Name == "location" || p.Name == "identity" || p.Name == "tags"
}
func keyCandidates(props []schema.Property, r lsp.Range) []lsp.CompletionItem {
candidates := make([]lsp.CompletionItem, 0)
propSet := make(map[string]bool)
for _, prop := range props {
if propSet[prop.Name] {
continue
}
propSet[prop.Name] = true
content := prop.Name
newText := ""
sortText := fmt.Sprintf("1%s", content)
if prop.Modifier == schema.Required {
sortText = fmt.Sprintf("0%s", content)
}
switch prop.Type {
case "string":
newText = fmt.Sprintf(`%s = "$0"`, content)
case "array":
newText = fmt.Sprintf(`%s = [$0]`, content)
case "object":
newText = fmt.Sprintf("%s = {\n\t$0\n}", content)
default:
newText = fmt.Sprintf(`%s = $0`, content)
}
candidates = append(candidates, lsp.CompletionItem{
Label: content,
Kind: lsp.PropertyCompletion,
Detail: fmt.Sprintf("%s (%s)", prop.Name, prop.Modifier),
Documentation: lsp.MarkupContent{
Kind: "markdown",
Value: fmt.Sprintf("Type: `%s` \n%s\n", prop.Type, prop.Description),
},
SortText: sortText,
InsertTextFormat: lsp.SnippetTextFormat,
InsertTextMode: lsp.AdjustIndentation,
TextEdit: &lsp.TextEdit{
Range: r,
NewText: newText,
},
Command: constTriggerSuggestCommand(),
})
}
return candidates
}
func requiredPropertiesCandidates(propertySets []schema.PropertySet, r lsp.Range) []lsp.CompletionItem {
candidates := make([]lsp.CompletionItem, 0)
for _, ps := range propertySets {
if len(ps.Properties) == 0 {
continue
}
props := make([]schema.Property, 0)
for _, prop := range ps.Properties {
props = append(props, prop)
}
for range props {
for i := 0; i < len(props)-1; i++ {
if props[i].Name > props[i+1].Name {
props[i], props[i+1] = props[i+1], props[i]
}
}
}
newText := ""
index := 1
for _, prop := range props {
if len(prop.Value) != 0 {
newText += fmt.Sprintf("%s = \"%s\"\n", prop.Name, prop.Value)
} else {
switch prop.Type {
case "string":
newText += fmt.Sprintf(`%s = "$%d"`, prop.Name, index)
case "array":
newText += fmt.Sprintf(`%s = [$%d]`, prop.Name, index)
case "object":
newText += fmt.Sprintf("%s = {\n\t$%d\n}", prop.Name, index)
default:
newText += fmt.Sprintf(`%s = $%d`, prop.Name, index)
}
newText += "\n"
index++
}
}
label := "required-properties"
if len(ps.Name) != 0 {
label = fmt.Sprintf("required-properties-%s", ps.Name)
}
detail := "Required properties"
if len(ps.Name) != 0 {
detail = fmt.Sprintf("Required properties - %s", ps.Name)
}
candidates = append(candidates, lsp.CompletionItem{
Label: label,
Kind: lsp.SnippetCompletion,
Detail: detail,
Documentation: lsp.MarkupContent{
Kind: "markdown",
Value: fmt.Sprintf("Type: `%s` \n```\n%s\n```\n", ps.Name, newText),
},
SortText: "0",
InsertTextFormat: lsp.SnippetTextFormat,
InsertTextMode: lsp.AdjustIndentation,
TextEdit: &lsp.TextEdit{
Range: r,
NewText: newText,
},
Command: constTriggerSuggestCommand(),
})
}
return candidates
}