1
0

brace-fold.js 1.4 KB

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