1
0

brace-fold.js 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. CodeMirror.braceRangeFinder = function(cm, start) {
  2. var line = start.line, lineText = cm.getLine(line);
  3. var at = lineText.length, startChar, tokenType;
  4. for (;;) {
  5. var found = lineText.lastIndexOf("{", at);
  6. if (found < start.ch) break;
  7. tokenType = cm.getTokenAt(CodeMirror.Pos(line, found + 1)).type;
  8. if (!/^(comment|string)/.test(tokenType)) { startChar = found; break; }
  9. at = found - 1;
  10. }
  11. if (startChar == null || lineText.lastIndexOf("}") > startChar) return;
  12. var count = 1, lastLine = cm.lineCount(), end, endCh;
  13. outer: for (var i = line + 1; i < lastLine; ++i) {
  14. var text = cm.getLine(i), pos = 0;
  15. for (;;) {
  16. var nextOpen = text.indexOf("{", pos), nextClose = text.indexOf("}", pos);
  17. if (nextOpen < 0) nextOpen = text.length;
  18. if (nextClose < 0) nextClose = text.length;
  19. pos = Math.min(nextOpen, nextClose);
  20. if (pos == text.length) break;
  21. if (cm.getTokenAt(CodeMirror.Pos(i, pos + 1)).type == tokenType) {
  22. if (pos == nextOpen) ++count;
  23. else if (!--count) { end = i; endCh = pos; break outer; }
  24. }
  25. ++pos;
  26. }
  27. }
  28. if (end == null || end == line + 1) return;
  29. return {from: CodeMirror.Pos(line, startChar + 1),
  30. to: CodeMirror.Pos(end, endCh)};
  31. };