map.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.generateMap = exports.clone = void 0;
  4. const sourcemap_codec_1 = require("@jridgewell/sourcemap-codec");
  5. const vscode_languageserver_textdocument_1 = require("vscode-languageserver-textdocument");
  6. const base_1 = require("./base");
  7. function clone(segments) {
  8. const cloned = [];
  9. for (const s of segments) {
  10. if (typeof s === 'string') {
  11. cloned.push(s);
  12. }
  13. else {
  14. cloned.push([...s]);
  15. }
  16. }
  17. return cloned;
  18. }
  19. exports.clone = clone;
  20. function generateMap(segments, readSource) {
  21. const cloned = clone(segments);
  22. const mappings = [];
  23. const sourceCode = new Map();
  24. let newLineIndex = (0, base_1.toString)(cloned).indexOf('\n');
  25. while (newLineIndex >= 0) {
  26. onLine((0, base_1.overwrite)(cloned, [0, newLineIndex + 1]));
  27. newLineIndex = (0, base_1.toString)(cloned).indexOf('\n');
  28. }
  29. onLine((0, base_1.overwrite)(cloned, [0, (0, base_1.getLength)(cloned)]));
  30. return (0, sourcemap_codec_1.encode)(mappings);
  31. function onLine(lineSegments) {
  32. const lineMapping = [];
  33. let currentColumn = 0;
  34. let hasCodeMapping = false;
  35. for (const s of lineSegments) {
  36. if (typeof s === 'string') {
  37. if (hasCodeMapping) {
  38. hasCodeMapping = false;
  39. // we don't break off last mapping for now
  40. }
  41. currentColumn += s.length;
  42. }
  43. else {
  44. hasCodeMapping = true;
  45. const source = s[1];
  46. const sourceOffset = s[2][0];
  47. if (!sourceCode.has(source)) {
  48. const readed = readSource(source);
  49. sourceCode.set(source, [readed[0], vscode_languageserver_textdocument_1.TextDocument.create('', '', 0, readed[1])]);
  50. }
  51. const [sourceIndex, document] = sourceCode.get(source);
  52. const position = document.positionAt(sourceOffset);
  53. lineMapping.push([
  54. currentColumn,
  55. sourceIndex,
  56. position.line,
  57. position.character,
  58. ]);
  59. currentColumn += s[0].length;
  60. }
  61. }
  62. mappings.push(lineMapping);
  63. }
  64. }
  65. exports.generateMap = generateMap;
  66. //# sourceMappingURL=map.js.map