You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
7.8 KiB

5 years ago
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var assert_1 = __importDefault(require("assert"));
  7. var util_1 = require("./util");
  8. var Mapping = /** @class */ (function () {
  9. function Mapping(sourceLines, sourceLoc, targetLoc) {
  10. if (targetLoc === void 0) { targetLoc = sourceLoc; }
  11. this.sourceLines = sourceLines;
  12. this.sourceLoc = sourceLoc;
  13. this.targetLoc = targetLoc;
  14. }
  15. Mapping.prototype.slice = function (lines, start, end) {
  16. if (end === void 0) { end = lines.lastPos(); }
  17. var sourceLines = this.sourceLines;
  18. var sourceLoc = this.sourceLoc;
  19. var targetLoc = this.targetLoc;
  20. function skip(name) {
  21. var sourceFromPos = sourceLoc[name];
  22. var targetFromPos = targetLoc[name];
  23. var targetToPos = start;
  24. if (name === "end") {
  25. targetToPos = end;
  26. }
  27. else {
  28. assert_1.default.strictEqual(name, "start");
  29. }
  30. return skipChars(sourceLines, sourceFromPos, lines, targetFromPos, targetToPos);
  31. }
  32. if (util_1.comparePos(start, targetLoc.start) <= 0) {
  33. if (util_1.comparePos(targetLoc.end, end) <= 0) {
  34. targetLoc = {
  35. start: subtractPos(targetLoc.start, start.line, start.column),
  36. end: subtractPos(targetLoc.end, start.line, start.column)
  37. };
  38. // The sourceLoc can stay the same because the contents of the
  39. // targetLoc have not changed.
  40. }
  41. else if (util_1.comparePos(end, targetLoc.start) <= 0) {
  42. return null;
  43. }
  44. else {
  45. sourceLoc = {
  46. start: sourceLoc.start,
  47. end: skip("end")
  48. };
  49. targetLoc = {
  50. start: subtractPos(targetLoc.start, start.line, start.column),
  51. end: subtractPos(end, start.line, start.column)
  52. };
  53. }
  54. }
  55. else {
  56. if (util_1.comparePos(targetLoc.end, start) <= 0) {
  57. return null;
  58. }
  59. if (util_1.comparePos(targetLoc.end, end) <= 0) {
  60. sourceLoc = {
  61. start: skip("start"),
  62. end: sourceLoc.end
  63. };
  64. targetLoc = {
  65. // Same as subtractPos(start, start.line, start.column):
  66. start: { line: 1, column: 0 },
  67. end: subtractPos(targetLoc.end, start.line, start.column)
  68. };
  69. }
  70. else {
  71. sourceLoc = {
  72. start: skip("start"),
  73. end: skip("end")
  74. };
  75. targetLoc = {
  76. // Same as subtractPos(start, start.line, start.column):
  77. start: { line: 1, column: 0 },
  78. end: subtractPos(end, start.line, start.column)
  79. };
  80. }
  81. }
  82. return new Mapping(this.sourceLines, sourceLoc, targetLoc);
  83. };
  84. Mapping.prototype.add = function (line, column) {
  85. return new Mapping(this.sourceLines, this.sourceLoc, {
  86. start: addPos(this.targetLoc.start, line, column),
  87. end: addPos(this.targetLoc.end, line, column)
  88. });
  89. };
  90. Mapping.prototype.subtract = function (line, column) {
  91. return new Mapping(this.sourceLines, this.sourceLoc, {
  92. start: subtractPos(this.targetLoc.start, line, column),
  93. end: subtractPos(this.targetLoc.end, line, column)
  94. });
  95. };
  96. Mapping.prototype.indent = function (by, skipFirstLine, noNegativeColumns) {
  97. if (skipFirstLine === void 0) { skipFirstLine = false; }
  98. if (noNegativeColumns === void 0) { noNegativeColumns = false; }
  99. if (by === 0) {
  100. return this;
  101. }
  102. var targetLoc = this.targetLoc;
  103. var startLine = targetLoc.start.line;
  104. var endLine = targetLoc.end.line;
  105. if (skipFirstLine && startLine === 1 && endLine === 1) {
  106. return this;
  107. }
  108. targetLoc = {
  109. start: targetLoc.start,
  110. end: targetLoc.end
  111. };
  112. if (!skipFirstLine || startLine > 1) {
  113. var startColumn = targetLoc.start.column + by;
  114. targetLoc.start = {
  115. line: startLine,
  116. column: noNegativeColumns
  117. ? Math.max(0, startColumn)
  118. : startColumn
  119. };
  120. }
  121. if (!skipFirstLine || endLine > 1) {
  122. var endColumn = targetLoc.end.column + by;
  123. targetLoc.end = {
  124. line: endLine,
  125. column: noNegativeColumns
  126. ? Math.max(0, endColumn)
  127. : endColumn
  128. };
  129. }
  130. return new Mapping(this.sourceLines, this.sourceLoc, targetLoc);
  131. };
  132. return Mapping;
  133. }());
  134. exports.default = Mapping;
  135. function addPos(toPos, line, column) {
  136. return {
  137. line: toPos.line + line - 1,
  138. column: (toPos.line === 1)
  139. ? toPos.column + column
  140. : toPos.column
  141. };
  142. }
  143. function subtractPos(fromPos, line, column) {
  144. return {
  145. line: fromPos.line - line + 1,
  146. column: (fromPos.line === line)
  147. ? fromPos.column - column
  148. : fromPos.column
  149. };
  150. }
  151. function skipChars(sourceLines, sourceFromPos, targetLines, targetFromPos, targetToPos) {
  152. var targetComparison = util_1.comparePos(targetFromPos, targetToPos);
  153. if (targetComparison === 0) {
  154. // Trivial case: no characters to skip.
  155. return sourceFromPos;
  156. }
  157. if (targetComparison < 0) {
  158. // Skipping forward.
  159. var sourceCursor = sourceLines.skipSpaces(sourceFromPos) || sourceLines.lastPos();
  160. var targetCursor = targetLines.skipSpaces(targetFromPos) || targetLines.lastPos();
  161. var lineDiff = targetToPos.line - targetCursor.line;
  162. sourceCursor.line += lineDiff;
  163. targetCursor.line += lineDiff;
  164. if (lineDiff > 0) {
  165. // If jumping to later lines, reset columns to the beginnings
  166. // of those lines.
  167. sourceCursor.column = 0;
  168. targetCursor.column = 0;
  169. }
  170. else {
  171. assert_1.default.strictEqual(lineDiff, 0);
  172. }
  173. while (util_1.comparePos(targetCursor, targetToPos) < 0 &&
  174. targetLines.nextPos(targetCursor, true)) {
  175. assert_1.default.ok(sourceLines.nextPos(sourceCursor, true));
  176. assert_1.default.strictEqual(sourceLines.charAt(sourceCursor), targetLines.charAt(targetCursor));
  177. }
  178. }
  179. else {
  180. // Skipping backward.
  181. var sourceCursor = sourceLines.skipSpaces(sourceFromPos, true) || sourceLines.firstPos();
  182. var targetCursor = targetLines.skipSpaces(targetFromPos, true) || targetLines.firstPos();
  183. var lineDiff = targetToPos.line - targetCursor.line;
  184. sourceCursor.line += lineDiff;
  185. targetCursor.line += lineDiff;
  186. if (lineDiff < 0) {
  187. // If jumping to earlier lines, reset columns to the ends of
  188. // those lines.
  189. sourceCursor.column = sourceLines.getLineLength(sourceCursor.line);
  190. targetCursor.column = targetLines.getLineLength(targetCursor.line);
  191. }
  192. else {
  193. assert_1.default.strictEqual(lineDiff, 0);
  194. }
  195. while (util_1.comparePos(targetToPos, targetCursor) < 0 &&
  196. targetLines.prevPos(targetCursor, true)) {
  197. assert_1.default.ok(sourceLines.prevPos(sourceCursor, true));
  198. assert_1.default.strictEqual(sourceLines.charAt(sourceCursor), targetLines.charAt(targetCursor));
  199. }
  200. }
  201. return sourceCursor;
  202. }