Skip to content

Commit 6851537

Browse files
authored
Merge pull request #483 from phikes/hand-in-file-line-to-template
feat: hand in file/line to generic line template
2 parents 551a0b4 + 0d314ae commit 6851537

File tree

2 files changed

+66
-14
lines changed

2 files changed

+66
-14
lines changed

src/__tests__/line-by-line-tests.ts

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,19 @@ describe('LineByLineRenderer', () => {
2323

2424
describe('makeLineHtml', () => {
2525
it('should work for insertions', () => {
26+
const file = {
27+
addedLines: 12,
28+
deletedLines: 41,
29+
language: 'js',
30+
oldName: 'my/file/name.js',
31+
newName: 'my/file/name.js',
32+
isCombined: false,
33+
isGitDiff: false,
34+
blocks: [],
35+
};
2636
const hoganUtils = new HoganJsUtils({});
2737
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
28-
const fileHtml = lineByLineRenderer.generateSingleLineHtml({
38+
const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, {
2939
type: CSSLineClass.INSERTS,
3040
prefix: '+',
3141
content: 'test',
@@ -49,9 +59,19 @@ describe('LineByLineRenderer', () => {
4959
});
5060

5161
it('should work for deletions', () => {
62+
const file = {
63+
addedLines: 12,
64+
deletedLines: 41,
65+
language: 'js',
66+
oldName: 'my/file/name.js',
67+
newName: 'my/file/name.js',
68+
isCombined: false,
69+
isGitDiff: false,
70+
blocks: [],
71+
};
5272
const hoganUtils = new HoganJsUtils({});
5373
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
54-
const fileHtml = lineByLineRenderer.generateSingleLineHtml({
74+
const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, {
5575
type: CSSLineClass.DELETES,
5676
prefix: '-',
5777
content: 'test',
@@ -75,9 +95,19 @@ describe('LineByLineRenderer', () => {
7595
});
7696

7797
it('should convert indents into non breakin spaces (2 white spaces)', () => {
98+
const file = {
99+
addedLines: 12,
100+
deletedLines: 41,
101+
language: 'js',
102+
oldName: 'my/file/name.js',
103+
newName: 'my/file/name.js',
104+
isCombined: false,
105+
isGitDiff: false,
106+
blocks: [],
107+
};
78108
const hoganUtils = new HoganJsUtils({});
79109
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
80-
const fileHtml = lineByLineRenderer.generateSingleLineHtml({
110+
const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, {
81111
type: CSSLineClass.INSERTS,
82112
prefix: '+',
83113
content: ' test',
@@ -101,9 +131,19 @@ describe('LineByLineRenderer', () => {
101131
});
102132

103133
it('should convert indents into non breakin spaces (4 white spaces)', () => {
134+
const file = {
135+
addedLines: 12,
136+
deletedLines: 41,
137+
language: 'js',
138+
oldName: 'my/file/name.js',
139+
newName: 'my/file/name.js',
140+
isCombined: false,
141+
isGitDiff: false,
142+
blocks: [],
143+
};
104144
const hoganUtils = new HoganJsUtils({});
105145
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
106-
const fileHtml = lineByLineRenderer.generateSingleLineHtml({
146+
const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, {
107147
type: CSSLineClass.INSERTS,
108148
prefix: '+',
109149
content: ' test',
@@ -127,9 +167,19 @@ describe('LineByLineRenderer', () => {
127167
});
128168

129169
it('should preserve tabs', () => {
170+
const file = {
171+
addedLines: 12,
172+
deletedLines: 41,
173+
language: 'js',
174+
oldName: 'my/file/name.js',
175+
newName: 'my/file/name.js',
176+
isCombined: false,
177+
isGitDiff: false,
178+
blocks: [],
179+
};
130180
const hoganUtils = new HoganJsUtils({});
131181
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
132-
const fileHtml = lineByLineRenderer.generateSingleLineHtml({
182+
const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, {
133183
type: CSSLineClass.INSERTS,
134184
prefix: '+',
135185
content: '\ttest',

src/line-by-line-renderer.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ export default class LineByLineRenderer {
103103
this.applyLineGroupping(block).forEach(([contextLines, oldLines, newLines]) => {
104104
if (oldLines.length && newLines.length && !contextLines.length) {
105105
this.applyRematchMatching(oldLines, newLines, matcher).map(([oldLines, newLines]) => {
106-
const { left, right } = this.processChangedLines(file.isCombined, oldLines, newLines);
106+
const { left, right } = this.processChangedLines(file, file.isCombined, oldLines, newLines);
107107
lines += left;
108108
lines += right;
109109
});
110110
} else if (contextLines.length) {
111111
contextLines.forEach(line => {
112112
const { prefix, content } = renderUtils.deconstructLine(line.content, file.isCombined);
113-
lines += this.generateSingleLineHtml({
113+
lines += this.generateSingleLineHtml(file, {
114114
type: renderUtils.CSSLineClass.CONTEXT,
115115
prefix: prefix,
116116
content: content,
@@ -119,7 +119,7 @@ export default class LineByLineRenderer {
119119
});
120120
});
121121
} else if (oldLines.length || newLines.length) {
122-
const { left, right } = this.processChangedLines(file.isCombined, oldLines, newLines);
122+
const { left, right } = this.processChangedLines(file, file.isCombined, oldLines, newLines);
123123
lines += left;
124124
lines += right;
125125
} else {
@@ -188,7 +188,7 @@ export default class LineByLineRenderer {
188188
return doMatching ? matcher(oldLines, newLines) : [[oldLines, newLines]];
189189
}
190190

191-
processChangedLines(isCombined: boolean, oldLines: DiffLine[], newLines: DiffLine[]): FileHtml {
191+
processChangedLines(file: DiffFile, isCombined: boolean, oldLines: DiffLine[], newLines: DiffLine[]): FileHtml {
192192
const fileHtml = {
193193
right: '',
194194
left: '',
@@ -240,22 +240,22 @@ export default class LineByLineRenderer {
240240
}
241241
: undefined;
242242

243-
const { left, right } = this.generateLineHtml(preparedOldLine, preparedNewLine);
243+
const { left, right } = this.generateLineHtml(file, preparedOldLine, preparedNewLine);
244244
fileHtml.left += left;
245245
fileHtml.right += right;
246246
}
247247

248248
return fileHtml;
249249
}
250250

251-
generateLineHtml(oldLine?: DiffPreparedLine, newLine?: DiffPreparedLine): FileHtml {
251+
generateLineHtml(file: DiffFile, oldLine?: DiffPreparedLine, newLine?: DiffPreparedLine): FileHtml {
252252
return {
253-
left: this.generateSingleLineHtml(oldLine),
254-
right: this.generateSingleLineHtml(newLine),
253+
left: this.generateSingleLineHtml(file, oldLine),
254+
right: this.generateSingleLineHtml(file, newLine),
255255
};
256256
}
257257

258-
generateSingleLineHtml(line?: DiffPreparedLine): string {
258+
generateSingleLineHtml(file: DiffFile, line?: DiffPreparedLine): string {
259259
if (line === undefined) return '';
260260

261261
const lineNumberHtml = this.hoganUtils.render(baseTemplatesPath, 'numbers', {
@@ -270,6 +270,8 @@ export default class LineByLineRenderer {
270270
prefix: line.prefix === ' ' ? ' ' : line.prefix,
271271
content: line.content,
272272
lineNumber: lineNumberHtml,
273+
line,
274+
file,
273275
});
274276
}
275277
}

0 commit comments

Comments
 (0)