本站源代码
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.

106 lines
3.8KB

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