You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.4KB

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