codemirror3, thanks Roberto
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
CodeMirror.braceRangeFinder = function(cm, start) {
|
||||
var line = start.line, lineText = cm.getLine(line);
|
||||
var at = lineText.length, startChar, tokenType;
|
||||
for (; at > 0;) {
|
||||
var found = lineText.lastIndexOf("{", at);
|
||||
var startToken = '{', endToken = '}';
|
||||
if (found < start.ch) {
|
||||
found = lineText.lastIndexOf("[", at);
|
||||
if (found < start.ch) break;
|
||||
startToken = '['; endToken = ']';
|
||||
}
|
||||
|
||||
tokenType = cm.getTokenAt(CodeMirror.Pos(line, found + 1)).type;
|
||||
if (!/^(comment|string)/.test(tokenType)) { startChar = found; break; }
|
||||
at = found - 1;
|
||||
}
|
||||
if (startChar == null || lineText.lastIndexOf(startToken) > startChar) return;
|
||||
var count = 1, lastLine = cm.lineCount(), end, endCh;
|
||||
outer: for (var i = line + 1; i < lastLine; ++i) {
|
||||
var text = cm.getLine(i), pos = 0;
|
||||
for (;;) {
|
||||
var nextOpen = text.indexOf(startToken, pos), nextClose = text.indexOf(endToken, pos);
|
||||
if (nextOpen < 0) nextOpen = text.length;
|
||||
if (nextClose < 0) nextClose = text.length;
|
||||
pos = Math.min(nextOpen, nextClose);
|
||||
if (pos == text.length) break;
|
||||
if (cm.getTokenAt(CodeMirror.Pos(i, pos + 1)).type == tokenType) {
|
||||
if (pos == nextOpen) ++count;
|
||||
else if (!--count) { end = i; endCh = pos; break outer; }
|
||||
}
|
||||
++pos;
|
||||
}
|
||||
}
|
||||
if (end == null || end == line + 1) return;
|
||||
return {from: CodeMirror.Pos(line, startChar + 1),
|
||||
to: CodeMirror.Pos(end, endCh)};
|
||||
};
|
||||
@@ -0,0 +1,32 @@
|
||||
CodeMirror.newFoldFunction = function(rangeFinder, widget) {
|
||||
if (widget == null) widget = "\u2194";
|
||||
if (typeof widget == "string") {
|
||||
var text = document.createTextNode(widget);
|
||||
widget = document.createElement("span");
|
||||
widget.appendChild(text);
|
||||
widget.className = "CodeMirror-foldmarker";
|
||||
}
|
||||
|
||||
return function(cm, pos) {
|
||||
if (typeof pos == "number") pos = CodeMirror.Pos(pos, 0);
|
||||
var range = rangeFinder(cm, pos);
|
||||
if (!range) return;
|
||||
|
||||
var present = cm.findMarksAt(range.from), cleared = 0;
|
||||
for (var i = 0; i < present.length; ++i) {
|
||||
if (present[i].__isFold) {
|
||||
++cleared;
|
||||
present[i].clear();
|
||||
}
|
||||
}
|
||||
if (cleared) return;
|
||||
|
||||
var myWidget = widget.cloneNode(true);
|
||||
CodeMirror.on(myWidget, "mousedown", function() {myRange.clear();});
|
||||
var myRange = cm.markText(range.from, range.to, {
|
||||
replacedWith: myWidget,
|
||||
clearOnEnter: true,
|
||||
__isFold: true
|
||||
});
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,11 @@
|
||||
CodeMirror.indentRangeFinder = function(cm, start) {
|
||||
var tabSize = cm.getOption("tabSize"), firstLine = cm.getLine(start.line);
|
||||
var myIndent = CodeMirror.countColumn(firstLine, null, tabSize);
|
||||
for (var i = start.line + 1, end = cm.lineCount(); i < end; ++i) {
|
||||
var curLine = cm.getLine(i);
|
||||
if (CodeMirror.countColumn(curLine, null, tabSize) < myIndent &&
|
||||
CodeMirror.countColumn(cm.getLine(i-1), null, tabSize) > myIndent)
|
||||
return {from: CodeMirror.Pos(start.line, firstLine.length),
|
||||
to: CodeMirror.Pos(i, curLine.length)};
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,64 @@
|
||||
CodeMirror.tagRangeFinder = (function() {
|
||||
var nameStartChar = "A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD";
|
||||
var nameChar = nameStartChar + "\-\:\.0-9\\u00B7\\u0300-\\u036F\\u203F-\\u2040";
|
||||
var xmlTagStart = new RegExp("<(/?)([" + nameStartChar + "][" + nameChar + "]*)", "g");
|
||||
|
||||
return function(cm, start) {
|
||||
var line = start.line, ch = start.ch, lineText = cm.getLine(line);
|
||||
|
||||
function nextLine() {
|
||||
if (line >= cm.lastLine()) return;
|
||||
ch = 0;
|
||||
lineText = cm.getLine(++line);
|
||||
return true;
|
||||
}
|
||||
function toTagEnd() {
|
||||
for (;;) {
|
||||
var gt = lineText.indexOf(">", ch);
|
||||
if (gt == -1) { if (nextLine()) continue; else return; }
|
||||
var lastSlash = lineText.lastIndexOf("/", gt);
|
||||
var selfClose = lastSlash > -1 && /^\s*$/.test(lineText.slice(lastSlash + 1, gt));
|
||||
ch = gt + 1;
|
||||
return selfClose ? "selfClose" : "regular";
|
||||
}
|
||||
}
|
||||
function toNextTag() {
|
||||
for (;;) {
|
||||
xmlTagStart.lastIndex = ch;
|
||||
var found = xmlTagStart.exec(lineText);
|
||||
if (!found) { if (nextLine()) continue; else return; }
|
||||
ch = found.index + found[0].length;
|
||||
return found;
|
||||
}
|
||||
}
|
||||
|
||||
var stack = [], startCh;
|
||||
for (;;) {
|
||||
var openTag = toNextTag(), end;
|
||||
if (!openTag || line != start.line || !(end = toTagEnd())) return;
|
||||
if (!openTag[1] && end != "selfClose") {
|
||||
stack.push(openTag[2]);
|
||||
startCh = ch;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
var next = toNextTag(), end, tagLine = line, tagCh = ch - (next ? next[0].length : 0);
|
||||
if (!next || !(end = toTagEnd())) return;
|
||||
if (end == "selfClose") continue;
|
||||
if (next[1]) { // closing tag
|
||||
for (var i = stack.length - 1; i >= 0; --i) if (stack[i] == next[2]) {
|
||||
stack.length = i;
|
||||
break;
|
||||
}
|
||||
if (!stack.length) return {
|
||||
from: CodeMirror.Pos(start.line, startCh),
|
||||
to: CodeMirror.Pos(tagLine, tagCh)
|
||||
};
|
||||
} else { // opening tag
|
||||
stack.push(next[2]);
|
||||
}
|
||||
}
|
||||
};
|
||||
})();
|
||||
Reference in New Issue
Block a user