Merge branch 'master' into master

This commit is contained in:
Lordfirespeed 2023-03-15 04:28:35 +00:00 committed by GitHub
commit 497c8aca5a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 12 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

@ -115,7 +115,7 @@ export default class LineByLineRenderer {
this.applyLineGrouping(block).forEach(([contextLines, oldLines, newLines]) => { this.applyLineGrouping(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.push(...left); lines.push(...left);
lines.push(...right); lines.push(...right);
}); });
@ -135,6 +135,20 @@ 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.isCombined, oldLines, newLines);
lines.push(...left); lines.push(...left);
lines.push(...right);
lines.push(
this.generateSingleLineHtml(file, {
type: renderUtils.CSSLineClass.CONTEXT,
prefix: prefix,
content: content,
oldNumber: line.oldNumber,
newNumber: line.newNumber,
}),
);
});
} else if (oldLines.length || newLines.length) {
const { left, right } = this.processChangedLines(file, file.isCombined, oldLines, newLines);
lines.push(...left);
lines.push(...right); lines.push(...right);
} else { } else {
console.error('Unknown state reached while processing groups of lines', contextLines, oldLines, newLines); console.error('Unknown state reached while processing groups of lines', contextLines, oldLines, newLines);
@ -205,7 +219,8 @@ 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: FileHtml = { const fileHtml: FileHtml = {
left: [], left: [],
right: [], right: [],
@ -257,7 +272,7 @@ export default class LineByLineRenderer {
} }
: undefined; : undefined;
const { left, right } = this.generateLineHtml(preparedOldLine, preparedNewLine); const { left, right } = this.generateLineHtml(file, preparedOldLine, preparedNewLine);
fileHtml.left.push(left); fileHtml.left.push(left);
fileHtml.right.push(right); fileHtml.right.push(right);
} }
@ -265,14 +280,14 @@ export default class LineByLineRenderer {
return fileHtml; return fileHtml;
} }
generateLineHtml(oldLine?: DiffPreparedLine, newLine?: DiffPreparedLine): LineHtml { generateLineHtml(file: DiffFile, oldLine?: DiffPreparedLine, newLine?: DiffPreparedLine): LineHtml {
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 oldLineNumberHtml = this.hoganUtils.render(genericTemplatesPath, 'line-number', { const oldLineNumberHtml = this.hoganUtils.render(genericTemplatesPath, 'line-number', {
@ -292,6 +307,9 @@ export default class LineByLineRenderer {
contentClass: 'd2h-code-line', contentClass: 'd2h-code-line',
prefix: line.prefix === ' ' ? ' ' : line.prefix, prefix: line.prefix === ' ' ? ' ' : line.prefix,
content: line.content, content: line.content,
lineNumber: lineNumberHtml,
line,
file,
}); });
return oldLineNumberHtml.concat(newLineNumberHtml, newLineContentHtml); return oldLineNumberHtml.concat(newLineNumberHtml, newLineContentHtml);