Esanders has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/343286 )

Change subject: Use diff_match_patch action constants for better readability
......................................................................

Use diff_match_patch action constants for better readability

Change-Id: I7a073426f746449f5500d9a6a082e3de58e3538f
---
M src/dm/ve.dm.VisualDiff.js
M src/ui/elements/ve.ui.DiffElement.js
M src/ve.DiffMatchPatch.js
3 files changed, 42 insertions(+), 23 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor 
refs/changes/86/343286/1

diff --git a/src/dm/ve.dm.VisualDiff.js b/src/dm/ve.dm.VisualDiff.js
index 5b076ff..353fa52 100644
--- a/src/dm/ve.dm.VisualDiff.js
+++ b/src/dm/ve.dm.VisualDiff.js
@@ -241,7 +241,9 @@
                insertLength,
                diffLength = 0,
                keepLength = 0,
-               diffInfo = [];
+               diffInfo = [],
+               DIFF_DELETE = ve.DiffMatchPatch.static.DIFF_DELETE,
+               DIFF_INSERT = ve.DiffMatchPatch.static.DIFF_INSERT;
 
        oldDocChildTree = new this.treeDiffer.Tree( oldDocChild, 
ve.DiffTreeNode );
        newDocChildTree = new this.treeDiffer.Tree( newDocChild, 
ve.DiffTreeNode );
@@ -318,9 +320,9 @@
                                if ( linearDiff ) {
                                        // Record how much content was removed 
and inserted
                                        for ( j = 0, jlen = linearDiff.length; 
j < jlen; j++ ) {
-                                               if ( linearDiff[ j ][ 0 ] === 1 
) {
+                                               if ( linearDiff[ j ][ 0 ] === 
DIFF_INSERT ) {
                                                        insertLength += 
linearDiff[ j ][ 1 ].length;
-                                               } else if ( linearDiff[ j ][ 0 
] === -1 ) {
+                                               } else if ( linearDiff[ j ][ 0 
] === DIFF_DELETE ) {
                                                        removeLength += 
linearDiff[ j ][ 1 ].length;
                                                }
                                        }
diff --git a/src/ui/elements/ve.ui.DiffElement.js 
b/src/ui/elements/ve.ui.DiffElement.js
index 4e47c81..ed363cf 100644
--- a/src/ui/elements/ve.ui.DiffElement.js
+++ b/src/ui/elements/ve.ui.DiffElement.js
@@ -699,6 +699,10 @@
  */
 ve.ui.DiffElement.prototype.annotateNode = function ( linearDiff ) {
        var i, ilen, range, type, typeAsString, annType, domElementType, 
changes, item,
+               DIFF_DELETE = ve.DiffMatchPatch.static.DIFF_DELETE,
+               DIFF_INSERT = ve.DiffMatchPatch.static.DIFF_INSERT,
+               DIFF_CHANGE_DELETE = 
ve.DiffMatchPatch.static.DIFF_CHANGE_DELETE,
+               DIFF_CHANGE_INSERT = 
ve.DiffMatchPatch.static.DIFF_CHANGE_INSERT,
                items = [],
                start = 0, // The starting index for a range for building an 
annotation
                end, transaction, annotatedLinearDiff,
@@ -722,22 +726,22 @@
                        type = linearDiff[ i ][ 0 ];
                        if ( type !== 0 ) {
                                switch ( type ) {
-                                       case -1:
+                                       case DIFF_DELETE:
                                                typeAsString = 'remove';
                                                domElementType = 'del';
                                                annType = 'textStyle/delete';
                                                break;
-                                       case 1:
+                                       case DIFF_INSERT:
                                                typeAsString = 'insert';
                                                domElementType = 'ins';
                                                annType = 'textStyle/insert';
                                                break;
-                                       case -2:
+                                       case DIFF_CHANGE_DELETE:
                                                typeAsString = 'change-remove';
                                                domElementType = 'span';
                                                annType = 'textStyle/span';
                                                break;
-                                       case 2:
+                                       case DIFF_CHANGE_INSERT:
                                                typeAsString = 'change-insert';
                                                domElementType = 'span';
                                                annType = 'textStyle/span';
diff --git a/src/ve.DiffMatchPatch.js b/src/ve.DiffMatchPatch.js
index 68e4e63..4466414 100644
--- a/src/ve.DiffMatchPatch.js
+++ b/src/ve.DiffMatchPatch.js
@@ -27,6 +27,14 @@
 
 OO.inheritClass( ve.DiffMatchPatch, diff_match_patch );
 
+/* Static properties */
+
+ve.DiffMatchPatch.static.DIFF_DELETE = -1;
+ve.DiffMatchPatch.static.DIFF_INSERT = 1;
+ve.DiffMatchPatch.static.DIFF_EQUAL = 0;
+ve.DiffMatchPatch.static.DIFF_CHANGE_DELETE = -2;
+ve.DiffMatchPatch.static.DIFF_CHANGE_INSERT = 2;
+
 /* Methods */
 
 ve.DiffMatchPatch.prototype.isEqualChar = function ( a, b ) {
@@ -64,7 +72,12 @@
 
 ve.DiffMatchPatch.prototype.getCleanDiff = function () {
        var diffs = this.diff_main.apply( this, arguments ),
-               store = this.store;
+               store = this.store,
+               DIFF_DELETE = this.constructor.static.DIFF_DELETE,
+               DIFF_INSERT = this.constructor.static.DIFF_INSERT,
+               DIFF_EQUAL = this.constructor.static.DIFF_EQUAL,
+               DIFF_CHANGE_DELETE = this.constructor.static.DIFF_CHANGE_DELETE,
+               DIFF_CHANGE_INSERT = this.constructor.static.DIFF_CHANGE_INSERT;
 
        /**
         * Get the index of the first or last wordbreak in a data array
@@ -147,7 +160,7 @@
                        data = diff[ i ][ 1 ];
                        // Should improve on JSON.stringify
                        if ( action + previousAction === 0 && JSON.stringify( 
data ) === JSON.stringify( previousData ) ) {
-                               diff.splice( i - 1, 2, [ 0, data ] );
+                               diff.splice( i - 1, 2, [ DIFF_EQUAL, data ] );
                                i++;
                        }
                        previousAction = action;
@@ -176,7 +189,7 @@
                for ( i = 0; i < diff.length; i++ ) {
                        action = diff[ i ][ 0 ];
                        data = diff[ i ][ 1 ];
-                       if ( action === 0 ) {
+                       if ( action === DIFF_EQUAL ) {
 
                                start = [];
                                end = [];
@@ -186,7 +199,7 @@
                                if ( firstWordbreak === -1 ) {
                                        // If there was no wordbreak, retain 
should be replaced with
                                        // remove-insert
-                                       diff.splice( i, 1, [ -1, data ], [ 1, 
data ] );
+                                       diff.splice( i, 1, [ DIFF_DELETE, data 
], [ DIFF_INSERT, data ] );
                                        i++;
                                } else {
                                        if ( i !== diff.length - 1 && !isBreak( 
data.concat( diff[ i + 1 ][ 1 ] ), data.length ) ) {
@@ -204,11 +217,11 @@
                                        // data (if anything; if firstWordbreak 
=== lastWordbreak !== -1, then
                                        // data has been spliced away 
completely).
                                        if ( start.length > 0 ) {
-                                               diff.splice( i, 0, [ -1, start 
], [ 1, start ] );
+                                               diff.splice( i, 0, [ 
DIFF_DELETE, start ], [ DIFF_INSERT, start ] );
                                                i += 2;
                                        }
                                        if ( end.length > 0 ) {
-                                               diff.splice( i + 1, 0, [ -1, 
end ], [ 1, end ] );
+                                               diff.splice( i + 1, 0, [ 
DIFF_DELETE, end ], [ DIFF_INSERT, end ] );
                                                i += 2;
                                        }
 
@@ -223,17 +236,17 @@
                for ( i = 0, ilen = diff.length; i < ilen; i++ ) {
                        action = diff[ i ][ 0 ];
                        data = diff[ i ][ 1 ];
-                       if ( action === -1 ) {
+                       if ( action === DIFF_DELETE ) {
                                remove = remove.concat( data );
-                       } else if ( action === 1 ) {
+                       } else if ( action === DIFF_INSERT ) {
                                insert = insert.concat( data );
-                       } else if ( action === 0 && data.length > 0 ) {
+                       } else if ( action === DIFF_EQUAL && data.length > 0 ) {
                                if ( remove.length > 0 ) {
-                                       cleanDiff.push( [ -1, remove ] );
+                                       cleanDiff.push( [ DIFF_DELETE, remove ] 
);
                                }
                                remove = [];
                                if ( insert.length > 0 ) {
-                                       cleanDiff.push( [ 1, insert ] );
+                                       cleanDiff.push( [ DIFF_INSERT, insert ] 
);
                                }
                                insert = [];
                                cleanDiff.push( diff[ i ] );
@@ -241,10 +254,10 @@
                }
 
                if ( remove.length > 0 ) {
-                       cleanDiff.push( [ -1, remove ] );
+                       cleanDiff.push( [ DIFF_DELETE, remove ] );
                }
                if ( insert.length > 0 ) {
-                       cleanDiff.push( [ 1, insert ] );
+                       cleanDiff.push( [ DIFF_INSERT, insert ] );
                }
 
                // Finally, go over any consecutive remove-inserts (also 
insert-removes?)
@@ -262,7 +275,7 @@
                        // (2)
                        if (
                                aData.length === bData.length &&
-                               ( ( aAction === -1 && bAction === 1 ) || ( 
aAction === 1 && bAction === -1 ) ) &&
+                               ( ( aAction === DIFF_DELETE && bAction === 
DIFF_INSERT ) || ( aAction === DIFF_INSERT && bAction === DIFF_DELETE ) ) &&
                                aData.every( compareData )
                        ) {
                                aAnnotations = new ve.dm.ElementLinearData( 
store, aData ).getAnnotationsFromRange( new ve.Range( 0, aData.length ), true );
@@ -279,8 +292,8 @@
 
                                if ( annotationChanges.length ) {
                                        cleanDiff[ i + 1 ].annotationChanges = 
annotationChanges;
-                                       cleanDiff[ i ][ 0 ] = aAction === -1 ? 
-2 : 2;
-                                       cleanDiff[ i + 1 ][ 0 ] = bAction === 
-1 ? -2 : 2;
+                                       cleanDiff[ i ][ 0 ] = aAction === 
DIFF_DELETE ? DIFF_CHANGE_DELETE : DIFF_CHANGE_INSERT;
+                                       cleanDiff[ i + 1 ][ 0 ] = bAction === 
DIFF_DELETE ? DIFF_CHANGE_DELETE : DIFF_CHANGE_INSERT;
                                }
 
                                // No need to check bItem against the following 
item

-- 
To view, visit https://gerrit.wikimedia.org/r/343286
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7a073426f746449f5500d9a6a082e3de58e3538f
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to