All files / diff/src/format side.ts

82.3% Statements 107/130
75.86% Branches 66/87
100% Functions 7/7
83.33% Lines 90/108

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  2x 2x     2x 2x 2x         2x 16x 16x 16x 16x 16x 16x     210x   2x 44x 44x 26x 51x 51x   528x 44x     2x             60x 60x 60x 60x 60x 135x 135x 135x                           135x         135x                   135x 135x 75x 75x     60x                       2x           16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 32x 32x 32x 32x 31x 31x 31x 31x   32x 15x 15x 15x 15x   32x 30x 30x 29x 29x     32x 32x   16x 32x 32x 20x 17x 17x 17x 17x 17x 15x   17x 17x   12x 12x 12x 12x 12x 12x                         2x           16x    
import type {LinePatch} from '../line';
import {paint} from './colors';
import {groups as toGroups} from './groups';
import {type ColorOptions, type Group, GROUP_TYPE, type SideOptions} from './types';
 
const GUTTER = 3;
const WIDTH = 130;
const TAB = 8;
 
/**
 * Where the two columns sit, which is the whole of the side-by-side layout.
 */
const geometry = (width: number, tabSize: number, expandTabs: boolean): [half: number, column2: number] => {
  const t = expandTabs ? 1 : tabSize;
  const gap = t + GUTTER;
  const unaligned = (width >> 1) + (gap >> 1) + (width & gap & 1);
  const off = unaligned - (unaligned % t);
  const half = Math.max(0, Math.min(off - GUTTER, width - off));
  return [half, half ? off : width];
};
 
const printable = (code: number): boolean => code >= 0x20 && code <= 0x7e;
 
const pad = (from: number, to: number, tabSize: number, expandTabs: boolean): string => {
  let out = '';
  if (!expandTabs)
    for (let tab = from + tabSize - (from % tabSize); tab <= to; tab += tabSize) {
      out += '\t';
      from = tab;
    }
  while (from++ < to) out += ' ';
  return out;
};
 
const half = (
  str: string,
  ind: number,
  bound: number,
  tabSize: number,
  expandTabs: boolean,
): [out: string, column: number] => {
  const length = str.length;
  let out = '';
  let inPos = 0;
  let outPos = 0;
  for (let i = 0; i < length; i++) {
    const c = str[i];
    const code = str.charCodeAt(i);
    Iif (code === 0x09) {
      const stop = inPos + tabSize - (inPos % tabSize);
      if (inPos === outPos) {
        if (expandTabs) {
          const to = bound < stop ? bound : stop;
          for (; outPos < to; outPos++) out += ' ';
        } else if (stop < bound) {
          outPos = stop;
          out += c;
        }
      }
      inPos = stop;
      continue;
    }
    Iif (code === 0x0d) {
      out += c + pad(0, ind, tabSize, expandTabs);
      inPos = outPos = 0;
      continue;
    }
    Iif (code === 0x08) {
      if (inPos !== 0 && --inPos < bound) {
        if (outPos <= inPos) for (; outPos < inPos; outPos++) out += ' ';
        else {
          outPos = inPos;
          out += c;
        }
      }
      continue;
    }
    Eif (printable(code)) inPos++;
    if (inPos <= bound) {
      Eif (printable(code)) outPos = inPos;
      out += c;
    }
  }
  return [out, outPos];
};
 
/**
 * Renders {@link groups} as a side-by-side diff, `diff -y`.
 *
 * @param groups The whole-file tiling, from {@link groups}.
 * @param src Source lines, no terminators.
 * @param dst Destination lines, no terminators.
 * @param options Printing options.
 * @returns Diff chunks, one per line.
 */
export function* sideBySideGroups(
  groups: Group[],
  src: string[],
  dst: string[],
  options?: SideOptions,
): Generator<string> {
  const expandTabs = !!options?.expandTabs;
  const tabSize = options?.tabSize || TAB;
  const [hw, c2o] = geometry(options?.width || WIDTH, tabSize, expandTabs);
  const gutter = (hw + c2o - 1) >> 1;
  const colors = options?.colors;
  const reset = colors?.reset ?? '';
  const leftColumn = !!options?.leftColumn;
  const suppress = !!options?.suppressCommonLines;
  const assist = !!options?.mergeAssist;
  const srcLast = options?.srcNoEol ? src.length - 1 : -1;
  const dstLast = options?.dstNoEol ? dst.length - 1 : -1;
  const row = (left: number, sep: string, right: number): string => {
    let out = '';
    let col = 0;
    let newline = false;
    if (left >= 0) {
      newline ||= left !== srcLast;
      const [text, reached] = half(src[left], 0, hw, tabSize, expandTabs);
      out += text;
      col = reached;
    }
    if (sep !== ' ') {
      out += pad(col, gutter, tabSize, expandTabs);
      col = gutter + 1;
      if (sep === '|' && newline !== (right !== dstLast)) sep = newline ? '/' : '\\';
      out += sep;
    }
    if (right >= 0) {
      newline ||= right !== dstLast;
      if (dst[right] !== '') {
        out += pad(col, c2o, tabSize, expandTabs);
        out += half(dst[right], c2o, hw, tabSize, expandTabs)[0];
      }
    }
    if (newline) out += '\n';
    return sep === '<' || sep === '>' ? paint(sep === '<' ? colors?.del : colors?.add, out, reset) : out;
  };
  for (const group of groups) {
    const {srcFrom, srcUpto, dstFrom, dstUpto} = group;
    if (group.type === GROUP_TYPE.UNCHANGED) {
      if (suppress || (srcFrom === srcUpto && dstFrom === dstUpto)) continue;
      if (assist) yield `i${srcUpto - srcFrom},${dstUpto - dstFrom}\n`;
      let i = srcFrom;
      let j = dstFrom;
      if (!leftColumn) {
        for (; i < srcUpto && j < dstUpto; i++, j++) yield row(i, ' ', j);
        for (; j < dstUpto; j++) yield row(-1, ')', j);
      }
      for (; i < srcUpto; i++) yield row(i, '(', -1);
      continue;
    }
    if (assist) yield `c${srcUpto - srcFrom},${dstUpto - dstFrom}\n`;
    let i = srcFrom;
    let j = dstFrom;
    for (; i < srcUpto && j < dstUpto; i++, j++) yield row(i, '|', j);
    for (let k = j; k < dstUpto; k++) yield row(-1, '>', k);
    for (let k = i; k < srcUpto; k++) yield row(k, '<', -1);
  }
}
 
/**
 * Serializes a line patch as a side-by-side diff, `diff -y`, in chunks.
 *
 * @param src Source lines, no terminators.
 * @param dst Destination lines, no terminators.
 * @param patch Patch, from `lines.diff` or `line.diff`
 * @param options Serialization options.
 * @returns Diff chunks, one line each.
 */
export function* sideBySide(
  src: string[],
  dst: string[],
  patch: LinePatch,
  options?: SideOptions & ColorOptions,
): Generator<string> {
  yield* sideBySideGroups(toGroups(patch, options), src, dst, options);
}