Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

60 рядки
2.1KB

  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.registerGlobalHelper("fold", "comment", function(mode) {
  13. return mode.blockCommentStart && mode.blockCommentEnd;
  14. }, function(cm, start) {
  15. var mode = cm.getModeAt(start), startToken = mode.blockCommentStart, endToken = mode.blockCommentEnd;
  16. if (!startToken || !endToken) return;
  17. var line = start.line, lineText = cm.getLine(line);
  18. var startCh;
  19. for (var at = start.ch, pass = 0;;) {
  20. var found = at <= 0 ? -1 : lineText.lastIndexOf(startToken, at - 1);
  21. if (found == -1) {
  22. if (pass == 1) return;
  23. pass = 1;
  24. at = lineText.length;
  25. continue;
  26. }
  27. if (pass == 1 && found < start.ch) return;
  28. if (/comment/.test(cm.getTokenTypeAt(CodeMirror.Pos(line, found + 1))) &&
  29. (found == 0 || lineText.slice(found - endToken.length, found) == endToken ||
  30. !/comment/.test(cm.getTokenTypeAt(CodeMirror.Pos(line, found))))) {
  31. startCh = found + startToken.length;
  32. break;
  33. }
  34. at = found - 1;
  35. }
  36. var depth = 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 (pos == nextOpen) ++depth;
  46. else if (!--depth) { end = i; endCh = pos; break outer; }
  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. });