browser/devtools/sourceeditor/codemirror/fold/foldcode.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 (function() {
michael@0 2 "use strict";
michael@0 3
michael@0 4 function doFold(cm, pos, options, force) {
michael@0 5 var finder = options && (options.call ? options : options.rangeFinder);
michael@0 6 if (!finder) finder = CodeMirror.fold.auto;
michael@0 7 if (typeof pos == "number") pos = CodeMirror.Pos(pos, 0);
michael@0 8 var minSize = options && options.minFoldSize || 0;
michael@0 9
michael@0 10 function getRange(allowFolded) {
michael@0 11 var range = finder(cm, pos);
michael@0 12 if (!range || range.to.line - range.from.line < minSize) return null;
michael@0 13 var marks = cm.findMarksAt(range.from);
michael@0 14 for (var i = 0; i < marks.length; ++i) {
michael@0 15 if (marks[i].__isFold && force !== "fold") {
michael@0 16 if (!allowFolded) return null;
michael@0 17 range.cleared = true;
michael@0 18 marks[i].clear();
michael@0 19 }
michael@0 20 }
michael@0 21 return range;
michael@0 22 }
michael@0 23
michael@0 24 var range = getRange(true);
michael@0 25 if (options && options.scanUp) while (!range && pos.line > cm.firstLine()) {
michael@0 26 pos = CodeMirror.Pos(pos.line - 1, 0);
michael@0 27 range = getRange(false);
michael@0 28 }
michael@0 29 if (!range || range.cleared || force === "unfold") return;
michael@0 30
michael@0 31 var myWidget = makeWidget(options);
michael@0 32 CodeMirror.on(myWidget, "mousedown", function() { myRange.clear(); });
michael@0 33 var myRange = cm.markText(range.from, range.to, {
michael@0 34 replacedWith: myWidget,
michael@0 35 clearOnEnter: true,
michael@0 36 __isFold: true
michael@0 37 });
michael@0 38 myRange.on("clear", function(from, to) {
michael@0 39 CodeMirror.signal(cm, "unfold", cm, from, to);
michael@0 40 });
michael@0 41 CodeMirror.signal(cm, "fold", cm, range.from, range.to);
michael@0 42 }
michael@0 43
michael@0 44 function makeWidget(options) {
michael@0 45 var widget = (options && options.widget) || "\u2194";
michael@0 46 if (typeof widget == "string") {
michael@0 47 var text = document.createTextNode(widget);
michael@0 48 widget = document.createElement("span");
michael@0 49 widget.appendChild(text);
michael@0 50 widget.className = "CodeMirror-foldmarker";
michael@0 51 }
michael@0 52 return widget;
michael@0 53 }
michael@0 54
michael@0 55 // Clumsy backwards-compatible interface
michael@0 56 CodeMirror.newFoldFunction = function(rangeFinder, widget) {
michael@0 57 return function(cm, pos) { doFold(cm, pos, {rangeFinder: rangeFinder, widget: widget}); };
michael@0 58 };
michael@0 59
michael@0 60 // New-style interface
michael@0 61 CodeMirror.defineExtension("foldCode", function(pos, options, force) {
michael@0 62 doFold(this, pos, options, force);
michael@0 63 });
michael@0 64
michael@0 65 CodeMirror.commands.fold = function(cm) {
michael@0 66 cm.foldCode(cm.getCursor());
michael@0 67 };
michael@0 68
michael@0 69 CodeMirror.registerHelper("fold", "combine", function() {
michael@0 70 var funcs = Array.prototype.slice.call(arguments, 0);
michael@0 71 return function(cm, start) {
michael@0 72 for (var i = 0; i < funcs.length; ++i) {
michael@0 73 var found = funcs[i](cm, start);
michael@0 74 if (found) return found;
michael@0 75 }
michael@0 76 };
michael@0 77 });
michael@0 78
michael@0 79 CodeMirror.registerHelper("fold", "auto", function(cm, start) {
michael@0 80 var helpers = cm.getHelpers(start, "fold");
michael@0 81 for (var i = 0; i < helpers.length; i++) {
michael@0 82 var cur = helpers[i](cm, start);
michael@0 83 if (cur) return cur;
michael@0 84 }
michael@0 85 });
michael@0 86 })();

mercurial