-
Notifications
You must be signed in to change notification settings - Fork 3
/
extension.js
186 lines (158 loc) · 6.37 KB
/
extension.js
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
var vscode = require("vscode")
var fs = require("fs")
var filtersArr = getJson("/snippets/filters.json")
var functionsArr = getJson("/snippets/functions.json")
var twigArr = getJson("/snippets/twig.json")
var loopArr = getJson("/tests/loop.json")
var testsArr = getJson("/tests/tests.json")
function getJson(relativePath) {
var file = fs.readFileSync(__dirname + relativePath, "utf8")
return JSON.parse(file)
}
function createHover(snippet) {
var example = typeof snippet.example === "undefined" ? "" : snippet.example
var description =
typeof snippet.description === "undefined" ? "" : snippet.description
return new vscode.Hover({
language: "html",
value: description + "\n\n" + example,
})
}
function activate(context) {
var hovers = vscode.languages.registerHoverProvider("html", {
provideHover(document, position, token) {
var range = document.getWordRangeAtPosition(position)
var word = document.getText(range)
for (var snippet in filtersArr) {
if (
filtersArr[snippet].prefix === word ||
filtersArr[snippet].hover === word
) {
return createHover(filtersArr[snippet])
}
}
for (var snippet in functionsArr) {
if (
functionsArr[snippet].prefix === word ||
functionsArr[snippet].hover === word
) {
return createHover(functionsArr[snippet])
}
}
for (var snippet in twigArr) {
if (
twigArr[snippet].prefix === word ||
twigArr[snippet].hover === word
) {
return createHover(twigArr[snippet])
}
}
},
})
context.subscriptions.push(hovers)
var filters = vscode.languages.registerCompletionItemProvider(
"html",
{
provideCompletionItems(document, position, token) {
var start = new vscode.Position(position.line, 0)
var range = new vscode.Range(start, position)
var text = document.getText(range)
var completionItems = []
if (text[text.length - 1] !== "|") {
return completionItems
}
for (var snippet in filtersArr) {
if (typeof filtersArr[snippet].text !== "undefined") {
var description =
typeof filtersArr[snippet].description ===
"undefined"
? ""
: filtersArr[snippet].description
var example =
typeof filtersArr[snippet].example === "undefined"
? ""
: "\n\n" + filtersArr[snippet].example
var item = new vscode.CompletionItem(snippet)
item.kind = vscode.CompletionItemKind.Function
item.detail = filtersArr[snippet].description
item.documentation = description + example
item.insertText = filtersArr[snippet].text
completionItems.push(item)
}
}
return completionItems
},
resolveCompletionItem(item, token) {
return item
},
},
"|"
)
context.subscriptions.push(filters)
var loopField = vscode.languages.registerCompletionItemProvider(
"html",
{
provideCompletionItems(document, position, token) {
var start = new vscode.Position(position.line, 0)
var range = new vscode.Range(start, position)
var text = document.getText(range)
var completionItems = []
if (text.substring(text.length - 5) !== "loop.") {
return completionItems
}
for (var snippet in loopArr) {
var description =
typeof loopArr[snippet].description === "undefined"
? ""
: loopArr[snippet].description
var item = new vscode.CompletionItem(snippet)
item.kind = vscode.CompletionItemKind.Property
item.detail = description
completionItems.push(item)
}
return completionItems
},
resolveCompletionItem(item, token) {
return item
},
},
"."
)
context.subscriptions.push(loopField)
var tests = vscode.languages.registerCompletionItemProvider(
"html",
{
provideCompletionItems(document, position, token) {
var start = new vscode.Position(position.line, 0)
var range = new vscode.Range(start, position)
var text = document.getText(range)
var completionItems = []
if (
!(text.substring(text.length - 3) === "is ") ||
text.substring(text.length - 3) === "not "
) {
return completionItems
}
for (var snippet in testsArr) {
var description =
typeof testsArr[snippet].description === "undefined"
? ""
: testsArr[snippet].description
var item = new vscode.CompletionItem(snippet)
item.kind = vscode.CompletionItemKind.Keyword
item.detail = description
completionItems.push(item)
}
return completionItems
},
resolveCompletionItem(item, token) {
return item
},
},
" "
)
context.subscriptions.push(tests)
}
exports.activate = activate
function deactivate() {}
exports.deactivate = deactivate