Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit c0f6679

Browse files
author
Tilde Ann Thurium
authored
Merge pull request #2007 from atom/tt-19-mar-destructure-bug
Fix bug expanding large-diff-gated patches with comments
2 parents 14c238c + 41136a6 commit c0f6679

File tree

2 files changed

+64
-15
lines changed

2 files changed

+64
-15
lines changed

lib/models/patch/multi-file-patch.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,7 @@ export default class MultiFilePatch {
2424
this.filePatchesByPath.set(filePatch.getPath(), filePatch);
2525
this.filePatchesByMarker.set(filePatch.getMarker(), filePatch);
2626

27-
let diffRow = 1;
28-
const index = new RBTree((a, b) => a.diffRow - b.diffRow);
29-
this.diffRowOffsetIndices.set(filePatch.getPath(), {startBufferRow: filePatch.getStartRange().start.row, index});
30-
31-
for (let hunkIndex = 0; hunkIndex < filePatch.getHunks().length; hunkIndex++) {
32-
const hunk = filePatch.getHunks()[hunkIndex];
33-
this.hunksByMarker.set(hunk.getMarker(), hunk);
34-
35-
// Advance past the hunk body
36-
diffRow += hunk.bufferRowCount();
37-
index.insert({diffRow, offset: hunkIndex + 1});
38-
39-
// Advance past the next hunk header
40-
diffRow++;
41-
}
27+
this.populateDiffRowOffsetIndices(filePatch);
4228
}
4329
}
4430

@@ -210,6 +196,28 @@ export default class MultiFilePatch {
210196
return Range.fromObject([[selectionRow, 0], [selectionRow, Infinity]]);
211197
}
212198

199+
isDiffRowOffsetIndexEmpty(filePatchPath) {
200+
const diffRowOffsetIndex = this.diffRowOffsetIndices.get(filePatchPath);
201+
return diffRowOffsetIndex.index.size === 0;
202+
}
203+
populateDiffRowOffsetIndices(filePatch) {
204+
let diffRow = 1;
205+
const index = new RBTree((a, b) => a.diffRow - b.diffRow);
206+
this.diffRowOffsetIndices.set(filePatch.getPath(), {startBufferRow: filePatch.getStartRange().start.row, index});
207+
208+
for (let hunkIndex = 0; hunkIndex < filePatch.getHunks().length; hunkIndex++) {
209+
const hunk = filePatch.getHunks()[hunkIndex];
210+
this.hunksByMarker.set(hunk.getMarker(), hunk);
211+
212+
// Advance past the hunk body
213+
diffRow += hunk.bufferRowCount();
214+
index.insert({diffRow, offset: hunkIndex + 1});
215+
216+
// Advance past the next hunk header
217+
diffRow++;
218+
}
219+
}
220+
213221
adoptBuffer(nextPatchBuffer) {
214222
nextPatchBuffer.clearAllLayers();
215223

@@ -332,6 +340,12 @@ export default class MultiFilePatch {
332340
for (const hunk of filePatch.getHunks()) {
333341
this.hunksByMarker.set(hunk.getMarker(), hunk);
334342
}
343+
344+
// if the patch was initially collapsed, we need to calculate
345+
// the diffRowOffsetIndices to calculate comment position.
346+
if (this.isDiffRowOffsetIndexEmpty(filePatch.getPath())) {
347+
this.populateDiffRowOffsetIndices(filePatch);
348+
}
335349
}
336350

337351
getMarkersBefore(filePatchIndex) {

test/models/patch/multi-file-patch.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,41 @@ describe('MultiFilePatch', function() {
780780
assert.strictEqual(multiFilePatch.getBufferRowForDiffPosition('2.txt', 13), 30);
781781
assert.strictEqual(multiFilePatch.getBufferRowForDiffPosition('2.txt', 15), 32);
782782
});
783+
784+
it('set the offset for diff-gated file patch upon expanding', function() {
785+
const {multiFilePatch} = multiFilePatchBuilder()
786+
.addFilePatch(fp => {
787+
fp.setOldFile(f => f.path('1.txt'));
788+
fp.addHunk(h => h.unchanged('0 (1)').added('1 (2)', '2 (3)').deleted('3 (4)').unchanged('4 (5)'));
789+
fp.addHunk(h => h.unchanged('5 (7)').added('6 (8)', '7 (9)', '8 (10)').unchanged('9 (11)'));
790+
fp.addHunk(h => h.unchanged('10 (13)').deleted('11 (14)').unchanged('12 (15)'));
791+
fp.renderStatus(TOO_LARGE);
792+
})
793+
.build();
794+
assert.isTrue(multiFilePatch.isDiffRowOffsetIndexEmpty('1.txt'));
795+
const [fp] = multiFilePatch.getFilePatches();
796+
multiFilePatch.expandFilePatch(fp);
797+
assert.isFalse(multiFilePatch.isDiffRowOffsetIndexEmpty('1.txt'));
798+
assert.strictEqual(multiFilePatch.getBufferRowForDiffPosition('1.txt', 11), 9);
799+
});
800+
801+
it('does not reset the offset for normally collapsed file patch upon expanding', function() {
802+
const {multiFilePatch} = multiFilePatchBuilder()
803+
.addFilePatch(fp => {
804+
fp.setOldFile(f => f.path('0.txt'));
805+
fp.addHunk(h => h.oldRow(1).unchanged('0-0').deleted('0-1', '0-2').unchanged('0-3'));
806+
})
807+
.build();
808+
809+
const [fp] = multiFilePatch.getFilePatches();
810+
const stub = sinon.stub(multiFilePatch, 'populateDiffRowOffsetIndices');
811+
812+
multiFilePatch.collapseFilePatch(fp);
813+
assert.strictEqual(multiFilePatch.getBuffer().getText(), '');
814+
815+
multiFilePatch.expandFilePatch(fp);
816+
assert.isFalse(stub.called);
817+
});
783818
});
784819

785820
describe('collapsing and expanding file patches', function() {

0 commit comments

Comments
 (0)