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 *--------------------------------------------------------------------------------------------*/
6 if (typeof module === "object" && typeof module.exports === "object") {
7 var v = factory(require, exports);
8 if (v !== undefined) module.exports = v;
10 else if (typeof define === "function" && define.amd) {
11 define(["require", "exports", "../parser/jsonParser", "../utils/strings", "../utils/colors", "../jsonLanguageTypes"], factory);
13 })(function (require, exports) {
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;
25 JSONDocumentSymbols.prototype.findDocumentSymbols = function (document, doc, context) {
27 if (context === void 0) { context = { resultLimit: Number.MAX_VALUE }; }
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') {
38 for (var _i = 0, _a = root.items; _i < _a.length; _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 });
48 if (context && context.onResultLimitExceeded) {
49 context.onResultLimitExceeded(resourceString);
61 { node: root, containerName: '' }
64 var limitExceeded = false;
66 var collectOutlineEntries = function (node, containerName) {
67 if (node.type === 'array') {
68 node.items.forEach(function (node) {
70 toVisit.push({ node: node, containerName: containerName });
74 else if (node.type === 'object') {
75 node.properties.forEach(function (property) {
76 var valueNode = property.valueNode;
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 });
92 // breath first traversal
93 while (nextToVisit < toVisit.length) {
94 var next = toVisit[nextToVisit++];
95 collectOutlineEntries(next.node, next.containerName);
97 if (limitExceeded && context && context.onResultLimitExceeded) {
98 context.onResultLimitExceeded(resourceString);
102 JSONDocumentSymbols.prototype.findDocumentSymbols2 = function (document, doc, context) {
104 if (context === void 0) { context = { resultLimit: Number.MAX_VALUE }; }
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') {
115 for (var _i = 0, _a = root.items; _i < _a.length; _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 });
126 if (context && context.onResultLimitExceeded) {
127 context.onResultLimitExceeded(resourceString);
140 { node: root, result: result }
143 var limitExceeded = false;
144 var collectOutlineEntries = function (node, result) {
145 if (node.type === 'array') {
146 node.items.forEach(function (node, index) {
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: [] };
155 toVisit.push({ result: symbol.children, node: node });
158 limitExceeded = true;
163 else if (node.type === 'object') {
164 node.properties.forEach(function (property) {
165 var valueNode = property.valueNode;
169 var range = getRange(document, property);
170 var selectionRange = getRange(document, property.keyNode);
172 var symbol = { name: _this.getKeyLabel(property), kind: _this.getSymbolKind(valueNode.type), range: range, selectionRange: selectionRange, children: children, detail: _this.getDetail(valueNode) };
174 toVisit.push({ result: children, node: valueNode });
177 limitExceeded = true;
183 // breath first traversal
184 while (nextToVisit < toVisit.length) {
185 var next = toVisit[nextToVisit++];
186 collectOutlineEntries(next.node, next.result);
188 if (limitExceeded && context && context.onResultLimitExceeded) {
189 context.onResultLimitExceeded(resourceString);
193 JSONDocumentSymbols.prototype.getSymbolKind = function (nodeType) {
196 return jsonLanguageTypes_1.SymbolKind.Module;
198 return jsonLanguageTypes_1.SymbolKind.String;
200 return jsonLanguageTypes_1.SymbolKind.Number;
202 return jsonLanguageTypes_1.SymbolKind.Array;
204 return jsonLanguageTypes_1.SymbolKind.Boolean;
206 return jsonLanguageTypes_1.SymbolKind.Variable;
209 JSONDocumentSymbols.prototype.getKeyLabel = function (property) {
210 var name = property.keyNode.value;
212 name = name.replace(/[\n]/g, '↵');
214 if (name && name.trim()) {
217 return "\"" + name + "\"";
219 JSONDocumentSymbols.prototype.getDetail = function (node) {
223 if (node.type === 'boolean' || node.type === 'number' || node.type === 'null' || node.type === 'string') {
224 return String(node.value);
227 if (node.type === 'array') {
228 return node.children.length ? undefined : '[]';
230 else if (node.type === 'object') {
231 return node.children.length ? undefined : '{}';
236 JSONDocumentSymbols.prototype.findDocumentColors = function (document, doc, context) {
237 return this.schemaService.getSchemaForResource(document.uri, doc).then(function (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));
250 var range = getRange(document, s.node);
251 result.push({ color: color, range: range });
253 visitedNode[nodeId] = true;
256 if (context && context.onResultLimitExceeded) {
257 context.onResultLimitExceeded(document.uri);
268 JSONDocumentSymbols.prototype.getColorPresentations = function (document, doc, color, range) {
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;
276 if (color.alpha === 1) {
277 label = "#" + toTwoDigitHex(red256) + toTwoDigitHex(green256) + toTwoDigitHex(blue256);
280 label = "#" + toTwoDigitHex(red256) + toTwoDigitHex(green256) + toTwoDigitHex(blue256) + toTwoDigitHex(Math.round(color.alpha * 255));
282 result.push({ label: label, textEdit: jsonLanguageTypes_1.TextEdit.replace(range, JSON.stringify(label)) });
285 return JSONDocumentSymbols;
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));