diff --git a/addon/merge/merge.js b/addon/merge/merge.js index f1f3aafc494f25e1d7ed125a8044557640df3d52..5b04b03292e0c746c3f535bb51ed7138b40b706c 100644 --- a/addon/merge/merge.js +++ b/addon/merge/merge.js @@ -37,7 +37,9 @@ constructor: DiffView, init: function(pane, orig, options) { this.edit = this.mv.edit; + (this.edit.state.diffViews || (this.edit.state.diffViews = [])).push(this); this.orig = CodeMirror(pane, copyObj({value: orig, readOnly: !this.mv.options.allowEditingOriginals}, copyObj(options))); + this.orig.state.diffViews = [this]; this.diff = getDiff(asString(orig), asString(options.value)); this.chunks = getChunks(this.diff); @@ -732,4 +734,42 @@ function posMin(a, b) { return (a.line - b.line || a.ch - b.ch) < 0 ? a : b; } function posMax(a, b) { return (a.line - b.line || a.ch - b.ch) > 0 ? a : b; } function posEq(a, b) { return a.line == b.line && a.ch == b.ch; } + + function findPrevDiff(chunks, start, isOrig) { + for (var i = chunks.length - 1; i >= 0; i--) { + var chunk = chunks[i]; + var to = (isOrig ? chunk.origTo : chunk.editTo) - 1; + if (to < start) return to; + } + } + + function findNextDiff(chunks, start, isOrig) { + for (var i = 0; i < chunks.length; i++) { + var chunk = chunks[i]; + var from = (isOrig ? chunk.origFrom : chunk.editFrom); + if (from > start) return from; + } + } + + function goNearbyDiff(cm, dir) { + var found = null, views = cm.state.diffViews, line = cm.getCursor().line; + if (views) for (var i = 0; i < views.length; i++) { + var dv = views[i], isOrig = cm == dv.orig; + ensureDiff(dv); + var pos = dir < 0 ? findPrevDiff(dv.chunks, line, isOrig) : findNextDiff(dv.chunks, line, isOrig); + if (pos != null && (found == null || (dir < 0 ? pos > found : pos < found))) + found = pos; + } + if (found != null) + cm.setCursor(found, 0); + else + return CodeMirror.Pass; + } + + CodeMirror.commands.goNextDiff = function(cm) { + return goNearbyDiff(cm, 1); + }; + CodeMirror.commands.goPrevDiff = function(cm) { + return goNearbyDiff(cm, -1); + }; }); diff --git a/doc/manual.html b/doc/manual.html index 923ca9b984a0140a15aa48829aed1a8ed7e6587b..d6f89569ba025c0912c38fdeeb24d1b5229e1e77 100644 --- a/doc/manual.html +++ b/doc/manual.html @@ -2836,7 +2836,10 @@ editor.setOption("extraKeys", { <dd>When true (the default), changed pieces of text are highlighted.</dd> </dl> - <a href="../demo/merge.html">Demo here</a>.</dd> + The addon also defines commands <code>"goNextDiff"</code> + and <code>"goPrevDiff"</code> to quickly jump to the next + changed chunk. <a href="../demo/merge.html">Demo + here</a>.</dd> <dt id="addon_tern"><a href="../addon/tern/tern.js"><code>tern/tern.js</code></a></dt> <dd>Provides integration with