123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- 'use strict';
- Object.defineProperty(exports, '__esModule', {
- value: true
- });
- exports.joinAlignedDiffsExpand = exports.joinAlignedDiffsNoExpand = void 0;
- var _cleanupSemantic = require('./cleanupSemantic');
- var _printDiffs = require('./printDiffs');
- const joinAlignedDiffsNoExpand = (diffs, options) => {
- const iLength = diffs.length;
- const nContextLines = options.contextLines;
- const nContextLines2 = nContextLines + nContextLines;
- let jLength = iLength;
- let hasExcessAtStartOrEnd = false;
- let nExcessesBetweenChanges = 0;
- let i = 0;
- while (i !== iLength) {
- const iStart = i;
- while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_EQUAL) {
- i += 1;
- }
- if (iStart !== i) {
- if (iStart === 0) {
-
- if (i > nContextLines) {
- jLength -= i - nContextLines;
- hasExcessAtStartOrEnd = true;
- }
- } else if (i === iLength) {
-
- const n = i - iStart;
- if (n > nContextLines) {
- jLength -= n - nContextLines;
- hasExcessAtStartOrEnd = true;
- }
- } else {
-
- const n = i - iStart;
- if (n > nContextLines2) {
- jLength -= n - nContextLines2;
- nExcessesBetweenChanges += 1;
- }
- }
- }
- while (i !== iLength && diffs[i][0] !== _cleanupSemantic.DIFF_EQUAL) {
- i += 1;
- }
- }
- const hasPatch = nExcessesBetweenChanges !== 0 || hasExcessAtStartOrEnd;
- if (nExcessesBetweenChanges !== 0) {
- jLength += nExcessesBetweenChanges + 1;
- } else if (hasExcessAtStartOrEnd) {
- jLength += 1;
- }
- const jLast = jLength - 1;
- const lines = [];
- let jPatchMark = 0;
- if (hasPatch) {
- lines.push('');
- }
- let aStart = 0;
- let bStart = 0;
- let aEnd = 0;
- let bEnd = 0;
- const pushCommonLine = line => {
- const j = lines.length;
- lines.push(
- (0, _printDiffs.printCommonLine)(line, j === 0 || j === jLast, options)
- );
- aEnd += 1;
- bEnd += 1;
- };
- const pushDeleteLine = line => {
- const j = lines.length;
- lines.push(
- (0, _printDiffs.printDeleteLine)(line, j === 0 || j === jLast, options)
- );
- aEnd += 1;
- };
- const pushInsertLine = line => {
- const j = lines.length;
- lines.push(
- (0, _printDiffs.printInsertLine)(line, j === 0 || j === jLast, options)
- );
- bEnd += 1;
- };
- i = 0;
- while (i !== iLength) {
- let iStart = i;
- while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_EQUAL) {
- i += 1;
- }
- if (iStart !== i) {
- if (iStart === 0) {
-
- if (i > nContextLines) {
- iStart = i - nContextLines;
- aStart = iStart;
- bStart = iStart;
- aEnd = aStart;
- bEnd = bStart;
- }
- for (let iCommon = iStart; iCommon !== i; iCommon += 1) {
- pushCommonLine(diffs[iCommon][1]);
- }
- } else if (i === iLength) {
-
- const iEnd = i - iStart > nContextLines ? iStart + nContextLines : i;
- for (let iCommon = iStart; iCommon !== iEnd; iCommon += 1) {
- pushCommonLine(diffs[iCommon][1]);
- }
- } else {
-
- const nCommon = i - iStart;
- if (nCommon > nContextLines2) {
- const iEnd = iStart + nContextLines;
- for (let iCommon = iStart; iCommon !== iEnd; iCommon += 1) {
- pushCommonLine(diffs[iCommon][1]);
- }
- lines[jPatchMark] = (0, _printDiffs.createPatchMark)(
- aStart,
- aEnd,
- bStart,
- bEnd,
- options
- );
- jPatchMark = lines.length;
- lines.push('');
- const nOmit = nCommon - nContextLines2;
- aStart = aEnd + nOmit;
- bStart = bEnd + nOmit;
- aEnd = aStart;
- bEnd = bStart;
- for (let iCommon = i - nContextLines; iCommon !== i; iCommon += 1) {
- pushCommonLine(diffs[iCommon][1]);
- }
- } else {
- for (let iCommon = iStart; iCommon !== i; iCommon += 1) {
- pushCommonLine(diffs[iCommon][1]);
- }
- }
- }
- }
- while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_DELETE) {
- pushDeleteLine(diffs[i][1]);
- i += 1;
- }
- while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_INSERT) {
- pushInsertLine(diffs[i][1]);
- i += 1;
- }
- }
- if (hasPatch) {
- lines[jPatchMark] = (0, _printDiffs.createPatchMark)(
- aStart,
- aEnd,
- bStart,
- bEnd,
- options
- );
- }
- return lines.join('\n');
- };
- exports.joinAlignedDiffsNoExpand = joinAlignedDiffsNoExpand;
- const joinAlignedDiffsExpand = (diffs, options) =>
- diffs
- .map((diff, i, diffs) => {
- const line = diff[1];
- const isFirstOrLast = i === 0 || i === diffs.length - 1;
- switch (diff[0]) {
- case _cleanupSemantic.DIFF_DELETE:
- return (0, _printDiffs.printDeleteLine)(line, isFirstOrLast, options);
- case _cleanupSemantic.DIFF_INSERT:
- return (0, _printDiffs.printInsertLine)(line, isFirstOrLast, options);
- default:
- return (0, _printDiffs.printCommonLine)(line, isFirstOrLast, options);
- }
- })
- .join('\n');
- exports.joinAlignedDiffsExpand = joinAlignedDiffsExpand;
|