All files / util/diff line.ts

92.22% Statements 166/180
82.6% Branches 57/69
100% Functions 3/3
94.66% Lines 142/150

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260178x                                                                                                       178x   1231x 1231x 1231x 1231x 36282x 33970x 33970x 30744x 30744x         33970x     1231x 33056x 33056x 33056x 33056x 33056x 29830x 29830x   3226x 3226x 3226x   3226x 3226x 3226x 4399x 4399x 3226x 3226x   1173x 1173x     1231x     1231x 1231x 4399x 4399x 4399x 4399x 1579x 1579x 1579x 1579x       571x 113x 182x 182x 182x     182x           83x 83x 83x 83x   99x 130x         4399x 4399x 4399x 1496x 1496x 1496x 1496x   177x 202x 202x 202x 24x   178x 178x 137x 240x 23x 23x 23x 23x 23x 23x 23x           23x 21x 21x 21x         2x 2x   23x 23x 23x 23x                     1231x     178x 3265x 3265x 3265x 1231x 1231x 1231x 1231x 1231x 1231x 1231x 1231x 1231x 4399x 4399x 4399x 4399x 4399x 4399x 4399x   3746x 3746x 3746x 3746x     4399x 4399x 4399x 1231x 958x 718x   718x 718x   240x 240x     273x 273x     3168x 3168x 3168x 936x 936x 2232x 507x 507x 507x 1725x 851x 851x 874x 874x 874x     4399x   1231x    
import * as str from './str';
 
export const enum LINE_PATCH_OP_TYPE {
  /**
   * The whole line is deleted. Delete the current src line and advance the src
   * counter.
   */
  DEL = -1,
 
  /**
   * Lines are equal in src and dst. Keep the line in src and advance, both, src
   * and dst counters.
   */
  EQL = 0,
 
  /**
   * The whole line is inserted. Insert the current dst line and advance the dst
   * counter.
   */
  INS = 1,
 
  /**
   * The line is modified. Execute inner diff between the current src and dst
   * lines. Keep the line in src and advance the src and dst counters.
   */
  MIX = 2,
}
 
export type LinePatchOp = [
  type: LINE_PATCH_OP_TYPE,
  /**
   * Assignment of this operation to the line in the `src` array.
   */
  src: number,
  /**
   * Assignment of this operation to the line in the `dst` array.
   */
  dst: number,
  /**
   * Character-level patch.
   */
  patch: str.Patch,
];
 
export type LinePatch = LinePatchOp[];
 
/**
 * Aggregate character-by-character patch into a line-by-line patch.
 *
 * @param patch Character-level patch
 * @returns Line-level patch
 */
export const agg = (patch: str.Patch): str.Patch[] => {
  // console.log(patch);
  const lines: str.Patch[] = [];
  const length = patch.length;
  let line: str.Patch = [];
  const push = (type: str.PATCH_OP_TYPE, str: string) => {
    if (!str.length) return;
    const length = line.length;
    if (length) {
      const lastOp = line[length - 1];
      Iif (lastOp[0] === type) {
        lastOp[1] += str;
        return;
      }
    }
    line.push([type, str]);
  };
  // console.log("PATCH", patch);
  LINES: for (let i = 0; i < length; i++) {
    const op = patch[i];
    const type = op[0];
    const str = op[1];
    const index = str.indexOf('\n');
    if (index < 0) {
      push(type, str);
      continue LINES;
    } else {
      push(type, str.slice(0, index + 1));
      if (line.length) lines.push(line);
      line = [];
    }
    let prevIndex = index;
    const strLen = str.length;
    LINE: while (prevIndex < strLen) {
      const nextIndex = str.indexOf('\n', prevIndex + 1);
      if (nextIndex < 0) {
        push(type, str.slice(prevIndex + 1));
        break LINE;
      }
      lines.push([[type, str.slice(prevIndex + 1, nextIndex + 1)]]);
      prevIndex = nextIndex;
    }
  }
  Iif (line.length) lines.push(line);
  // console.log("LINES", lines);
  {
    const length = lines.length;
    for (let i = 0; i < length; i++) {
      const line = lines[i];
      let lineLength = line.length;
      NORMALIZE_LINE_START: {
        if (lineLength < 2) break NORMALIZE_LINE_START;
        const firstOp = line[0];
        const secondOp = line[1];
        const secondOpType = secondOp[0];
        if (
          firstOp[0] === str.PATCH_OP_TYPE.EQL &&
          (secondOpType === str.PATCH_OP_TYPE.DEL || secondOpType === str.PATCH_OP_TYPE.INS)
        ) {
          for (let j = 2; j < lineLength; j++) if (line[j][0] !== secondOpType) break NORMALIZE_LINE_START;
          for (let j = i + 1; j < length; j++) {
            const targetLine = lines[j];
            const targetLineLength = targetLine.length;
            const pfx = firstOp[1];
            let targetLineFirstOp: str.PatchOperation;
            let targetLineSecondOp: str.PatchOperation;
            if (
              targetLine.length > 1 &&
              (targetLineFirstOp = targetLine[0])[0] === secondOpType &&
              (targetLineSecondOp = targetLine[1])[0] === str.PATCH_OP_TYPE.EQL &&
              pfx === targetLineFirstOp[1]
            ) {
              line.splice(0, 1);
              secondOp[1] = pfx + secondOp[1];
              targetLineSecondOp[1] = pfx + targetLineSecondOp[1];
              targetLine.splice(0, 1);
            } else {
              for (let k = 0; k < targetLineLength; k++)
                if (targetLine[k][0] !== secondOpType) break NORMALIZE_LINE_START;
            }
          }
        }
      }
      lineLength = line.length;
      NORMALIZE_LINE_END: {
        if (lineLength < 2) break NORMALIZE_LINE_END;
        const lastOp = line[line.length - 1];
        const lastOpStr = lastOp[1];
        const secondLastOp = line[line.length - 2];
        if (lastOp[0] === str.PATCH_OP_TYPE.DEL) {
          // if (lastOp[0] === PATCH_OP_TYPE.DELETE && secondLastOp[0] === PATCH_OP_TYPE.EQUAL) {
          for (let j = i + 1; j < length; j++) {
            const targetLine = lines[j];
            const targetLineLength = targetLine.length;
            if (targetLineLength <= 1) {
              if (targetLine[0][0] !== str.PATCH_OP_TYPE.DEL) break NORMALIZE_LINE_END;
            } else {
              const targetLineLastOp = targetLine[targetLine.length - 1];
              if (targetLineLastOp[0] !== str.PATCH_OP_TYPE.EQL) break NORMALIZE_LINE_END;
              for (let k = 0; k < targetLine.length - 1; k++)
                if (targetLine[k][0] !== str.PATCH_OP_TYPE.DEL) break NORMALIZE_LINE_END;
              let keepStr = targetLineLastOp[1];
              const keepStrEndsWithNl = keepStr.endsWith('\n');
              Iif (!keepStrEndsWithNl) keepStr += '\n';
              Iif (keepStr.length > lastOpStr.length) break NORMALIZE_LINE_END;
              Iif (!lastOpStr.endsWith(keepStr)) break NORMALIZE_LINE_END;
              const index = lastOpStr.length - keepStr.length;
              Iif (index < 0) {
                (lastOp[0] as str.PATCH_OP_TYPE) = str.PATCH_OP_TYPE.EQL;
                Iif (secondLastOp[0] === str.PATCH_OP_TYPE.EQL) {
                  secondLastOp[1] += lastOpStr;
                  line.splice(lineLength - 1, 1);
                }
              } else if (index === 0) {
                line.splice(lineLength - 1, 1);
                if (secondLastOp[0] === str.PATCH_OP_TYPE.EQL) {
                  secondLastOp[1] += keepStr;
                } else E{
                  line.push([str.PATCH_OP_TYPE.EQL, keepStr]);
                }
              } else {
                lastOp[1] = lastOpStr.slice(0, index);
                line.push([str.PATCH_OP_TYPE.EQL, keepStr]);
              }
              const targetLineSecondLastOp = targetLine[targetLine.length - 2];
              if (targetLineSecondLastOp[0] === str.PATCH_OP_TYPE.DEL) {
                targetLineSecondLastOp[1] += keepStrEndsWithNl ? keepStr : keepStr.slice(0, -1);
                targetLine.splice(targetLineLength - 1, 1);
              } else E{
                (targetLineLastOp[0] as str.PATCH_OP_TYPE) = str.PATCH_OP_TYPE.DEL;
              }
            }
          }
        }
      }
    }
  }
  // console.log("NORMALIZED LINES", lines);
  return lines;
};
 
