I am using the diff_main
method of Google's DiffMatchPatch library to get diffs which I then use in my app. Consider this case:
Old string:
Tracker.Dependency.prototype.changed = function () {
for (var id in this._dependentsById)
this._dependentsById[id]._compute();
};
New string:
Tracker.Dependency.prototype.changed = function () {
for (var id in this._dependentsById)
this._dependentsById[id]._compute();
};
Tracker.autorun = function (f) {
constructingComputation = true;
var c = new Tracker.Computation(f);
return c;
};
The addition diff I get is:
;
};
Tracker.autorun = function (f) {
constructingComputation = true;
var c = new Tracker.Computation(f);
return c
Whereas it would seem that for human consumption a more reasonable diff would be:
Tracker.autorun = function (f) {
constructingComputation = true;
var c = new Tracker.Computation(f);
return c;
};
Is there any way I can make DiffMatchPatch produce the second result rather than the first?
You can see an example here: https://jsfiddle.net/puje78vL/1/
I have created a JSFiddle based on the library's author example page (assuming that you want the Javascript version based on the question tag).
And using this code would give me what you expect:
You can also look at the console and see the raw answer (the arrays) where you can also see that
diff_main
is returning what you are expecting. Are you doing something different? If so please share your code.New Info
Now that you provided the full text I can give you a better answer: the result you are seeing is ok, it is just the way the algorithm works
I'll try to explain to you what is going on and how to fix this. Let's take a look at the final part of each text:
Text 1
Text 2
Lets notice this:
changed
function on Text 1 has no carriage return after it.changed
function on Text 2 has carriage return after it.autorun
function on Text 2 has no carriage return after it.So the algorithm that calculates the diffs will match
1
with3
, leaving2
as the added text. This is why tou are getting that output.Now in order to get the desired output you will need to match
1
with2
. This means add a new empty line at the end of Text 1 as you can see on tour updated JSFIddle:Take notice that if you use just this text the algorithm will work correctly (as I showd in my orginal answer). It is after you add some more text when this confussion starts to happen, not sure why.