All files / app/assets/javascripts/merge_conflicts utils.js

88.69% Statements 102/115
78.94% Branches 45/57
94.44% Functions 17/18
88.28% Lines 98/111

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                  3x 28x   28x     3x 106x 106x 106x 2x 1x 1x 1x     1x 1x 1x       106x     3x 79x                         3x 315x 315x                       3x 546x 546x   546x                             3x 79x                         3x 26x   26x 104x 104x   104x 39x     104x 312x   312x 39x 39x     312x 312x     104x 39x       26x     3x 26x 26x   26x 104x   104x 39x 39x     104x 312x   312x 78x 39x 39x 39x     234x   234x 234x       104x     26x 312x   26x     3x 13x 26x 26x 26x 26x 26x 26x 26x   26x 26x 26x   26x 26x         26x       3x 1x 9x 4x   5x     1x 6x 6x 6x 6x   6x 2x 2x 2x 2x   6x   1x     3x                            
import {
  ORIGIN_HEADER_TEXT,
  ORIGIN_BUTTON_TITLE,
  HEAD_HEADER_TEXT,
  HEAD_BUTTON_TITLE,
  DEFAULT_RESOLVE_MODE,
  CONFLICT_TYPES,
} from './constants';
 
export const getFilePath = (file) => {
  const { old_path, new_path } = file;
  // eslint-disable-next-line camelcase
  return old_path === new_path ? new_path : `${old_path} → ${new_path}`;
};
 
export const checkLineLengths = ({ left, right }) => {
  const wLeft = [...left];
  const wRight = [...right];
  if (left.length !== right.length) {
    if (left.length > right.length) {
      const diff = left.length - right.length;
      for (let i = 0; i < diff; i += 1) {
        wRight.push({ lineType: 'emptyLine', richText: '' });
      }
    } else {
      const diff = right.length - left.length;
      for (let i = 0; i < diff; i += 1) {
        wLeft.push({ lineType: 'emptyLine', richText: '' });
      }
    }
  }
  return { left: wLeft, right: wRight };
};
 
export const getHeadHeaderLine = (id) => {
  return {
    id,
    richText: HEAD_HEADER_TEXT,
    buttonTitle: HEAD_BUTTON_TITLE,
    type: 'new',
    section: 'head',
    isHeader: true,
    isHead: true,
    isSelected: false,
    isUnselected: false,
  };
};
 
export const decorateLineForInlineView = (line, id, conflict) => {
  const { type } = line;
  return {
    id,
    hasConflict: conflict,
    isHead: type === 'new',
    isOrigin: type === 'old',
    hasMatch: type === 'match',
    richText: line.rich_text,
    isSelected: false,
    isUnselected: false,
  };
};
 
export const getLineForParallelView = (line, id, lineType, isHead) => {
  const { old_line: oldLine, new_line: newLine, rich_text: richText } = line;
  const hasConflict = lineType === 'conflict';
 
  return {
    id,
    lineType,
    hasConflict,
    isHead: hasConflict && isHead,
    isOrigin: hasConflict && !isHead,
    hasMatch: lineType === 'match',
    lineNumber: isHead ? newLine : oldLine,
    section: isHead ? 'head' : 'origin',
    richText,
    isSelected: false,
    isUnselected: false,
  };
};
 
export const getOriginHeaderLine = (id) => {
  return {
    id,
    richText: ORIGIN_HEADER_TEXT,
    buttonTitle: ORIGIN_BUTTON_TITLE,
    type: 'old',
    section: 'origin',
    isHeader: true,
    isOrigin: true,
    isSelected: false,
    isUnselected: false,
  };
};
 
export const setInlineLine = (file) => {
  const inlineLines = [];
 
  file.sections.forEach((section) => {
    let currentLineType = 'new';
    const { conflict, lines, id } = section;
 
    if (conflict) {
      inlineLines.push(getHeadHeaderLine(id));
    }
 
    lines.forEach((line) => {
      const { type } = line;
 
      if ((type === 'new' || type === 'old') && currentLineType !== type) {
        currentLineType = type;
        inlineLines.push({ lineType: 'emptyLine', richText: '' });
      }
 
      const decoratedLine = decorateLineForInlineView(line, id, conflict);
      inlineLines.push(decoratedLine);
    });
 
    if (conflict) {
      inlineLines.push(getOriginHeaderLine(id));
    }
  });
 
  return inlineLines;
};
 
export const setParallelLine = (file) => {
  const parallelLines = [];
  let linesObj = { left: [], right: [] };
 
  file.sections.forEach((section) => {
    const { conflict, lines, id } = section;
 
    if (conflict) {
      linesObj.left.push(getOriginHeaderLine(id));
      linesObj.right.push(getHeadHeaderLine(id));
    }
 
    lines.forEach((line) => {
      const { type } = line;
 
      if (conflict) {
        if (type === 'old') {
          linesObj.left.push(getLineForParallelView(line, id, 'conflict'));
        } else Eif (type === 'new') {
          linesObj.right.push(getLineForParallelView(line, id, 'conflict', true));
        }
      } else {
        const lineType = type || 'context';
 
        linesObj.left.push(getLineForParallelView(line, id, lineType));
        linesObj.right.push(getLineForParallelView(line, id, lineType, true));
      }
    });
 
    linesObj = checkLineLengths(linesObj);
  });
 
  for (let i = 0, len = linesObj.left.length; i < len; i += 1) {
    parallelLines.push([linesObj.right[i], linesObj.left[i]]);
  }
  return parallelLines;
};
 
export const decorateFiles = (files) => {
  return files.map((file) => {
    const f = { ...file };
    f.content = '';
    f.resolutionData = {};
    f.promptDiscardConfirmation = false;
    f.resolveMode = DEFAULT_RESOLVE_MODE;
    f.filePath = getFilePath(file);
    f.blobPath = f.blob_path;
 
    if (f.type === CONFLICT_TYPES.TEXT) {
      f.showEditor = false;
      f.loadEditor = false;
 
      f.inlineLines = setInlineLine(file);
      f.parallelLines = setParallelLine(file);
    } else Eif (f.type === CONFLICT_TYPES.TEXT_EDITOR) {
      f.showEditor = true;
      f.loadEditor = true;
    }
    return f;
  });
};
 
export const restoreFileLinesState = (file) => {
  const inlineLines = file.inlineLines.map((line) => {
    if (line.hasConflict || line.isHeader) {
      return { ...line, isSelected: false, isUnselected: false };
    }
    return { ...line };
  });
 
  const parallelLines = file.parallelLines.map((lines) => {
    const left = { ...lines[0] };
    const right = { ...lines[1] };
    const isLeftMatch = left.hasConflict || left.isHeader;
    const isRightMatch = right.hasConflict || right.isHeader;
 
    if (isLeftMatch || isRightMatch) {
      left.isSelected = false;
      left.isUnselected = false;
      right.isSelected = false;
      right.isUnselected = false;
    }
    return [left, right];
  });
  return { inlineLines, parallelLines };
};
 
export const markLine = (line, selection) => {
  const updated = { ...line };
  if (selection === 'head' && line.isHead) {
    updated.isSelected = true;
    updated.isUnselected = false;
  } else if (selection === 'origin' && updated.isOrigin) {
    updated.isSelected = true;
    updated.isUnselected = false;
  } else {
    updated.isSelected = false;
    updated.isUnselected = true;
  }
  return updated;
};