jsonLinks.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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", "../jsonLanguageTypes"], factory);
  12. }
  13. })(function (require, exports) {
  14. "use strict";
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. exports.findLinks = void 0;
  17. var jsonLanguageTypes_1 = require("../jsonLanguageTypes");
  18. function findLinks(document, doc) {
  19. var links = [];
  20. doc.visit(function (node) {
  21. var _a;
  22. if (node.type === "property" && node.keyNode.value === "$ref" && ((_a = node.valueNode) === null || _a === void 0 ? void 0 : _a.type) === 'string') {
  23. var path = node.valueNode.value;
  24. var targetNode = findTargetNode(doc, path);
  25. if (targetNode) {
  26. var targetPos = document.positionAt(targetNode.offset);
  27. links.push({
  28. target: document.uri + "#" + (targetPos.line + 1) + "," + (targetPos.character + 1),
  29. range: createRange(document, node.valueNode)
  30. });
  31. }
  32. }
  33. return true;
  34. });
  35. return Promise.resolve(links);
  36. }
  37. exports.findLinks = findLinks;
  38. function createRange(document, node) {
  39. return jsonLanguageTypes_1.Range.create(document.positionAt(node.offset + 1), document.positionAt(node.offset + node.length - 1));
  40. }
  41. function findTargetNode(doc, path) {
  42. var tokens = parseJSONPointer(path);
  43. if (!tokens) {
  44. return null;
  45. }
  46. return findNode(tokens, doc.root);
  47. }
  48. function findNode(pointer, node) {
  49. if (!node) {
  50. return null;
  51. }
  52. if (pointer.length === 0) {
  53. return node;
  54. }
  55. var token = pointer.shift();
  56. if (node && node.type === 'object') {
  57. var propertyNode = node.properties.find(function (propertyNode) { return propertyNode.keyNode.value === token; });
  58. if (!propertyNode) {
  59. return null;
  60. }
  61. return findNode(pointer, propertyNode.valueNode);
  62. }
  63. else if (node && node.type === 'array') {
  64. if (token.match(/^(0|[1-9][0-9]*)$/)) {
  65. var index = Number.parseInt(token);
  66. var arrayItem = node.items[index];
  67. if (!arrayItem) {
  68. return null;
  69. }
  70. return findNode(pointer, arrayItem);
  71. }
  72. }
  73. return null;
  74. }
  75. function parseJSONPointer(path) {
  76. if (path === "#") {
  77. return [];
  78. }
  79. if (path[0] !== '#' || path[1] !== '/') {
  80. return null;
  81. }
  82. return path.substring(2).split(/\//).map(unescape);
  83. }
  84. function unescape(str) {
  85. return str.replace(/~1/g, '/').replace(/~0/g, '~');
  86. }
  87. });