Yogesh Unavane
Yogesh Unavane

Reputation: 265

Make the CodeMirror merge view addon display a count of differences found

While using CodeMirror’s merge addon, I am interested in knowing the count of differences found in the L.H.S. and the R.H.S. textareas, respectively.

Is there a way of displaying the count?

Upvotes: 3

Views: 1503

Answers (2)

Rory O'Kane
Rory O'Kane

Reputation: 30398

You could implement it yourself, by using the diff-match-patch library that the merge addon depends on. Write an updateDiffCount function that uses the following algorithm:

  1. Get the two text versions you are comparing as strings.
  2. Call diff_main on the strings and diff_cleanupSemantic on the result, as in this code example.
  3. Loop through the results and count the result arrays whose first element is not 0.
  4. Set the text contents of some element on your page to that count.

Run this updateDiffCount function whenever the text in the textarea is edited, after a delay.

Upvotes: 2

michal.jakubeczy
michal.jakubeczy

Reputation: 9469

I added some code to Rory's solution to make it easier to implement:

var text1 = document.getElementById('text1').value;
var text2 = document.getElementById('text2').value;

var d = dmp.diff_main(text1, text2);

// you can optionally add some cleanup
// dmp.diff_cleanupSemantic(d); or dmp.diff_cleanupEfficiency(d);

alert('Difference count: ' + d.filter(l => l[0] === -1).length);

Upvotes: 1

Related Questions