brace-fold.js 3.7 KB

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