feat: hand in file/line to generic line template

This commit is contained in:
Phillip Kessels 2023-02-20 15:07:34 +00:00
parent 09cbe87595
commit 0d314aecd1
No known key found for this signature in database
GPG key ID: 67EABA7195979CD4
2 changed files with 66 additions and 14 deletions

View file

@ -23,9 +23,19 @@ describe('LineByLineRenderer', () => {
describe('makeLineHtml', () => { describe('makeLineHtml', () => {
it('should work for insertions', () => { it('should work for insertions', () => {
const file = {
addedLines: 12,
deletedLines: 41,
language: 'js',
oldName: 'my/file/name.js',
newName: 'my/file/name.js',
isCombined: false,
isGitDiff: false,
blocks: [],
};
const hoganUtils = new HoganJsUtils({}); const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
const fileHtml = lineByLineRenderer.generateSingleLineHtml({ const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, {
type: CSSLineClass.INSERTS, type: CSSLineClass.INSERTS,
prefix: '+', prefix: '+',
content: 'test', content: 'test',
@ -49,9 +59,19 @@ describe('LineByLineRenderer', () => {
}); });
it('should work for deletions', () => { it('should work for deletions', () => {
const file = {
addedLines: 12,
deletedLines: 41,
language: 'js',
oldName: 'my/file/name.js',
newName: 'my/file/name.js',
isCombined: false,
isGitDiff: false,
blocks: [],
};
const hoganUtils = new HoganJsUtils({}); const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
const fileHtml = lineByLineRenderer.generateSingleLineHtml({ const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, {
type: CSSLineClass.DELETES, type: CSSLineClass.DELETES,
prefix: '-', prefix: '-',
content: 'test', content: 'test',
@ -75,9 +95,19 @@ describe('LineByLineRenderer', () => {
}); });
it('should convert indents into non breakin spaces (2 white spaces)', () => { it('should convert indents into non breakin spaces (2 white spaces)', () => {
const file = {
addedLines: 12,
deletedLines: 41,
language: 'js',
oldName: 'my/file/name.js',
newName: 'my/file/name.js',
isCombined: false,
isGitDiff: false,
blocks: [],
};
const hoganUtils = new HoganJsUtils({}); const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
const fileHtml = lineByLineRenderer.generateSingleLineHtml({ const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, {
type: CSSLineClass.INSERTS, type: CSSLineClass.INSERTS,
prefix: '+', prefix: '+',
content: ' test', content: ' test',
@ -101,9 +131,19 @@ describe('LineByLineRenderer', () => {
}); });
it('should convert indents into non breakin spaces (4 white spaces)', () => { it('should convert indents into non breakin spaces (4 white spaces)', () => {
const file = {
addedLines: 12,
deletedLines: 41,
language: 'js',
oldName: 'my/file/name.js',
newName: 'my/file/name.js',
isCombined: false,
isGitDiff: false,
blocks: [],
};
const hoganUtils = new HoganJsUtils({}); const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
const fileHtml = lineByLineRenderer.generateSingleLineHtml({ const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, {
type: CSSLineClass.INSERTS, type: CSSLineClass.INSERTS,
prefix: '+', prefix: '+',
content: ' test', content: ' test',
@ -127,9 +167,19 @@ describe('LineByLineRenderer', () => {
}); });
it('should preserve tabs', () => { it('should preserve tabs', () => {
const file = {
addedLines: 12,
deletedLines: 41,
language: 'js',
oldName: 'my/file/name.js',
newName: 'my/file/name.js',
isCombined: false,
isGitDiff: false,
blocks: [],
};
const hoganUtils = new HoganJsUtils({}); const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
const fileHtml = lineByLineRenderer.generateSingleLineHtml({ const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, {
type: CSSLineClass.INSERTS, type: CSSLineClass.INSERTS,
prefix: '+', prefix: '+',
content: '\ttest', content: '\ttest',

View file

@ -103,14 +103,14 @@ export default class LineByLineRenderer {
this.applyLineGroupping(block).forEach(([contextLines, oldLines, newLines]) => { this.applyLineGroupping(block).forEach(([contextLines, oldLines, newLines]) => {
if (oldLines.length && newLines.length && !contextLines.length) { if (oldLines.length && newLines.length && !contextLines.length) {
this.applyRematchMatching(oldLines, newLines, matcher).map(([oldLines, newLines]) => { this.applyRematchMatching(oldLines, newLines, matcher).map(([oldLines, newLines]) => {
const { left, right } = this.processChangedLines(file.isCombined, oldLines, newLines); const { left, right } = this.processChangedLines(file, file.isCombined, oldLines, newLines);
lines += left; lines += left;
lines += right; lines += right;
}); });
} else if (contextLines.length) { } else if (contextLines.length) {
contextLines.forEach(line => { contextLines.forEach(line => {
const { prefix, content } = renderUtils.deconstructLine(line.content, file.isCombined); const { prefix, content } = renderUtils.deconstructLine(line.content, file.isCombined);
lines += this.generateSingleLineHtml({ lines += this.generateSingleLineHtml(file, {
type: renderUtils.CSSLineClass.CONTEXT, type: renderUtils.CSSLineClass.CONTEXT,
prefix: prefix, prefix: prefix,
content: content, content: content,
@ -119,7 +119,7 @@ export default class LineByLineRenderer {
}); });
}); });
} else if (oldLines.length || newLines.length) { } else if (oldLines.length || newLines.length) {
const { left, right } = this.processChangedLines(file.isCombined, oldLines, newLines); const { left, right } = this.processChangedLines(file, file.isCombined, oldLines, newLines);
lines += left; lines += left;
lines += right; lines += right;
} else { } else {
@ -188,7 +188,7 @@ export default class LineByLineRenderer {
return doMatching ? matcher(oldLines, newLines) : [[oldLines, newLines]]; return doMatching ? matcher(oldLines, newLines) : [[oldLines, newLines]];
} }
processChangedLines(isCombined: boolean, oldLines: DiffLine[], newLines: DiffLine[]): FileHtml { processChangedLines(file: DiffFile, isCombined: boolean, oldLines: DiffLine[], newLines: DiffLine[]): FileHtml {
const fileHtml = { const fileHtml = {
right: '', right: '',
left: '', left: '',
@ -240,7 +240,7 @@ export default class LineByLineRenderer {
} }
: undefined; : undefined;
const { left, right } = this.generateLineHtml(preparedOldLine, preparedNewLine); const { left, right } = this.generateLineHtml(file, preparedOldLine, preparedNewLine);
fileHtml.left += left; fileHtml.left += left;
fileHtml.right += right; fileHtml.right += right;
} }
@ -248,14 +248,14 @@ export default class LineByLineRenderer {
return fileHtml; return fileHtml;
} }
generateLineHtml(oldLine?: DiffPreparedLine, newLine?: DiffPreparedLine): FileHtml { generateLineHtml(file: DiffFile, oldLine?: DiffPreparedLine, newLine?: DiffPreparedLine): FileHtml {
return { return {
left: this.generateSingleLineHtml(oldLine), left: this.generateSingleLineHtml(file, oldLine),
right: this.generateSingleLineHtml(newLine), right: this.generateSingleLineHtml(file, newLine),
}; };
} }
generateSingleLineHtml(line?: DiffPreparedLine): string { generateSingleLineHtml(file: DiffFile, line?: DiffPreparedLine): string {
if (line === undefined) return ''; if (line === undefined) return '';
const lineNumberHtml = this.hoganUtils.render(baseTemplatesPath, 'numbers', { const lineNumberHtml = this.hoganUtils.render(baseTemplatesPath, 'numbers', {
@ -270,6 +270,8 @@ export default class LineByLineRenderer {
prefix: line.prefix === ' ' ? ' ' : line.prefix, prefix: line.prefix === ' ' ? ' ' : line.prefix,
content: line.content, content: line.content,
lineNumber: lineNumberHtml, lineNumber: lineNumberHtml,
line,
file,
}); });
} }
} }