From c4d0decc27095e0653caf75a6d98ab64555b65e5 Mon Sep 17 00:00:00 2001 From: Katrina Uychaco Date: Fri, 16 Nov 2018 15:00:28 -0800 Subject: [PATCH] Add more comments to explain logic in `getNextSelectionRange` Co-Authored-By: Vanessa Yuen --- lib/models/patch/multi-file-patch.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/models/patch/multi-file-patch.js b/lib/models/patch/multi-file-patch.js index 03a10ed552..c329d66ff4 100644 --- a/lib/models/patch/multi-file-patch.js +++ b/lib/models/patch/multi-file-patch.js @@ -136,7 +136,7 @@ export default class MultiFilePatch { let lastSelectionIndex = 0; // counts unselected lines in changed regions from the old patch - // until we get to the bottom-most selected line from the old patch. + // until we get to the bottom-most selected line from the old patch (lastMax). patchLoop: for (const lastFilePatch of lastMultiFilePatch.getFilePatches()) { for (const hunk of lastFilePatch.getHunks()) { let includesMax = false; @@ -160,13 +160,18 @@ export default class MultiFilePatch { } } + // Iterate over changed lines in new patch in order to find the + // new row to be selected based on the last selection index. + // As we walk through the changed lines, we whittle down the + // remaining lines until we reach the row that corresponds to the + // last selected index + let newSelectionRow = 0; let remainingChangedLines = lastSelectionIndex; let foundRow = false; let lastChangedRow; - // counts the same number of lines but backwards in the new patch. patchLoop: for (const filePatch of this.getFilePatches()) { for (const hunk of filePatch.getHunks()) { for (const change of hunk.getChanges()) { @@ -182,7 +187,10 @@ export default class MultiFilePatch { } } - if (!foundRow) { // the selected line was the last changed line + // If we never got to the last selected index, that means it is + // no longer present in the new patch (ie. we staged the last line of the file). + // In this case we want the next selected line to be the last changed row in the file + if (!foundRow) { newSelectionRow = lastChangedRow; }