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.

48 lines
1.6 KiB

7 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/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 lineIndent(cm, lineNo) {
  13. var text = cm.getLine(lineNo)
  14. var spaceTo = text.search(/\S/)
  15. if (spaceTo == -1 || /\bcomment\b/.test(cm.getTokenTypeAt(CodeMirror.Pos(lineNo, spaceTo + 1))))
  16. return -1
  17. return CodeMirror.countColumn(text, null, cm.getOption("tabSize"))
  18. }
  19. CodeMirror.registerHelper("fold", "indent", function(cm, start) {
  20. var myIndent = lineIndent(cm, start.line)
  21. if (myIndent < 0) return
  22. var lastLineInFold = null
  23. // Go through lines until we find a line that definitely doesn't belong in
  24. // the block we're folding, or to the end.
  25. for (var i = start.line + 1, end = cm.lastLine(); i <= end; ++i) {
  26. var indent = lineIndent(cm, i)
  27. if (indent == -1) {
  28. } else if (indent > myIndent) {
  29. // Lines with a greater indent are considered part of the block.
  30. lastLineInFold = i;
  31. } else {
  32. // If this line has non-space, non-comment content, and is
  33. // indented less or equal to the start line, it is the start of
  34. // another block.
  35. break;
  36. }
  37. }
  38. if (lastLineInFold) return {
  39. from: CodeMirror.Pos(start.line, cm.getLine(start.line).length),
  40. to: CodeMirror.Pos(lastLineInFold, cm.getLine(lastLineInFold).length)
  41. };
  42. });
  43. });