jsonSelectionRanges.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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", "jsonc-parser"], factory);
  12. }
  13. })(function (require, exports) {
  14. "use strict";
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. exports.getSelectionRanges = void 0;
  17. var jsonLanguageTypes_1 = require("../jsonLanguageTypes");
  18. var jsonc_parser_1 = require("jsonc-parser");
  19. function getSelectionRanges(document, positions, doc) {
  20. function getSelectionRange(position) {
  21. var offset = document.offsetAt(position);
  22. var node = doc.getNodeFromOffset(offset, true);
  23. var result = [];
  24. while (node) {
  25. switch (node.type) {
  26. case 'string':
  27. case 'object':
  28. case 'array':
  29. // range without ", [ or {
  30. var cStart = node.offset + 1, cEnd = node.offset + node.length - 1;
  31. if (cStart < cEnd && offset >= cStart && offset <= cEnd) {
  32. result.push(newRange(cStart, cEnd));
  33. }
  34. result.push(newRange(node.offset, node.offset + node.length));
  35. break;
  36. case 'number':
  37. case 'boolean':
  38. case 'null':
  39. case 'property':
  40. result.push(newRange(node.offset, node.offset + node.length));
  41. break;
  42. }
  43. if (node.type === 'property' || node.parent && node.parent.type === 'array') {
  44. var afterCommaOffset = getOffsetAfterNextToken(node.offset + node.length, 5 /* CommaToken */);
  45. if (afterCommaOffset !== -1) {
  46. result.push(newRange(node.offset, afterCommaOffset));
  47. }
  48. }
  49. node = node.parent;
  50. }
  51. var current = undefined;
  52. for (var index = result.length - 1; index >= 0; index--) {
  53. current = jsonLanguageTypes_1.SelectionRange.create(result[index], current);
  54. }
  55. if (!current) {
  56. current = jsonLanguageTypes_1.SelectionRange.create(jsonLanguageTypes_1.Range.create(position, position));
  57. }
  58. return current;
  59. }
  60. function newRange(start, end) {
  61. return jsonLanguageTypes_1.Range.create(document.positionAt(start), document.positionAt(end));
  62. }
  63. var scanner = jsonc_parser_1.createScanner(document.getText(), true);
  64. function getOffsetAfterNextToken(offset, expectedToken) {
  65. scanner.setPosition(offset);
  66. var token = scanner.scan();
  67. if (token === expectedToken) {
  68. return scanner.getTokenOffset() + scanner.getTokenLength();
  69. }
  70. return -1;
  71. }
  72. return positions.map(getSelectionRange);
  73. }
  74. exports.getSelectionRanges = getSelectionRanges;
  75. });