michael@0: CodeMirror.registerHelper("fold", "brace", function(cm, start) { michael@0: var line = start.line, lineText = cm.getLine(line); michael@0: var startCh, tokenType; michael@0: michael@0: function findOpening(openCh) { michael@0: for (var at = start.ch, pass = 0;;) { michael@0: var found = at <= 0 ? -1 : lineText.lastIndexOf(openCh, at - 1); michael@0: if (found == -1) { michael@0: if (pass == 1) break; michael@0: pass = 1; michael@0: at = lineText.length; michael@0: continue; michael@0: } michael@0: if (pass == 1 && found < start.ch) break; michael@0: tokenType = cm.getTokenTypeAt(CodeMirror.Pos(line, found + 1)); michael@0: if (!/^(comment|string)/.test(tokenType)) return found + 1; michael@0: at = found - 1; michael@0: } michael@0: } michael@0: michael@0: var startToken = "{", endToken = "}", startCh = findOpening("{"); michael@0: if (startCh == null) { michael@0: startToken = "[", endToken = "]"; michael@0: startCh = findOpening("["); michael@0: } michael@0: michael@0: if (startCh == null) return; michael@0: var count = 1, lastLine = cm.lastLine(), end, endCh; michael@0: outer: for (var i = line; i <= lastLine; ++i) { michael@0: var text = cm.getLine(i), pos = i == line ? startCh : 0; michael@0: for (;;) { michael@0: var nextOpen = text.indexOf(startToken, pos), nextClose = text.indexOf(endToken, pos); michael@0: if (nextOpen < 0) nextOpen = text.length; michael@0: if (nextClose < 0) nextClose = text.length; michael@0: pos = Math.min(nextOpen, nextClose); michael@0: if (pos == text.length) break; michael@0: if (cm.getTokenTypeAt(CodeMirror.Pos(i, pos + 1)) == tokenType) { michael@0: if (pos == nextOpen) ++count; michael@0: else if (!--count) { end = i; endCh = pos; break outer; } michael@0: } michael@0: ++pos; michael@0: } michael@0: } michael@0: if (end == null || line == end && endCh == startCh) return; michael@0: return {from: CodeMirror.Pos(line, startCh), michael@0: to: CodeMirror.Pos(end, endCh)}; michael@0: }); michael@0: CodeMirror.braceRangeFinder = CodeMirror.fold.brace; // deprecated michael@0: michael@0: CodeMirror.registerHelper("fold", "import", function(cm, start) { michael@0: function hasImport(line) { michael@0: if (line < cm.firstLine() || line > cm.lastLine()) return null; michael@0: var start = cm.getTokenAt(CodeMirror.Pos(line, 1)); michael@0: if (!/\S/.test(start.string)) start = cm.getTokenAt(CodeMirror.Pos(line, start.end + 1)); michael@0: if (start.type != "keyword" || start.string != "import") return null; michael@0: // Now find closing semicolon, return its position michael@0: for (var i = line, e = Math.min(cm.lastLine(), line + 10); i <= e; ++i) { michael@0: var text = cm.getLine(i), semi = text.indexOf(";"); michael@0: if (semi != -1) return {startCh: start.end, end: CodeMirror.Pos(i, semi)}; michael@0: } michael@0: } michael@0: michael@0: var start = start.line, has = hasImport(start), prev; michael@0: if (!has || hasImport(start - 1) || ((prev = hasImport(start - 2)) && prev.end.line == start - 1)) michael@0: return null; michael@0: for (var end = has.end;;) { michael@0: var next = hasImport(end.line + 1); michael@0: if (next == null) break; michael@0: end = next.end; michael@0: } michael@0: return {from: cm.clipPos(CodeMirror.Pos(start, has.startCh + 1)), to: end}; michael@0: }); michael@0: CodeMirror.importRangeFinder = CodeMirror.fold["import"]; // deprecated michael@0: michael@0: CodeMirror.registerHelper("fold", "include", function(cm, start) { michael@0: function hasInclude(line) { michael@0: if (line < cm.firstLine() || line > cm.lastLine()) return null; michael@0: var start = cm.getTokenAt(CodeMirror.Pos(line, 1)); michael@0: if (!/\S/.test(start.string)) start = cm.getTokenAt(CodeMirror.Pos(line, start.end + 1)); michael@0: if (start.type == "meta" && start.string.slice(0, 8) == "#include") return start.start + 8; michael@0: } michael@0: michael@0: var start = start.line, has = hasInclude(start); michael@0: if (has == null || hasInclude(start - 1) != null) return null; michael@0: for (var end = start;;) { michael@0: var next = hasInclude(end + 1); michael@0: if (next == null) break; michael@0: ++end; michael@0: } michael@0: return {from: CodeMirror.Pos(start, has + 1), michael@0: to: cm.clipPos(CodeMirror.Pos(end))}; michael@0: }); michael@0: CodeMirror.includeRangeFinder = CodeMirror.fold.include; // deprecated