jsonLanguageService.js 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 { JSONCompletion } from './services/jsonCompletion';
  6. import { JSONHover } from './services/jsonHover';
  7. import { JSONValidation } from './services/jsonValidation';
  8. import { JSONDocumentSymbols } from './services/jsonDocumentSymbols';
  9. import { parse as parseJSON, newJSONDocument } from './parser/jsonParser';
  10. import { schemaContributions } from './services/configuration';
  11. import { JSONSchemaService } from './services/jsonSchemaService';
  12. import { getFoldingRanges } from './services/jsonFolding';
  13. import { getSelectionRanges } from './services/jsonSelectionRanges';
  14. import { format as formatJSON } from 'jsonc-parser';
  15. import { Range, TextEdit } from './jsonLanguageTypes';
  16. import { findLinks } from './services/jsonLinks';
  17. export * from './jsonLanguageTypes';
  18. export function getLanguageService(params) {
  19. var promise = params.promiseConstructor || Promise;
  20. var jsonSchemaService = new JSONSchemaService(params.schemaRequestService, params.workspaceContext, promise);
  21. jsonSchemaService.setSchemaContributions(schemaContributions);
  22. var jsonCompletion = new JSONCompletion(jsonSchemaService, params.contributions, promise, params.clientCapabilities);
  23. var jsonHover = new JSONHover(jsonSchemaService, params.contributions, promise);
  24. var jsonDocumentSymbols = new JSONDocumentSymbols(jsonSchemaService);
  25. var jsonValidation = new JSONValidation(jsonSchemaService, promise);
  26. return {
  27. configure: function (settings) {
  28. jsonSchemaService.clearExternalSchemas();
  29. if (settings.schemas) {
  30. settings.schemas.forEach(function (settings) {
  31. jsonSchemaService.registerExternalSchema(settings.uri, settings.fileMatch, settings.schema);
  32. });
  33. }
  34. jsonValidation.configure(settings);
  35. },
  36. resetSchema: function (uri) { return jsonSchemaService.onResourceChange(uri); },
  37. doValidation: jsonValidation.doValidation.bind(jsonValidation),
  38. parseJSONDocument: function (document) { return parseJSON(document, { collectComments: true }); },
  39. newJSONDocument: function (root, diagnostics) { return newJSONDocument(root, diagnostics); },
  40. getMatchingSchemas: jsonSchemaService.getMatchingSchemas.bind(jsonSchemaService),
  41. doResolve: jsonCompletion.doResolve.bind(jsonCompletion),
  42. doComplete: jsonCompletion.doComplete.bind(jsonCompletion),
  43. findDocumentSymbols: jsonDocumentSymbols.findDocumentSymbols.bind(jsonDocumentSymbols),
  44. findDocumentSymbols2: jsonDocumentSymbols.findDocumentSymbols2.bind(jsonDocumentSymbols),
  45. findDocumentColors: jsonDocumentSymbols.findDocumentColors.bind(jsonDocumentSymbols),
  46. getColorPresentations: jsonDocumentSymbols.getColorPresentations.bind(jsonDocumentSymbols),
  47. doHover: jsonHover.doHover.bind(jsonHover),
  48. getFoldingRanges: getFoldingRanges,
  49. getSelectionRanges: getSelectionRanges,
  50. findDefinition: function () { return Promise.resolve([]); },
  51. findLinks: findLinks,
  52. format: function (d, r, o) {
  53. var range = undefined;
  54. if (r) {
  55. var offset = d.offsetAt(r.start);
  56. var length = d.offsetAt(r.end) - offset;
  57. range = { offset: offset, length: length };
  58. }
  59. var options = { tabSize: o ? o.tabSize : 4, insertSpaces: (o === null || o === void 0 ? void 0 : o.insertSpaces) === true, insertFinalNewline: (o === null || o === void 0 ? void 0 : o.insertFinalNewline) === true, eol: '\n' };
  60. return formatJSON(d.getText(), range, options).map(function (e) {
  61. return TextEdit.replace(Range.create(d.positionAt(e.offset), d.positionAt(e.offset + e.length)), e.content);
  62. });
  63. }
  64. };
  65. }