Skip to content
Advertisement

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement