vscode-json-languageservice 5.7.2 → 6.0.0-next.2

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 (52) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/lib/esm/jsonContributions.d.ts +1 -1
  3. package/lib/esm/jsonLanguageService.d.ts +2 -2
  4. package/lib/esm/jsonLanguageService.js +13 -13
  5. package/lib/esm/jsonLanguageTypes.d.ts +10 -2
  6. package/lib/esm/jsonSchema.d.ts +71 -2
  7. package/lib/esm/parser/jsonParser.js +224 -93
  8. package/lib/esm/services/configuration.js +2 -2
  9. package/lib/esm/services/jsonCompletion.js +5 -5
  10. package/lib/esm/services/jsonDocumentSymbols.js +4 -4
  11. package/lib/esm/services/jsonFolding.js +1 -1
  12. package/lib/esm/services/jsonHover.js +2 -2
  13. package/lib/esm/services/jsonLinks.js +94 -3
  14. package/lib/esm/services/jsonSchemaService.js +639 -100
  15. package/lib/esm/services/jsonSelectionRanges.js +1 -1
  16. package/lib/esm/services/jsonValidation.js +4 -4
  17. package/lib/esm/services/schemas/draft-2019-09-flat.d.ts +1 -1
  18. package/lib/esm/services/schemas/draft-2020-12-flat.d.ts +1 -1
  19. package/lib/esm/services/vocabularies.js +139 -0
  20. package/lib/esm/utils/format.js +1 -1
  21. package/lib/esm/utils/sort.js +3 -3
  22. package/package.json +23 -20
  23. package/lib/umd/jsonContributions.d.ts +0 -21
  24. package/lib/umd/jsonContributions.js +0 -12
  25. package/lib/umd/jsonLanguageService.d.ts +0 -30
  26. package/lib/umd/jsonLanguageService.js +0 -79
  27. package/lib/umd/jsonLanguageTypes.d.ts +0 -305
  28. package/lib/umd/jsonLanguageTypes.js +0 -109
  29. package/lib/umd/jsonSchema.d.ts +0 -92
  30. package/lib/umd/jsonSchema.js +0 -12
  31. package/lib/umd/parser/jsonParser.js +0 -1365
  32. package/lib/umd/services/configuration.js +0 -536
  33. package/lib/umd/services/jsonCompletion.js +0 -982
  34. package/lib/umd/services/jsonDocumentSymbols.js +0 -285
  35. package/lib/umd/services/jsonFolding.js +0 -133
  36. package/lib/umd/services/jsonHover.js +0 -125
  37. package/lib/umd/services/jsonLinks.js +0 -85
  38. package/lib/umd/services/jsonSchemaService.js +0 -631
  39. package/lib/umd/services/jsonSelectionRanges.js +0 -74
  40. package/lib/umd/services/jsonValidation.js +0 -165
  41. package/lib/umd/services/schemas/draft-2019-09-flat.d.ts +0 -278
  42. package/lib/umd/services/schemas/draft-2019-09-flat.js +0 -314
  43. package/lib/umd/services/schemas/draft-2020-12-flat.d.ts +0 -276
  44. package/lib/umd/services/schemas/draft-2020-12-flat.js +0 -307
  45. package/lib/umd/utils/colors.js +0 -83
  46. package/lib/umd/utils/format.js +0 -33
  47. package/lib/umd/utils/glob.js +0 -137
  48. package/lib/umd/utils/json.js +0 -55
  49. package/lib/umd/utils/objects.js +0 -86
  50. package/lib/umd/utils/propertyTree.js +0 -90
  51. package/lib/umd/utils/sort.js +0 -384
  52. package/lib/umd/utils/strings.js +0 -97
