Skip to content

Commit 9a316b3

Browse files
committed
Update previous patch to change option name and clean up code
Issue #4288
1 parent 1502f80 commit 9a316b3

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

addon/merge/merge.js

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
(this.edit.state.diffViews || (this.edit.state.diffViews = [])).push(this);
4141
this.orig = CodeMirror(pane, copyObj({value: orig, readOnly: !this.mv.options.allowEditingOriginals}, copyObj(options)));
4242
this.orig.state.diffViews = [this];
43-
this.classes.location = options.mergeMarkerLocation || "background";
43+
var classLocation = options.chunkClassLocation || "background";
44+
if (Object.prototype.toString.call(classLocation) != "[object Array]") classLocation = [classLocation]
45+
this.classes.classLocation = classLocation
4446

4547
this.diff = getDiff(asString(orig), asString(options.value));
4648
this.chunks = getChunks(this.diff);
@@ -192,22 +194,22 @@
192194

193195
// Updating the marks for editor content
194196

195-
function clearMarks(editor, arr, classes) {
196-
function removeLineClass(l, klass) {
197-
var classList = Array.isArray(classes.location) ? classes.location : [classes.location]
198-
for (var c = 0; c < classList.length; ++c) {
199-
editor.removeLineClass(l, classList[c], klass)
200-
}
197+
function removeClass(editor, line, classes) {
198+
var locs = classes.classLocation
199+
for (var i = 0; i < locs.length; i++) {
200+
editor.removeLineClass(line, locs[i], classes.chunk);
201+
editor.removeLineClass(line, locs[i], classes.start);
202+
editor.removeLineClass(line, locs[i], classes.chunk);
201203
}
204+
}
205+
206+
function clearMarks(editor, arr, classes) {
202207
for (var i = 0; i < arr.length; ++i) {
203208
var mark = arr[i];
204-
if (mark instanceof CodeMirror.TextMarker) {
209+
if (mark instanceof CodeMirror.TextMarker)
205210
mark.clear();
206-
} else if (mark.parent) {
207-
removeLineClass(mark, classes.chunk);
208-
removeLineClass(mark, classes.start);
209-
removeLineClass(mark, classes.end);
210-
}
211+
else if (mark.parent)
212+
removeClass(editor, mark, classes);
211213
}
212214
arr.length = 0;
213215
}
@@ -233,34 +235,30 @@
233235
});
234236
}
235237

238+
function addClass(editor, lineNr, classes, main, start, end) {
239+
var locs = classes.classLocation, line = editor.getLineHandle(lineNr);
240+
for (var i = 0; i < locs.length; i++) {
241+
if (main) editor.addLineClass(line, locs[i], classes.chunk);
242+
if (start) editor.addLineClass(line, locs[i], classes.start);
243+
if (end) editor.addLineClass(line, locs[i], classes.end);
244+
}
245+
return line;
246+
}
247+
236248
function markChanges(editor, diff, type, marks, from, to, classes) {
237249
var pos = Pos(0, 0);
238250
var top = Pos(from, 0), bot = editor.clipPos(Pos(to - 1));
239251
var cls = type == DIFF_DELETE ? classes.del : classes.insert;
240252
function markChunk(start, end) {
241-
function addLineClass(l, klass) {
242-
var classList = Array.isArray(classes.location) ? classes.location : [classes.location]
243-
var ret = null
244-
for (var c = 0; c < classList.length; ++c) {
245-
ret = editor.addLineClass(l, classList[c], klass)
246-
}
247-
248-
return ret
249-
}
250-
251253
var bfrom = Math.max(from, start), bto = Math.min(to, end);
252-
for (var i = bfrom; i < bto; ++i) {
253-
var line = addLineClass(i, classes.chunk);
254-
if (i == start) addLineClass(line, classes.start);
255-
if (i == end - 1) addLineClass(line, classes.end);
256-
marks.push(line);
257-
}
254+
for (var i = bfrom; i < bto; ++i)
255+
marks.push(addClass(editor, i, classes, true, i == start, i == end - 1));
258256
// When the chunk is empty, make sure a horizontal line shows up
259257
if (start == end && bfrom == end && bto == end) {
260258
if (bfrom)
261-
marks.push(addLineClass(bfrom - 1, classes.end));
259+
marks.push(addClass(editor, bfrom - 1, classes, false, false, true));
262260
else
263-
marks.push(addLineClass(bfrom, classes.start));
261+
marks.push(addClass(editor, bfrom, classes, false, true, false));
264262
}
265263
}
266264

doc/manual.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2986,9 +2986,11 @@ <h2 id="addons">Addons</h2>
29862986
<dt><code><strong>showDifferences</strong>: boolean</code></dt>
29872987
<dd>When true (the default), changed pieces of text are
29882988
highlighted.</dd>
2989-
<dt><code><strong>mergeMarkerLocations</strong>: string|Array</code></dt>
2990-
<dd>By default the merge highlights are added using addLineClass with "background".
2991-
Override this to customize it to be any valid `where` parameter or an Array of valid `where`
2989+
<dt><code><strong>chunkClassLocation</strong>: string|Array</code></dt>
2990+
<dd>By default the chunk highlights are added
2991+
using <a href="#addLineClass"><code>addLineClass</code></a>
2992+
with "background". Override this to customize it to be any
2993+
valid `where` parameter or an Array of valid `where`
29922994
parameters.</dd>
29932995
</dl>
29942996
The addon also defines commands <code>"goNextDiff"</code>

0 commit comments

Comments
 (0)