2019-10-12 21:45:49 +00:00
|
|
|
import HoganJsUtils from "./hoganjs-utils";
|
|
|
|
|
import * as Rematch from "./rematch";
|
|
|
|
|
import * as renderUtils from "./render-utils";
|
2019-11-26 09:44:09 +00:00
|
|
|
import { DiffFile, DiffLine, LineType } from "./types";
|
2019-10-12 21:45:49 +00:00
|
|
|
|
|
|
|
|
export interface LineByLineRendererConfig extends renderUtils.RenderConfig {
|
|
|
|
|
renderNothingWhenEmpty?: boolean;
|
|
|
|
|
matchingMaxComparisons?: number;
|
|
|
|
|
maxLineSizeInBlockForComparison?: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const defaultLineByLineRendererConfig = {
|
2019-10-13 18:21:19 +00:00
|
|
|
...renderUtils.defaultRenderConfig,
|
2019-10-12 21:45:49 +00:00
|
|
|
renderNothingWhenEmpty: false,
|
|
|
|
|
matchingMaxComparisons: 2500,
|
2019-10-13 18:21:19 +00:00
|
|
|
maxLineSizeInBlockForComparison: 200
|
2019-10-12 21:45:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const genericTemplatesPath = "generic";
|
|
|
|
|
const baseTemplatesPath = "line-by-line";
|
|
|
|
|
const iconsBaseTemplatesPath = "icon";
|
|
|
|
|
const tagsBaseTemplatesPath = "tag";
|
|
|
|
|
|
|
|
|
|
export default class LineByLineRenderer {
|
|
|
|
|
private readonly hoganUtils: HoganJsUtils;
|
|
|
|
|
private readonly config: typeof defaultLineByLineRendererConfig;
|
|
|
|
|
|
2019-10-21 22:37:42 +00:00
|
|
|
constructor(hoganUtils: HoganJsUtils, config: LineByLineRendererConfig = {}) {
|
2019-10-12 21:45:49 +00:00
|
|
|
this.hoganUtils = hoganUtils;
|
|
|
|
|
this.config = { ...defaultLineByLineRendererConfig, ...config };
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 22:37:42 +00:00
|
|
|
render(diffFiles: DiffFile[]): string | undefined {
|
2019-11-26 09:09:59 +00:00
|
|
|
const diffsHtml = diffFiles
|
|
|
|
|
.map(file => {
|
|
|
|
|
let diffs;
|
|
|
|
|
if (file.blocks.length) {
|
|
|
|
|
diffs = this.generateFileHtml(file);
|
|
|
|
|
} else {
|
|
|
|
|
diffs = this.generateEmptyDiff();
|
|
|
|
|
}
|
|
|
|
|
return this.makeFileDiffHtml(file, diffs);
|
|
|
|
|
})
|
|
|
|
|
.join("\n");
|
2019-10-12 21:45:49 +00:00
|
|
|
|
2019-11-26 09:09:59 +00:00
|
|
|
return this.hoganUtils.render(genericTemplatesPath, "wrapper", { content: diffsHtml });
|
2019-10-12 21:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Make this private after improving tests
|
2019-10-21 22:37:42 +00:00
|
|
|
makeFileDiffHtml(file: DiffFile, diffs: string): string {
|
2019-10-12 21:45:49 +00:00
|
|
|
if (this.config.renderNothingWhenEmpty && Array.isArray(file.blocks) && file.blocks.length === 0) return "";
|
|
|
|
|
|
|
|
|
|
const fileDiffTemplate = this.hoganUtils.template(baseTemplatesPath, "file-diff");
|
|
|
|
|
const filePathTemplate = this.hoganUtils.template(genericTemplatesPath, "file-path");
|
|
|
|
|
const fileIconTemplate = this.hoganUtils.template(iconsBaseTemplatesPath, "file");
|
|
|
|
|
const fileTagTemplate = this.hoganUtils.template(tagsBaseTemplatesPath, renderUtils.getFileIcon(file));
|
|
|
|
|
|
|
|
|
|
return fileDiffTemplate.render({
|
|
|
|
|
file: file,
|
|
|
|
|
fileHtmlId: renderUtils.getHtmlId(file),
|
|
|
|
|
diffs: diffs,
|
|
|
|
|
filePath: filePathTemplate.render(
|
|
|
|
|
{
|
|
|
|
|
fileDiffName: renderUtils.filenameDiff(file)
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
fileIcon: fileIconTemplate,
|
|
|
|
|
fileTag: fileTagTemplate
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Make this private after improving tests
|
|
|
|
|
generateEmptyDiff(): string {
|
|
|
|
|
return this.hoganUtils.render(genericTemplatesPath, "empty-diff", {
|
|
|
|
|
contentClass: "d2h-code-line",
|
|
|
|
|
CSSLineClass: renderUtils.CSSLineClass
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Make this private after improving tests
|
2019-10-21 22:37:42 +00:00
|
|
|
generateFileHtml(file: DiffFile): string {
|
2019-11-26 09:44:09 +00:00
|
|
|
const matcher = Rematch.newMatcherFn(
|
|
|
|
|
Rematch.newDistanceFn((e: DiffLine) => renderUtils.deconstructLine(e.content, file.isCombined).content)
|
2019-10-21 22:37:42 +00:00
|
|
|
);
|
2019-10-12 21:45:49 +00:00
|
|
|
|
|
|
|
|
return file.blocks
|
|
|
|
|
.map(block => {
|
2019-11-26 09:44:09 +00:00
|
|
|
let lines = this.hoganUtils.render(genericTemplatesPath, "column-line-number", {
|
|
|
|
|
CSSLineClass: renderUtils.CSSLineClass,
|
|
|
|
|
blockHeader: block.header,
|
|
|
|
|
lineClass: "d2h-code-linenumber",
|
|
|
|
|
contentClass: "d2h-code-line"
|
|
|
|
|
});
|
2019-10-21 22:37:42 +00:00
|
|
|
let oldLines: DiffLine[] = [];
|
|
|
|
|
let newLines: DiffLine[] = [];
|
2019-10-12 21:45:49 +00:00
|
|
|
|
|
|
|
|
const processChangeBlock = (): void => {
|
|
|
|
|
let matches;
|
|
|
|
|
let insertType: renderUtils.CSSLineClass;
|
|
|
|
|
let deleteType: renderUtils.CSSLineClass;
|
|
|
|
|
|
|
|
|
|
const comparisons = oldLines.length * newLines.length;
|
|
|
|
|
|
|
|
|
|
const maxLineSizeInBlock = Math.max.apply(
|
|
|
|
|
null,
|
|
|
|
|
[0].concat(oldLines.concat(newLines).map(elem => elem.content.length))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const doMatching =
|
|
|
|
|
comparisons < this.config.matchingMaxComparisons &&
|
|
|
|
|
maxLineSizeInBlock < this.config.maxLineSizeInBlockForComparison &&
|
|
|
|
|
(this.config.matching === "lines" || this.config.matching === "words");
|
|
|
|
|
|
|
|
|
|
if (doMatching) {
|
|
|
|
|
matches = matcher(oldLines, newLines);
|
|
|
|
|
insertType = renderUtils.CSSLineClass.INSERT_CHANGES;
|
|
|
|
|
deleteType = renderUtils.CSSLineClass.DELETE_CHANGES;
|
|
|
|
|
} else {
|
|
|
|
|
matches = [[oldLines, newLines]];
|
|
|
|
|
insertType = renderUtils.CSSLineClass.INSERTS;
|
|
|
|
|
deleteType = renderUtils.CSSLineClass.DELETES;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
matches.forEach(match => {
|
|
|
|
|
oldLines = match[0];
|
|
|
|
|
newLines = match[1];
|
|
|
|
|
|
|
|
|
|
let processedOldLines = "";
|
|
|
|
|
let processedNewLines = "";
|
|
|
|
|
|
|
|
|
|
const common = Math.min(oldLines.length, newLines.length);
|
|
|
|
|
|
|
|
|
|
let oldLine, newLine;
|
|
|
|
|
for (let j = 0; j < common; j++) {
|
|
|
|
|
oldLine = oldLines[j];
|
|
|
|
|
newLine = newLines[j];
|
|
|
|
|
|
|
|
|
|
const diff = renderUtils.diffHighlight(oldLine.content, newLine.content, file.isCombined, this.config);
|
|
|
|
|
|
|
|
|
|
processedOldLines += this.makeLineHtml(
|
|
|
|
|
file.isCombined,
|
|
|
|
|
deleteType,
|
|
|
|
|
diff.oldLine.content,
|
|
|
|
|
oldLine.oldNumber,
|
|
|
|
|
oldLine.newNumber,
|
|
|
|
|
diff.oldLine.prefix
|
|
|
|
|
);
|
|
|
|
|
processedNewLines += this.makeLineHtml(
|
|
|
|
|
file.isCombined,
|
|
|
|
|
insertType,
|
|
|
|
|
diff.newLine.content,
|
|
|
|
|
newLine.oldNumber,
|
|
|
|
|
newLine.newNumber,
|
|
|
|
|
diff.newLine.prefix
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lines += processedOldLines + processedNewLines;
|
|
|
|
|
lines += this.processLines(file.isCombined, oldLines.slice(common), newLines.slice(common));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
oldLines = [];
|
|
|
|
|
newLines = [];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < block.lines.length; i++) {
|
|
|
|
|
const diffLine = block.lines[i];
|
2019-11-26 09:44:09 +00:00
|
|
|
const { prefix, content } = renderUtils.deconstructLine(diffLine.content, file.isCombined);
|
2019-10-12 21:45:49 +00:00
|
|
|
|
|
|
|
|
if (
|
2019-10-21 22:37:42 +00:00
|
|
|
diffLine.type !== LineType.INSERT &&
|
|
|
|
|
(newLines.length > 0 || (diffLine.type !== LineType.DELETE && oldLines.length > 0))
|
2019-10-12 21:45:49 +00:00
|
|
|
) {
|
|
|
|
|
processChangeBlock();
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 22:37:42 +00:00
|
|
|
if (diffLine.type === LineType.CONTEXT) {
|
2019-10-12 21:45:49 +00:00
|
|
|
lines += this.makeLineHtml(
|
|
|
|
|
file.isCombined,
|
|
|
|
|
renderUtils.toCSSClass(diffLine.type),
|
2019-11-26 09:44:09 +00:00
|
|
|
content,
|
2019-10-12 21:45:49 +00:00
|
|
|
diffLine.oldNumber,
|
|
|
|
|
diffLine.newNumber,
|
|
|
|
|
prefix
|
|
|
|
|
);
|
2019-10-21 22:37:42 +00:00
|
|
|
} else if (diffLine.type === LineType.INSERT && !oldLines.length) {
|
2019-10-12 21:45:49 +00:00
|
|
|
lines += this.makeLineHtml(
|
|
|
|
|
file.isCombined,
|
|
|
|
|
renderUtils.toCSSClass(diffLine.type),
|
2019-11-26 09:44:09 +00:00
|
|
|
content,
|
2019-10-12 21:45:49 +00:00
|
|
|
diffLine.oldNumber,
|
|
|
|
|
diffLine.newNumber,
|
|
|
|
|
prefix
|
|
|
|
|
);
|
2019-10-21 22:37:42 +00:00
|
|
|
} else if (diffLine.type === LineType.DELETE) {
|
2019-10-12 21:45:49 +00:00
|
|
|
oldLines.push(diffLine);
|
2019-10-21 22:37:42 +00:00
|
|
|
} else if (diffLine.type === LineType.INSERT && Boolean(oldLines.length)) {
|
2019-10-12 21:45:49 +00:00
|
|
|
newLines.push(diffLine);
|
|
|
|
|
} else {
|
|
|
|
|
console.error("Unknown state in html line-by-line generator");
|
|
|
|
|
processChangeBlock();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
processChangeBlock();
|
|
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
|
})
|
|
|
|
|
.join("\n");
|
|
|
|
|
}
|
2019-11-26 09:44:09 +00:00
|
|
|
|
|
|
|
|
// TODO: Make this private after improving tests
|
|
|
|
|
makeLineHtml(
|
|
|
|
|
isCombined: boolean,
|
|
|
|
|
type: renderUtils.CSSLineClass,
|
|
|
|
|
content: string,
|
|
|
|
|
oldNumber?: number,
|
|
|
|
|
newNumber?: number,
|
|
|
|
|
possiblePrefix?: string
|
|
|
|
|
): string {
|
|
|
|
|
const lineNumberTemplate = this.hoganUtils.render(baseTemplatesPath, "numbers", {
|
|
|
|
|
oldNumber: oldNumber || "",
|
|
|
|
|
newNumber: newNumber || ""
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let lineWithoutPrefix = content;
|
|
|
|
|
let prefix = possiblePrefix;
|
|
|
|
|
|
|
|
|
|
if (!prefix) {
|
|
|
|
|
const lineWithPrefix = renderUtils.deconstructLine(content, isCombined);
|
|
|
|
|
prefix = lineWithPrefix.prefix;
|
|
|
|
|
lineWithoutPrefix = lineWithPrefix.content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (prefix === " ") {
|
|
|
|
|
prefix = " ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.hoganUtils.render(genericTemplatesPath, "line", {
|
|
|
|
|
type: type,
|
|
|
|
|
lineClass: "d2h-code-linenumber",
|
|
|
|
|
contentClass: "d2h-code-line",
|
|
|
|
|
prefix: prefix,
|
|
|
|
|
content: lineWithoutPrefix,
|
|
|
|
|
lineNumber: lineNumberTemplate
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Make this private after improving tests
|
|
|
|
|
processLines(isCombined: boolean, oldLines: DiffLine[], newLines: DiffLine[]): string {
|
|
|
|
|
let lines = "";
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < oldLines.length; i++) {
|
|
|
|
|
const oldLine = oldLines[i];
|
|
|
|
|
lines += this.makeLineHtml(
|
|
|
|
|
isCombined,
|
|
|
|
|
renderUtils.toCSSClass(oldLine.type),
|
|
|
|
|
oldLine.content,
|
|
|
|
|
oldLine.oldNumber,
|
|
|
|
|
oldLine.newNumber
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (let j = 0; j < newLines.length; j++) {
|
|
|
|
|
const newLine = newLines[j];
|
|
|
|
|
lines += this.makeLineHtml(
|
|
|
|
|
isCombined,
|
|
|
|
|
renderUtils.toCSSClass(newLine.type),
|
|
|
|
|
newLine.content,
|
|
|
|
|
newLine.oldNumber,
|
|
|
|
|
newLine.newNumber
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
|
}
|
2019-10-12 21:45:49 +00:00
|
|
|
}
|