@@ -1,86 +0,0 @@
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"], factory);
12
- }
13
- })(function (require, exports) {
14
- "use strict";
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.equals = equals;
17
- exports.isNumber = isNumber;
18
- exports.isDefined = isDefined;
19
- exports.isBoolean = isBoolean;
20
- exports.isString = isString;
21
- exports.isObject = isObject;
22
- function equals(one, other) {
23
- if (one === other) {
24
- return true;
25
- }
26
- if (one === null || one === undefined || other === null || other === undefined) {
27
- return false;
28
- }
29
- if (typeof one !== typeof other) {
30
- return false;
31
- }
32
- if (typeof one !== 'object') {
33
- return false;
34
- }
35
- if ((Array.isArray(one)) !== (Array.isArray(other))) {
36
- return false;
37
- }
38
- let i, key;
39
- if (Array.isArray(one)) {
40
- if (one.length !== other.length) {
41
- return false;
42
- }
43
- for (i = 0; i < one.length; i++) {
44
- if (!equals(one[i], other[i])) {
45
- return false;
46
- }
47
- }
48
- }
49
- else {
50
- const oneKeys = [];
51
- for (key in one) {
52
- oneKeys.push(key);
53
- }
54
- oneKeys.sort();
55
- const otherKeys = [];
56
- for (key in other) {
57
- otherKeys.push(key);
58
- }
59
- otherKeys.sort();
60
- if (!equals(oneKeys, otherKeys)) {
61
- return false;
62
- }
63
- for (i = 0; i < oneKeys.length; i++) {
64
- if (!equals(one[oneKeys[i]], other[oneKeys[i]])) {
65
- return false;
66
- }
67
- }
68
- }
69
- return true;
70
- }
71
- function isNumber(val) {
72
- return typeof val === 'number';
73
- }
74
- function isDefined(val) {
75
- return typeof val !== 'undefined';
76
- }
77
- function isBoolean(val) {
78
- return typeof val === 'boolean';
79
- }
80
- function isString(val) {
81
- return typeof val === 'string';
82
- }
83
- function isObject(val) {
84
- return typeof val === 'object' && val !== null && !Array.isArray(val);
85
- }
86
- });
@@ -1,90 +0,0 @@
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"], factory);
12
- }
13
- })(function (require, exports) {
14
- "use strict";
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.PropertyTree = exports.Container = void 0;
17
- var Container;
18
- (function (Container) {
19
- Container[Container["Object"] = 0] = "Object";
20
- Container[Container["Array"] = 1] = "Array";
21
- })(Container || (exports.Container = Container = {}));
22
- class PropertyTree {
23
- constructor(propertyName, beginningLineNumber) {
24
- this.propertyName = propertyName ?? '';
25
- this.beginningLineNumber = beginningLineNumber;
26
- this.childrenProperties = [];
27
- this.lastProperty = false;
28
- this.noKeyName = false;
29
- }
30
- addChildProperty(childProperty) {
31
- childProperty.parent = this;
32
- if (this.childrenProperties.length > 0) {
33
- let insertionIndex = 0;
34
- if (childProperty.noKeyName) {
35
- insertionIndex = this.childrenProperties.length;
36
- }
37
- else {
38
- insertionIndex = binarySearchOnPropertyArray(this.childrenProperties, childProperty, compareProperties);
39
- }
40
- if (insertionIndex < 0) {
41
- insertionIndex = (insertionIndex * -1) - 1;
42
- }
43
- this.childrenProperties.splice(insertionIndex, 0, childProperty);
44
- }
45
- else {
46
- this.childrenProperties.push(childProperty);
47
- }
48
- return childProperty;
49
- }
50
- }
51
- exports.PropertyTree = PropertyTree;
52
- function compareProperties(propertyTree1, propertyTree2) {
53
- const propertyName1 = propertyTree1.propertyName.toLowerCase();
54
- const propertyName2 = propertyTree2.propertyName.toLowerCase();
55
- if (propertyName1 < propertyName2) {
56
- return -1;
57
- }
58
- else if (propertyName1 > propertyName2) {
59
- return 1;
60
- }
61
- return 0;
62
- }
63
- function binarySearchOnPropertyArray(propertyTreeArray, propertyTree, compare_fn) {
64
- const propertyName = propertyTree.propertyName.toLowerCase();
65
- const firstPropertyInArrayName = propertyTreeArray[0].propertyName.toLowerCase();
66
- const lastPropertyInArrayName = propertyTreeArray[propertyTreeArray.length - 1].propertyName.toLowerCase();
67
- if (propertyName < firstPropertyInArrayName) {
68
- return 0;
69
- }
70
- if (propertyName > lastPropertyInArrayName) {
71
- return propertyTreeArray.length;
72
- }
73
- let m = 0;
74
- let n = propertyTreeArray.length - 1;
75
- while (m <= n) {
76
- let k = (n + m) >> 1;
77
- let cmp = compare_fn(propertyTree, propertyTreeArray[k]);
78
- if (cmp > 0) {
79
- m = k + 1;
80
- }
81
- else if (cmp < 0) {
82
- n = k - 1;
83
- }
84
- else {
85
- return k;
86
- }
87
- }
88
- return -m - 1;
89
- }
90
- });
@@ -1,384 +0,0 @@
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", "./format", "./propertyTree"], factory);
12
- }
13
- })(function (require, exports) {
14
- "use strict";
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.sort = sort;
17
- const jsonc_parser_1 = require("jsonc-parser");
18
- const jsonLanguageTypes_1 = require("../jsonLanguageTypes");
19
- const format_1 = require("./format");
20
- const propertyTree_1 = require("./propertyTree");
21
- function sort(documentToSort, formattingOptions) {
22
- const options = {
23
- ...formattingOptions,
24
- keepLines: false, // keepLines must be false so that the properties are on separate lines for the sorting
25
- };
26
- const formattedJsonString = jsonLanguageTypes_1.TextDocument.applyEdits(documentToSort, (0, format_1.format)(documentToSort, options, undefined));
27
- const formattedJsonDocument = jsonLanguageTypes_1.TextDocument.create('test://test.json', 'json', 0, formattedJsonString);
28
- const jsonPropertyTree = findJsoncPropertyTree(formattedJsonDocument);
29
- const sortedJsonDocument = sortJsoncDocument(formattedJsonDocument, jsonPropertyTree);
30
- const edits = (0, format_1.format)(sortedJsonDocument, options, undefined);
31
- const sortedAndFormattedJsonDocument = jsonLanguageTypes_1.TextDocument.applyEdits(sortedJsonDocument, edits);
32
- return [jsonLanguageTypes_1.TextEdit.replace(jsonLanguageTypes_1.Range.create(jsonLanguageTypes_1.Position.create(0, 0), documentToSort.positionAt(documentToSort.getText().length)), sortedAndFormattedJsonDocument)];
33
- }
34
- function findJsoncPropertyTree(formattedDocument) {
35
- const formattedString = formattedDocument.getText();
36
- const scanner = (0, jsonc_parser_1.createScanner)(formattedString, false);
37
- // The tree that will be returned
38
- let rootTree = new propertyTree_1.PropertyTree();
39
- // The tree where the current properties can be added as children
40
- let currentTree = rootTree;
41
- // The tree representing the current property analyzed
42
- let currentProperty = rootTree;
43
- // The tree representing the previous property analyzed
44
- let lastProperty = rootTree;
45
- // The current scanned token
46
- let token = undefined;
47
- // Line number of the last token found
48
- let lastTokenLine = 0;
49
- // Total number of characters on the lines prior to current line
50
- let numberOfCharactersOnPreviousLines = 0;
51
- // The last token scanned that is not trivial, nor a comment
52
- let lastNonTriviaNonCommentToken = undefined;
53
- // The second to last token scanned that is not trivial, nor a comment
54
- let secondToLastNonTriviaNonCommentToken = undefined;
55
- // Line number of last token that is not trivial, nor a comment
56
- let lineOfLastNonTriviaNonCommentToken = -1;
57
- // End index on its line of last token that is not trivial, nor a comment
58
- let endIndexOfLastNonTriviaNonCommentToken = -1;
59
- // Line number of the start of the range of current/next property
60
- let beginningLineNumber = 0;
61
- // Line number of the end of the range of current/next property
62
- let endLineNumber = 0;
63
- // Stack indicating whether we are inside of an object or an array
64
- let currentContainerStack = [];
65
- // Boolean indicating that the current property end line number needs to be updated. Used only when block comments are encountered.
66
- let updateLastPropertyEndLineNumber = false;
67
- // Boolean indicating that the beginning line number should be updated. Used only when block comments are encountered.
68
- let updateBeginningLineNumber = false;
69
- while ((token = scanner.scan()) !== 17 /* SyntaxKind.EOF */) {
70
- // In the case when a block comment has been encountered that starts on the same line as the comma ending a property, update the end line of that
71
- // property so that it covers the block comment. For example, if we have:
72
- // 1. "key" : {}, /* some block
73
- // 2. comment */
74
- // Then, the end line of the property "key" should be line 2 not line 1
75
- if (updateLastPropertyEndLineNumber === true
76
- && token !== 14 /* SyntaxKind.LineBreakTrivia */
77
- && token !== 15 /* SyntaxKind.Trivia */
78
- && token !== 12 /* SyntaxKind.LineCommentTrivia */
79
- && token !== 13 /* SyntaxKind.BlockCommentTrivia */
80
- && currentProperty.endLineNumber === undefined) {
81
- let endLineNumber = scanner.getTokenStartLine();
82
- // Update the end line number in the case when the last property visited is a container (object or array)
83
- if (secondToLastNonTriviaNonCommentToken === 2 /* SyntaxKind.CloseBraceToken */
84
- || secondToLastNonTriviaNonCommentToken === 4 /* SyntaxKind.CloseBracketToken */) {
85
- lastProperty.endLineNumber = endLineNumber - 1;
86
- }
87
- // Update the end line number in the case when the last property visited is a simple property
88
- else {
89
- currentProperty.endLineNumber = endLineNumber - 1;
90
- }
91
- beginningLineNumber = endLineNumber;
92
- updateLastPropertyEndLineNumber = false;
93
- }
94
- // When a block comment follows an open brace or an open bracket, that block comment should be associated to that brace or bracket, not the property below it. For example, for:
95
- // 1. { /*
96
- // 2. ... */
97
- // 3. "key" : {}
98
- // 4. }
99
- // Instead of associating the block comment to the property on line 3, it is associate to the property on line 1
100
- if (updateBeginningLineNumber === true
101
- && token !== 14 /* SyntaxKind.LineBreakTrivia */
102
- && token !== 15 /* SyntaxKind.Trivia */
103
- && token !== 12 /* SyntaxKind.LineCommentTrivia */
104
- && token !== 13 /* SyntaxKind.BlockCommentTrivia */) {
105
- beginningLineNumber = scanner.getTokenStartLine();
106
- updateBeginningLineNumber = false;
107
- }
108
- // Update the number of characters on all the previous lines each time the new token is on a different line to the previous token
109
- if (scanner.getTokenStartLine() !== lastTokenLine) {
110
- for (let i = lastTokenLine; i < scanner.getTokenStartLine(); i++) {
111
- const lengthOfLine = formattedDocument.getText(jsonLanguageTypes_1.Range.create(jsonLanguageTypes_1.Position.create(i, 0), jsonLanguageTypes_1.Position.create(i + 1, 0))).length;
112
- numberOfCharactersOnPreviousLines = numberOfCharactersOnPreviousLines + lengthOfLine;
113
- }
114
- lastTokenLine = scanner.getTokenStartLine();
115
- }
116
- switch (token) {
117
- // When a string is found, if it follows an open brace or a comma token and it is within an object, then it corresponds to a key name, not a simple string
118
- case 10 /* SyntaxKind.StringLiteral */: {
119
- if ((lastNonTriviaNonCommentToken === undefined
120
- || lastNonTriviaNonCommentToken === 1 /* SyntaxKind.OpenBraceToken */
121
- || (lastNonTriviaNonCommentToken === 5 /* SyntaxKind.CommaToken */
122
- && currentContainerStack[currentContainerStack.length - 1] === propertyTree_1.Container.Object))) {
123
- // In that case create the child property which starts at beginningLineNumber, add it to the current tree
124
- const childProperty = new propertyTree_1.PropertyTree(scanner.getTokenValue(), beginningLineNumber);
125
- lastProperty = currentProperty;
126
- currentProperty = currentTree.addChildProperty(childProperty);
127
- }
128
- break;
129
- }
130
- // When the token is an open bracket, then we enter into an array
131
- case 3 /* SyntaxKind.OpenBracketToken */: {
132
- // If the root tree beginning line number is not defined, then this open bracket is the first open bracket in the document
133
- if (rootTree.beginningLineNumber === undefined) {
134
- rootTree.beginningLineNumber = scanner.getTokenStartLine();
135
- }
136
- // Suppose we are inside of an object, then the current array is associated to a key, and has already been created
137
- // We have the following configuration: {"a": "val", "array": [...], "b": "val"}
138
- // In that case navigate down to the child property
139
- if (currentContainerStack[currentContainerStack.length - 1] === propertyTree_1.Container.Object) {
140
- currentTree = currentProperty;
141
- }
142
- // Suppose we are inside of an array, then since the current array is not associated to a key, it has not been created yet
143
- // We have the following configuration: ["a", [...], "b"]
144
- // In that case create the property and navigate down
145
- else if (currentContainerStack[currentContainerStack.length - 1] === propertyTree_1.Container.Array) {
146
- const childProperty = new propertyTree_1.PropertyTree(scanner.getTokenValue(), beginningLineNumber);
147
- childProperty.noKeyName = true;
148
- lastProperty = currentProperty;
149
- currentProperty = currentTree.addChildProperty(childProperty);
150
- currentTree = currentProperty;
151
- }
152
- currentContainerStack.push(propertyTree_1.Container.Array);
153
- currentProperty.type = propertyTree_1.Container.Array;
154
- beginningLineNumber = scanner.getTokenStartLine();
155
- beginningLineNumber++;
156
- break;
157
- }
158
- // When the token is an open brace, then we enter into an object
159
- case 1 /* SyntaxKind.OpenBraceToken */: {
160
- // If the root tree beginning line number is not defined, then this open brace is the first open brace in the document
161
- if (rootTree.beginningLineNumber === undefined) {
162
- rootTree.beginningLineNumber = scanner.getTokenStartLine();
163
- }
164
- // 1. If we are inside of an objet, the current object is associated to a key and has already been created
165
- // We have the following configuration: {"a": "val", "object": {...}, "b": "val"}
166
- // 2. Otherwise the current object property is inside of an array, not associated to a key name and the property has not yet been created
167
- // We have the following configuration: ["a", {...}, "b"]
168
- else if (currentContainerStack[currentContainerStack.length - 1] === propertyTree_1.Container.Array) {
169
- const childProperty = new propertyTree_1.PropertyTree(scanner.getTokenValue(), beginningLineNumber);
170
- childProperty.noKeyName = true;
171
- lastProperty = currentProperty;
172
- currentProperty = currentTree.addChildProperty(childProperty);
173
- }
174
- currentProperty.type = propertyTree_1.Container.Object;
175
- currentContainerStack.push(propertyTree_1.Container.Object);
176
- currentTree = currentProperty;
177
- beginningLineNumber = scanner.getTokenStartLine();
178
- beginningLineNumber++;
179
- break;
180
- }
181
- case 4 /* SyntaxKind.CloseBracketToken */: {
182
- endLineNumber = scanner.getTokenStartLine();
183
- currentContainerStack.pop();
184
- // If the last non-trivial non-comment token is a closing brace or bracket, then the currentProperty end line number has not been set yet so set it
185
- // The configuration considered is: [..., {}] or [..., []]
186
- if (currentProperty.endLineNumber === undefined
187
- && (lastNonTriviaNonCommentToken === 2 /* SyntaxKind.CloseBraceToken */
188
- || lastNonTriviaNonCommentToken === 4 /* SyntaxKind.CloseBracketToken */)) {
189
- currentProperty.endLineNumber = endLineNumber - 1;
190
- currentProperty.lastProperty = true;
191
- currentProperty.lineWhereToAddComma = lineOfLastNonTriviaNonCommentToken;
192
- currentProperty.indexWhereToAddComa = endIndexOfLastNonTriviaNonCommentToken;
193
- lastProperty = currentProperty;
194
- currentProperty = currentProperty ? currentProperty.parent : undefined;
195
- currentTree = currentProperty;
196
- }
197
- rootTree.endLineNumber = endLineNumber;
198
- beginningLineNumber = endLineNumber + 1;
199
- break;
200
- }
201
- case 2 /* SyntaxKind.CloseBraceToken */: {
202
- endLineNumber = scanner.getTokenStartLine();
203
- currentContainerStack.pop();
204
- // If we are not inside of an empty object
205
- if (lastNonTriviaNonCommentToken !== 1 /* SyntaxKind.OpenBraceToken */) {
206
- // If current property end line number has not yet been defined, define it
207
- if (currentProperty.endLineNumber === undefined) {
208
- currentProperty.endLineNumber = endLineNumber - 1;
209
- // The current property is also the last property
210
- currentProperty.lastProperty = true;
211
- // The last property of an object is associated with the line and index of where to add the comma, in case after sorting, it is no longer the last property
212
- currentProperty.lineWhereToAddComma = lineOfLastNonTriviaNonCommentToken;
213
- currentProperty.indexWhereToAddComa = endIndexOfLastNonTriviaNonCommentToken;
214
- }
215
- lastProperty = currentProperty;
216
- currentProperty = currentProperty ? currentProperty.parent : undefined;
217
- currentTree = currentProperty;
218
- }
219
- rootTree.endLineNumber = scanner.getTokenStartLine();
220
- beginningLineNumber = endLineNumber + 1;
221
- break;
222
- }
223
- case 5 /* SyntaxKind.CommaToken */: {
224
- endLineNumber = scanner.getTokenStartLine();
225
- // If the current container is an object or the current container is an array and the last non-trivia non-comment token is a closing brace or a closing bracket
226
- // Then update the end line number of the current property
227
- if (currentProperty.endLineNumber === undefined
228
- && (currentContainerStack[currentContainerStack.length - 1] === propertyTree_1.Container.Object
229
- || (currentContainerStack[currentContainerStack.length - 1] === propertyTree_1.Container.Array
230
- && (lastNonTriviaNonCommentToken === 2 /* SyntaxKind.CloseBraceToken */
231
- || lastNonTriviaNonCommentToken === 4 /* SyntaxKind.CloseBracketToken */)))) {
232
- currentProperty.endLineNumber = endLineNumber;
233
- // Store the line and the index of the comma in case it needs to be removed during the sorting
234
- currentProperty.commaIndex = scanner.getTokenOffset() - numberOfCharactersOnPreviousLines;
235
- currentProperty.commaLine = endLineNumber;
236
- }
237
- if (lastNonTriviaNonCommentToken === 2 /* SyntaxKind.CloseBraceToken */
238
- || lastNonTriviaNonCommentToken === 4 /* SyntaxKind.CloseBracketToken */) {
239
- lastProperty = currentProperty;
240
- currentProperty = currentProperty ? currentProperty.parent : undefined;
241
- currentTree = currentProperty;
242
- }
243
- beginningLineNumber = endLineNumber + 1;
244
- break;
245
- }
246
- case 13 /* SyntaxKind.BlockCommentTrivia */: {
247
- // If the last non trivia non-comment token is a comma and the block comment starts on the same line as the comma, then update the end line number of the current property. For example if:
248
- // 1. {}, /* ...
249
- // 2. ..*/
250
- // The the property on line 1 shoud end on line 2, not line 1
251
- // In the case we are in an array we update the end line number only if the second to last non-trivia non-comment token is a closing brace or bracket
252
- if (lastNonTriviaNonCommentToken === 5 /* SyntaxKind.CommaToken */
253
- && lineOfLastNonTriviaNonCommentToken === scanner.getTokenStartLine()
254
- && (currentContainerStack[currentContainerStack.length - 1] === propertyTree_1.Container.Array
255
- && (secondToLastNonTriviaNonCommentToken === 2 /* SyntaxKind.CloseBraceToken */
256
- || secondToLastNonTriviaNonCommentToken === 4 /* SyntaxKind.CloseBracketToken */)
257
- || currentContainerStack[currentContainerStack.length - 1] === propertyTree_1.Container.Object)) {
258
- if (currentContainerStack[currentContainerStack.length - 1] === propertyTree_1.Container.Array && (secondToLastNonTriviaNonCommentToken === 2 /* SyntaxKind.CloseBraceToken */ || secondToLastNonTriviaNonCommentToken === 4 /* SyntaxKind.CloseBracketToken */) || currentContainerStack[currentContainerStack.length - 1] === propertyTree_1.Container.Object) {
259
- currentProperty.endLineNumber = undefined;
260
- updateLastPropertyEndLineNumber = true;
261
- }
262
- }
263
- // When the block comment follows an open brace or an open token, we have the following scenario:
264
- // { /**
265
- // ../
266
- // }
267
- // The block comment should be assigned to the open brace not the first property below it
268
- if ((lastNonTriviaNonCommentToken === 1 /* SyntaxKind.OpenBraceToken */
269
- || lastNonTriviaNonCommentToken === 3 /* SyntaxKind.OpenBracketToken */)
270
- && lineOfLastNonTriviaNonCommentToken === scanner.getTokenStartLine()) {
271
- updateBeginningLineNumber = true;
272
- }
273
- break;
274
- }
275
- }
276
- // Update the last and second to last non-trivia non-comment tokens
277
- if (token !== 14 /* SyntaxKind.LineBreakTrivia */
278
- && token !== 13 /* SyntaxKind.BlockCommentTrivia */
279
- && token !== 12 /* SyntaxKind.LineCommentTrivia */
280
- && token !== 15 /* SyntaxKind.Trivia */) {
281
- secondToLastNonTriviaNonCommentToken = lastNonTriviaNonCommentToken;
282
- lastNonTriviaNonCommentToken = token;
283
- lineOfLastNonTriviaNonCommentToken = scanner.getTokenStartLine();
284
- endIndexOfLastNonTriviaNonCommentToken = scanner.getTokenOffset() + scanner.getTokenLength() - numberOfCharactersOnPreviousLines;
285
- }
286
- }
287
- return rootTree;
288
- }
289
- function sortJsoncDocument(jsonDocument, propertyTree) {
290
- if (propertyTree.childrenProperties.length === 0) {
291
- return jsonDocument;
292
- }
293
- const sortedJsonDocument = jsonLanguageTypes_1.TextDocument.create('test://test.json', 'json', 0, jsonDocument.getText());
294
- const queueToSort = [];
295
- updateSortingQueue(queueToSort, propertyTree, propertyTree.beginningLineNumber);
296
- while (queueToSort.length > 0) {
297
- const dataToSort = queueToSort.shift();
298
- const propertyTreeArray = dataToSort.propertyTreeArray;
299
- let beginningLineNumber = dataToSort.beginningLineNumber;
300
- for (let i = 0; i < propertyTreeArray.length; i++) {
301
- const propertyTree = propertyTreeArray[i];
302
- const range = jsonLanguageTypes_1.Range.create(jsonLanguageTypes_1.Position.create(propertyTree.beginningLineNumber, 0), jsonLanguageTypes_1.Position.create(propertyTree.endLineNumber + 1, 0));
303
- const jsonContentToReplace = jsonDocument.getText(range);
304
- const jsonDocumentToReplace = jsonLanguageTypes_1.TextDocument.create('test://test.json', 'json', 0, jsonContentToReplace);
305
- if (propertyTree.lastProperty === true && i !== propertyTreeArray.length - 1) {
306
- const lineWhereToAddComma = propertyTree.lineWhereToAddComma - propertyTree.beginningLineNumber;
307
- const indexWhereToAddComma = propertyTree.indexWhereToAddComa;
308
- const edit = {
309
- range: jsonLanguageTypes_1.Range.create(jsonLanguageTypes_1.Position.create(lineWhereToAddComma, indexWhereToAddComma), jsonLanguageTypes_1.Position.create(lineWhereToAddComma, indexWhereToAddComma)),
310
- text: ','
311
- };
312
- jsonLanguageTypes_1.TextDocument.update(jsonDocumentToReplace, [edit], 1);
313
- }
314
- else if (propertyTree.lastProperty === false && i === propertyTreeArray.length - 1) {
315
- const commaIndex = propertyTree.commaIndex;
316
- const commaLine = propertyTree.commaLine;
317
- const lineWhereToRemoveComma = commaLine - propertyTree.beginningLineNumber;
318
- const edit = {
319
- range: jsonLanguageTypes_1.Range.create(jsonLanguageTypes_1.Position.create(lineWhereToRemoveComma, commaIndex), jsonLanguageTypes_1.Position.create(lineWhereToRemoveComma, commaIndex + 1)),
320
- text: ''
321
- };
322
- jsonLanguageTypes_1.TextDocument.update(jsonDocumentToReplace, [edit], 1);
323
- }
324
- const length = propertyTree.endLineNumber - propertyTree.beginningLineNumber + 1;
325
- const edit = {
326
- range: jsonLanguageTypes_1.Range.create(jsonLanguageTypes_1.Position.create(beginningLineNumber, 0), jsonLanguageTypes_1.Position.create(beginningLineNumber + length, 0)),
327
- text: jsonDocumentToReplace.getText()
328
- };
329
- jsonLanguageTypes_1.TextDocument.update(sortedJsonDocument, [edit], 1);
330
- updateSortingQueue(queueToSort, propertyTree, beginningLineNumber);
331
- beginningLineNumber = beginningLineNumber + length;
332
- }
333
- }
334
- return sortedJsonDocument;
335
- }
336
- function sortProperties(properties) {
337
- properties.sort((a, b) => a.propertyName.localeCompare(b.propertyName));
338
- }
339
- function updateSortingQueue(queue, propertyTree, beginningLineNumber) {
340
- if (propertyTree.childrenProperties.length === 0) {
341
- return;
342
- }
343
- if (propertyTree.type === propertyTree_1.Container.Object) {
344
- let minimumBeginningLineNumber = Infinity;
345
- for (const childProperty of propertyTree.childrenProperties) {
346
- if (childProperty.beginningLineNumber < minimumBeginningLineNumber) {
347
- minimumBeginningLineNumber = childProperty.beginningLineNumber;
348
- }
349
- }
350
- const diff = minimumBeginningLineNumber - propertyTree.beginningLineNumber;
351
- beginningLineNumber = beginningLineNumber + diff;
352
- sortProperties(propertyTree.childrenProperties);
353
- queue.push(new SortingRange(beginningLineNumber, propertyTree.childrenProperties));
354
- }
355
- else if (propertyTree.type === propertyTree_1.Container.Array) {
356
- updateSortingQueueForArrayProperties(queue, propertyTree, beginningLineNumber);
357
- }
358
- }
359
- function updateSortingQueueForArrayProperties(queue, propertyTree, beginningLineNumber) {
360
- for (const subObject of propertyTree.childrenProperties) {
361
- // If the child property of the array is an object, then you can sort the properties within this object
362
- if (subObject.type === propertyTree_1.Container.Object) {
363
- let minimumBeginningLineNumber = Infinity;
364
- for (const childProperty of subObject.childrenProperties) {
365
- if (childProperty.beginningLineNumber < minimumBeginningLineNumber) {
366
- minimumBeginningLineNumber = childProperty.beginningLineNumber;
367
- }
368
- }
369
- const diff = minimumBeginningLineNumber - subObject.beginningLineNumber;
370
- queue.push(new SortingRange(beginningLineNumber + subObject.beginningLineNumber - propertyTree.beginningLineNumber + diff, subObject.childrenProperties));
371
- }
372
- // If the child property of the array is an array, then you need to recurse on the children properties, until you find an object to sort
373
- if (subObject.type === propertyTree_1.Container.Array) {
374
- updateSortingQueueForArrayProperties(queue, subObject, beginningLineNumber + subObject.beginningLineNumber - propertyTree.beginningLineNumber);
375
- }
376
- }
377
- }
378
- class SortingRange {
379
- constructor(beginningLineNumber, propertyTreeArray) {
380
- this.beginningLineNumber = beginningLineNumber;
381
- this.propertyTreeArray = propertyTreeArray;
382
- }
383
- }
384
- });
@@ -1,97 +0,0 @@
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"], factory);
12
- }
13
- })(function (require, exports) {
14
- "use strict";
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.startsWith = startsWith;
17
- exports.endsWith = endsWith;
18
- exports.convertSimple2RegExpPattern = convertSimple2RegExpPattern;
19
- exports.repeat = repeat;
20
- exports.extendedRegExp = extendedRegExp;
21
- exports.stringLength = stringLength;
22
- function startsWith(haystack, needle) {
23
- if (haystack.length < needle.length) {
24
- return false;
25
- }
26
- for (let i = 0; i < needle.length; i++) {
27
- if (haystack[i] !== needle[i]) {
28
- return false;
29
- }
30
- }
31
- return true;
32
- }
33
- /**
34
- * Determines if haystack ends with needle.
35
- */
36
- function endsWith(haystack, needle) {
37
- const diff = haystack.length - needle.length;
38
- if (diff > 0) {
39
- return haystack.lastIndexOf(needle) === diff;
40
- }
41
- else if (diff === 0) {
42
- return haystack === needle;
43
- }
44
- else {
45
- return false;
46
- }
47
- }
48
- function convertSimple2RegExpPattern(pattern) {
49
- return pattern.replace(/[\-\\\{\}\+\?\|\^\$\.\,\[\]\(\)\#\s]/g, '\\$&').replace(/[\*]/g, '.*');
50
- }
51
- function repeat(value, count) {
52
- let s = '';
53
- while (count > 0) {
54
- if ((count & 1) === 1) {
55
- s += value;
56
- }
57
- value += value;
58
- count = count >>> 1;
59
- }
60
- return s;
61
- }
62
- function extendedRegExp(pattern) {
63
- let flags = '';
64
- if (startsWith(pattern, '(?i)')) {
65
- pattern = pattern.substring(4);
66
- flags = 'i';
67
- }
68
- try {
69
- return new RegExp(pattern, flags + 'u');
70
- }
71
- catch (e) {
72
- // could be an exception due to the 'u ' flag
73
- try {
74
- return new RegExp(pattern, flags);
75
- }
76
- catch (e) {
77
- // invalid pattern
78
- return undefined;
79
- }
80
- }
81
- }
82
- // from https://tanishiking.github.io/posts/count-unicode-codepoint/#work-hard-with-for-statements
83
- function stringLength(str) {
84
- let count = 0;
85
- for (let i = 0; i < str.length; i++) {
86
- count++;
87
- // obtain the i-th 16-bit
88
- const code = str.charCodeAt(i);
89
- if (0xD800 <= code && code <= 0xDBFF) {
90
- // if the i-th 16bit is an upper surrogate
91
- // skip the next 16 bits (lower surrogate)
92
- i++;
93
- }
94
- }
95
- return count;
96
- }
97
- });