brace-fold.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. CodeMirror.registerHelper("fold", "brace", function(cm, start) {
  2. var line = start.line, lineText = cm.getLine(line);
  3. var startCh, tokenType;
  4. function findOpening(openCh) {
  5. for (var at = start.ch, pass = 0;;) {
  6. var found = at <= 0 ? -1 : lineText.lastIndexOf(openCh, at - 1);
  7. if (found == -1) {
  8. if (pass == 1) break;
  9. pass = 1;
  10. at = lineText.length;
  11. continue;
  12. }
  13. if (pass == 1 && found < start.ch) break;
  14. tokenType = cm.getTokenTypeAt(CodeMirror.Pos(line, found + 1));
  15. if (!/^(comment|string)/.test(tokenType)) return found + 1;
  16. at = found - 1;
  17. }
  18. }
  19. var startToken = "{", endToken = "}", startCh = findOpening("{");
  20. if (startCh == null) {
  21. startToken = "[", endToken = "]";
  22. startCh = findOpening("[");
  23. }
  24. if (startCh == null) return;
  25. var count = 1, lastLine = cm.lastLine(), end, endCh;
  26. outer: for (var i = line; i <= lastLine; ++i) {
  27. var text = cm.getLine(i), pos = i == line ? startCh : 0;
  28. for (;;) {
  29. var nextOpen = text.indexOf(startToken, pos), nextClose = text.indexOf(endToken, pos);
  30. if (nextOpen < 0) nextOpen = text.length;
  31. if (nextClose < 0) nextClose = text.length;
  32. pos = Math.min(nextOpen, nextClose);
  33. if (pos == text.length) break;
  34. if (cm.getTokenTypeAt(CodeMirror.Pos(i, pos + 1)) == tokenType) {
  35. if (pos == nextOpen) ++count;
  36. else if (!--count) { end = i; endCh = pos; break outer; }
  37. }
  38. ++pos;
  39. }
  40. }
  41. if (end == null || line == end && endCh == startCh) return;
  42. return {from: CodeMirror.Pos(line, startCh),
  43. to: CodeMirror.Pos(end, endCh)};
  44. });
  45. CodeMirror.braceRangeFinder = CodeMirror.fold.brace; // deprecated
  46. CodeMirror.registerHelper("fold", "import", function(cm, start) {
  47. function hasImport(line) {
  48. if (line < cm.firstLine() || line > cm.lastLine()) return null;
  49. var start = cm.getTokenAt(CodeMirror.Pos(line, 1));
  50. if (!/\S/.test(start.string)) start = cm.getTokenAt(CodeMirror.Pos(line, start.end + 1));
  51. if (start.type != "keyword" || start.string != "import") return null;
  52. // Now find closing semicolon, return its position
  53. for (var i = line, e = Math.min(cm.lastLine(), line + 10); i <= e; ++i) {
  54. var text = cm.getLine(i), semi = text.indexOf(";");
  55. if (semi != -1) return {startCh: start.end, end: CodeMirror.Pos(i, semi)};
  56. }
  57. }
  58. var start = start.line, has = hasImport(start), prev;
  59. if (!has || hasImport(start - 1) || ((prev = hasImport(start - 2)) && prev.end.line == start - 1))
  60. return null;
  61. for (var end = has.end;;) {
  62. var next = hasImport(end.line + 1);
  63. if (next == null) break;
  64. end = next.end;
  65. }
  66. return {from: cm.clipPos(CodeMirror.Pos(start, has.startCh + 1)), to: end};
  67. });
  68. CodeMirror.importRangeFinder = CodeMirror.fold["import"]; // deprecated
  69. CodeMirror.registerHelper("fold", "include", function(cm, start) {
  70. function hasInclude(line) {
  71. if (line < cm.firstLine() || line > cm.lastLine()) return null;
  72. var start = cm.getTokenAt(CodeMirror.Pos(line, 1));
  73. if (!/\S/.test(start.string)) start = cm.getTokenAt(CodeMirror.Pos(line, start.end + 1));
  74. if (start.type == "meta" && start.string.slice(0, 8) == "#include") return start.start + 8;
  75. }
  76. var start = start.line, has = hasInclude(start);
  77. if (has == null || hasInclude(start - 1) != null) return null;
  78. for (var end = start;;) {
  79. var next = hasInclude(end + 1);
  80. if (next == null) break;
  81. ++end;
  82. }
  83. return {from: CodeMirror.Pos(start, has + 1),
  84. to: cm.clipPos(CodeMirror.Pos(end))};
  85. });
  86. CodeMirror.includeRangeFinder = CodeMirror.fold.include; // deprecated