vscode-json-languageservice 4.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +133 -0
- package/LICENSE.md +47 -0
- package/README.md +65 -0
- package/lib/esm/jsonContributions.d.ts +17 -0
- package/lib/esm/jsonContributions.js +1 -0
- package/lib/esm/jsonLanguageService.d.ts +28 -0
- package/lib/esm/jsonLanguageService.js +65 -0
- package/lib/esm/jsonLanguageTypes.d.ts +275 -0
- package/lib/esm/jsonLanguageTypes.js +45 -0
- package/lib/esm/jsonSchema.d.ts +70 -0
- package/lib/esm/jsonSchema.js +1 -0
- package/lib/esm/parser/jsonParser.js +1212 -0
- package/lib/esm/services/configuration.js +528 -0
- package/lib/esm/services/jsonCompletion.js +934 -0
- package/lib/esm/services/jsonDocumentSymbols.js +278 -0
- package/lib/esm/services/jsonFolding.js +121 -0
- package/lib/esm/services/jsonHover.js +112 -0
- package/lib/esm/services/jsonLinks.js +73 -0
- package/lib/esm/services/jsonSchemaService.js +535 -0
- package/lib/esm/services/jsonSelectionRanges.js +61 -0
- package/lib/esm/services/jsonValidation.js +146 -0
- package/lib/esm/utils/colors.js +69 -0
- package/lib/esm/utils/glob.js +124 -0
- package/lib/esm/utils/json.js +42 -0
- package/lib/esm/utils/objects.js +65 -0
- package/lib/esm/utils/strings.js +52 -0
- package/lib/umd/jsonContributions.d.ts +17 -0
- package/lib/umd/jsonContributions.js +12 -0
- package/lib/umd/jsonLanguageService.d.ts +28 -0
- package/lib/umd/jsonLanguageService.js +89 -0
- package/lib/umd/jsonLanguageTypes.d.ts +275 -0
- package/lib/umd/jsonLanguageTypes.js +92 -0
- package/lib/umd/jsonSchema.d.ts +70 -0
- package/lib/umd/jsonSchema.js +12 -0
- package/lib/umd/parser/jsonParser.js +1231 -0
- package/lib/umd/services/configuration.js +541 -0
- package/lib/umd/services/jsonCompletion.js +947 -0
- package/lib/umd/services/jsonDocumentSymbols.js +291 -0
- package/lib/umd/services/jsonFolding.js +135 -0
- package/lib/umd/services/jsonHover.js +125 -0
- package/lib/umd/services/jsonLinks.js +87 -0
- package/lib/umd/services/jsonSchemaService.js +548 -0
- package/lib/umd/services/jsonSelectionRanges.js +75 -0
- package/lib/umd/services/jsonValidation.js +159 -0
- package/lib/umd/utils/colors.js +85 -0
- package/lib/umd/utils/glob.js +138 -0
- package/lib/umd/utils/json.js +56 -0
- package/lib/umd/utils/objects.js +83 -0
- package/lib/umd/utils/strings.js +70 -0
- package/package.json +55 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/*---------------------------------------------------------------------------------------------
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
4
|
+
*--------------------------------------------------------------------------------------------*/
|
|
5
|
+
(function (factory) {
|
|
6
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
7
|
+
var v = factory(require, exports);
|
|
8
|
+
if (v !== undefined) module.exports = v;
|
|
9
|
+
}
|
|
10
|
+
else if (typeof define === "function" && define.amd) {
|
|
11
|
+
define(["require", "exports", "../parser/jsonParser", "../utils/strings", "../utils/colors", "../jsonLanguageTypes"], factory);
|
|
12
|
+
}
|
|
13
|
+
})(function (require, exports) {
|
|
14
|
+
"use strict";
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.JSONDocumentSymbols = void 0;
|
|
17
|
+
var Parser = require("../parser/jsonParser");
|
|
18
|
+
var Strings = require("../utils/strings");
|
|
19
|
+
var colors_1 = require("../utils/colors");
|
|
20
|
+
var jsonLanguageTypes_1 = require("../jsonLanguageTypes");
|
|
21
|
+
var JSONDocumentSymbols = /** @class */ (function () {
|
|
22
|
+
function JSONDocumentSymbols(schemaService) {
|
|
23
|
+
this.schemaService = schemaService;
|
|
24
|
+
}
|
|
25
|
+
JSONDocumentSymbols.prototype.findDocumentSymbols = function (document, doc, context) {
|
|
26
|
+
var _this = this;
|
|
27
|
+
if (context === void 0) { context = { resultLimit: Number.MAX_VALUE }; }
|
|
28
|
+
var root = doc.root;
|
|
29
|
+
if (!root) {
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
var limit = context.resultLimit || Number.MAX_VALUE;
|
|
33
|
+
// special handling for key bindings
|
|
34
|
+
var resourceString = document.uri;
|
|
35
|
+
if ((resourceString === 'vscode://defaultsettings/keybindings.json') || Strings.endsWith(resourceString.toLowerCase(), '/user/keybindings.json')) {
|
|
36
|
+
if (root.type === 'array') {
|
|
37
|
+
var result_1 = [];
|
|
38
|
+
for (var _i = 0, _a = root.items; _i < _a.length; _i++) {
|
|
39
|
+
var item = _a[_i];
|
|
40
|
+
if (item.type === 'object') {
|
|
41
|
+
for (var _b = 0, _c = item.properties; _b < _c.length; _b++) {
|
|
42
|
+
var property = _c[_b];
|
|
43
|
+
if (property.keyNode.value === 'key' && property.valueNode) {
|
|
44
|
+
var location = jsonLanguageTypes_1.Location.create(document.uri, getRange(document, item));
|
|
45
|
+
result_1.push({ name: Parser.getNodeValue(property.valueNode), kind: jsonLanguageTypes_1.SymbolKind.Function, location: location });
|
|
46
|
+
limit--;
|
|
47
|
+
if (limit <= 0) {
|
|
48
|
+
if (context && context.onResultLimitExceeded) {
|
|
49
|
+
context.onResultLimitExceeded(resourceString);
|
|
50
|
+
}
|
|
51
|
+
return result_1;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return result_1;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
var toVisit = [
|
|
61
|
+
{ node: root, containerName: '' }
|
|
62
|
+
];
|
|
63
|
+
var nextToVisit = 0;
|
|
64
|
+
var limitExceeded = false;
|
|
65
|
+
var result = [];
|
|
66
|
+
var collectOutlineEntries = function (node, containerName) {
|
|
67
|
+
if (node.type === 'array') {
|
|
68
|
+
node.items.forEach(function (node) {
|
|
69
|
+
if (node) {
|
|
70
|
+
toVisit.push({ node: node, containerName: containerName });
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
else if (node.type === 'object') {
|
|
75
|
+
node.properties.forEach(function (property) {
|
|
76
|
+
var valueNode = property.valueNode;
|
|
77
|
+
if (valueNode) {
|
|
78
|
+
if (limit > 0) {
|
|
79
|
+
limit--;
|
|
80
|
+
var location = jsonLanguageTypes_1.Location.create(document.uri, getRange(document, property));
|
|
81
|
+
var childContainerName = containerName ? containerName + '.' + property.keyNode.value : property.keyNode.value;
|
|
82
|
+
result.push({ name: _this.getKeyLabel(property), kind: _this.getSymbolKind(valueNode.type), location: location, containerName: containerName });
|
|
83
|
+
toVisit.push({ node: valueNode, containerName: childContainerName });
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
limitExceeded = true;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
// breath first traversal
|
|
93
|
+
while (nextToVisit < toVisit.length) {
|
|
94
|
+
var next = toVisit[nextToVisit++];
|
|
95
|
+
collectOutlineEntries(next.node, next.containerName);
|
|
96
|
+
}
|
|
97
|
+
if (limitExceeded && context && context.onResultLimitExceeded) {
|
|
98
|
+
context.onResultLimitExceeded(resourceString);
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
};
|
|
102
|
+
JSONDocumentSymbols.prototype.findDocumentSymbols2 = function (document, doc, context) {
|
|
103
|
+
var _this = this;
|
|
104
|
+
if (context === void 0) { context = { resultLimit: Number.MAX_VALUE }; }
|
|
105
|
+
var root = doc.root;
|
|
106
|
+
if (!root) {
|
|
107
|
+
return [];
|
|
108
|
+
}
|
|
109
|
+
var limit = context.resultLimit || Number.MAX_VALUE;
|
|
110
|
+
// special handling for key bindings
|
|
111
|
+
var resourceString = document.uri;
|
|
112
|
+
if ((resourceString === 'vscode://defaultsettings/keybindings.json') || Strings.endsWith(resourceString.toLowerCase(), '/user/keybindings.json')) {
|
|
113
|
+
if (root.type === 'array') {
|
|
114
|
+
var result_2 = [];
|
|
115
|
+
for (var _i = 0, _a = root.items; _i < _a.length; _i++) {
|
|
116
|
+
var item = _a[_i];
|
|
117
|
+
if (item.type === 'object') {
|
|
118
|
+
for (var _b = 0, _c = item.properties; _b < _c.length; _b++) {
|
|
119
|
+
var property = _c[_b];
|
|
120
|
+
if (property.keyNode.value === 'key' && property.valueNode) {
|
|
121
|
+
var range = getRange(document, item);
|
|
122
|
+
var selectionRange = getRange(document, property.keyNode);
|
|
123
|
+
result_2.push({ name: Parser.getNodeValue(property.valueNode), kind: jsonLanguageTypes_1.SymbolKind.Function, range: range, selectionRange: selectionRange });
|
|
124
|
+
limit--;
|
|
125
|
+
if (limit <= 0) {
|
|
126
|
+
if (context && context.onResultLimitExceeded) {
|
|
127
|
+
context.onResultLimitExceeded(resourceString);
|
|
128
|
+
}
|
|
129
|
+
return result_2;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return result_2;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
var result = [];
|
|
139
|
+
var toVisit = [
|
|
140
|
+
{ node: root, result: result }
|
|
141
|
+
];
|
|
142
|
+
var nextToVisit = 0;
|
|
143
|
+
var limitExceeded = false;
|
|
144
|
+
var collectOutlineEntries = function (node, result) {
|
|
145
|
+
if (node.type === 'array') {
|
|
146
|
+
node.items.forEach(function (node, index) {
|
|
147
|
+
if (node) {
|
|
148
|
+
if (limit > 0) {
|
|
149
|
+
limit--;
|
|
150
|
+
var range = getRange(document, node);
|
|
151
|
+
var selectionRange = range;
|
|
152
|
+
var name = String(index);
|
|
153
|
+
var symbol = { name: name, kind: _this.getSymbolKind(node.type), range: range, selectionRange: selectionRange, children: [] };
|
|
154
|
+
result.push(symbol);
|
|
155
|
+
toVisit.push({ result: symbol.children, node: node });
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
limitExceeded = true;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
else if (node.type === 'object') {
|
|
164
|
+
node.properties.forEach(function (property) {
|
|
165
|
+
var valueNode = property.valueNode;
|
|
166
|
+
if (valueNode) {
|
|
167
|
+
if (limit > 0) {
|
|
168
|
+
limit--;
|
|
169
|
+
var range = getRange(document, property);
|
|
170
|
+
var selectionRange = getRange(document, property.keyNode);
|
|
171
|
+
var children = [];
|
|
172
|
+
var symbol = { name: _this.getKeyLabel(property), kind: _this.getSymbolKind(valueNode.type), range: range, selectionRange: selectionRange, children: children, detail: _this.getDetail(valueNode) };
|
|
173
|
+
result.push(symbol);
|
|
174
|
+
toVisit.push({ result: children, node: valueNode });
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
limitExceeded = true;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
// breath first traversal
|
|
184
|
+
while (nextToVisit < toVisit.length) {
|
|
185
|
+
var next = toVisit[nextToVisit++];
|
|
186
|
+
collectOutlineEntries(next.node, next.result);
|
|
187
|
+
}
|
|
188
|
+
if (limitExceeded && context && context.onResultLimitExceeded) {
|
|
189
|
+
context.onResultLimitExceeded(resourceString);
|
|
190
|
+
}
|
|
191
|
+
return result;
|
|
192
|
+
};
|
|
193
|
+
JSONDocumentSymbols.prototype.getSymbolKind = function (nodeType) {
|
|
194
|
+
switch (nodeType) {
|
|
195
|
+
case 'object':
|
|
196
|
+
return jsonLanguageTypes_1.SymbolKind.Module;
|
|
197
|
+
case 'string':
|
|
198
|
+
return jsonLanguageTypes_1.SymbolKind.String;
|
|
199
|
+
case 'number':
|
|
200
|
+
return jsonLanguageTypes_1.SymbolKind.Number;
|
|
201
|
+
case 'array':
|
|
202
|
+
return jsonLanguageTypes_1.SymbolKind.Array;
|
|
203
|
+
case 'boolean':
|
|
204
|
+
return jsonLanguageTypes_1.SymbolKind.Boolean;
|
|
205
|
+
default: // 'null'
|
|
206
|
+
return jsonLanguageTypes_1.SymbolKind.Variable;
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
JSONDocumentSymbols.prototype.getKeyLabel = function (property) {
|
|
210
|
+
var name = property.keyNode.value;
|
|
211
|
+
if (name) {
|
|
212
|
+
name = name.replace(/[\n]/g, '↵');
|
|
213
|
+
}
|
|
214
|
+
if (name && name.trim()) {
|
|
215
|
+
return name;
|
|
216
|
+
}
|
|
217
|
+
return "\"" + name + "\"";
|
|
218
|
+
};
|
|
219
|
+
JSONDocumentSymbols.prototype.getDetail = function (node) {
|
|
220
|
+
if (!node) {
|
|
221
|
+
return undefined;
|
|
222
|
+
}
|
|
223
|
+
if (node.type === 'boolean' || node.type === 'number' || node.type === 'null' || node.type === 'string') {
|
|
224
|
+
return String(node.value);
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
if (node.type === 'array') {
|
|
228
|
+
return node.children.length ? undefined : '[]';
|
|
229
|
+
}
|
|
230
|
+
else if (node.type === 'object') {
|
|
231
|
+
return node.children.length ? undefined : '{}';
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return undefined;
|
|
235
|
+
};
|
|
236
|
+
JSONDocumentSymbols.prototype.findDocumentColors = function (document, doc, context) {
|
|
237
|
+
return this.schemaService.getSchemaForResource(document.uri, doc).then(function (schema) {
|
|
238
|
+
var result = [];
|
|
239
|
+
if (schema) {
|
|
240
|
+
var limit = context && typeof context.resultLimit === 'number' ? context.resultLimit : Number.MAX_VALUE;
|
|
241
|
+
var matchingSchemas = doc.getMatchingSchemas(schema.schema);
|
|
242
|
+
var visitedNode = {};
|
|
243
|
+
for (var _i = 0, matchingSchemas_1 = matchingSchemas; _i < matchingSchemas_1.length; _i++) {
|
|
244
|
+
var s = matchingSchemas_1[_i];
|
|
245
|
+
if (!s.inverted && s.schema && (s.schema.format === 'color' || s.schema.format === 'color-hex') && s.node && s.node.type === 'string') {
|
|
246
|
+
var nodeId = String(s.node.offset);
|
|
247
|
+
if (!visitedNode[nodeId]) {
|
|
248
|
+
var color = colors_1.colorFromHex(Parser.getNodeValue(s.node));
|
|
249
|
+
if (color) {
|
|
250
|
+
var range = getRange(document, s.node);
|
|
251
|
+
result.push({ color: color, range: range });
|
|
252
|
+
}
|
|
253
|
+
visitedNode[nodeId] = true;
|
|
254
|
+
limit--;
|
|
255
|
+
if (limit <= 0) {
|
|
256
|
+
if (context && context.onResultLimitExceeded) {
|
|
257
|
+
context.onResultLimitExceeded(document.uri);
|
|
258
|
+
}
|
|
259
|
+
return result;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return result;
|
|
266
|
+
});
|
|
267
|
+
};
|
|
268
|
+
JSONDocumentSymbols.prototype.getColorPresentations = function (document, doc, color, range) {
|
|
269
|
+
var result = [];
|
|
270
|
+
var red256 = Math.round(color.red * 255), green256 = Math.round(color.green * 255), blue256 = Math.round(color.blue * 255);
|
|
271
|
+
function toTwoDigitHex(n) {
|
|
272
|
+
var r = n.toString(16);
|
|
273
|
+
return r.length !== 2 ? '0' + r : r;
|
|
274
|
+
}
|
|
275
|
+
var label;
|
|
276
|
+
if (color.alpha === 1) {
|
|
277
|
+
label = "#" + toTwoDigitHex(red256) + toTwoDigitHex(green256) + toTwoDigitHex(blue256);
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
label = "#" + toTwoDigitHex(red256) + toTwoDigitHex(green256) + toTwoDigitHex(blue256) + toTwoDigitHex(Math.round(color.alpha * 255));
|
|
281
|
+
}
|
|
282
|
+
result.push({ label: label, textEdit: jsonLanguageTypes_1.TextEdit.replace(range, JSON.stringify(label)) });
|
|
283
|
+
return result;
|
|
284
|
+
};
|
|
285
|
+
return JSONDocumentSymbols;
|
|
286
|
+
}());
|
|
287
|
+
exports.JSONDocumentSymbols = JSONDocumentSymbols;
|
|
288
|
+
function getRange(document, node) {
|
|
289
|
+
return jsonLanguageTypes_1.Range.create(document.positionAt(node.offset), document.positionAt(node.offset + node.length));
|
|
290
|
+
}
|
|
291
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/*---------------------------------------------------------------------------------------------
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
4
|
+
*--------------------------------------------------------------------------------------------*/
|
|
5
|
+
(function (factory) {
|
|
6
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
7
|
+
var v = factory(require, exports);
|
|
8
|
+
if (v !== undefined) module.exports = v;
|
|
9
|
+
}
|
|
10
|
+
else if (typeof define === "function" && define.amd) {
|
|
11
|
+
define(["require", "exports", "jsonc-parser", "../jsonLanguageTypes"], factory);
|
|
12
|
+
}
|
|
13
|
+
})(function (require, exports) {
|
|
14
|
+
"use strict";
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.getFoldingRanges = void 0;
|
|
17
|
+
var jsonc_parser_1 = require("jsonc-parser");
|
|
18
|
+
var jsonLanguageTypes_1 = require("../jsonLanguageTypes");
|
|
19
|
+
function getFoldingRanges(document, context) {
|
|
20
|
+
var ranges = [];
|
|
21
|
+
var nestingLevels = [];
|
|
22
|
+
var stack = [];
|
|
23
|
+
var prevStart = -1;
|
|
24
|
+
var scanner = jsonc_parser_1.createScanner(document.getText(), false);
|
|
25
|
+
var token = scanner.scan();
|
|
26
|
+
function addRange(range) {
|
|
27
|
+
ranges.push(range);
|
|
28
|
+
nestingLevels.push(stack.length);
|
|
29
|
+
}
|
|
30
|
+
while (token !== 17 /* EOF */) {
|
|
31
|
+
switch (token) {
|
|
32
|
+
case 1 /* OpenBraceToken */:
|
|
33
|
+
case 3 /* OpenBracketToken */: {
|
|
34
|
+
var startLine = document.positionAt(scanner.getTokenOffset()).line;
|
|
35
|
+
var range = { startLine: startLine, endLine: startLine, kind: token === 1 /* OpenBraceToken */ ? 'object' : 'array' };
|
|
36
|
+
stack.push(range);
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
case 2 /* CloseBraceToken */:
|
|
40
|
+
case 4 /* CloseBracketToken */: {
|
|
41
|
+
var kind = token === 2 /* CloseBraceToken */ ? 'object' : 'array';
|
|
42
|
+
if (stack.length > 0 && stack[stack.length - 1].kind === kind) {
|
|
43
|
+
var range = stack.pop();
|
|
44
|
+
var line = document.positionAt(scanner.getTokenOffset()).line;
|
|
45
|
+
if (range && line > range.startLine + 1 && prevStart !== range.startLine) {
|
|
46
|
+
range.endLine = line - 1;
|
|
47
|
+
addRange(range);
|
|
48
|
+
prevStart = range.startLine;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
case 13 /* BlockCommentTrivia */: {
|
|
54
|
+
var startLine = document.positionAt(scanner.getTokenOffset()).line;
|
|
55
|
+
var endLine = document.positionAt(scanner.getTokenOffset() + scanner.getTokenLength()).line;
|
|
56
|
+
if (scanner.getTokenError() === 1 /* UnexpectedEndOfComment */ && startLine + 1 < document.lineCount) {
|
|
57
|
+
scanner.setPosition(document.offsetAt(jsonLanguageTypes_1.Position.create(startLine + 1, 0)));
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
if (startLine < endLine) {
|
|
61
|
+
addRange({ startLine: startLine, endLine: endLine, kind: jsonLanguageTypes_1.FoldingRangeKind.Comment });
|
|
62
|
+
prevStart = startLine;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
case 12 /* LineCommentTrivia */: {
|
|
68
|
+
var text = document.getText().substr(scanner.getTokenOffset(), scanner.getTokenLength());
|
|
69
|
+
var m = text.match(/^\/\/\s*#(region\b)|(endregion\b)/);
|
|
70
|
+
if (m) {
|
|
71
|
+
var line = document.positionAt(scanner.getTokenOffset()).line;
|
|
72
|
+
if (m[1]) { // start pattern match
|
|
73
|
+
var range = { startLine: line, endLine: line, kind: jsonLanguageTypes_1.FoldingRangeKind.Region };
|
|
74
|
+
stack.push(range);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
var i = stack.length - 1;
|
|
78
|
+
while (i >= 0 && stack[i].kind !== jsonLanguageTypes_1.FoldingRangeKind.Region) {
|
|
79
|
+
i--;
|
|
80
|
+
}
|
|
81
|
+
if (i >= 0) {
|
|
82
|
+
var range = stack[i];
|
|
83
|
+
stack.length = i;
|
|
84
|
+
if (line > range.startLine && prevStart !== range.startLine) {
|
|
85
|
+
range.endLine = line;
|
|
86
|
+
addRange(range);
|
|
87
|
+
prevStart = range.startLine;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
token = scanner.scan();
|
|
96
|
+
}
|
|
97
|
+
var rangeLimit = context && context.rangeLimit;
|
|
98
|
+
if (typeof rangeLimit !== 'number' || ranges.length <= rangeLimit) {
|
|
99
|
+
return ranges;
|
|
100
|
+
}
|
|
101
|
+
if (context && context.onRangeLimitExceeded) {
|
|
102
|
+
context.onRangeLimitExceeded(document.uri);
|
|
103
|
+
}
|
|
104
|
+
var counts = [];
|
|
105
|
+
for (var _i = 0, nestingLevels_1 = nestingLevels; _i < nestingLevels_1.length; _i++) {
|
|
106
|
+
var level = nestingLevels_1[_i];
|
|
107
|
+
if (level < 30) {
|
|
108
|
+
counts[level] = (counts[level] || 0) + 1;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
var entries = 0;
|
|
112
|
+
var maxLevel = 0;
|
|
113
|
+
for (var i = 0; i < counts.length; i++) {
|
|
114
|
+
var n = counts[i];
|
|
115
|
+
if (n) {
|
|
116
|
+
if (n + entries > rangeLimit) {
|
|
117
|
+
maxLevel = i;
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
entries += n;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
var result = [];
|
|
124
|
+
for (var i = 0; i < ranges.length; i++) {
|
|
125
|
+
var level = nestingLevels[i];
|
|
126
|
+
if (typeof level === 'number') {
|
|
127
|
+
if (level < maxLevel || (level === maxLevel && entries++ < rangeLimit)) {
|
|
128
|
+
result.push(ranges[i]);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
exports.getFoldingRanges = getFoldingRanges;
|
|
135
|
+
});
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/*---------------------------------------------------------------------------------------------
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
4
|
+
*--------------------------------------------------------------------------------------------*/
|
|
5
|
+
(function (factory) {
|
|
6
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
7
|
+
var v = factory(require, exports);
|
|
8
|
+
if (v !== undefined) module.exports = v;
|
|
9
|
+
}
|
|
10
|
+
else if (typeof define === "function" && define.amd) {
|
|
11
|
+
define(["require", "exports", "../parser/jsonParser", "../jsonLanguageTypes"], factory);
|
|
12
|
+
}
|
|
13
|
+
})(function (require, exports) {
|
|
14
|
+
"use strict";
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.JSONHover = void 0;
|
|
17
|
+
var Parser = require("../parser/jsonParser");
|
|
18
|
+
var jsonLanguageTypes_1 = require("../jsonLanguageTypes");
|
|
19
|
+
var JSONHover = /** @class */ (function () {
|
|
20
|
+
function JSONHover(schemaService, contributions, promiseConstructor) {
|
|
21
|
+
if (contributions === void 0) { contributions = []; }
|
|
22
|
+
this.schemaService = schemaService;
|
|
23
|
+
this.contributions = contributions;
|
|
24
|
+
this.promise = promiseConstructor || Promise;
|
|
25
|
+
}
|
|
26
|
+
JSONHover.prototype.doHover = function (document, position, doc) {
|
|
27
|
+
var offset = document.offsetAt(position);
|
|
28
|
+
var node = doc.getNodeFromOffset(offset);
|
|
29
|
+
if (!node || (node.type === 'object' || node.type === 'array') && offset > node.offset + 1 && offset < node.offset + node.length - 1) {
|
|
30
|
+
return this.promise.resolve(null);
|
|
31
|
+
}
|
|
32
|
+
var hoverRangeNode = node;
|
|
33
|
+
// use the property description when hovering over an object key
|
|
34
|
+
if (node.type === 'string') {
|
|
35
|
+
var parent = node.parent;
|
|
36
|
+
if (parent && parent.type === 'property' && parent.keyNode === node) {
|
|
37
|
+
node = parent.valueNode;
|
|
38
|
+
if (!node) {
|
|
39
|
+
return this.promise.resolve(null);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
var hoverRange = jsonLanguageTypes_1.Range.create(document.positionAt(hoverRangeNode.offset), document.positionAt(hoverRangeNode.offset + hoverRangeNode.length));
|
|
44
|
+
var createHover = function (contents) {
|
|
45
|
+
var result = {
|
|
46
|
+
contents: contents,
|
|
47
|
+
range: hoverRange
|
|
48
|
+
};
|
|
49
|
+
return result;
|
|
50
|
+
};
|
|
51
|
+
var location = Parser.getNodePath(node);
|
|
52
|
+
for (var i = this.contributions.length - 1; i >= 0; i--) {
|
|
53
|
+
var contribution = this.contributions[i];
|
|
54
|
+
var promise = contribution.getInfoContribution(document.uri, location);
|
|
55
|
+
if (promise) {
|
|
56
|
+
return promise.then(function (htmlContent) { return createHover(htmlContent); });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return this.schemaService.getSchemaForResource(document.uri, doc).then(function (schema) {
|
|
60
|
+
if (schema && node) {
|
|
61
|
+
var matchingSchemas = doc.getMatchingSchemas(schema.schema, node.offset);
|
|
62
|
+
var title_1 = undefined;
|
|
63
|
+
var markdownDescription_1 = undefined;
|
|
64
|
+
var markdownEnumValueDescription_1 = undefined, enumValue_1 = undefined;
|
|
65
|
+
matchingSchemas.every(function (s) {
|
|
66
|
+
if (s.node === node && !s.inverted && s.schema) {
|
|
67
|
+
title_1 = title_1 || s.schema.title;
|
|
68
|
+
markdownDescription_1 = markdownDescription_1 || s.schema.markdownDescription || toMarkdown(s.schema.description);
|
|
69
|
+
if (s.schema.enum) {
|
|
70
|
+
var idx = s.schema.enum.indexOf(Parser.getNodeValue(node));
|
|
71
|
+
if (s.schema.markdownEnumDescriptions) {
|
|
72
|
+
markdownEnumValueDescription_1 = s.schema.markdownEnumDescriptions[idx];
|
|
73
|
+
}
|
|
74
|
+
else if (s.schema.enumDescriptions) {
|
|
75
|
+
markdownEnumValueDescription_1 = toMarkdown(s.schema.enumDescriptions[idx]);
|
|
76
|
+
}
|
|
77
|
+
if (markdownEnumValueDescription_1) {
|
|
78
|
+
enumValue_1 = s.schema.enum[idx];
|
|
79
|
+
if (typeof enumValue_1 !== 'string') {
|
|
80
|
+
enumValue_1 = JSON.stringify(enumValue_1);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return true;
|
|
86
|
+
});
|
|
87
|
+
var result = '';
|
|
88
|
+
if (title_1) {
|
|
89
|
+
result = toMarkdown(title_1);
|
|
90
|
+
}
|
|
91
|
+
if (markdownDescription_1) {
|
|
92
|
+
if (result.length > 0) {
|
|
93
|
+
result += "\n\n";
|
|
94
|
+
}
|
|
95
|
+
result += markdownDescription_1;
|
|
96
|
+
}
|
|
97
|
+
if (markdownEnumValueDescription_1) {
|
|
98
|
+
if (result.length > 0) {
|
|
99
|
+
result += "\n\n";
|
|
100
|
+
}
|
|
101
|
+
result += "`" + toMarkdownCodeBlock(enumValue_1) + "`: " + markdownEnumValueDescription_1;
|
|
102
|
+
}
|
|
103
|
+
return createHover([result]);
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
return JSONHover;
|
|
109
|
+
}());
|
|
110
|
+
exports.JSONHover = JSONHover;
|
|
111
|
+
function toMarkdown(plain) {
|
|
112
|
+
if (plain) {
|
|
113
|
+
var res = plain.replace(/([^\n\r])(\r?\n)([^\n\r])/gm, '$1\n\n$3'); // single new lines to \n\n (Markdown paragraph)
|
|
114
|
+
return res.replace(/[\\`*_{}[\]()#+\-.!]/g, "\\$&"); // escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash
|
|
115
|
+
}
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
function toMarkdownCodeBlock(content) {
|
|
119
|
+
// see https://daringfireball.net/projects/markdown/syntax#precode
|
|
120
|
+
if (content.indexOf('`') !== -1) {
|
|
121
|
+
return '`` ' + content + ' ``';
|
|
122
|
+
}
|
|
123
|
+
return content;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/*---------------------------------------------------------------------------------------------
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
4
|
+
*--------------------------------------------------------------------------------------------*/
|
|
5
|
+
(function (factory) {
|
|
6
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
7
|
+
var v = factory(require, exports);
|
|
8
|
+
if (v !== undefined) module.exports = v;
|
|
9
|
+
}
|
|
10
|
+
else if (typeof define === "function" && define.amd) {
|
|
11
|
+
define(["require", "exports", "../jsonLanguageTypes"], factory);
|
|
12
|
+
}
|
|
13
|
+
})(function (require, exports) {
|
|
14
|
+
"use strict";
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.findLinks = void 0;
|
|
17
|
+
var jsonLanguageTypes_1 = require("../jsonLanguageTypes");
|
|
18
|
+
function findLinks(document, doc) {
|
|
19
|
+
var links = [];
|
|
20
|
+
doc.visit(function (node) {
|
|
21
|
+
var _a;
|
|
22
|
+
if (node.type === "property" && node.keyNode.value === "$ref" && ((_a = node.valueNode) === null || _a === void 0 ? void 0 : _a.type) === 'string') {
|
|
23
|
+
var path = node.valueNode.value;
|
|
24
|
+
var targetNode = findTargetNode(doc, path);
|
|
25
|
+
if (targetNode) {
|
|
26
|
+
var targetPos = document.positionAt(targetNode.offset);
|
|
27
|
+
links.push({
|
|
28
|
+
target: document.uri + "#" + (targetPos.line + 1) + "," + (targetPos.character + 1),
|
|
29
|
+
range: createRange(document, node.valueNode)
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return true;
|
|
34
|
+
});
|
|
35
|
+
return Promise.resolve(links);
|
|
36
|
+
}
|
|
37
|
+
exports.findLinks = findLinks;
|
|
38
|
+
function createRange(document, node) {
|
|
39
|
+
return jsonLanguageTypes_1.Range.create(document.positionAt(node.offset + 1), document.positionAt(node.offset + node.length - 1));
|
|
40
|
+
}
|
|
41
|
+
function findTargetNode(doc, path) {
|
|
42
|
+
var tokens = parseJSONPointer(path);
|
|
43
|
+
if (!tokens) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
return findNode(tokens, doc.root);
|
|
47
|
+
}
|
|
48
|
+
function findNode(pointer, node) {
|
|
49
|
+
if (!node) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
if (pointer.length === 0) {
|
|
53
|
+
return node;
|
|
54
|
+
}
|
|
55
|
+
var token = pointer.shift();
|
|
56
|
+
if (node && node.type === 'object') {
|
|
57
|
+
var propertyNode = node.properties.find(function (propertyNode) { return propertyNode.keyNode.value === token; });
|
|
58
|
+
if (!propertyNode) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
return findNode(pointer, propertyNode.valueNode);
|
|
62
|
+
}
|
|
63
|
+
else if (node && node.type === 'array') {
|
|
64
|
+
if (token.match(/^(0|[1-9][0-9]*)$/)) {
|
|
65
|
+
var index = Number.parseInt(token);
|
|
66
|
+
var arrayItem = node.items[index];
|
|
67
|
+
if (!arrayItem) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
return findNode(pointer, arrayItem);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
function parseJSONPointer(path) {
|
|
76
|
+
if (path === "#") {
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
if (path[0] !== '#' || path[1] !== '/') {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
return path.substring(2).split(/\//).map(unescape);
|
|
83
|
+
}
|
|
84
|
+
function unescape(str) {
|
|
85
|
+
return str.replace(/~1/g, '/').replace(/~0/g, '~');
|
|
86
|
+
}
|
|
87
|
+
});
|