Skip to content

Commit e78dc12

Browse files
committed
Closes #136 by preventing infinite scroll loop when sync scroll active
1 parent d3f93b0 commit e78dc12

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 1.5.2 - 2017-12-29
2+
* Fixed infinite scroll loop when performing a next/prev diff command #136
3+
14
## 1.5.1 - 2017-08-29
25
* Fixed selected line losing its background color with some themes - thanks skylerlee!
36

lib/diff-view.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ module.exports = class DiffView {
9393

9494
/**
9595
* Called to move the current selection highlight to the next diff chunk.
96+
* @param isSyncScrollEnabled Only autoscroll one editor if sync scroll is enabled or we will get in an infinite loop
9697
*/
97-
nextDiff() {
98+
nextDiff(isSyncScrollEnabled) {
9899
if(this._isSelectionActive) {
99100
this._selectedChunkIndex++;
100101
if(this._selectedChunkIndex >= this.getNumDifferences()) {
@@ -104,7 +105,7 @@ module.exports = class DiffView {
104105
this._isSelectionActive = true;
105106
}
106107

107-
var success = this._selectChunk(this._selectedChunkIndex, true);
108+
var success = this._selectChunk(this._selectedChunkIndex, true, isSyncScrollEnabled);
108109
if(!success) {
109110
return -1;
110111
}
@@ -114,8 +115,9 @@ module.exports = class DiffView {
114115

115116
/**
116117
* Called to move the current selection highlight to the previous diff chunk.
118+
* @param isSyncScrollEnabled Only autoscroll one editor if sync scroll is enabled or we will get in an infinite loop
117119
*/
118-
prevDiff() {
120+
prevDiff(isSyncScrollEnabled) {
119121
if(this._isSelectionActive) {
120122
this._selectedChunkIndex--;
121123
if(this._selectedChunkIndex < 0) {
@@ -125,7 +127,7 @@ module.exports = class DiffView {
125127
this._isSelectionActive = true;
126128
}
127129

128-
var success = this._selectChunk(this._selectedChunkIndex, true);
130+
var success = this._selectChunk(this._selectedChunkIndex, true, isSyncScrollEnabled);
129131
if(!success) {
130132
return -1;
131133
}
@@ -218,6 +220,10 @@ module.exports = class DiffView {
218220
}
219221
}
220222

223+
/**
224+
* Restores soft wrap to the appropriate editor.
225+
* @param editorIndex The index of the editor to restore soft wrap to.
226+
*/
221227
restoreEditorSoftWrap(editorIndex) {
222228
if(editorIndex === 1) {
223229
this._editorDiffExtender1.getEditor().setSoftWrapped(true);
@@ -243,10 +249,20 @@ module.exports = class DiffView {
243249
return Array.isArray(this._chunks) ? this._chunks.length : 0;
244250
}
245251

252+
/**
253+
* Gets the marker layers in use by the editors.
254+
* @return An object containing the marker layers and approriate information.
255+
*/
246256
getMarkerLayers() {
247257
return this._markerLayers;
248258
}
249259

260+
/**
261+
* Handles when the cursor moves in the editor. Will highlight chunks that have a cursor in them.
262+
* @param cursor The cursor object from the event.
263+
* @param oldBufferPosition The old position of the cursor in the buffer.
264+
* @param newBufferPosition The new position of the cursor in the buffer.
265+
*/
250266
handleCursorChange(cursor, oldBufferPosition, newBufferPosition) {
251267
var editorIndex = (cursor.editor === this._editorDiffExtender1.getEditor()) ? 1 : 2;
252268
var oldPositionChunkIndex = this._getChunkIndexByLineNumber(editorIndex, oldBufferPosition.row);
@@ -272,8 +288,10 @@ module.exports = class DiffView {
272288
* given index.
273289
*
274290
* @param index The index of the diff chunk to highlight in both editors.
291+
* @param isNextOrPrev Whether we are moving to a direct sibling (if not, this is a click)
292+
* @param isSyncScrollEnabled Only autoscroll one editor if sync scroll is enabled or we will get in an infinite loop
275293
*/
276-
_selectChunk(index, isNextOrPrev) {
294+
_selectChunk(index, isNextOrPrev, isSyncScrollEnabled) {
277295
var diffChunk = this._chunks[index];
278296
if(diffChunk != null) {
279297
diffChunk.isSelected = true;
@@ -284,7 +302,7 @@ module.exports = class DiffView {
284302
this._editorDiffExtender2.deselectAllLines();
285303
// scroll the editors
286304
this._editorDiffExtender1.getEditor().setCursorBufferPosition([diffChunk.oldLineStart, 0], {autoscroll: true});
287-
this._editorDiffExtender2.getEditor().setCursorBufferPosition([diffChunk.newLineStart, 0], {autoscroll: true});
305+
this._editorDiffExtender2.getEditor().setCursorBufferPosition([diffChunk.newLineStart, 0], {autoscroll: !isSyncScrollEnabled});
288306
}
289307

290308
// highlight selection in both editors
@@ -297,6 +315,12 @@ module.exports = class DiffView {
297315
return false;
298316
}
299317

318+
/**
319+
* Gets the index of a chunk by the line number.
320+
* @param editorIndex The index of the editor to check.
321+
* @param lineNumber The line number to use to check if it is in a chunk.
322+
* @return The index of the chunk.
323+
*/
300324
_getChunkIndexByLineNumber(editorIndex, lineNumber) {
301325
for(var i=0; i<this._chunks.length; i++) {
302326
var diffChunk = this._chunks[i];

lib/split-diff.coffee

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,21 @@ module.exports = SplitDiff =
154154
# called by "Move to next diff" command
155155
nextDiff: ->
156156
if @diffView?
157-
selectedIndex = @diffView.nextDiff()
157+
isSyncScrollEnabled = false
158+
scrollSyncType = @options.scrollSyncType ? @_getConfig('scrollSyncType')
159+
if scrollSyncType == 'Vertical + Horizontal' || scrollSyncType == 'Vertical'
160+
isSyncScrollEnabled = true
161+
selectedIndex = @diffView.nextDiff(isSyncScrollEnabled)
158162
@footerView?.showSelectionCount( selectedIndex + 1 )
159163

160164
# called by "Move to previous diff" command
161165
prevDiff: ->
162166
if @diffView?
163-
selectedIndex = @diffView.prevDiff()
167+
isSyncScrollEnabled = false
168+
scrollSyncType = @options.scrollSyncType ? @_getConfig('scrollSyncType')
169+
if scrollSyncType == 'Vertical + Horizontal' || scrollSyncType == 'Vertical'
170+
isSyncScrollEnabled = true
171+
selectedIndex = @diffView.prevDiff(isSyncScrollEnabled)
164172
@footerView?.showSelectionCount( selectedIndex + 1 )
165173

166174
# called by "Copy to right" command

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"repository": "https://github.com/mupchrch/split-diff",
2626
"license": "MIT",
2727
"engines": {
28-
"atom": ">= 1.17 < 2.0.0"
28+
"atom": ">= 1.19 < 2.0.0"
2929
},
3030
"dependencies": {
3131
"diff": "latest"

0 commit comments

Comments
 (0)