jsonWorker.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  6. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  7. return new (P || (P = Promise))(function (resolve, reject) {
  8. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  9. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  10. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  11. step((generator = generator.apply(thisArg, _arguments || [])).next());
  12. });
  13. };
  14. var __generator = (this && this.__generator) || function (thisArg, body) {
  15. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  16. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  17. function verb(n) { return function (v) { return step([n, v]); }; }
  18. function step(op) {
  19. if (f) throw new TypeError("Generator is already executing.");
  20. while (_) try {
  21. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  22. if (y = 0, t) op = [op[0] & 2, t.value];
  23. switch (op[0]) {
  24. case 0: case 1: t = op; break;
  25. case 4: _.label++; return { value: op[1], done: false };
  26. case 5: _.label++; y = op[1]; op = [0]; continue;
  27. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  28. default:
  29. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  30. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  31. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  32. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  33. if (t[2]) _.ops.pop();
  34. _.trys.pop(); continue;
  35. }
  36. op = body.call(thisArg, _);
  37. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  38. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  39. }
  40. };
  41. import * as jsonService from './_deps/vscode-json-languageservice/jsonLanguageService.js';
  42. import { URI } from './_deps/vscode-uri/index.js';
  43. var defaultSchemaRequestService;
  44. if (typeof fetch !== 'undefined') {
  45. defaultSchemaRequestService = function (url) {
  46. return fetch(url).then(function (response) { return response.text(); });
  47. };
  48. }
  49. var JSONWorker = /** @class */ (function () {
  50. function JSONWorker(ctx, createData) {
  51. this._ctx = ctx;
  52. this._languageSettings = createData.languageSettings;
  53. this._languageId = createData.languageId;
  54. this._languageService = jsonService.getLanguageService({
  55. workspaceContext: {
  56. resolveRelativePath: function (relativePath, resource) {
  57. var base = resource.substr(0, resource.lastIndexOf('/') + 1);
  58. return resolvePath(base, relativePath);
  59. }
  60. },
  61. schemaRequestService: createData.enableSchemaRequest && defaultSchemaRequestService
  62. });
  63. this._languageService.configure(this._languageSettings);
  64. }
  65. JSONWorker.prototype.doValidation = function (uri) {
  66. return __awaiter(this, void 0, void 0, function () {
  67. var document, jsonDocument;
  68. return __generator(this, function (_a) {
  69. document = this._getTextDocument(uri);
  70. if (document) {
  71. jsonDocument = this._languageService.parseJSONDocument(document);
  72. return [2 /*return*/, this._languageService.doValidation(document, jsonDocument, this._languageSettings)];
  73. }
  74. return [2 /*return*/, Promise.resolve([])];
  75. });
  76. });
  77. };
  78. JSONWorker.prototype.doComplete = function (uri, position) {
  79. return __awaiter(this, void 0, void 0, function () {
  80. var document, jsonDocument;
  81. return __generator(this, function (_a) {
  82. document = this._getTextDocument(uri);
  83. jsonDocument = this._languageService.parseJSONDocument(document);
  84. return [2 /*return*/, this._languageService.doComplete(document, position, jsonDocument)];
  85. });
  86. });
  87. };
  88. JSONWorker.prototype.doResolve = function (item) {
  89. return __awaiter(this, void 0, void 0, function () {
  90. return __generator(this, function (_a) {
  91. return [2 /*return*/, this._languageService.doResolve(item)];
  92. });
  93. });
  94. };
  95. JSONWorker.prototype.doHover = function (uri, position) {
  96. return __awaiter(this, void 0, void 0, function () {
  97. var document, jsonDocument;
  98. return __generator(this, function (_a) {
  99. document = this._getTextDocument(uri);
  100. jsonDocument = this._languageService.parseJSONDocument(document);
  101. return [2 /*return*/, this._languageService.doHover(document, position, jsonDocument)];
  102. });
  103. });
  104. };
  105. JSONWorker.prototype.format = function (uri, range, options) {
  106. return __awaiter(this, void 0, void 0, function () {
  107. var document, textEdits;
  108. return __generator(this, function (_a) {
  109. document = this._getTextDocument(uri);
  110. textEdits = this._languageService.format(document, range, options);
  111. return [2 /*return*/, Promise.resolve(textEdits)];
  112. });
  113. });
  114. };
  115. JSONWorker.prototype.resetSchema = function (uri) {
  116. return __awaiter(this, void 0, void 0, function () {
  117. return __generator(this, function (_a) {
  118. return [2 /*return*/, Promise.resolve(this._languageService.resetSchema(uri))];
  119. });
  120. });
  121. };
  122. JSONWorker.prototype.findDocumentSymbols = function (uri) {
  123. return __awaiter(this, void 0, void 0, function () {
  124. var document, jsonDocument, symbols;
  125. return __generator(this, function (_a) {
  126. document = this._getTextDocument(uri);
  127. jsonDocument = this._languageService.parseJSONDocument(document);
  128. symbols = this._languageService.findDocumentSymbols(document, jsonDocument);
  129. return [2 /*return*/, Promise.resolve(symbols)];
  130. });
  131. });
  132. };
  133. JSONWorker.prototype.findDocumentColors = function (uri) {
  134. return __awaiter(this, void 0, void 0, function () {
  135. var document, jsonDocument, colorSymbols;
  136. return __generator(this, function (_a) {
  137. document = this._getTextDocument(uri);
  138. jsonDocument = this._languageService.parseJSONDocument(document);
  139. colorSymbols = this._languageService.findDocumentColors(document, jsonDocument);
  140. return [2 /*return*/, Promise.resolve(colorSymbols)];
  141. });
  142. });
  143. };
  144. JSONWorker.prototype.getColorPresentations = function (uri, color, range) {
  145. return __awaiter(this, void 0, void 0, function () {
  146. var document, jsonDocument, colorPresentations;
  147. return __generator(this, function (_a) {
  148. document = this._getTextDocument(uri);
  149. jsonDocument = this._languageService.parseJSONDocument(document);
  150. colorPresentations = this._languageService.getColorPresentations(document, jsonDocument, color, range);
  151. return [2 /*return*/, Promise.resolve(colorPresentations)];
  152. });
  153. });
  154. };
  155. JSONWorker.prototype.getFoldingRanges = function (uri, context) {
  156. return __awaiter(this, void 0, void 0, function () {
  157. var document, ranges;
  158. return __generator(this, function (_a) {
  159. document = this._getTextDocument(uri);
  160. ranges = this._languageService.getFoldingRanges(document, context);
  161. return [2 /*return*/, Promise.resolve(ranges)];
  162. });
  163. });
  164. };
  165. JSONWorker.prototype.getSelectionRanges = function (uri, positions) {
  166. return __awaiter(this, void 0, void 0, function () {
  167. var document, jsonDocument, ranges;
  168. return __generator(this, function (_a) {
  169. document = this._getTextDocument(uri);
  170. jsonDocument = this._languageService.parseJSONDocument(document);
  171. ranges = this._languageService.getSelectionRanges(document, positions, jsonDocument);
  172. return [2 /*return*/, Promise.resolve(ranges)];
  173. });
  174. });
  175. };
  176. JSONWorker.prototype._getTextDocument = function (uri) {
  177. var models = this._ctx.getMirrorModels();
  178. for (var _i = 0, models_1 = models; _i < models_1.length; _i++) {
  179. var model = models_1[_i];
  180. if (model.uri.toString() === uri) {
  181. return jsonService.TextDocument.create(uri, this._languageId, model.version, model.getValue());
  182. }
  183. }
  184. return null;
  185. };
  186. return JSONWorker;
  187. }());
  188. export { JSONWorker };
  189. // URI path utilities, will (hopefully) move to vscode-uri
  190. var Slash = '/'.charCodeAt(0);
  191. var Dot = '.'.charCodeAt(0);
  192. function isAbsolutePath(path) {
  193. return path.charCodeAt(0) === Slash;
  194. }
  195. function resolvePath(uriString, path) {
  196. if (isAbsolutePath(path)) {
  197. var uri = URI.parse(uriString);
  198. var parts = path.split('/');
  199. return uri.with({ path: normalizePath(parts) }).toString();
  200. }
  201. return joinPath(uriString, path);
  202. }
  203. function normalizePath(parts) {
  204. var newParts = [];
  205. for (var _i = 0, parts_1 = parts; _i < parts_1.length; _i++) {
  206. var part = parts_1[_i];
  207. if (part.length === 0 || (part.length === 1 && part.charCodeAt(0) === Dot)) {
  208. // ignore
  209. }
  210. else if (part.length === 2 && part.charCodeAt(0) === Dot && part.charCodeAt(1) === Dot) {
  211. newParts.pop();
  212. }
  213. else {
  214. newParts.push(part);
  215. }
  216. }
  217. if (parts.length > 1 && parts[parts.length - 1].length === 0) {
  218. newParts.push('');
  219. }
  220. var res = newParts.join('/');
  221. if (parts[0].length === 0) {
  222. res = '/' + res;
  223. }
  224. return res;
  225. }
  226. function joinPath(uriString) {
  227. var paths = [];
  228. for (var _i = 1; _i < arguments.length; _i++) {
  229. paths[_i - 1] = arguments[_i];
  230. }
  231. var uri = URI.parse(uriString);
  232. var parts = uri.path.split('/');
  233. for (var _a = 0, paths_1 = paths; _a < paths_1.length; _a++) {
  234. var path = paths_1[_a];
  235. parts.push.apply(parts, path.split('/'));
  236. }
  237. return uri.with({ path: normalizePath(parts) }).toString();
  238. }
  239. export function create(ctx, createData) {
  240. return new JSONWorker(ctx, createData);
  241. }