提交 | 用户 | 时间
|
e7c126
|
1 |
/* |
H |
2 |
* Copyright 1999-2018 Alibaba Group Holding Ltd. |
|
3 |
* |
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
* you may not use this file except in compliance with the License. |
|
6 |
* You may obtain a copy of the License at |
|
7 |
* |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
* See the License for the specific language governing permissions and |
|
14 |
* limitations under the License. |
|
15 |
*/ |
|
16 |
|
|
17 |
// declare global: diff_match_patch, DIFF_INSERT, DIFF_DELETE, DIFF_EQUAL |
|
18 |
|
|
19 |
(function(mod) { |
|
20 |
if (typeof exports == "object" && typeof module == "object") // CommonJS |
|
21 |
mod(require("../../lib/codemirror")); // Note non-packaged dependency diff_match_patch |
|
22 |
else if (typeof define == "function" && define.amd) // AMD |
|
23 |
define(["../../lib/codemirror", "diff_match_patch"], mod); |
|
24 |
else // Plain browser env |
|
25 |
mod(CodeMirror); |
|
26 |
})(function(CodeMirror) { |
|
27 |
"use strict"; |
|
28 |
var Pos = CodeMirror.Pos; |
|
29 |
var svgNS = "http://www.w3.org/2000/svg"; |
|
30 |
|
|
31 |
function DiffView(mv, type) { |
|
32 |
this.mv = mv; |
|
33 |
this.type = type; |
|
34 |
this.classes = type == "left" |
|
35 |
? {chunk: "CodeMirror-merge-l-chunk", |
|
36 |
start: "CodeMirror-merge-l-chunk-start", |
|
37 |
end: "CodeMirror-merge-l-chunk-end", |
|
38 |
insert: "CodeMirror-merge-l-inserted", |
|
39 |
del: "CodeMirror-merge-l-deleted", |
|
40 |
connect: "CodeMirror-merge-l-connect"} |
|
41 |
: {chunk: "CodeMirror-merge-r-chunk", |
|
42 |
start: "CodeMirror-merge-r-chunk-start", |
|
43 |
end: "CodeMirror-merge-r-chunk-end", |
|
44 |
insert: "CodeMirror-merge-r-inserted", |
|
45 |
del: "CodeMirror-merge-r-deleted", |
|
46 |
connect: "CodeMirror-merge-r-connect"}; |
|
47 |
} |
|
48 |
|
|
49 |
DiffView.prototype = { |
|
50 |
constructor: DiffView, |
|
51 |
init: function(pane, orig, options) { |
|
52 |
this.edit = this.mv.edit; |
|
53 |
;(this.edit.state.diffViews || (this.edit.state.diffViews = [])).push(this); |
|
54 |
this.orig = CodeMirror(pane, copyObj({value: orig, readOnly: !this.mv.options.allowEditingOriginals}, copyObj(options))); |
|
55 |
if (this.mv.options.connect == "align") { |
|
56 |
if (!this.edit.state.trackAlignable) this.edit.state.trackAlignable = new TrackAlignable(this.edit) |
|
57 |
this.orig.state.trackAlignable = new TrackAlignable(this.orig) |
|
58 |
} |
|
59 |
|
|
60 |
this.orig.state.diffViews = [this]; |
|
61 |
var classLocation = options.chunkClassLocation || "background"; |
|
62 |
if (Object.prototype.toString.call(classLocation) != "[object Array]") classLocation = [classLocation] |
|
63 |
this.classes.classLocation = classLocation |
|
64 |
|
|
65 |
this.diff = getDiff(asString(orig), asString(options.value), this.mv.options.ignoreWhitespace); |
|
66 |
this.chunks = getChunks(this.diff); |
|
67 |
this.diffOutOfDate = this.dealigned = false; |
|
68 |
this.needsScrollSync = null |
|
69 |
|
|
70 |
this.showDifferences = options.showDifferences !== false; |
|
71 |
}, |
|
72 |
registerEvents: function(otherDv) { |
|
73 |
this.forceUpdate = registerUpdate(this); |
|
74 |
setScrollLock(this, true, false); |
|
75 |
registerScroll(this, otherDv); |
|
76 |
}, |
|
77 |
setShowDifferences: function(val) { |
|
78 |
val = val !== false; |
|
79 |
if (val != this.showDifferences) { |
|
80 |
this.showDifferences = val; |
|
81 |
this.forceUpdate("full"); |
|
82 |
} |
|
83 |
} |
|
84 |
}; |
|
85 |
|
|
86 |
function ensureDiff(dv) { |
|
87 |
if (dv.diffOutOfDate) { |
|
88 |
dv.diff = getDiff(dv.orig.getValue(), dv.edit.getValue(), dv.mv.options.ignoreWhitespace); |
|
89 |
dv.chunks = getChunks(dv.diff); |
|
90 |
dv.diffOutOfDate = false; |
|
91 |
CodeMirror.signal(dv.edit, "updateDiff", dv.diff); |
|
92 |
} |
|
93 |
} |
|
94 |
|
|
95 |
var updating = false; |
|
96 |
function registerUpdate(dv) { |
|
97 |
var edit = {from: 0, to: 0, marked: []}; |
|
98 |
var orig = {from: 0, to: 0, marked: []}; |
|
99 |
var debounceChange, updatingFast = false; |
|
100 |
function update(mode) { |
|
101 |
updating = true; |
|
102 |
updatingFast = false; |
|
103 |
if (mode == "full") { |
|
104 |
if (dv.svg) clear(dv.svg); |
|
105 |
if (dv.copyButtons) clear(dv.copyButtons); |
|
106 |
clearMarks(dv.edit, edit.marked, dv.classes); |
|
107 |
clearMarks(dv.orig, orig.marked, dv.classes); |
|
108 |
edit.from = edit.to = orig.from = orig.to = 0; |
|
109 |
} |
|
110 |
ensureDiff(dv); |
|
111 |
if (dv.showDifferences) { |
|
112 |
updateMarks(dv.edit, dv.diff, edit, DIFF_INSERT, dv.classes); |
|
113 |
updateMarks(dv.orig, dv.diff, orig, DIFF_DELETE, dv.classes); |
|
114 |
} |
|
115 |
|
|
116 |
if (dv.mv.options.connect == "align") |
|
117 |
alignChunks(dv); |
|
118 |
makeConnections(dv); |
|
119 |
if (dv.needsScrollSync != null) syncScroll(dv, dv.needsScrollSync) |
|
120 |
|
|
121 |
updating = false; |
|
122 |
} |
|
123 |
function setDealign(fast) { |
|
124 |
if (updating) return; |
|
125 |
dv.dealigned = true; |
|
126 |
set(fast); |
|
127 |
} |
|
128 |
function set(fast) { |
|
129 |
if (updating || updatingFast) return; |
|
130 |
clearTimeout(debounceChange); |
|
131 |
if (fast === true) updatingFast = true; |
|
132 |
debounceChange = setTimeout(update, fast === true ? 20 : 250); |
|
133 |
} |
|
134 |
function change(_cm, change) { |
|
135 |
if (!dv.diffOutOfDate) { |
|
136 |
dv.diffOutOfDate = true; |
|
137 |
edit.from = edit.to = orig.from = orig.to = 0; |
|
138 |
} |
|
139 |
// Update faster when a line was added/removed |
|
140 |
setDealign(change.text.length - 1 != change.to.line - change.from.line); |
|
141 |
} |
|
142 |
function swapDoc() { |
|
143 |
dv.diffOutOfDate = true; |
|
144 |
dv.dealigned = true; |
|
145 |
update("full"); |
|
146 |
} |
|
147 |
dv.edit.on("change", change); |
|
148 |
dv.orig.on("change", change); |
|
149 |
dv.edit.on("swapDoc", swapDoc); |
|
150 |
dv.orig.on("swapDoc", swapDoc); |
|
151 |
if (dv.mv.options.connect == "align") { |
|
152 |
CodeMirror.on(dv.edit.state.trackAlignable, "realign", setDealign) |
|
153 |
CodeMirror.on(dv.orig.state.trackAlignable, "realign", setDealign) |
|
154 |
} |
|
155 |
dv.edit.on("viewportChange", function() { set(false); }); |
|
156 |
dv.orig.on("viewportChange", function() { set(false); }); |
|
157 |
update(); |
|
158 |
return update; |
|
159 |
} |
|
160 |
|
|
161 |
function registerScroll(dv, otherDv) { |
|
162 |
dv.edit.on("scroll", function() { |
|
163 |
syncScroll(dv, true) && makeConnections(dv); |
|
164 |
}); |
|
165 |
dv.orig.on("scroll", function() { |
|
166 |
syncScroll(dv, false) && makeConnections(dv); |
|
167 |
if (otherDv) syncScroll(otherDv, true) && makeConnections(otherDv); |
|
168 |
}); |
|
169 |
} |
|
170 |
|
|
171 |
function syncScroll(dv, toOrig) { |
|
172 |
// Change handler will do a refresh after a timeout when diff is out of date |
|
173 |
if (dv.diffOutOfDate) { |
|
174 |
if (dv.lockScroll && dv.needsScrollSync == null) dv.needsScrollSync = toOrig |
|
175 |
return false |
|
176 |
} |
|
177 |
dv.needsScrollSync = null |
|
178 |
if (!dv.lockScroll) return true; |
|
179 |
var editor, other, now = +new Date; |
|
180 |
if (toOrig) { editor = dv.edit; other = dv.orig; } |
|
181 |
else { editor = dv.orig; other = dv.edit; } |
|
182 |
// Don't take action if the position of this editor was recently set |
|
183 |
// (to prevent feedback loops) |
|
184 |
if (editor.state.scrollSetBy == dv && (editor.state.scrollSetAt || 0) + 250 > now) return false; |
|
185 |
|
|
186 |
var sInfo = editor.getScrollInfo(); |
|
187 |
if (dv.mv.options.connect == "align") { |
|
188 |
targetPos = sInfo.top; |
|
189 |
} else { |
|
190 |
var halfScreen = .5 * sInfo.clientHeight, midY = sInfo.top + halfScreen; |
|
191 |
var mid = editor.lineAtHeight(midY, "local"); |
|
192 |
var around = chunkBoundariesAround(dv.chunks, mid, toOrig); |
|
193 |
var off = getOffsets(editor, toOrig ? around.edit : around.orig); |
|
194 |
var offOther = getOffsets(other, toOrig ? around.orig : around.edit); |
|
195 |
var ratio = (midY - off.top) / (off.bot - off.top); |
|
196 |
var targetPos = (offOther.top - halfScreen) + ratio * (offOther.bot - offOther.top); |
|
197 |
|
|
198 |
var botDist, mix; |
|
199 |
// Some careful tweaking to make sure no space is left out of view |
|
200 |
// when scrolling to top or bottom. |
|
201 |
if (targetPos > sInfo.top && (mix = sInfo.top / halfScreen) < 1) { |
|
202 |
targetPos = targetPos * mix + sInfo.top * (1 - mix); |
|
203 |
} else if ((botDist = sInfo.height - sInfo.clientHeight - sInfo.top) < halfScreen) { |
|
204 |
var otherInfo = other.getScrollInfo(); |
|
205 |
var botDistOther = otherInfo.height - otherInfo.clientHeight - targetPos; |
|
206 |
if (botDistOther > botDist && (mix = botDist / halfScreen) < 1) |
|
207 |
targetPos = targetPos * mix + (otherInfo.height - otherInfo.clientHeight - botDist) * (1 - mix); |
|
208 |
} |
|
209 |
} |
|
210 |
|
|
211 |
other.scrollTo(sInfo.left, targetPos); |
|
212 |
other.state.scrollSetAt = now; |
|
213 |
other.state.scrollSetBy = dv; |
|
214 |
return true; |
|
215 |
} |
|
216 |
|
|
217 |
function getOffsets(editor, around) { |
|
218 |
var bot = around.after; |
|
219 |
if (bot == null) bot = editor.lastLine() + 1; |
|
220 |
return {top: editor.heightAtLine(around.before || 0, "local"), |
|
221 |
bot: editor.heightAtLine(bot, "local")}; |
|
222 |
} |
|
223 |
|
|
224 |
function setScrollLock(dv, val, action) { |
|
225 |
dv.lockScroll = val; |
|
226 |
if (val && action != false) syncScroll(dv, DIFF_INSERT) && makeConnections(dv); |
|
227 |
dv.lockButton.innerHTML = val ? "\u21db\u21da" : "\u21db \u21da"; |
|
228 |
} |
|
229 |
|
|
230 |
// Updating the marks for editor content |
|
231 |
|
|
232 |
function removeClass(editor, line, classes) { |
|
233 |
var locs = classes.classLocation |
|
234 |
for (var i = 0; i < locs.length; i++) { |
|
235 |
editor.removeLineClass(line, locs[i], classes.chunk); |
|
236 |
editor.removeLineClass(line, locs[i], classes.start); |
|
237 |
editor.removeLineClass(line, locs[i], classes.end); |
|
238 |
} |
|
239 |
} |
|
240 |
|
|
241 |
function clearMarks(editor, arr, classes) { |
|
242 |
for (var i = 0; i < arr.length; ++i) { |
|
243 |
var mark = arr[i]; |
|
244 |
if (mark instanceof CodeMirror.TextMarker) |
|
245 |
mark.clear(); |
|
246 |
else if (mark.parent) |
|
247 |
removeClass(editor, mark, classes); |
|
248 |
} |
|
249 |
arr.length = 0; |
|
250 |
} |
|
251 |
|
|
252 |
// FIXME maybe add a margin around viewport to prevent too many updates |
|
253 |
function updateMarks(editor, diff, state, type, classes) { |
|
254 |
var vp = editor.getViewport(); |
|
255 |
editor.operation(function() { |
|
256 |
if (state.from == state.to || vp.from - state.to > 20 || state.from - vp.to > 20) { |
|
257 |
clearMarks(editor, state.marked, classes); |
|
258 |
markChanges(editor, diff, type, state.marked, vp.from, vp.to, classes); |
|
259 |
state.from = vp.from; state.to = vp.to; |
|
260 |
} else { |
|
261 |
if (vp.from < state.from) { |
|
262 |
markChanges(editor, diff, type, state.marked, vp.from, state.from, classes); |
|
263 |
state.from = vp.from; |
|
264 |
} |
|
265 |
if (vp.to > state.to) { |
|
266 |
markChanges(editor, diff, type, state.marked, state.to, vp.to, classes); |
|
267 |
state.to = vp.to; |
|
268 |
} |
|
269 |
} |
|
270 |
}); |
|
271 |
} |
|
272 |
|
|
273 |
function addClass(editor, lineNr, classes, main, start, end) { |
|
274 |
var locs = classes.classLocation, line = editor.getLineHandle(lineNr); |
|
275 |
for (var i = 0; i < locs.length; i++) { |
|
276 |
if (main) editor.addLineClass(line, locs[i], classes.chunk); |
|
277 |
if (start) editor.addLineClass(line, locs[i], classes.start); |
|
278 |
if (end) editor.addLineClass(line, locs[i], classes.end); |
|
279 |
} |
|
280 |
return line; |
|
281 |
} |
|
282 |
|
|
283 |
function markChanges(editor, diff, type, marks, from, to, classes) { |
|
284 |
var pos = Pos(0, 0); |
|
285 |
var top = Pos(from, 0), bot = editor.clipPos(Pos(to - 1)); |
|
286 |
var cls = type == DIFF_DELETE ? classes.del : classes.insert; |
|
287 |
function markChunk(start, end) { |
|
288 |
var bfrom = Math.max(from, start), bto = Math.min(to, end); |
|
289 |
for (var i = bfrom; i < bto; ++i) |
|
290 |
marks.push(addClass(editor, i, classes, true, i == start, i == end - 1)); |
|
291 |
// When the chunk is empty, make sure a horizontal line shows up |
|
292 |
if (start == end && bfrom == end && bto == end) { |
|
293 |
if (bfrom) |
|
294 |
marks.push(addClass(editor, bfrom - 1, classes, false, false, true)); |
|
295 |
else |
|
296 |
marks.push(addClass(editor, bfrom, classes, false, true, false)); |
|
297 |
} |
|
298 |
} |
|
299 |
|
|
300 |
var chunkStart = 0, pending = false; |
|
301 |
for (var i = 0; i < diff.length; ++i) { |
|
302 |
var part = diff[i], tp = part[0], str = part[1]; |
|
303 |
if (tp == DIFF_EQUAL) { |
|
304 |
var cleanFrom = pos.line + (startOfLineClean(diff, i) ? 0 : 1); |
|
305 |
moveOver(pos, str); |
|
306 |
var cleanTo = pos.line + (endOfLineClean(diff, i) ? 1 : 0); |
|
307 |
if (cleanTo > cleanFrom) { |
|
308 |
if (pending) { markChunk(chunkStart, cleanFrom); pending = false } |
|
309 |
chunkStart = cleanTo; |
|
310 |
} |
|
311 |
} else { |
|
312 |
pending = true |
|
313 |
if (tp == type) { |
|
314 |
var end = moveOver(pos, str, true); |
|
315 |
var a = posMax(top, pos), b = posMin(bot, end); |
|
316 |
if (!posEq(a, b)) |
|
317 |
marks.push(editor.markText(a, b, {className: cls})); |
|
318 |
pos = end; |
|
319 |
} |
|
320 |
} |
|
321 |
} |
|
322 |
if (pending) markChunk(chunkStart, pos.line + 1); |
|
323 |
} |
|
324 |
|
|
325 |
// Updating the gap between editor and original |
|
326 |
|
|
327 |
function makeConnections(dv) { |
|
328 |
if (!dv.showDifferences) return; |
|
329 |
|
|
330 |
if (dv.svg) { |
|
331 |
clear(dv.svg); |
|
332 |
var w = dv.gap.offsetWidth; |
|
333 |
attrs(dv.svg, "width", w, "height", dv.gap.offsetHeight); |
|
334 |
} |
|
335 |
if (dv.copyButtons) clear(dv.copyButtons); |
|
336 |
|
|
337 |
var vpEdit = dv.edit.getViewport(), vpOrig = dv.orig.getViewport(); |
|
338 |
var outerTop = dv.mv.wrap.getBoundingClientRect().top |
|
339 |
var sTopEdit = outerTop - dv.edit.getScrollerElement().getBoundingClientRect().top + dv.edit.getScrollInfo().top |
|
340 |
var sTopOrig = outerTop - dv.orig.getScrollerElement().getBoundingClientRect().top + dv.orig.getScrollInfo().top; |
|
341 |
for (var i = 0; i < dv.chunks.length; i++) { |
|
342 |
var ch = dv.chunks[i]; |
|
343 |
if (ch.editFrom <= vpEdit.to && ch.editTo >= vpEdit.from && |
|
344 |
ch.origFrom <= vpOrig.to && ch.origTo >= vpOrig.from) |
|
345 |
drawConnectorsForChunk(dv, ch, sTopOrig, sTopEdit, w); |
|
346 |
} |
|
347 |
} |
|
348 |
|
|
349 |
function getMatchingOrigLine(editLine, chunks) { |
|
350 |
var editStart = 0, origStart = 0; |
|
351 |
for (var i = 0; i < chunks.length; i++) { |
|
352 |
var chunk = chunks[i]; |
|
353 |
if (chunk.editTo > editLine && chunk.editFrom <= editLine) return null; |
|
354 |
if (chunk.editFrom > editLine) break; |
|
355 |
editStart = chunk.editTo; |
|
356 |
origStart = chunk.origTo; |
|
357 |
} |
|
358 |
return origStart + (editLine - editStart); |
|
359 |
} |
|
360 |
|
|
361 |
// Combines information about chunks and widgets/markers to return |
|
362 |
// an array of lines, in a single editor, that probably need to be |
|
363 |
// aligned with their counterparts in the editor next to it. |
|
364 |
function alignableFor(cm, chunks, isOrig) { |
|
365 |
var tracker = cm.state.trackAlignable |
|
366 |
var start = cm.firstLine(), trackI = 0 |
|
367 |
var result = [] |
|
368 |
for (var i = 0;; i++) { |
|
369 |
var chunk = chunks[i] |
|
370 |
var chunkStart = !chunk ? 1e9 : isOrig ? chunk.origFrom : chunk.editFrom |
|
371 |
for (; trackI < tracker.alignable.length; trackI += 2) { |
|
372 |
var n = tracker.alignable[trackI] + 1 |
|
373 |
if (n <= start) continue |
|
374 |
if (n <= chunkStart) result.push(n) |
|
375 |
else break |
|
376 |
} |
|
377 |
if (!chunk) break |
|
378 |
result.push(start = isOrig ? chunk.origTo : chunk.editTo) |
|
379 |
} |
|
380 |
return result |
|
381 |
} |
|
382 |
|
|
383 |
// Given information about alignable lines in two editors, fill in |
|
384 |
// the result (an array of three-element arrays) to reflect the |
|
385 |
// lines that need to be aligned with each other. |
|
386 |
function mergeAlignable(result, origAlignable, chunks, setIndex) { |
|
387 |
var rI = 0, origI = 0, chunkI = 0, diff = 0 |
|
388 |
outer: for (;; rI++) { |
|
389 |
var nextR = result[rI], nextO = origAlignable[origI] |
|
390 |
if (!nextR && nextO == null) break |
|
391 |
|
|
392 |
var rLine = nextR ? nextR[0] : 1e9, oLine = nextO == null ? 1e9 : nextO |
|
393 |
while (chunkI < chunks.length) { |
|
394 |
var chunk = chunks[chunkI] |
|
395 |
if (chunk.origFrom <= oLine && chunk.origTo > oLine) { |
|
396 |
origI++ |
|
397 |
rI-- |
|
398 |
continue outer; |
|
399 |
} |
|
400 |
if (chunk.editTo > rLine) { |
|
401 |
if (chunk.editFrom <= rLine) continue outer; |
|
402 |
break |
|
403 |
} |
|
404 |
diff += (chunk.origTo - chunk.origFrom) - (chunk.editTo - chunk.editFrom) |
|
405 |
chunkI++ |
|
406 |
} |
|
407 |
if (rLine == oLine - diff) { |
|
408 |
nextR[setIndex] = oLine |
|
409 |
origI++ |
|
410 |
} else if (rLine < oLine - diff) { |
|
411 |
nextR[setIndex] = rLine + diff |
|
412 |
} else { |
|
413 |
var record = [oLine - diff, null, null] |
|
414 |
record[setIndex] = oLine |
|
415 |
result.splice(rI, 0, record) |
|
416 |
origI++ |
|
417 |
} |
|
418 |
} |
|
419 |
} |
|
420 |
|
|
421 |
function findAlignedLines(dv, other) { |
|
422 |
var alignable = alignableFor(dv.edit, dv.chunks, false), result = [] |
|
423 |
if (other) for (var i = 0, j = 0; i < other.chunks.length; i++) { |
|
424 |
var n = other.chunks[i].editTo |
|
425 |
while (j < alignable.length && alignable[j] < n) j++ |
|
426 |
if (j == alignable.length || alignable[j] != n) alignable.splice(j++, 0, n) |
|
427 |
} |
|
428 |
for (var i = 0; i < alignable.length; i++) |
|
429 |
result.push([alignable[i], null, null]) |
|
430 |
|
|
431 |
mergeAlignable(result, alignableFor(dv.orig, dv.chunks, true), dv.chunks, 1) |
|
432 |
if (other) |
|
433 |
mergeAlignable(result, alignableFor(other.orig, other.chunks, true), other.chunks, 2) |
|
434 |
|
|
435 |
return result |
|
436 |
} |
|
437 |
|
|
438 |
function alignChunks(dv, force) { |
|
439 |
if (!dv.dealigned && !force) return; |
|
440 |
if (!dv.orig.curOp) return dv.orig.operation(function() { |
|
441 |
alignChunks(dv, force); |
|
442 |
}); |
|
443 |
|
|
444 |
dv.dealigned = false; |
|
445 |
var other = dv.mv.left == dv ? dv.mv.right : dv.mv.left; |
|
446 |
if (other) { |
|
447 |
ensureDiff(other); |
|
448 |
other.dealigned = false; |
|
449 |
} |
|
450 |
var linesToAlign = findAlignedLines(dv, other); |
|
451 |
|
|
452 |
// Clear old aligners |
|
453 |
var aligners = dv.mv.aligners; |
|
454 |
for (var i = 0; i < aligners.length; i++) |
|
455 |
aligners[i].clear(); |
|
456 |
aligners.length = 0; |
|
457 |
|
|
458 |
var cm = [dv.edit, dv.orig], scroll = []; |
|
459 |
if (other) cm.push(other.orig); |
|
460 |
for (var i = 0; i < cm.length; i++) |
|
461 |
scroll.push(cm[i].getScrollInfo().top); |
|
462 |
|
|
463 |
for (var ln = 0; ln < linesToAlign.length; ln++) |
|
464 |
alignLines(cm, linesToAlign[ln], aligners); |
|
465 |
|
|
466 |
for (var i = 0; i < cm.length; i++) |
|
467 |
cm[i].scrollTo(null, scroll[i]); |
|
468 |
} |
|
469 |
|
|
470 |
function alignLines(cm, lines, aligners) { |
|
471 |
var maxOffset = 0, offset = []; |
|
472 |
for (var i = 0; i < cm.length; i++) if (lines[i] != null) { |
|
473 |
var off = cm[i].heightAtLine(lines[i], "local"); |
|
474 |
offset[i] = off; |
|
475 |
maxOffset = Math.max(maxOffset, off); |
|
476 |
} |
|
477 |
for (var i = 0; i < cm.length; i++) if (lines[i] != null) { |
|
478 |
var diff = maxOffset - offset[i]; |
|
479 |
if (diff > 1) |
|
480 |
aligners.push(padAbove(cm[i], lines[i], diff)); |
|
481 |
} |
|
482 |
} |
|
483 |
|
|
484 |
function padAbove(cm, line, size) { |
|
485 |
var above = true; |
|
486 |
if (line > cm.lastLine()) { |
|
487 |
line--; |
|
488 |
above = false; |
|
489 |
} |
|
490 |
var elt = document.createElement("div"); |
|
491 |
elt.className = "CodeMirror-merge-spacer"; |
|
492 |
elt.style.height = size + "px"; elt.style.minWidth = "1px"; |
|
493 |
return cm.addLineWidget(line, elt, {height: size, above: above, mergeSpacer: true, handleMouseEvents: true}); |
|
494 |
} |
|
495 |
|
|
496 |
function drawConnectorsForChunk(dv, chunk, sTopOrig, sTopEdit, w) { |
|
497 |
var flip = dv.type == "left"; |
|
498 |
var top = dv.orig.heightAtLine(chunk.origFrom, "local", true) - sTopOrig; |
|
499 |
if (dv.svg) { |
|
500 |
var topLpx = top; |
|
501 |
var topRpx = dv.edit.heightAtLine(chunk.editFrom, "local", true) - sTopEdit; |
|
502 |
if (flip) { var tmp = topLpx; topLpx = topRpx; topRpx = tmp; } |
|
503 |
var botLpx = dv.orig.heightAtLine(chunk.origTo, "local", true) - sTopOrig; |
|
504 |
var botRpx = dv.edit.heightAtLine(chunk.editTo, "local", true) - sTopEdit; |
|
505 |
if (flip) { var tmp = botLpx; botLpx = botRpx; botRpx = tmp; } |
|
506 |
var curveTop = " C " + w/2 + " " + topRpx + " " + w/2 + " " + topLpx + " " + (w + 2) + " " + topLpx; |
|
507 |
var curveBot = " C " + w/2 + " " + botLpx + " " + w/2 + " " + botRpx + " -1 " + botRpx; |
|
508 |
attrs(dv.svg.appendChild(document.createElementNS(svgNS, "path")), |
|
509 |
"d", "M -1 " + topRpx + curveTop + " L " + (w + 2) + " " + botLpx + curveBot + " z", |
|
510 |
"class", dv.classes.connect); |
|
511 |
} |
|
512 |
if (dv.copyButtons) { |
|
513 |
var copy = dv.copyButtons.appendChild(elt("div", dv.type == "left" ? "\u21dd" : "\u21dc", |
|
514 |
"CodeMirror-merge-copy")); |
|
515 |
var editOriginals = dv.mv.options.allowEditingOriginals; |
|
516 |
copy.title = editOriginals ? "Push to left" : "Revert chunk"; |
|
517 |
copy.chunk = chunk; |
|
518 |
copy.style.top = (chunk.origTo > chunk.origFrom ? top : dv.edit.heightAtLine(chunk.editFrom, "local") - sTopEdit) + "px"; |
|
519 |
|
|
520 |
if (editOriginals) { |
|
521 |
var topReverse = dv.edit.heightAtLine(chunk.editFrom, "local") - sTopEdit; |
|
522 |
var copyReverse = dv.copyButtons.appendChild(elt("div", dv.type == "right" ? "\u21dd" : "\u21dc", |
|
523 |
"CodeMirror-merge-copy-reverse")); |
|
524 |
copyReverse.title = "Push to right"; |
|
525 |
copyReverse.chunk = {editFrom: chunk.origFrom, editTo: chunk.origTo, |
|
526 |
origFrom: chunk.editFrom, origTo: chunk.editTo}; |
|
527 |
copyReverse.style.top = topReverse + "px"; |
|
528 |
dv.type == "right" ? copyReverse.style.left = "2px" : copyReverse.style.right = "2px"; |
|
529 |
} |
|
530 |
} |
|
531 |
} |
|
532 |
|
|
533 |
function copyChunk(dv, to, from, chunk) { |
|
534 |
if (dv.diffOutOfDate) return; |
|
535 |
var origStart = chunk.origTo > from.lastLine() ? Pos(chunk.origFrom - 1) : Pos(chunk.origFrom, 0) |
|
536 |
var origEnd = Pos(chunk.origTo, 0) |
|
537 |
var editStart = chunk.editTo > to.lastLine() ? Pos(chunk.editFrom - 1) : Pos(chunk.editFrom, 0) |
|
538 |
var editEnd = Pos(chunk.editTo, 0) |
|
539 |
var handler = dv.mv.options.revertChunk |
|
540 |
if (handler) |
|
541 |
handler(dv.mv, from, origStart, origEnd, to, editStart, editEnd) |
|
542 |
else |
|
543 |
to.replaceRange(from.getRange(origStart, origEnd), editStart, editEnd) |
|
544 |
} |
|
545 |
|
|
546 |
// Merge view, containing 0, 1, or 2 diff views. |
|
547 |
|
|
548 |
var MergeView = CodeMirror.MergeView = function(node, options) { |
|
549 |
if (!(this instanceof MergeView)) return new MergeView(node, options); |
|
550 |
|
|
551 |
this.options = options; |
|
552 |
var origLeft = options.origLeft, origRight = options.origRight == null ? options.orig : options.origRight; |
|
553 |
|
|
554 |
var hasLeft = origLeft != null, hasRight = origRight != null; |
|
555 |
var panes = 1 + (hasLeft ? 1 : 0) + (hasRight ? 1 : 0); |
|
556 |
var wrap = [], left = this.left = null, right = this.right = null; |
|
557 |
var self = this; |
|
558 |
|
|
559 |
if (hasLeft) { |
|
560 |
left = this.left = new DiffView(this, "left"); |
|
561 |
var leftPane = elt("div", null, "CodeMirror-merge-pane CodeMirror-merge-left"); |
|
562 |
wrap.push(leftPane); |
|
563 |
wrap.push(buildGap(left)); |
|
564 |
} |
|
565 |
|
|
566 |
var editPane = elt("div", null, "CodeMirror-merge-pane CodeMirror-merge-editor"); |
|
567 |
wrap.push(editPane); |
|
568 |
|
|
569 |
if (hasRight) { |
|
570 |
right = this.right = new DiffView(this, "right"); |
|
571 |
wrap.push(buildGap(right)); |
|
572 |
var rightPane = elt("div", null, "CodeMirror-merge-pane CodeMirror-merge-right"); |
|
573 |
wrap.push(rightPane); |
|
574 |
} |
|
575 |
|
|
576 |
(hasRight ? rightPane : editPane).className += " CodeMirror-merge-pane-rightmost"; |
|
577 |
|
|
578 |
wrap.push(elt("div", null, null, "height: 0; clear: both;")); |
|
579 |
|
|
580 |
var wrapElt = this.wrap = node.appendChild(elt("div", wrap, "CodeMirror-merge CodeMirror-merge-" + panes + "pane")); |
|
581 |
this.edit = CodeMirror(editPane, copyObj(options)); |
|
582 |
|
|
583 |
if (left) left.init(leftPane, origLeft, options); |
|
584 |
if (right) right.init(rightPane, origRight, options); |
|
585 |
if (options.collapseIdentical) |
|
586 |
this.editor().operation(function() { |
|
587 |
collapseIdenticalStretches(self, options.collapseIdentical); |
|
588 |
}); |
|
589 |
if (options.connect == "align") { |
|
590 |
this.aligners = []; |
|
591 |
alignChunks(this.left || this.right, true); |
|
592 |
} |
|
593 |
if (left) left.registerEvents(right) |
|
594 |
if (right) right.registerEvents(left) |
|
595 |
|
|
596 |
|
|
597 |
var onResize = function() { |
|
598 |
if (left) makeConnections(left); |
|
599 |
if (right) makeConnections(right); |
|
600 |
}; |
|
601 |
CodeMirror.on(window, "resize", onResize); |
|
602 |
var resizeInterval = setInterval(function() { |
|
603 |
for (var p = wrapElt.parentNode; p && p != document.body; p = p.parentNode) {} |
|
604 |
if (!p) { clearInterval(resizeInterval); CodeMirror.off(window, "resize", onResize); } |
|
605 |
}, 5000); |
|
606 |
}; |
|
607 |
|
|
608 |
function buildGap(dv) { |
|
609 |
var lock = dv.lockButton = elt("div", null, "CodeMirror-merge-scrolllock"); |
|
610 |
lock.title = "Toggle locked scrolling"; |
|
611 |
var lockWrap = elt("div", [lock], "CodeMirror-merge-scrolllock-wrap"); |
|
612 |
CodeMirror.on(lock, "click", function() { setScrollLock(dv, !dv.lockScroll); }); |
|
613 |
var gapElts = [lockWrap]; |
|
614 |
if (dv.mv.options.revertButtons !== false) { |
|
615 |
dv.copyButtons = elt("div", null, "CodeMirror-merge-copybuttons-" + dv.type); |
|
616 |
CodeMirror.on(dv.copyButtons, "click", function(e) { |
|
617 |
var node = e.target || e.srcElement; |
|
618 |
if (!node.chunk) return; |
|
619 |
if (node.className == "CodeMirror-merge-copy-reverse") { |
|
620 |
copyChunk(dv, dv.orig, dv.edit, node.chunk); |
|
621 |
return; |
|
622 |
} |
|
623 |
copyChunk(dv, dv.edit, dv.orig, node.chunk); |
|
624 |
}); |
|
625 |
gapElts.unshift(dv.copyButtons); |
|
626 |
} |
|
627 |
if (dv.mv.options.connect != "align") { |
|
628 |
var svg = document.createElementNS && document.createElementNS(svgNS, "svg"); |
|
629 |
if (svg && !svg.createSVGRect) svg = null; |
|
630 |
dv.svg = svg; |
|
631 |
if (svg) gapElts.push(svg); |
|
632 |
} |
|
633 |
|
|
634 |
return dv.gap = elt("div", gapElts, "CodeMirror-merge-gap"); |
|
635 |
} |
|
636 |
|
|
637 |
MergeView.prototype = { |
|
638 |
constructor: MergeView, |
|
639 |
editor: function() { return this.edit; }, |
|
640 |
rightOriginal: function() { return this.right && this.right.orig; }, |
|
641 |
leftOriginal: function() { return this.left && this.left.orig; }, |
|
642 |
setShowDifferences: function(val) { |
|
643 |
if (this.right) this.right.setShowDifferences(val); |
|
644 |
if (this.left) this.left.setShowDifferences(val); |
|
645 |
}, |
|
646 |
rightChunks: function() { |
|
647 |
if (this.right) { ensureDiff(this.right); return this.right.chunks; } |
|
648 |
}, |
|
649 |
leftChunks: function() { |
|
650 |
if (this.left) { ensureDiff(this.left); return this.left.chunks; } |
|
651 |
} |
|
652 |
}; |
|
653 |
|
|
654 |
function asString(obj) { |
|
655 |
if (typeof obj == "string") return obj; |
|
656 |
else return obj.getValue(); |
|
657 |
} |
|
658 |
|
|
659 |
// Operations on diffs |
|
660 |
|
|
661 |
var dmp = new diff_match_patch(); |
|
662 |
function getDiff(a, b, ignoreWhitespace) { |
|
663 |
var diff = dmp.diff_main(a, b); |
|
664 |
// The library sometimes leaves in empty parts, which confuse the algorithm |
|
665 |
for (var i = 0; i < diff.length; ++i) { |
|
666 |
var part = diff[i]; |
|
667 |
if (ignoreWhitespace ? !/[^ \t]/.test(part[1]) : !part[1]) { |
|
668 |
diff.splice(i--, 1); |
|
669 |
} else if (i && diff[i - 1][0] == part[0]) { |
|
670 |
diff.splice(i--, 1); |
|
671 |
diff[i][1] += part[1]; |
|
672 |
} |
|
673 |
} |
|
674 |
return diff; |
|
675 |
} |
|
676 |
|
|
677 |
function getChunks(diff) { |
|
678 |
var chunks = []; |
|
679 |
var startEdit = 0, startOrig = 0; |
|
680 |
var edit = Pos(0, 0), orig = Pos(0, 0); |
|
681 |
for (var i = 0; i < diff.length; ++i) { |
|
682 |
var part = diff[i], tp = part[0]; |
|
683 |
if (tp == DIFF_EQUAL) { |
|
684 |
var startOff = !startOfLineClean(diff, i) || edit.line < startEdit || orig.line < startOrig ? 1 : 0; |
|
685 |
var cleanFromEdit = edit.line + startOff, cleanFromOrig = orig.line + startOff; |
|
686 |
moveOver(edit, part[1], null, orig); |
|
687 |
var endOff = endOfLineClean(diff, i) ? 1 : 0; |
|
688 |
var cleanToEdit = edit.line + endOff, cleanToOrig = orig.line + endOff; |
|
689 |
if (cleanToEdit > cleanFromEdit) { |
|
690 |
if (i) chunks.push({origFrom: startOrig, origTo: cleanFromOrig, |
|
691 |
editFrom: startEdit, editTo: cleanFromEdit}); |
|
692 |
startEdit = cleanToEdit; startOrig = cleanToOrig; |
|
693 |
} |
|
694 |
} else { |
|
695 |
moveOver(tp == DIFF_INSERT ? edit : orig, part[1]); |
|
696 |
} |
|
697 |
} |
|
698 |
if (startEdit <= edit.line || startOrig <= orig.line) |
|
699 |
chunks.push({origFrom: startOrig, origTo: orig.line + 1, |
|
700 |
editFrom: startEdit, editTo: edit.line + 1}); |
|
701 |
return chunks; |
|
702 |
} |
|
703 |
|
|
704 |
function endOfLineClean(diff, i) { |
|
705 |
if (i == diff.length - 1) return true; |
|
706 |
var next = diff[i + 1][1]; |
|
707 |
if ((next.length == 1 && i < diff.length - 2) || next.charCodeAt(0) != 10) return false; |
|
708 |
if (i == diff.length - 2) return true; |
|
709 |
next = diff[i + 2][1]; |
|
710 |
return (next.length > 1 || i == diff.length - 3) && next.charCodeAt(0) == 10; |
|
711 |
} |
|
712 |
|
|
713 |
function startOfLineClean(diff, i) { |
|
714 |
if (i == 0) return true; |
|
715 |
var last = diff[i - 1][1]; |
|
716 |
if (last.charCodeAt(last.length - 1) != 10) return false; |
|
717 |
if (i == 1) return true; |
|
718 |
last = diff[i - 2][1]; |
|
719 |
return last.charCodeAt(last.length - 1) == 10; |
|
720 |
} |
|
721 |
|
|
722 |
function chunkBoundariesAround(chunks, n, nInEdit) { |
|
723 |
var beforeE, afterE, beforeO, afterO; |
|
724 |
for (var i = 0; i < chunks.length; i++) { |
|
725 |
var chunk = chunks[i]; |
|
726 |
var fromLocal = nInEdit ? chunk.editFrom : chunk.origFrom; |
|
727 |
var toLocal = nInEdit ? chunk.editTo : chunk.origTo; |
|
728 |
if (afterE == null) { |
|
729 |
if (fromLocal > n) { afterE = chunk.editFrom; afterO = chunk.origFrom; } |
|
730 |
else if (toLocal > n) { afterE = chunk.editTo; afterO = chunk.origTo; } |
|
731 |
} |
|
732 |
if (toLocal <= n) { beforeE = chunk.editTo; beforeO = chunk.origTo; } |
|
733 |
else if (fromLocal <= n) { beforeE = chunk.editFrom; beforeO = chunk.origFrom; } |
|
734 |
} |
|
735 |
return {edit: {before: beforeE, after: afterE}, orig: {before: beforeO, after: afterO}}; |
|
736 |
} |
|
737 |
|
|
738 |
function collapseSingle(cm, from, to) { |
|
739 |
cm.addLineClass(from, "wrap", "CodeMirror-merge-collapsed-line"); |
|
740 |
var widget = document.createElement("span"); |
|
741 |
widget.className = "CodeMirror-merge-collapsed-widget"; |
|
742 |
widget.title = "Identical text collapsed. Click to expand."; |
|
743 |
var mark = cm.markText(Pos(from, 0), Pos(to - 1), { |
|
744 |
inclusiveLeft: true, |
|
745 |
inclusiveRight: true, |
|
746 |
replacedWith: widget, |
|
747 |
clearOnEnter: true |
|
748 |
}); |
|
749 |
function clear() { |
|
750 |
mark.clear(); |
|
751 |
cm.removeLineClass(from, "wrap", "CodeMirror-merge-collapsed-line"); |
|
752 |
} |
|
753 |
CodeMirror.on(widget, "click", clear); |
|
754 |
return {mark: mark, clear: clear}; |
|
755 |
} |
|
756 |
|
|
757 |
function collapseStretch(size, editors) { |
|
758 |
var marks = []; |
|
759 |
function clear() { |
|
760 |
for (var i = 0; i < marks.length; i++) marks[i].clear(); |
|
761 |
} |
|
762 |
for (var i = 0; i < editors.length; i++) { |
|
763 |
var editor = editors[i]; |
|
764 |
var mark = collapseSingle(editor.cm, editor.line, editor.line + size); |
|
765 |
marks.push(mark); |
|
766 |
mark.mark.on("clear", clear); |
|
767 |
} |
|
768 |
return marks[0].mark; |
|
769 |
} |
|
770 |
|
|
771 |
function unclearNearChunks(dv, margin, off, clear) { |
|
772 |
for (var i = 0; i < dv.chunks.length; i++) { |
|
773 |
var chunk = dv.chunks[i]; |
|
774 |
for (var l = chunk.editFrom - margin; l < chunk.editTo + margin; l++) { |
|
775 |
var pos = l + off; |
|
776 |
if (pos >= 0 && pos < clear.length) clear[pos] = false; |
|
777 |
} |
|
778 |
} |
|
779 |
} |
|
780 |
|
|
781 |
function collapseIdenticalStretches(mv, margin) { |
|
782 |
if (typeof margin != "number") margin = 2; |
|
783 |
var clear = [], edit = mv.editor(), off = edit.firstLine(); |
|
784 |
for (var l = off, e = edit.lastLine(); l <= e; l++) clear.push(true); |
|
785 |
if (mv.left) unclearNearChunks(mv.left, margin, off, clear); |
|
786 |
if (mv.right) unclearNearChunks(mv.right, margin, off, clear); |
|
787 |
|
|
788 |
for (var i = 0; i < clear.length; i++) { |
|
789 |
if (clear[i]) { |
|
790 |
var line = i + off; |
|
791 |
for (var size = 1; i < clear.length - 1 && clear[i + 1]; i++, size++) {} |
|
792 |
if (size > margin) { |
|
793 |
var editors = [{line: line, cm: edit}]; |
|
794 |
if (mv.left) editors.push({line: getMatchingOrigLine(line, mv.left.chunks), cm: mv.left.orig}); |
|
795 |
if (mv.right) editors.push({line: getMatchingOrigLine(line, mv.right.chunks), cm: mv.right.orig}); |
|
796 |
var mark = collapseStretch(size, editors); |
|
797 |
if (mv.options.onCollapse) mv.options.onCollapse(mv, line, size, mark); |
|
798 |
} |
|
799 |
} |
|
800 |
} |
|
801 |
} |
|
802 |
|
|
803 |
// General utilities |
|
804 |
|
|
805 |
function elt(tag, content, className, style) { |
|
806 |
var e = document.createElement(tag); |
|
807 |
if (className) e.className = className; |
|
808 |
if (style) e.style.cssText = style; |
|
809 |
if (typeof content == "string") e.appendChild(document.createTextNode(content)); |
|
810 |
else if (content) for (var i = 0; i < content.length; ++i) e.appendChild(content[i]); |
|
811 |
return e; |
|
812 |
} |
|
813 |
|
|
814 |
function clear(node) { |
|
815 |
for (var count = node.childNodes.length; count > 0; --count) |
|
816 |
node.removeChild(node.firstChild); |
|
817 |
} |
|
818 |
|
|
819 |
function attrs(elt) { |
|
820 |
for (var i = 1; i < arguments.length; i += 2) |
|
821 |
elt.setAttribute(arguments[i], arguments[i+1]); |
|
822 |
} |
|
823 |
|
|
824 |
function copyObj(obj, target) { |
|
825 |
if (!target) target = {}; |
|
826 |
for (var prop in obj) if (obj.hasOwnProperty(prop)) target[prop] = obj[prop]; |
|
827 |
return target; |
|
828 |
} |
|
829 |
|
|
830 |
function moveOver(pos, str, copy, other) { |
|
831 |
var out = copy ? Pos(pos.line, pos.ch) : pos, at = 0; |
|
832 |
for (;;) { |
|
833 |
var nl = str.indexOf("\n", at); |
|
834 |
if (nl == -1) break; |
|
835 |
++out.line; |
|
836 |
if (other) ++other.line; |
|
837 |
at = nl + 1; |
|
838 |
} |
|
839 |
out.ch = (at ? 0 : out.ch) + (str.length - at); |
|
840 |
if (other) other.ch = (at ? 0 : other.ch) + (str.length - at); |
|
841 |
return out; |
|
842 |
} |
|
843 |
|
|
844 |
// Tracks collapsed markers and line widgets, in order to be able to |
|
845 |
// accurately align the content of two editors. |
|
846 |
|
|
847 |
var F_WIDGET = 1, F_WIDGET_BELOW = 2, F_MARKER = 4 |
|
848 |
|
|
849 |
function TrackAlignable(cm) { |
|
850 |
this.cm = cm |
|
851 |
this.alignable = [] |
|
852 |
this.height = cm.doc.height |
|
853 |
var self = this |
|
854 |
cm.on("markerAdded", function(_, marker) { |
|
855 |
if (!marker.collapsed) return |
|
856 |
var found = marker.find(1) |
|
857 |
if (found != null) self.set(found.line, F_MARKER) |
|
858 |
}) |
|
859 |
cm.on("markerCleared", function(_, marker, _min, max) { |
|
860 |
if (max != null && marker.collapsed) |
|
861 |
self.check(max, F_MARKER, self.hasMarker) |
|
862 |
}) |
|
863 |
cm.on("markerChanged", this.signal.bind(this)) |
|
864 |
cm.on("lineWidgetAdded", function(_, widget, lineNo) { |
|
865 |
if (widget.mergeSpacer) return |
|
866 |
if (widget.above) self.set(lineNo - 1, F_WIDGET_BELOW) |
|
867 |
else self.set(lineNo, F_WIDGET) |
|
868 |
}) |
|
869 |
cm.on("lineWidgetCleared", function(_, widget, lineNo) { |
|
870 |
if (widget.mergeSpacer) return |
|
871 |
if (widget.above) self.check(lineNo - 1, F_WIDGET_BELOW, self.hasWidgetBelow) |
|
872 |
else self.check(lineNo, F_WIDGET, self.hasWidget) |
|
873 |
}) |
|
874 |
cm.on("lineWidgetChanged", this.signal.bind(this)) |
|
875 |
cm.on("change", function(_, change) { |
|
876 |
var start = change.from.line, nBefore = change.to.line - change.from.line |
|
877 |
var nAfter = change.text.length - 1, end = start + nAfter |
|
878 |
if (nBefore || nAfter) self.map(start, nBefore, nAfter) |
|
879 |
self.check(end, F_MARKER, self.hasMarker) |
|
880 |
if (nBefore || nAfter) self.check(change.from.line, F_MARKER, self.hasMarker) |
|
881 |
}) |
|
882 |
cm.on("viewportChange", function() { |
|
883 |
if (self.cm.doc.height != self.height) self.signal() |
|
884 |
}) |
|
885 |
} |
|
886 |
|
|
887 |
TrackAlignable.prototype = { |
|
888 |
signal: function() { |
|
889 |
CodeMirror.signal(this, "realign") |
|
890 |
this.height = this.cm.doc.height |
|
891 |
}, |
|
892 |
|
|
893 |
set: function(n, flags) { |
|
894 |
var pos = -1 |
|
895 |
for (; pos < this.alignable.length; pos += 2) { |
|
896 |
var diff = this.alignable[pos] - n |
|
897 |
if (diff == 0) { |
|
898 |
if ((this.alignable[pos + 1] & flags) == flags) return |
|
899 |
this.alignable[pos + 1] |= flags |
|
900 |
this.signal() |
|
901 |
return |
|
902 |
} |
|
903 |
if (diff > 0) break |
|
904 |
} |
|
905 |
this.signal() |
|
906 |
this.alignable.splice(pos, 0, n, flags) |
|
907 |
}, |
|
908 |
|
|
909 |
find: function(n) { |
|
910 |
for (var i = 0; i < this.alignable.length; i += 2) |
|
911 |
if (this.alignable[i] == n) return i |
|
912 |
return -1 |
|
913 |
}, |
|
914 |
|
|
915 |
check: function(n, flag, pred) { |
|
916 |
var found = this.find(n) |
|
917 |
if (found == -1 || !(this.alignable[found + 1] & flag)) return |
|
918 |
if (!pred.call(this, n)) { |
|
919 |
this.signal() |
|
920 |
var flags = this.alignable[found + 1] & ~flag |
|
921 |
if (flags) this.alignable[found + 1] = flags |
|
922 |
else this.alignable.splice(found, 2) |
|
923 |
} |
|
924 |
}, |
|
925 |
|
|
926 |
hasMarker: function(n) { |
|
927 |
var handle = this.cm.getLineHandle(n) |
|
928 |
if (handle.markedSpans) for (var i = 0; i < handle.markedSpans.length; i++) |
|
929 |
if (handle.markedSpans[i].mark.collapsed && handle.markedSpans[i].to != null) |
|
930 |
return true |
|
931 |
return false |
|
932 |
}, |
|
933 |
|
|
934 |
hasWidget: function(n) { |
|
935 |
var handle = this.cm.getLineHandle(n) |
|
936 |
if (handle.widgets) for (var i = 0; i < handle.widgets.length; i++) |
|
937 |
if (!handle.widgets[i].above && !handle.widgets[i].mergeSpacer) return true |
|
938 |
return false |
|
939 |
}, |
|
940 |
|
|
941 |
hasWidgetBelow: function(n) { |
|
942 |
if (n == this.cm.lastLine()) return false |
|
943 |
var handle = this.cm.getLineHandle(n + 1) |
|
944 |
if (handle.widgets) for (var i = 0; i < handle.widgets.length; i++) |
|
945 |
if (handle.widgets[i].above && !handle.widgets[i].mergeSpacer) return true |
|
946 |
return false |
|
947 |
}, |
|
948 |
|
|
949 |
map: function(from, nBefore, nAfter) { |
|
950 |
var diff = nAfter - nBefore, to = from + nBefore, widgetFrom = -1, widgetTo = -1 |
|
951 |
for (var i = 0; i < this.alignable.length; i += 2) { |
|
952 |
var n = this.alignable[i] |
|
953 |
if (n == from && (this.alignable[i + 1] & F_WIDGET_BELOW)) widgetFrom = i |
|
954 |
if (n == to && (this.alignable[i + 1] & F_WIDGET_BELOW)) widgetTo = i |
|
955 |
if (n <= from) continue |
|
956 |
else if (n < to) this.alignable.splice(i--, 2) |
|
957 |
else this.alignable[i] += diff |
|
958 |
} |
|
959 |
if (widgetFrom > -1) { |
|
960 |
var flags = this.alignable[widgetFrom + 1] |
|
961 |
if (flags == F_WIDGET_BELOW) this.alignable.splice(widgetFrom, 2) |
|
962 |
else this.alignable[widgetFrom + 1] = flags & ~F_WIDGET_BELOW |
|
963 |
} |
|
964 |
if (widgetTo > -1 && nAfter) |
|
965 |
this.set(from + nAfter, F_WIDGET_BELOW) |
|
966 |
} |
|
967 |
} |
|
968 |
|
|
969 |
function posMin(a, b) { return (a.line - b.line || a.ch - b.ch) < 0 ? a : b; } |
|
970 |
function posMax(a, b) { return (a.line - b.line || a.ch - b.ch) > 0 ? a : b; } |
|
971 |
function posEq(a, b) { return a.line == b.line && a.ch == b.ch; } |
|
972 |
|
|
973 |
function findPrevDiff(chunks, start, isOrig) { |
|
974 |
for (var i = chunks.length - 1; i >= 0; i--) { |
|
975 |
var chunk = chunks[i]; |
|
976 |
var to = (isOrig ? chunk.origTo : chunk.editTo) - 1; |
|
977 |
if (to < start) return to; |
|
978 |
} |
|
979 |
} |
|
980 |
|
|
981 |
function findNextDiff(chunks, start, isOrig) { |
|
982 |
for (var i = 0; i < chunks.length; i++) { |
|
983 |
var chunk = chunks[i]; |
|
984 |
var from = (isOrig ? chunk.origFrom : chunk.editFrom); |
|
985 |
if (from > start) return from; |
|
986 |
} |
|
987 |
} |
|
988 |
|
|
989 |
function goNearbyDiff(cm, dir) { |
|
990 |
var found = null, views = cm.state.diffViews, line = cm.getCursor().line; |
|
991 |
if (views) for (var i = 0; i < views.length; i++) { |
|
992 |
var dv = views[i], isOrig = cm == dv.orig; |
|
993 |
ensureDiff(dv); |
|
994 |
var pos = dir < 0 ? findPrevDiff(dv.chunks, line, isOrig) : findNextDiff(dv.chunks, line, isOrig); |
|
995 |
if (pos != null && (found == null || (dir < 0 ? pos > found : pos < found))) |
|
996 |
found = pos; |
|
997 |
} |
|
998 |
if (found != null) |
|
999 |
cm.setCursor(found, 0); |
|
1000 |
else |
|
1001 |
return CodeMirror.Pass; |
|
1002 |
} |
|
1003 |
|
|
1004 |
CodeMirror.commands.goNextDiff = function(cm) { |
|
1005 |
return goNearbyDiff(cm, 1); |
|
1006 |
}; |
|
1007 |
CodeMirror.commands.goPrevDiff = function(cm) { |
|
1008 |
return goNearbyDiff(cm, -1); |
|
1009 |
}; |
|
1010 |
}); |