Skip to content

Commit 2400b15

Browse files
committed
Moves word diff logic to diff-view. Removes extra info passed from compute-diff. Fixes footer ignore whitespace bug.
1 parent 8c1dd39 commit 2400b15

File tree

5 files changed

+181
-185
lines changed

5 files changed

+181
-185
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.0.7 - XXXX-XX-XX
2+
* Fixed issue where toggling ignore whitespace from command wouldn't update footer bar
3+
* Huge code refactor!
4+
15
## 1.0.6 - 2016-10-24
26
* Fixed next/prev diff highlight skipping a selection after copying to right/left
37
* Added keybinding for copy to left/right and scoped it to just the editors #73

lib/compute-diff.js

+3-19
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ var oldText = fs.readFileSync( oldTextPath );
88
var newText = fs.readFileSync( newTextPath );
99

1010
var diffChunks = _computeDiffChunks( oldText, newText, isWhitespaceIgnored );
11-
var offsets = _computeOffsets( diffChunks.chunks );
12-
var orderedChunks = _orderDiffChunks( diffChunks.chunks );
11+
var offsets = _computeOffsets( diffChunks );
12+
var orderedChunks = _orderDiffChunks( diffChunks );
1313

1414
console.log( JSON.stringify({
15-
addedLines: diffChunks.addedLines,
16-
removedLines: diffChunks.removedLines,
1715
oldLineOffsets: offsets.oldLineOffsets,
1816
newLineOffsets: offsets.newLineOffsets,
1917
chunks: orderedChunks
@@ -38,30 +36,20 @@ function _computeDiffChunks(oldText, newText, isWhitespaceIgnored) {
3836
}
3937

4038
var chunks = [];
41-
var addedCount = 0;
42-
var removedCount = 0;
4339
var nextOffset = 0;
4440
var offset = 0;
4541

46-
var addedLines = [];
47-
var removedLines = [];
4842
lineDiff.forEach( function(part) {
4943
var added = part.added,
5044
removed = part.removed,
5145
value = part.value;
5246
var count = part.count;//value.split('\n').length - 1;
5347
if (!added && !removed) {
54-
addedCount += count;
55-
removedCount += count;
5648
offset = nextOffset;
5749
nextOffset = 0;
5850
} else if (added) {
59-
addedLines.push([addedCount, addedCount+count])
60-
addedCount += count;
6151
nextOffset += count;
6252
} else {
63-
removedLines.push([removedCount, removedCount+count])
64-
removedCount += count;
6553
nextOffset -= count;
6654
}
6755
chunks.push({
@@ -74,11 +62,7 @@ function _computeDiffChunks(oldText, newText, isWhitespaceIgnored) {
7462
offset = 0;
7563
});
7664

77-
return {
78-
addedLines: addedLines,
79-
removedLines: removedLines,
80-
chunks: chunks,
81-
};
65+
return chunks;
8266
}
8367

8468
function _computeOffsets(diffChunks) {

lib/diff-view.js

+134-28
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use babel'
22

33
import EditorDiffExtender from './editor-diff-extender';
4+
import ComputeWordDiff from './compute-word-diff';
5+
46

57
module.exports = class DiffView {
68
/*
79
* @param editors Array of editors being diffed.
810
* @param diff ex: {
9-
* addedLines: [startIndex, endIndex],
10-
* removedLines: [startIndex, endIndex],
1111
* oldLineOffsets: [lineNumber: numOffsetLines, ...],
1212
* newLineOffsets: [lineNumber: numOffsetLines, ...],
1313
* chunks: [{
@@ -33,19 +33,47 @@ module.exports = class DiffView {
3333
* @param chunks The diff chunks to highlight.
3434
* @param leftHighlightType The type of highlight (ex: 'added').
3535
* @param rightHighlightType The type of highlight (ex: 'removed').
36+
* @param isWordDiffEnabled Whether differences between words per line should be highlighted.
37+
* @param isWhitespaceIgnored Whether whitespace should be ignored.
3638
*/
37-
displayDiff( diff, leftHighlightType, rightHighlightType ) {
39+
displayDiff( diff, leftHighlightType, rightHighlightType, isWordDiffEnabled, isWhitespaceIgnored ) {
3840
this._chunks = diff.chunks;
3941

42+
// make the last chunk equal size on both screens so the editors retain sync scroll #58
43+
if( this._chunks.length > 0 ) {
44+
var lastChunk = this._chunks[ this._chunks.length - 1 ];
45+
var oldChunkRange = lastChunk.oldLineEnd - lastChunk.oldLineStart;
46+
var newChunkRange = lastChunk.newLineEnd - lastChunk.newLineStart;
47+
if( oldChunkRange > newChunkRange ) {
48+
// make the offset as large as needed to make the chunk the same size in both editors
49+
diff.newLineOffsets[ lastChunk.newLineStart + newChunkRange ] = oldChunkRange - newChunkRange;
50+
} else if( newChunkRange > oldChunkRange ) {
51+
// make the offset as large as needed to make the chunk the same size in both editors
52+
diff.oldLineOffsets[ lastChunk.oldLineStart + oldChunkRange ] = newChunkRange - oldChunkRange;
53+
}
54+
}
55+
4056
for( chunk of this._chunks ) {
4157
this._editorDiffExtender1.highlightLines( chunk.oldLineStart, chunk.oldLineEnd, leftHighlightType );
4258
this._editorDiffExtender2.highlightLines( chunk.newLineStart, chunk.newLineEnd, rightHighlightType );
59+
60+
if( isWordDiffEnabled ) {
61+
this._highlightWordsInChunk( chunk, leftHighlightType, rightHighlightType, isWhitespaceIgnored );
62+
}
4363
}
4464

4565
this._editorDiffExtender1.setLineOffsets( diff.oldLineOffsets );
4666
this._editorDiffExtender2.setLineOffsets( diff.newLineOffsets );
4767
}
4868

69+
/**
70+
* Clears the diff highlighting and offsets from the editors.
71+
*/
72+
clearDiff() {
73+
this._editorDiffExtender1.destroyMarkers();
74+
this._editorDiffExtender2.destroyMarkers();
75+
}
76+
4977
/**
5078
* Called to move the current selection highlight to the next diff chunk.
5179
*/
@@ -80,27 +108,6 @@ module.exports = class DiffView {
80108
return this._selectedChunkIndex;
81109
}
82110

83-
/**
84-
* Selects and highlights the diff chunk in both editors according to the
85-
* given index.
86-
*
87-
* @param index The index of the diff chunk to highlight in both editors.
88-
*/
89-
_selectChunk( index ) {
90-
var diffChunk = this._chunks[index];
91-
if( diffChunk != null ) {
92-
// deselect previous next/prev highlights
93-
this._editorDiffExtender1.deselectAllLines();
94-
this._editorDiffExtender2.deselectAllLines();
95-
// highlight and scroll editor 1
96-
this._editorDiffExtender1.selectLines( diffChunk.oldLineStart, diffChunk.oldLineEnd );
97-
this._editorDiffExtender1.getEditor().setCursorBufferPosition( [diffChunk.oldLineStart, 0], {autoscroll: true} );
98-
// highlight and scroll editor 2
99-
this._editorDiffExtender2.selectLines( diffChunk.newLineStart, diffChunk.newLineEnd );
100-
this._editorDiffExtender2.getEditor().setCursorBufferPosition( [diffChunk.newLineStart, 0], {autoscroll: true} );
101-
}
102-
}
103-
104111
/**
105112
* Copies the currently selected diff chunk from the left editor to the right
106113
* editor.
@@ -175,11 +182,110 @@ module.exports = class DiffView {
175182
}
176183

177184
/**
178-
* Gets the editor diff abstractions.
185+
* Cleans up the editor indicated by index. A clean up will remove the editor
186+
* or the pane if necessary. Typically left editor == 1 and right editor == 2.
179187
*
180-
* @return EditorDiffExtender Array containing the editors being diffed.
188+
* @param editorIndex The index of the editor to clean up.
181189
*/
182-
getEditorDiffExtenders() {
183-
return [this._editorDiffExtender1, this._editorDiffExtender2];
190+
cleanUpEditor( editorIndex ) {
191+
if( editorIndex === 1 ) {
192+
this._editorDiffExtender1.cleanUp();
193+
} else if( editorIndex === 2 ) {
194+
this._editorDiffExtender2.cleanUp();
195+
}
196+
}
197+
198+
/**
199+
* Destroys the editor diff extenders.
200+
*/
201+
destroy() {
202+
this._editorDiffExtender1.destroy();
203+
this._editorDiffExtender2.destroy();
204+
}
205+
206+
/**
207+
* Gets the number of differences between the editors.
208+
*
209+
* @return int The number of differences between the editors.
210+
*/
211+
getNumDifferences() {
212+
return this._chunks.length;
213+
}
214+
215+
// ----------------------------------------------------------------------- //
216+
// --------------------------- PRIVATE METHODS --------------------------- //
217+
// ----------------------------------------------------------------------- //
218+
219+
/**
220+
* Selects and highlights the diff chunk in both editors according to the
221+
* given index.
222+
*
223+
* @param index The index of the diff chunk to highlight in both editors.
224+
*/
225+
_selectChunk( index ) {
226+
var diffChunk = this._chunks[index];
227+
if( diffChunk != null ) {
228+
// deselect previous next/prev highlights
229+
this._editorDiffExtender1.deselectAllLines();
230+
this._editorDiffExtender2.deselectAllLines();
231+
// highlight and scroll editor 1
232+
this._editorDiffExtender1.selectLines( diffChunk.oldLineStart, diffChunk.oldLineEnd );
233+
this._editorDiffExtender1.getEditor().setCursorBufferPosition( [diffChunk.oldLineStart, 0], {autoscroll: true} );
234+
// highlight and scroll editor 2
235+
this._editorDiffExtender2.selectLines( diffChunk.newLineStart, diffChunk.newLineEnd );
236+
this._editorDiffExtender2.getEditor().setCursorBufferPosition( [diffChunk.newLineStart, 0], {autoscroll: true} );
237+
}
238+
}
239+
240+
/**
241+
* Highlights the word diff of the chunk passed in.
242+
*
243+
* @param chunk The chunk that should have its words highlighted.
244+
*/
245+
_highlightWordsInChunk( chunk, leftHighlightType, rightHighlightType, isWhitespaceIgnored ) {
246+
var leftLineNumber = chunk.oldLineStart;
247+
var rightLineNumber = chunk.newLineStart;
248+
// for each line that has a corresponding line
249+
while( leftLineNumber < chunk.oldLineEnd && rightLineNumber < chunk.newLineEnd ) {
250+
// do word diff
251+
var wordDiff = ComputeWordDiff.computeWordDiff(
252+
this._editorDiffExtender1.getEditor().lineTextForBufferRow( leftLineNumber ),
253+
this._editorDiffExtender2.getEditor().lineTextForBufferRow( rightLineNumber )
254+
);
255+
this._editorDiffExtender1.setWordHighlights( leftLineNumber, wordDiff.removedWords, leftHighlightType, isWhitespaceIgnored );
256+
this._editorDiffExtender2.setWordHighlights( rightLineNumber, wordDiff.addedWords, rightHighlightType, isWhitespaceIgnored );
257+
258+
leftLineNumber++;
259+
rightLineNumber++;
260+
}
261+
262+
// highlight remaining lines in left editor
263+
while( leftLineNumber < chunk.oldLineEnd ) {
264+
this._editorDiffExtender1.setWordHighlights(
265+
leftLineNumber,
266+
[{
267+
changed: true,
268+
value: this._editorDiffExtender1.getEditor().lineTextForBufferRow( leftLineNumber )
269+
}],
270+
leftHighlightType,
271+
isWhitespaceIgnored
272+
);
273+
274+
leftLineNumber++;
275+
}
276+
// highlight remaining lines in the right editor
277+
while( rightLineNumber < chunk.newLineEnd ) {
278+
this._editorDiffExtender2.setWordHighlights(
279+
rightLineNumber,
280+
[{
281+
changed: true,
282+
value: this._editorDiffExtender2.getEditor().lineTextForBufferRow( rightLineNumber )
283+
}],
284+
rightHighlightType,
285+
isWhitespaceIgnored
286+
);
287+
288+
rightLineNumber++;
289+
}
184290
}
185291
};

0 commit comments

Comments
 (0)