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