-
Notifications
You must be signed in to change notification settings - Fork 8
/
candidates.go
185 lines (170 loc) · 5.21 KB
/
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
package tfschema
import (
"fmt"
"sort"
"strings"
"github.com/Azure/azapi-lsp/internal/azure"
lsp "github.com/Azure/azapi-lsp/internal/protocol"
)
func PropertiesCandidates(props []Property, r lsp.Range) []lsp.CompletionItem {
candidates := make([]lsp.CompletionItem, 0)
for index, prop := range props {
candidates = append(candidates, lsp.CompletionItem{
Label: prop.Name,
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: fmt.Sprintf("%04d", index),
InsertTextFormat: lsp.SnippetTextFormat,
InsertTextMode: lsp.AdjustIndentation,
TextEdit: &lsp.TextEdit{
Range: r,
NewText: prop.CompletionNewText,
},
Command: constTriggerSuggestCommand(),
})
}
return candidates
}
func valueCandidates(values []string, r lsp.Range, isOrdered bool) []lsp.CompletionItem {
candidates := make([]lsp.CompletionItem, 0)
for index, value := range values {
literal := strings.Trim(value, `"`)
sortText := "0" + literal
if isOrdered {
sortText = fmt.Sprintf("%04d", index)
}
candidates = append(candidates, lsp.CompletionItem{
Label: value,
Kind: lsp.ValueCompletion,
Documentation: lsp.MarkupContent{
Kind: "markdown",
Value: fmt.Sprintf("Value: `%s` \n", literal),
},
SortText: sortText,
InsertTextFormat: lsp.PlainTextTextFormat,
InsertTextMode: lsp.AdjustIndentation,
TextEdit: &lsp.TextEdit{
Range: r,
NewText: value,
},
})
}
return candidates
}
func resourceTypeCandidates(prefix *string, r lsp.Range) []lsp.CompletionItem {
return typeCandidates(prefix, r, false)
}
func dataSourceTypeCandidates(prefix *string, r lsp.Range) []lsp.CompletionItem {
return typeCandidates(prefix, r, true)
}
func typeCandidates(prefix *string, r lsp.Range, allowReadOnly bool) []lsp.CompletionItem {
candidates := make([]lsp.CompletionItem, 0)
if prefix == nil || !strings.Contains(*prefix, "@") {
for resourceType, resource := range azure.GetAzureSchema().Resources {
if len(resource.Definitions) > 0 {
def, err := resource.Definitions[0].GetDefinition()
if err == nil && def.IsReadOnly() && !allowReadOnly {
continue
}
}
candidates = append(candidates, lsp.CompletionItem{
Label: fmt.Sprintf(`"%s"`, resourceType),
Kind: lsp.ValueCompletion,
Documentation: lsp.MarkupContent{
Kind: "markdown",
Value: fmt.Sprintf("Type: `%s` \n", resourceType),
},
SortText: resourceType,
InsertTextFormat: lsp.SnippetTextFormat,
InsertTextMode: lsp.AdjustIndentation,
TextEdit: &lsp.TextEdit{
Range: r,
NewText: fmt.Sprintf(`"%s@$0"`, resourceType),
},
Command: constTriggerSuggestCommand(),
})
}
} else {
resourceType := (*prefix)[0:strings.Index(*prefix, "@")]
apiVersions := azure.GetApiVersions(resourceType)
sort.Strings(apiVersions)
length := len(apiVersions)
for index, apiVersion := range apiVersions {
candidates = append(candidates, lsp.CompletionItem{
Label: fmt.Sprintf(`"%s@%s"`, resourceType, apiVersion),
Kind: lsp.ValueCompletion,
Documentation: lsp.MarkupContent{
Kind: "markdown",
Value: fmt.Sprintf("Type: `%s` \nAPI Version: `%s`", resourceType, apiVersion),
},
SortText: fmt.Sprintf("%04d", length-index),
InsertTextFormat: lsp.PlainTextTextFormat,
InsertTextMode: lsp.AdjustIndentation,
TextEdit: &lsp.TextEdit{
Range: r,
NewText: fmt.Sprintf(`"%s@%s"`, resourceType, apiVersion),
},
})
}
}
return candidates
}
func locationCandidates(_ *string, r lsp.Range) []lsp.CompletionItem {
values := make([]string, 0)
for _, location := range supportedLocations() {
values = append(values, fmt.Sprintf(`"%s"`, location))
}
return valueCandidates(values, r, true)
}
func identityTypesCandidates(_ *string, r lsp.Range) []lsp.CompletionItem {
values := []string{
`"SystemAssigned"`,
`"UserAssigned"`,
`"SystemAssigned, UserAssigned"`,
}
return valueCandidates(values, r, false)
}
func boolCandidates(_ *string, r lsp.Range) []lsp.CompletionItem {
return valueCandidates([]string{"true", "false"}, r, false)
}
func bodyJsonencodeFuncCandidate() lsp.CompletionItem {
return lsp.CompletionItem{
Label: `jsonencode({})`,
Kind: lsp.ValueCompletion,
Documentation: lsp.MarkupContent{
Kind: "markdown",
Value: "`jsonencode` encodes a given value to a string using JSON syntax.",
},
SortText: `jsonencode`,
InsertTextFormat: lsp.SnippetTextFormat,
InsertTextMode: lsp.AdjustIndentation,
TextEdit: &lsp.TextEdit{
NewText: "jsonencode({\n\t$0\n})",
},
Command: constTriggerSuggestCommand(),
}
}
func resourceHttpMethodCandidates(_ *string, r lsp.Range) []lsp.CompletionItem {
return valueCandidates([]string{
`"POST"`,
`"PATCH"`,
`"PUT"`,
`"DELETE"`,
}, r, true)
}
func dataSourceHttpMethodCandidates(_ *string, r lsp.Range) []lsp.CompletionItem {
return valueCandidates([]string{
`"POST"`,
`"GET"`,
}, r, true)
}
func constTriggerSuggestCommand() *lsp.Command {
return &lsp.Command{
Command: "editor.action.triggerSuggest",
Title: "Suggest",
}
}