2018-06-21 08:22:40 -04:00
|
|
|
import _ from 'underscore';
|
2018-10-18 03:50:38 -04:00
|
|
|
import { diffModes } from '~/ide/constants';
|
2018-06-21 08:22:40 -04:00
|
|
|
import {
|
|
|
|
LINE_POSITION_LEFT,
|
|
|
|
LINE_POSITION_RIGHT,
|
|
|
|
TEXT_DIFF_POSITION_TYPE,
|
2018-09-10 14:41:57 -04:00
|
|
|
LEGACY_DIFF_NOTE_TYPE,
|
2018-06-21 08:22:40 -04:00
|
|
|
DIFF_NOTE_TYPE,
|
|
|
|
NEW_LINE_TYPE,
|
|
|
|
OLD_LINE_TYPE,
|
|
|
|
MATCH_LINE_TYPE,
|
2018-09-05 04:28:49 -04:00
|
|
|
LINES_TO_BE_RENDERED_DIRECTLY,
|
|
|
|
MAX_LINES_TO_BE_RENDERED,
|
2018-06-21 08:22:40 -04:00
|
|
|
} from '../constants';
|
|
|
|
|
|
|
|
export function findDiffFile(files, hash) {
|
2018-11-09 14:48:41 -05:00
|
|
|
return files.filter(file => file.file_hash === hash)[0];
|
2018-06-21 08:22:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getReversePosition = linePosition => {
|
|
|
|
if (linePosition === LINE_POSITION_RIGHT) {
|
|
|
|
return LINE_POSITION_LEFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return LINE_POSITION_RIGHT;
|
|
|
|
};
|
|
|
|
|
2018-10-06 13:16:40 -04:00
|
|
|
export function getFormData(params) {
|
2018-06-21 08:22:40 -04:00
|
|
|
const {
|
|
|
|
note,
|
|
|
|
noteableType,
|
|
|
|
noteableData,
|
|
|
|
diffFile,
|
|
|
|
noteTargetLine,
|
|
|
|
diffViewType,
|
|
|
|
linePosition,
|
2018-10-18 03:50:38 -04:00
|
|
|
positionType,
|
2018-06-21 08:22:40 -04:00
|
|
|
} = params;
|
|
|
|
|
|
|
|
const position = JSON.stringify({
|
2018-11-09 14:48:41 -05:00
|
|
|
base_sha: diffFile.diff_refs.base_sha,
|
|
|
|
start_sha: diffFile.diff_refs.start_sha,
|
|
|
|
head_sha: diffFile.diff_refs.head_sha,
|
|
|
|
old_path: diffFile.old_path,
|
|
|
|
new_path: diffFile.new_path,
|
2018-10-18 03:50:38 -04:00
|
|
|
position_type: positionType || TEXT_DIFF_POSITION_TYPE,
|
2018-11-09 14:48:41 -05:00
|
|
|
old_line: noteTargetLine ? noteTargetLine.old_line : null,
|
|
|
|
new_line: noteTargetLine ? noteTargetLine.new_line : null,
|
2018-10-18 03:50:38 -04:00
|
|
|
x: params.x,
|
|
|
|
y: params.y,
|
2018-11-06 04:40:03 -05:00
|
|
|
width: params.width,
|
|
|
|
height: params.height,
|
2018-06-21 08:22:40 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const postData = {
|
|
|
|
view: diffViewType,
|
|
|
|
line_type: linePosition === LINE_POSITION_RIGHT ? NEW_LINE_TYPE : OLD_LINE_TYPE,
|
2018-11-09 14:48:41 -05:00
|
|
|
merge_request_diff_head_sha: diffFile.diff_refs.head_sha,
|
2018-06-21 08:22:40 -04:00
|
|
|
in_reply_to_discussion_id: '',
|
|
|
|
note_project_id: '',
|
|
|
|
target_type: noteableData.targetType,
|
|
|
|
target_id: noteableData.id,
|
2018-09-18 12:12:37 -04:00
|
|
|
return_discussion: true,
|
2018-06-21 08:22:40 -04:00
|
|
|
note: {
|
|
|
|
note,
|
|
|
|
position,
|
|
|
|
noteable_type: noteableType,
|
|
|
|
noteable_id: noteableData.id,
|
|
|
|
commit_id: '',
|
2018-09-14 05:17:31 -04:00
|
|
|
type:
|
2018-11-09 14:48:41 -05:00
|
|
|
diffFile.diff_refs.start_sha && diffFile.diff_refs.head_sha
|
2018-09-14 05:17:31 -04:00
|
|
|
? DIFF_NOTE_TYPE
|
|
|
|
: LEGACY_DIFF_NOTE_TYPE,
|
2018-11-09 14:48:41 -05:00
|
|
|
line_code: noteTargetLine ? noteTargetLine.line_code : null,
|
2018-06-21 08:22:40 -04:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-10-06 13:16:40 -04:00
|
|
|
return postData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getNoteFormData(params) {
|
|
|
|
const data = getFormData(params);
|
|
|
|
|
2018-06-21 08:22:40 -04:00
|
|
|
return {
|
2018-10-06 13:16:40 -04:00
|
|
|
endpoint: params.noteableData.create_note_path,
|
|
|
|
data,
|
2018-06-21 08:22:40 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const findIndexInInlineLines = (lines, lineNumbers) => {
|
|
|
|
const { oldLineNumber, newLineNumber } = lineNumbers;
|
|
|
|
|
|
|
|
return _.findIndex(
|
|
|
|
lines,
|
2018-11-09 14:48:41 -05:00
|
|
|
line => line.old_line === oldLineNumber && line.new_line === newLineNumber,
|
2018-06-21 08:22:40 -04:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const findIndexInParallelLines = (lines, lineNumbers) => {
|
|
|
|
const { oldLineNumber, newLineNumber } = lineNumbers;
|
|
|
|
|
|
|
|
return _.findIndex(
|
|
|
|
lines,
|
|
|
|
line =>
|
|
|
|
line.left &&
|
|
|
|
line.right &&
|
2018-11-09 14:48:41 -05:00
|
|
|
line.left.old_line === oldLineNumber &&
|
|
|
|
line.right.new_line === newLineNumber,
|
2018-06-21 08:22:40 -04:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export function removeMatchLine(diffFile, lineNumbers, bottom) {
|
2018-11-09 14:48:41 -05:00
|
|
|
const indexForInline = findIndexInInlineLines(diffFile.highlighted_diff_lines, lineNumbers);
|
|
|
|
const indexForParallel = findIndexInParallelLines(diffFile.parallel_diff_lines, lineNumbers);
|
2018-06-21 08:22:40 -04:00
|
|
|
const factor = bottom ? 1 : -1;
|
|
|
|
|
2018-11-09 14:48:41 -05:00
|
|
|
diffFile.highlighted_diff_lines.splice(indexForInline + factor, 1);
|
|
|
|
diffFile.parallel_diff_lines.splice(indexForParallel + factor, 1);
|
2018-06-21 08:22:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function addLineReferences(lines, lineNumbers, bottom) {
|
|
|
|
const { oldLineNumber, newLineNumber } = lineNumbers;
|
|
|
|
const lineCount = lines.length;
|
|
|
|
let matchLineIndex = -1;
|
|
|
|
|
|
|
|
const linesWithNumbers = lines.map((l, index) => {
|
2018-11-09 14:48:41 -05:00
|
|
|
if (l.type === MATCH_LINE_TYPE) {
|
2018-06-21 08:22:40 -04:00
|
|
|
matchLineIndex = index;
|
|
|
|
} else {
|
2018-11-09 14:48:41 -05:00
|
|
|
Object.assign(l, {
|
|
|
|
old_line: bottom ? oldLineNumber + index + 1 : oldLineNumber + index - lineCount,
|
|
|
|
new_line: bottom ? newLineNumber + index + 1 : newLineNumber + index - lineCount,
|
2018-06-21 08:22:40 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-09 14:48:41 -05:00
|
|
|
return l;
|
2018-06-21 08:22:40 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
if (matchLineIndex > -1) {
|
|
|
|
const line = linesWithNumbers[matchLineIndex];
|
|
|
|
const targetLine = bottom
|
|
|
|
? linesWithNumbers[matchLineIndex - 1]
|
|
|
|
: linesWithNumbers[matchLineIndex + 1];
|
|
|
|
|
|
|
|
Object.assign(line, {
|
2018-11-09 14:48:41 -05:00
|
|
|
meta_data: {
|
|
|
|
old_pos: targetLine.old_line,
|
|
|
|
new_pos: targetLine.new_line,
|
2018-06-21 08:22:40 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return linesWithNumbers;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addContextLines(options) {
|
|
|
|
const { inlineLines, parallelLines, contextLines, lineNumbers } = options;
|
|
|
|
const normalizedParallelLines = contextLines.map(line => ({
|
|
|
|
left: line,
|
|
|
|
right: line,
|
|
|
|
}));
|
|
|
|
|
|
|
|
if (options.bottom) {
|
|
|
|
inlineLines.push(...contextLines);
|
|
|
|
parallelLines.push(...normalizedParallelLines);
|
|
|
|
} else {
|
|
|
|
const inlineIndex = findIndexInInlineLines(inlineLines, lineNumbers);
|
|
|
|
const parallelIndex = findIndexInParallelLines(parallelLines, lineNumbers);
|
|
|
|
inlineLines.splice(inlineIndex, 0, ...contextLines);
|
|
|
|
parallelLines.splice(parallelIndex, 0, ...normalizedParallelLines);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-05 08:05:57 -04:00
|
|
|
/**
|
|
|
|
* Trims the first char of the `richText` property when it's either a space or a diff symbol.
|
|
|
|
* @param {Object} line
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
export function trimFirstCharOfLineContent(line = {}) {
|
2018-09-02 17:26:25 -04:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2018-08-06 17:13:34 -04:00
|
|
|
delete line.text;
|
2018-09-02 17:26:25 -04:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2018-08-13 04:47:54 -04:00
|
|
|
line.discussions = [];
|
2018-08-06 17:13:34 -04:00
|
|
|
|
2018-07-05 08:05:57 -04:00
|
|
|
const parsedLine = Object.assign({}, line);
|
|
|
|
|
2018-11-09 14:48:41 -05:00
|
|
|
if (line.rich_text) {
|
|
|
|
const firstChar = parsedLine.rich_text.charAt(0);
|
2018-07-05 08:05:57 -04:00
|
|
|
|
|
|
|
if (firstChar === ' ' || firstChar === '+' || firstChar === '-') {
|
2018-11-09 14:48:41 -05:00
|
|
|
parsedLine.rich_text = line.rich_text.substring(1);
|
2018-07-05 08:05:57 -04:00
|
|
|
}
|
2018-06-21 08:22:40 -04:00
|
|
|
}
|
|
|
|
|
2018-07-05 08:05:57 -04:00
|
|
|
return parsedLine;
|
2018-06-21 08:22:40 -04:00
|
|
|
}
|
2018-08-07 22:50:01 -04:00
|
|
|
|
2018-09-07 10:20:57 -04:00
|
|
|
// This prepares and optimizes the incoming diff data from the server
|
|
|
|
// by setting up incremental rendering and removing unneeded data
|
2018-09-05 04:28:49 -04:00
|
|
|
export function prepareDiffData(diffData) {
|
2018-11-09 14:48:41 -05:00
|
|
|
const filesLength = diffData.diff_files.length;
|
2018-09-06 19:27:21 -04:00
|
|
|
let showingLines = 0;
|
2018-09-07 11:57:07 -04:00
|
|
|
for (let i = 0; i < filesLength; i += 1) {
|
2018-11-09 14:48:41 -05:00
|
|
|
const file = diffData.diff_files[i];
|
2018-09-05 04:28:49 -04:00
|
|
|
|
2018-11-09 14:48:41 -05:00
|
|
|
if (file.parallel_diff_lines) {
|
|
|
|
const linesLength = file.parallel_diff_lines.length;
|
2018-09-07 11:57:07 -04:00
|
|
|
for (let u = 0; u < linesLength; u += 1) {
|
2018-11-09 14:48:41 -05:00
|
|
|
const line = file.parallel_diff_lines[u];
|
2018-09-05 04:28:49 -04:00
|
|
|
if (line.left) {
|
|
|
|
line.left = trimFirstCharOfLineContent(line.left);
|
|
|
|
}
|
|
|
|
if (line.right) {
|
|
|
|
line.right = trimFirstCharOfLineContent(line.right);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-09 14:48:41 -05:00
|
|
|
if (file.highlighted_diff_lines) {
|
|
|
|
const linesLength = file.highlighted_diff_lines.length;
|
2018-09-07 11:57:07 -04:00
|
|
|
for (let u = 0; u < linesLength; u += 1) {
|
2018-11-09 14:48:41 -05:00
|
|
|
const line = file.highlighted_diff_lines[u];
|
2018-09-10 04:50:41 -04:00
|
|
|
Object.assign(line, { ...trimFirstCharOfLineContent(line) });
|
2018-09-05 04:28:49 -04:00
|
|
|
}
|
2018-11-09 14:48:41 -05:00
|
|
|
showingLines += file.parallel_diff_lines.length;
|
2018-09-05 04:28:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Object.assign(file, {
|
|
|
|
renderIt: showingLines < LINES_TO_BE_RENDERED_DIRECTLY,
|
|
|
|
collapsed: file.text && showingLines > MAX_LINES_TO_BE_RENDERED,
|
2018-10-18 03:50:38 -04:00
|
|
|
discussions: [],
|
2018-09-05 04:28:49 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-08 02:37:41 -04:00
|
|
|
export function getDiffPositionByLineCode(diffFiles) {
|
2018-08-07 22:50:01 -04:00
|
|
|
return diffFiles.reduce((acc, diffFile) => {
|
|
|
|
// We can only use highlightedDiffLines to create the map of diff lines because
|
|
|
|
// highlightedDiffLines will also include every parallel diff line in it.
|
2018-11-09 14:48:41 -05:00
|
|
|
if (diffFile.highlighted_diff_lines) {
|
|
|
|
diffFile.highlighted_diff_lines.forEach(line => {
|
|
|
|
if (line.line_code) {
|
|
|
|
acc[line.line_code] = {
|
|
|
|
base_sha: diffFile.diff_refs.base_sha,
|
|
|
|
head_sha: diffFile.diff_refs.head_sha,
|
|
|
|
start_sha: diffFile.diff_refs.start_sha,
|
|
|
|
new_path: diffFile.new_path,
|
|
|
|
old_path: diffFile.old_path,
|
|
|
|
old_line: line.old_line,
|
|
|
|
new_line: line.new_line,
|
|
|
|
line_code: line.line_code,
|
|
|
|
position_type: 'text',
|
2018-09-10 14:41:57 -04:00
|
|
|
};
|
2018-08-07 22:50:01 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
}
|
2018-09-08 02:37:41 -04:00
|
|
|
|
|
|
|
// This method will check whether the discussion is still applicable
|
|
|
|
// to the diff line in question regarding different versions of the MR
|
2018-09-18 11:27:53 -04:00
|
|
|
export function isDiscussionApplicableToLine({ discussion, diffPosition, latestDiff }) {
|
2018-11-09 14:48:41 -05:00
|
|
|
const { line_code, ...diffPositionCopy } = diffPosition;
|
2018-09-08 02:37:41 -04:00
|
|
|
|
2018-09-10 14:41:57 -04:00
|
|
|
if (discussion.original_position && discussion.position) {
|
2018-11-09 14:48:41 -05:00
|
|
|
const originalRefs = discussion.original_position;
|
|
|
|
const refs = discussion.position;
|
2018-09-10 14:41:57 -04:00
|
|
|
|
|
|
|
return _.isEqual(refs, diffPositionCopy) || _.isEqual(originalRefs, diffPositionCopy);
|
|
|
|
}
|
|
|
|
|
2018-11-09 14:48:41 -05:00
|
|
|
// eslint-disable-next-line
|
|
|
|
return latestDiff && discussion.active && line_code === discussion.line_code;
|
2018-09-08 02:37:41 -04:00
|
|
|
}
|
2018-10-03 05:05:43 -04:00
|
|
|
|
|
|
|
export const generateTreeList = files =>
|
|
|
|
files.reduce(
|
|
|
|
(acc, file) => {
|
2018-11-09 14:48:41 -05:00
|
|
|
const split = file.new_path.split('/');
|
2018-10-03 05:05:43 -04:00
|
|
|
|
|
|
|
split.forEach((name, i) => {
|
|
|
|
const parent = acc.treeEntries[split.slice(0, i).join('/')];
|
|
|
|
const path = `${parent ? `${parent.path}/` : ''}${name}`;
|
|
|
|
|
|
|
|
if (!acc.treeEntries[path]) {
|
2018-11-09 14:48:41 -05:00
|
|
|
const type = path === file.new_path ? 'blob' : 'tree';
|
2018-10-03 05:05:43 -04:00
|
|
|
acc.treeEntries[path] = {
|
|
|
|
key: path,
|
|
|
|
path,
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
tree: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
const entry = acc.treeEntries[path];
|
|
|
|
|
|
|
|
if (type === 'blob') {
|
|
|
|
Object.assign(entry, {
|
|
|
|
changed: true,
|
2018-11-09 14:48:41 -05:00
|
|
|
tempFile: file.new_file,
|
|
|
|
deleted: file.deleted_file,
|
|
|
|
fileHash: file.file_hash,
|
|
|
|
addedLines: file.added_lines,
|
|
|
|
removedLines: file.removed_lines,
|
2018-10-03 05:05:43 -04:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Object.assign(entry, {
|
|
|
|
opened: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
(parent ? parent.tree : acc.tree).push(entry);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{ treeEntries: {}, tree: [] },
|
|
|
|
);
|
2018-10-18 03:50:38 -04:00
|
|
|
|
|
|
|
export const getDiffMode = diffFile => {
|
2018-11-09 14:48:41 -05:00
|
|
|
const diffModeKey = Object.keys(diffModes).find(key => diffFile[`${key}_file`]);
|
2018-10-18 03:50:38 -04:00
|
|
|
return diffModes[diffModeKey] || diffModes.replaced;
|
|
|
|
};
|