Skip to content

Commit c1d6701

Browse files
author
mhawamdeh
committed
preserve context
1 parent 6975f09 commit c1d6701

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

src/side-by-side-renderer.ts

+28-21
Original file line numberDiff line numberDiff line change
@@ -100,34 +100,41 @@ export default class SideBySideRenderer {
100100
left: this.makeHeaderHtml(block.header, file),
101101
right: this.makeHeaderHtml(''),
102102
};
103-
103+
let contextStash: (DiffLineContent & DiffLineContext)[] = []; // if white space are ignored, context should be relevant
104+
let processContext = false;
104105
this.applyLineGroupping(block).forEach(([contextLines, oldLines, newLines]) => {
105106
if (oldLines.length && newLines.length && !contextLines.length) {
106107
this.applyRematchMatching(oldLines, newLines, matcher).map(([oldLines, newLines]) => {
107108
const { left, right } = this.processChangedLines(file.isCombined, oldLines, newLines);
108109
fileHtml.left += left;
109110
fileHtml.right += right;
111+
if (!left && !right) {
112+
contextStash = [];
113+
processContext = false;
114+
} else processContext = true;
110115
});
111-
} else if (contextLines.length) {
112-
contextLines.forEach(line => {
113-
const { prefix, content } = renderUtils.deconstructLine(line.content, file.isCombined);
114-
const { left, right } = this.generateLineHtml(
115-
{
116-
type: renderUtils.CSSLineClass.CONTEXT,
117-
prefix: prefix,
118-
content: content,
119-
number: line.oldNumber,
120-
},
121-
{
122-
type: renderUtils.CSSLineClass.CONTEXT,
123-
prefix: prefix,
124-
content: content,
125-
number: line.newNumber,
126-
},
127-
);
128-
fileHtml.left += left;
129-
fileHtml.right += right;
130-
});
116+
} else if (contextLines.length || contextStash.length) {
117+
if (!processContext) contextStash = contextStash.concat(contextLines);
118+
else
119+
contextLines.concat(processContext ? contextStash : []).forEach(line => {
120+
const { prefix, content } = renderUtils.deconstructLine(line.content, file.isCombined);
121+
const { left, right } = this.generateLineHtml(
122+
{
123+
type: renderUtils.CSSLineClass.CONTEXT,
124+
prefix: prefix,
125+
content: content,
126+
number: line.oldNumber,
127+
},
128+
{
129+
type: renderUtils.CSSLineClass.CONTEXT,
130+
prefix: prefix,
131+
content: content,
132+
number: line.newNumber,
133+
},
134+
);
135+
fileHtml.left += left;
136+
fileHtml.right += right;
137+
});
131138
} else if (oldLines.length || newLines.length) {
132139
const { left, right } = this.processChangedLines(file.isCombined, oldLines, newLines);
133140
fileHtml.left += left;

0 commit comments

Comments
 (0)