export const diff = (src: string[], dst: string[]): LinePatch => {
  const srcTxt = src.join('\n') + '\n';
  const dstTxt = dst.join('\n') + '\n';
  if (srcTxt === dstTxt) return [];
  const strPatch = str.diff(srcTxt, dstTxt);
  const lines = agg(strPatch);
  const length = lines.length;
  const patch: LinePatch = [];
  let srcIdx = -1;
  let dstIdx = -1;
  const srcLength = src.length;
  const dstLength = dst.length;
  for (let i = 0; i < length; i++) {
    const line = lines[i];
    let lineLength = line.length;
    Iif (!lineLength) continue;
    const lastOp = line[lineLength - 1];
    const lastOpType = lastOp[0];
    const txt = lastOp[1];
    if (txt === '\n') line.splice(lineLength - 1, 1);
    else {
      const strLength = txt.length;
      if (txt[strLength - 1] === '\n') {
        Iif (strLength === 1) line.splice(lineLength - 1, 1);
        else lastOp[1] = txt.slice(0, strLength - 1);
      }
    }
    let lineType: LINE_PATCH_OP_TYPE = LINE_PATCH_OP_TYPE.EQL;
    lineLength = line.length;
    if (i + 1 === length) {
      if (srcIdx + 1 < srcLength) {
        if (dstIdx + 1 < dstLength) {
          lineType =
            lineLength === 1 && line[0][0] === str.PATCH_OP_TYPE.EQL ? LINE_PATCH_OP_TYPE.EQL : LINE_PATCH_OP_TYPE.MIX;
          srcIdx++;
          dstIdx++;
        } else {
          lineType = LINE_PATCH_OP_TYPE.DEL;
          srcIdx++;
        }
      } else {
        lineType = LINE_PATCH_OP_TYPE.INS;
        dstIdx++;
      }
    } else {
      const op = line[0];
      const type = op[0];
      if (lineLength === 1 && type === lastOpType && type === str.PATCH_OP_TYPE.EQL) {
        srcIdx++;
        dstIdx++;
      } else if (lastOpType === str.PATCH_OP_TYPE.EQL) {
        lineType = LINE_PATCH_OP_TYPE.MIX;
        srcIdx++;
        dstIdx++;
      } else if (lastOpType === str.PATCH_OP_TYPE.INS) {
        lineType = LINE_PATCH_OP_TYPE.INS;
        dstIdx++;
      } else if (lastOpType === str.PATCH_OP_TYPE.DEL) {
        lineType = LINE_PATCH_OP_TYPE.DEL;
        srcIdx++;
      }
    }
    patch.push([lineType, srcIdx, dstIdx, line]);
  }
  return patch;
};