1
0

merge.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. // declare global: diff_match_patch, DIFF_INSERT, DIFF_DELETE, DIFF_EQUAL
  4. (function(mod) {
  5. if (typeof exports == "object" && typeof module == "object") // CommonJS
  6. mod(require("../../lib/codemirror"), require("diff_match_patch"));
  7. else if (typeof define == "function" && define.amd) // AMD
  8. define(["../../lib/codemirror", "diff_match_patch"], mod);
  9. else // Plain browser env
  10. mod(CodeMirror, diff_match_patch);
  11. })(function(CodeMirror, diff_match_patch) {
  12. "use strict";
  13. var Pos = CodeMirror.Pos;
  14. var svgNS = "http://www.w3.org/2000/svg";
  15. function DiffView(mv, type) {
  16. this.mv = mv;
  17. this.type = type;
  18. this.classes = type == "left"
  19. ? {chunk: "CodeMirror-merge-l-chunk",
  20. start: "CodeMirror-merge-l-chunk-start",
  21. end: "CodeMirror-merge-l-chunk-end",
  22. insert: "CodeMirror-merge-l-inserted",
  23. del: "CodeMirror-merge-l-deleted",
  24. connect: "CodeMirror-merge-l-connect"}
  25. : {chunk: "CodeMirror-merge-r-chunk",
  26. start: "CodeMirror-merge-r-chunk-start",
  27. end: "CodeMirror-merge-r-chunk-end",
  28. insert: "CodeMirror-merge-r-inserted",
  29. del: "CodeMirror-merge-r-deleted",
  30. connect: "CodeMirror-merge-r-connect"};
  31. if (mv.options.connect == "align")
  32. this.aligners = [];
  33. }
  34. DiffView.prototype = {
  35. constructor: DiffView,
  36. init: function(pane, orig, options) {
  37. this.edit = this.mv.edit;
  38. this.orig = CodeMirror(pane, copyObj({value: orig, readOnly: !this.mv.options.allowEditingOriginals}, copyObj(options)));
  39. this.diff = getDiff(asString(orig), asString(options.value));
  40. this.diffOutOfDate = false;
  41. this.showDifferences = options.showDifferences !== false;
  42. this.forceUpdate = registerUpdate(this);
  43. setScrollLock(this, true, false);
  44. registerScroll(this);
  45. },
  46. setShowDifferences: function(val) {
  47. val = val !== false;
  48. if (val != this.showDifferences) {
  49. this.showDifferences = val;
  50. this.forceUpdate("full");
  51. }
  52. }
  53. };
  54. function ensureDiff(dv) {
  55. if (dv.diffOutOfDate) {
  56. dv.diff = getDiff(dv.orig.getValue(), dv.edit.getValue());
  57. dv.diffOutOfDate = false;
  58. CodeMirror.signal(dv.edit, "updateDiff", dv.diff);
  59. }
  60. }
  61. function registerUpdate(dv) {
  62. var edit = {from: 0, to: 0, marked: []};
  63. var orig = {from: 0, to: 0, marked: []};
  64. var debounceChange;
  65. function update(mode) {
  66. if (mode == "full") {
  67. if (dv.svg) clear(dv.svg);
  68. if (dv.copyButtons) clear(dv.copyButtons);
  69. clearMarks(dv.edit, edit.marked, dv.classes);
  70. clearMarks(dv.orig, orig.marked, dv.classes);
  71. edit.from = edit.to = orig.from = orig.to = 0;
  72. }
  73. ensureDiff(dv);
  74. if (dv.showDifferences) {
  75. updateMarks(dv.edit, dv.diff, edit, DIFF_INSERT, dv.classes);
  76. updateMarks(dv.orig, dv.diff, orig, DIFF_DELETE, dv.classes);
  77. }
  78. makeConnections(dv);
  79. }
  80. function set(slow) {
  81. clearTimeout(debounceChange);
  82. debounceChange = setTimeout(update, slow == true ? 250 : 100);
  83. }
  84. function change() {
  85. if (!dv.diffOutOfDate) {
  86. dv.diffOutOfDate = true;
  87. edit.from = edit.to = orig.from = orig.to = 0;
  88. }
  89. set(true);
  90. }
  91. dv.edit.on("change", change);
  92. dv.orig.on("change", change);
  93. dv.edit.on("markerAdded", set);
  94. dv.edit.on("markerCleared", set);
  95. dv.orig.on("markerAdded", set);
  96. dv.orig.on("markerCleared", set);
  97. dv.edit.on("viewportChange", set);
  98. dv.orig.on("viewportChange", set);
  99. update();
  100. return update;
  101. }
  102. function registerScroll(dv) {
  103. dv.edit.on("scroll", function() {
  104. syncScroll(dv, DIFF_INSERT) && makeConnections(dv);
  105. });
  106. dv.orig.on("scroll", function() {
  107. syncScroll(dv, DIFF_DELETE) && makeConnections(dv);
  108. });
  109. }
  110. function syncScroll(dv, type) {
  111. // Change handler will do a refresh after a timeout when diff is out of date
  112. if (dv.diffOutOfDate) return false;
  113. if (!dv.lockScroll) return true;
  114. var editor, other, now = +new Date;
  115. if (type == DIFF_INSERT) { editor = dv.edit; other = dv.orig; }
  116. else { editor = dv.orig; other = dv.edit; }
  117. // Don't take action if the position of this editor was recently set
  118. // (to prevent feedback loops)
  119. if (editor.state.scrollSetBy == dv && (editor.state.scrollSetAt || 0) + 50 > now) return false;
  120. var sInfo = editor.getScrollInfo();
  121. if (dv.mv.options.connect == "align") {
  122. targetPos = sInfo.top;
  123. } else {
  124. var halfScreen = .5 * sInfo.clientHeight, midY = sInfo.top + halfScreen;
  125. var mid = editor.lineAtHeight(midY, "local");
  126. var around = chunkBoundariesAround(dv.diff, mid, type == DIFF_INSERT);
  127. var off = getOffsets(editor, type == DIFF_INSERT ? around.edit : around.orig);
  128. var offOther = getOffsets(other, type == DIFF_INSERT ? around.orig : around.edit);
  129. var ratio = (midY - off.top) / (off.bot - off.top);
  130. var targetPos = (offOther.top - halfScreen) + ratio * (offOther.bot - offOther.top);
  131. var botDist, mix;
  132. // Some careful tweaking to make sure no space is left out of view
  133. // when scrolling to top or bottom.
  134. if (targetPos > sInfo.top && (mix = sInfo.top / halfScreen) < 1) {
  135. targetPos = targetPos * mix + sInfo.top * (1 - mix);
  136. } else if ((botDist = sInfo.height - sInfo.clientHeight - sInfo.top) < halfScreen) {
  137. var otherInfo = other.getScrollInfo();
  138. var botDistOther = otherInfo.height - otherInfo.clientHeight - targetPos;
  139. if (botDistOther > botDist && (mix = botDist / halfScreen) < 1)
  140. targetPos = targetPos * mix + (otherInfo.height - otherInfo.clientHeight - botDist) * (1 - mix);
  141. }
  142. }
  143. other.scrollTo(sInfo.left, targetPos);
  144. other.state.scrollSetAt = now;
  145. other.state.scrollSetBy = dv;
  146. return true;
  147. }
  148. function getOffsets(editor, around) {
  149. var bot = around.after;
  150. if (bot == null) bot = editor.lastLine() + 1;
  151. return {top: editor.heightAtLine(around.before || 0, "local"),
  152. bot: editor.heightAtLine(bot, "local")};
  153. }
  154. function setScrollLock(dv, val, action) {
  155. dv.lockScroll = val;
  156. if (val && action != false) syncScroll(dv, DIFF_INSERT) && makeConnections(dv);
  157. dv.lockButton.innerHTML = val ? "\u21db\u21da" : "\u21db&nbsp;&nbsp;\u21da";
  158. }
  159. // Updating the marks for editor content
  160. function clearMarks(editor, arr, classes) {
  161. for (var i = 0; i < arr.length; ++i) {
  162. var mark = arr[i];
  163. if (mark instanceof CodeMirror.TextMarker) {
  164. mark.clear();
  165. } else if (mark.parent) {
  166. editor.removeLineClass(mark, "background", classes.chunk);
  167. editor.removeLineClass(mark, "background", classes.start);
  168. editor.removeLineClass(mark, "background", classes.end);
  169. }
  170. }
  171. arr.length = 0;
  172. }
  173. // FIXME maybe add a margin around viewport to prevent too many updates
  174. function updateMarks(editor, diff, state, type, classes) {
  175. var vp = editor.getViewport();
  176. editor.operation(function() {
  177. if (state.from == state.to || vp.from - state.to > 20 || state.from - vp.to > 20) {
  178. clearMarks(editor, state.marked, classes);
  179. markChanges(editor, diff, type, state.marked, vp.from, vp.to, classes);
  180. state.from = vp.from; state.to = vp.to;
  181. } else {
  182. if (vp.from < state.from) {
  183. markChanges(editor, diff, type, state.marked, vp.from, state.from, classes);
  184. state.from = vp.from;
  185. }
  186. if (vp.to > state.to) {
  187. markChanges(editor, diff, type, state.marked, state.to, vp.to, classes);
  188. state.to = vp.to;
  189. }
  190. }
  191. });
  192. }
  193. function markChanges(editor, diff, type, marks, from, to, classes) {
  194. var pos = Pos(0, 0);
  195. var top = Pos(from, 0), bot = editor.clipPos(Pos(to - 1));
  196. var cls = type == DIFF_DELETE ? classes.del : classes.insert;
  197. function markChunk(start, end) {
  198. var bfrom = Math.max(from, start), bto = Math.min(to, end);
  199. for (var i = bfrom; i < bto; ++i) {
  200. var line = editor.addLineClass(i, "background", classes.chunk);
  201. if (i == start) editor.addLineClass(line, "background", classes.start);
  202. if (i == end - 1) editor.addLineClass(line, "background", classes.end);
  203. marks.push(line);
  204. }
  205. // When the chunk is empty, make sure a horizontal line shows up
  206. if (start == end && bfrom == end && bto == end) {
  207. if (bfrom)
  208. marks.push(editor.addLineClass(bfrom - 1, "background", classes.end));
  209. else
  210. marks.push(editor.addLineClass(bfrom, "background", classes.start));
  211. }
  212. }
  213. var chunkStart = 0;
  214. for (var i = 0; i < diff.length; ++i) {
  215. var part = diff[i], tp = part[0], str = part[1];
  216. if (tp == DIFF_EQUAL) {
  217. var cleanFrom = pos.line + (startOfLineClean(diff, i) ? 0 : 1);
  218. moveOver(pos, str);
  219. var cleanTo = pos.line + (endOfLineClean(diff, i) ? 1 : 0);
  220. if (cleanTo > cleanFrom) {
  221. if (i) markChunk(chunkStart, cleanFrom);
  222. chunkStart = cleanTo;
  223. }
  224. } else {
  225. if (tp == type) {
  226. var end = moveOver(pos, str, true);
  227. var a = posMax(top, pos), b = posMin(bot, end);
  228. if (!posEq(a, b))
  229. marks.push(editor.markText(a, b, {className: cls}));
  230. pos = end;
  231. }
  232. }
  233. }
  234. if (chunkStart <= pos.line) markChunk(chunkStart, pos.line + 1);
  235. }
  236. // Updating the gap between editor and original
  237. function makeConnections(dv) {
  238. if (!dv.showDifferences) return;
  239. var align = dv.mv.options.connect == "align";
  240. if (align) {
  241. if (!dv.orig.curOp) return dv.orig.operation(function() {
  242. makeConnections(dv);
  243. });
  244. for (var i = 0; i < dv.aligners.length; i++)
  245. dv.aligners[i].clear();
  246. dv.aligners.length = 0;
  247. var extraSpaceAbove = {edit: 0, orig: 0};
  248. }
  249. if (dv.svg) {
  250. clear(dv.svg);
  251. var w = dv.gap.offsetWidth;
  252. attrs(dv.svg, "width", w, "height", dv.gap.offsetHeight);
  253. }
  254. if (dv.copyButtons) clear(dv.copyButtons);
  255. var vpEdit = dv.edit.getViewport(), vpOrig = dv.orig.getViewport();
  256. var sTopEdit = dv.edit.getScrollInfo().top, sTopOrig = dv.orig.getScrollInfo().top;
  257. iterateChunks(dv.diff, function(topOrig, botOrig, topEdit, botEdit) {
  258. if (topEdit <= vpEdit.to && botEdit >= vpEdit.from &&
  259. topOrig <= vpOrig.to && botOrig >= vpOrig.from)
  260. drawConnectorsForChunk(dv, topOrig, botOrig, topEdit, botEdit, sTopOrig, sTopEdit, w);
  261. if (align && (topEdit <= vpEdit.to || topOrig <= vpOrig.to)) {
  262. var above = (botEdit < vpEdit.from && botOrig < vpOrig.from);
  263. alignChunks(dv, topOrig, botOrig, topEdit, botEdit, above && extraSpaceAbove);
  264. }
  265. });
  266. if (align) {
  267. if (extraSpaceAbove.edit)
  268. dv.aligners.push(padBelow(dv.edit, 0, extraSpaceAbove.edit));
  269. if (extraSpaceAbove.orig)
  270. dv.aligners.push(padBelow(dv.orig, 0, extraSpaceAbove.orig));
  271. }
  272. }
  273. function drawConnectorsForChunk(dv, topOrig, botOrig, topEdit, botEdit, sTopOrig, sTopEdit, w) {
  274. var flip = dv.type == "left";
  275. var top = dv.orig.heightAtLine(topOrig, "local") - sTopOrig;
  276. if (dv.svg) {
  277. var topLpx = top;
  278. var topRpx = dv.edit.heightAtLine(topEdit, "local") - sTopEdit;
  279. if (flip) { var tmp = topLpx; topLpx = topRpx; topRpx = tmp; }
  280. var botLpx = dv.orig.heightAtLine(botOrig, "local") - sTopOrig;
  281. var botRpx = dv.edit.heightAtLine(botEdit, "local") - sTopEdit;
  282. if (flip) { var tmp = botLpx; botLpx = botRpx; botRpx = tmp; }
  283. var curveTop = " C " + w/2 + " " + topRpx + " " + w/2 + " " + topLpx + " " + (w + 2) + " " + topLpx;
  284. var curveBot = " C " + w/2 + " " + botLpx + " " + w/2 + " " + botRpx + " -1 " + botRpx;
  285. attrs(dv.svg.appendChild(document.createElementNS(svgNS, "path")),
  286. "d", "M -1 " + topRpx + curveTop + " L " + (w + 2) + " " + botLpx + curveBot + " z",
  287. "class", dv.classes.connect);
  288. }
  289. if (dv.copyButtons) {
  290. var copy = dv.copyButtons.appendChild(elt("div", dv.type == "left" ? "\u21dd" : "\u21dc",
  291. "CodeMirror-merge-copy"));
  292. var editOriginals = dv.mv.options.allowEditingOriginals;
  293. copy.title = editOriginals ? "Push to left" : "Revert chunk";
  294. copy.chunk = {topEdit: topEdit, botEdit: botEdit, topOrig: topOrig, botOrig: botOrig};
  295. copy.style.top = top + "px";
  296. if (editOriginals) {
  297. var topReverse = dv.orig.heightAtLine(topEdit, "local") - sTopEdit;
  298. var copyReverse = dv.copyButtons.appendChild(elt("div", dv.type == "right" ? "\u21dd" : "\u21dc",
  299. "CodeMirror-merge-copy-reverse"));
  300. copyReverse.title = "Push to right";
  301. copyReverse.chunk = {topEdit: topOrig, botEdit: botOrig, topOrig: topEdit, botOrig: botEdit};
  302. copyReverse.style.top = topReverse + "px";
  303. dv.type == "right" ? copyReverse.style.left = "2px" : copyReverse.style.right = "2px";
  304. }
  305. }
  306. }
  307. function alignChunks(dv, topOrig, botOrig, topEdit, botEdit, aboveViewport) {
  308. var topOrigPx = dv.orig.heightAtLine(topOrig, "local");
  309. var botOrigPx = dv.orig.heightAtLine(botOrig, "local");
  310. var topEditPx = dv.edit.heightAtLine(topEdit, "local");
  311. var botEditPx = dv.edit.heightAtLine(botEdit, "local");
  312. var origH = botOrigPx -topOrigPx, editH = botEditPx - topEditPx;
  313. var diff = editH - origH;
  314. if (diff > 1) {
  315. if (aboveViewport) aboveViewport.orig += diff;
  316. else dv.aligners.push(padBelow(dv.orig, botOrig - 1, diff));
  317. } else if (diff < -1) {
  318. if (aboveViewport) aboveViewport.edit -= diff;
  319. else dv.aligners.push(padBelow(dv.edit, botEdit - 1, -diff));
  320. }
  321. return 0;
  322. }
  323. function padBelow(cm, line, size) {
  324. var elt = document.createElement("div");
  325. elt.style.height = size + "px"; elt.style.minWidth = "1px";
  326. return cm.addLineWidget(line, elt, {height: size});
  327. }
  328. function copyChunk(dv, to, from, chunk) {
  329. if (dv.diffOutOfDate) return;
  330. to.replaceRange(from.getRange(Pos(chunk.topOrig, 0), Pos(chunk.botOrig, 0)),
  331. Pos(chunk.topEdit, 0), Pos(chunk.botEdit, 0));
  332. }
  333. // Merge view, containing 0, 1, or 2 diff views.
  334. var MergeView = CodeMirror.MergeView = function(node, options) {
  335. if (!(this instanceof MergeView)) return new MergeView(node, options);
  336. this.options = options;
  337. var origLeft = options.origLeft, origRight = options.origRight == null ? options.orig : options.origRight;
  338. if (origLeft && origRight) {
  339. if (options.connect == "align")
  340. throw new Error("connect: \"align\" is not supported for three-way merge views");
  341. if (options.collapseIdentical)
  342. throw new Error("collapseIdentical option is not supported for three-way merge views");
  343. }
  344. var hasLeft = origLeft != null, hasRight = origRight != null;
  345. var panes = 1 + (hasLeft ? 1 : 0) + (hasRight ? 1 : 0);
  346. var wrap = [], left = this.left = null, right = this.right = null;
  347. if (hasLeft) {
  348. left = this.left = new DiffView(this, "left");
  349. var leftPane = elt("div", null, "CodeMirror-merge-pane");
  350. wrap.push(leftPane);
  351. wrap.push(buildGap(left));
  352. }
  353. var editPane = elt("div", null, "CodeMirror-merge-pane");
  354. wrap.push(editPane);
  355. if (hasRight) {
  356. right = this.right = new DiffView(this, "right");
  357. wrap.push(buildGap(right));
  358. var rightPane = elt("div", null, "CodeMirror-merge-pane");
  359. wrap.push(rightPane);
  360. }
  361. (hasRight ? rightPane : editPane).className += " CodeMirror-merge-pane-rightmost";
  362. wrap.push(elt("div", null, null, "height: 0; clear: both;"));
  363. var wrapElt = this.wrap = node.appendChild(elt("div", wrap, "CodeMirror-merge CodeMirror-merge-" + panes + "pane"));
  364. this.edit = CodeMirror(editPane, copyObj(options));
  365. if (left) left.init(leftPane, origLeft, options);
  366. if (right) right.init(rightPane, origRight, options);
  367. if (options.collapseIdentical)
  368. collapseIdenticalStretches(left || right, options.collapseIdentical);
  369. var onResize = function() {
  370. if (left) makeConnections(left);
  371. if (right) makeConnections(right);
  372. };
  373. CodeMirror.on(window, "resize", onResize);
  374. var resizeInterval = setInterval(function() {
  375. for (var p = wrapElt.parentNode; p && p != document.body; p = p.parentNode) {}
  376. if (!p) { clearInterval(resizeInterval); CodeMirror.off(window, "resize", onResize); }
  377. }, 5000);
  378. };
  379. function buildGap(dv) {
  380. var lock = dv.lockButton = elt("div", null, "CodeMirror-merge-scrolllock");
  381. lock.title = "Toggle locked scrolling";
  382. var lockWrap = elt("div", [lock], "CodeMirror-merge-scrolllock-wrap");
  383. CodeMirror.on(lock, "click", function() { setScrollLock(dv, !dv.lockScroll); });
  384. var gapElts = [lockWrap];
  385. if (dv.mv.options.revertButtons !== false) {
  386. dv.copyButtons = elt("div", null, "CodeMirror-merge-copybuttons-" + dv.type);
  387. CodeMirror.on(dv.copyButtons, "click", function(e) {
  388. var node = e.target || e.srcElement;
  389. if (!node.chunk) return;
  390. if (node.className == "CodeMirror-merge-copy-reverse") {
  391. copyChunk(dv, dv.orig, dv.edit, node.chunk);
  392. return;
  393. }
  394. copyChunk(dv, dv.edit, dv.orig, node.chunk);
  395. });
  396. gapElts.unshift(dv.copyButtons);
  397. }
  398. if (dv.mv.options.connect != "align") {
  399. var svg = document.createElementNS && document.createElementNS(svgNS, "svg");
  400. if (svg && !svg.createSVGRect) svg = null;
  401. dv.svg = svg;
  402. if (svg) gapElts.push(svg);
  403. }
  404. return dv.gap = elt("div", gapElts, "CodeMirror-merge-gap");
  405. }
  406. MergeView.prototype = {
  407. constuctor: MergeView,
  408. editor: function() { return this.edit; },
  409. rightOriginal: function() { return this.right && this.right.orig; },
  410. leftOriginal: function() { return this.left && this.left.orig; },
  411. setShowDifferences: function(val) {
  412. if (this.right) this.right.setShowDifferences(val);
  413. if (this.left) this.left.setShowDifferences(val);
  414. },
  415. rightChunks: function() {
  416. return this.right && getChunks(this.right);
  417. },
  418. leftChunks: function() {
  419. return this.left && getChunks(this.left);
  420. }
  421. };
  422. function asString(obj) {
  423. if (typeof obj == "string") return obj;
  424. else return obj.getValue();
  425. }
  426. // Operations on diffs
  427. var dmp = new diff_match_patch();
  428. function getDiff(a, b) {
  429. var diff = dmp.diff_main(a, b);
  430. dmp.diff_cleanupSemantic(diff);
  431. // The library sometimes leaves in empty parts, which confuse the algorithm
  432. for (var i = 0; i < diff.length; ++i) {
  433. var part = diff[i];
  434. if (!part[1]) {
  435. diff.splice(i--, 1);
  436. } else if (i && diff[i - 1][0] == part[0]) {
  437. diff.splice(i--, 1);
  438. diff[i][1] += part[1];
  439. }
  440. }
  441. return diff;
  442. }
  443. function iterateChunks(diff, f) {
  444. var startEdit = 0, startOrig = 0;
  445. var edit = Pos(0, 0), orig = Pos(0, 0);
  446. for (var i = 0; i < diff.length; ++i) {
  447. var part = diff[i], tp = part[0];
  448. if (tp == DIFF_EQUAL) {
  449. var startOff = startOfLineClean(diff, i) ? 0 : 1;
  450. var cleanFromEdit = edit.line + startOff, cleanFromOrig = orig.line + startOff;
  451. moveOver(edit, part[1], null, orig);
  452. var endOff = endOfLineClean(diff, i) ? 1 : 0;
  453. var cleanToEdit = edit.line + endOff, cleanToOrig = orig.line + endOff;
  454. if (cleanToEdit > cleanFromEdit) {
  455. if (i) f(startOrig, cleanFromOrig, startEdit, cleanFromEdit);
  456. startEdit = cleanToEdit; startOrig = cleanToOrig;
  457. }
  458. } else {
  459. moveOver(tp == DIFF_INSERT ? edit : orig, part[1]);
  460. }
  461. }
  462. if (startEdit <= edit.line || startOrig <= orig.line)
  463. f(startOrig, orig.line + 1, startEdit, edit.line + 1);
  464. }
  465. function getChunks(dv) {
  466. ensureDiff(dv);
  467. var collect = [];
  468. iterateChunks(dv.diff, function(topOrig, botOrig, topEdit, botEdit) {
  469. collect.push({origFrom: topOrig, origTo: botOrig,
  470. editFrom: topEdit, editTo: botEdit});
  471. });
  472. return collect;
  473. }
  474. function endOfLineClean(diff, i) {
  475. if (i == diff.length - 1) return true;
  476. var next = diff[i + 1][1];
  477. if (next.length == 1 || next.charCodeAt(0) != 10) return false;
  478. if (i == diff.length - 2) return true;
  479. next = diff[i + 2][1];
  480. return next.length > 1 && next.charCodeAt(0) == 10;
  481. }
  482. function startOfLineClean(diff, i) {
  483. if (i == 0) return true;
  484. var last = diff[i - 1][1];
  485. if (last.charCodeAt(last.length - 1) != 10) return false;
  486. if (i == 1) return true;
  487. last = diff[i - 2][1];
  488. return last.charCodeAt(last.length - 1) == 10;
  489. }
  490. function chunkBoundariesAround(diff, n, nInEdit) {
  491. var beforeE, afterE, beforeO, afterO;
  492. iterateChunks(diff, function(fromOrig, toOrig, fromEdit, toEdit) {
  493. var fromLocal = nInEdit ? fromEdit : fromOrig;
  494. var toLocal = nInEdit ? toEdit : toOrig;
  495. if (afterE == null) {
  496. if (fromLocal > n) { afterE = fromEdit; afterO = fromOrig; }
  497. else if (toLocal > n) { afterE = toEdit; afterO = toOrig; }
  498. }
  499. if (toLocal <= n) { beforeE = toEdit; beforeO = toOrig; }
  500. else if (fromLocal <= n) { beforeE = fromEdit; beforeO = fromOrig; }
  501. });
  502. return {edit: {before: beforeE, after: afterE}, orig: {before: beforeO, after: afterO}};
  503. }
  504. function collapseSingle(cm, from, to) {
  505. cm.addLineClass(from, "wrap", "CodeMirror-merge-collapsed-line");
  506. var widget = document.createElement("span");
  507. widget.className = "CodeMirror-merge-collapsed-widget";
  508. widget.title = "Identical text collapsed. Click to expand.";
  509. var mark = cm.markText(Pos(from, 0), Pos(to - 1), {
  510. inclusiveLeft: true,
  511. inclusiveRight: true,
  512. replacedWith: widget,
  513. clearOnEnter: true
  514. });
  515. function clear() {
  516. mark.clear();
  517. cm.removeLineClass(from, "wrap", "CodeMirror-merge-collapsed-line");
  518. }
  519. widget.addEventListener("click", clear);
  520. return {mark: mark, clear: clear};
  521. }
  522. function collapseStretch(dv, origStart, editStart, size) {
  523. var mOrig = collapseSingle(dv.orig, origStart, origStart + size);
  524. var mEdit = collapseSingle(dv.edit, editStart, editStart + size);
  525. mOrig.mark.on("clear", function() { mEdit.clear(); });
  526. mEdit.mark.on("clear", function() { mOrig.clear(); });
  527. }
  528. function collapseIdenticalStretches(dv, margin) {
  529. if (typeof margin != "number") margin = 2;
  530. var lastOrig = dv.orig.firstLine(), lastEdit = dv.edit.firstLine();
  531. iterateChunks(dv.diff, function(topOrig, botOrig, _topEdit, botEdit) {
  532. var identicalSize = topOrig - margin - lastOrig;
  533. if (identicalSize > margin)
  534. collapseStretch(dv, lastOrig, lastEdit, identicalSize);
  535. lastOrig = botOrig + margin; lastEdit = botEdit + margin;
  536. });
  537. var bottomSize = dv.orig.lastLine() + 1 - lastOrig;
  538. if (bottomSize > margin)
  539. collapseStretch(dv, lastOrig, lastEdit, bottomSize);
  540. }
  541. // General utilities
  542. function elt(tag, content, className, style) {
  543. var e = document.createElement(tag);
  544. if (className) e.className = className;
  545. if (style) e.style.cssText = style;
  546. if (typeof content == "string") e.appendChild(document.createTextNode(content));
  547. else if (content) for (var i = 0; i < content.length; ++i) e.appendChild(content[i]);
  548. return e;
  549. }
  550. function clear(node) {
  551. for (var count = node.childNodes.length; count > 0; --count)
  552. node.removeChild(node.firstChild);
  553. }
  554. function attrs(elt) {
  555. for (var i = 1; i < arguments.length; i += 2)
  556. elt.setAttribute(arguments[i], arguments[i+1]);
  557. }
  558. function copyObj(obj, target) {
  559. if (!target) target = {};
  560. for (var prop in obj) if (obj.hasOwnProperty(prop)) target[prop] = obj[prop];
  561. return target;
  562. }
  563. function moveOver(pos, str, copy, other) {
  564. var out = copy ? Pos(pos.line, pos.ch) : pos, at = 0;
  565. for (;;) {
  566. var nl = str.indexOf("\n", at);
  567. if (nl == -1) break;
  568. ++out.line;
  569. if (other) ++other.line;
  570. at = nl + 1;
  571. }
  572. out.ch = (at ? 0 : out.ch) + (str.length - at);
  573. if (other) other.ch = (at ? 0 : other.ch) + (str.length - at);
  574. return out;
  575. }
  576. function posMin(a, b) { return (a.line - b.line || a.ch - b.ch) < 0 ? a : b; }
  577. function posMax(a, b) { return (a.line - b.line || a.ch - b.ch) > 0 ? a : b; }
  578. function posEq(a, b) { return a.line == b.line && a.ch == b.ch; }
  579. });