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.

hardwrap.js 6.0KB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. var Pos = CodeMirror.Pos;
  13. function findParagraph(cm, pos, options) {
  14. var startRE = options.paragraphStart || cm.getHelper(pos, "paragraphStart");
  15. for (var start = pos.line, first = cm.firstLine(); start > first; --start) {
  16. var line = cm.getLine(start);
  17. if (startRE && startRE.test(line)) break;
  18. if (!/\S/.test(line)) { ++start; break; }
  19. }
  20. var endRE = options.paragraphEnd || cm.getHelper(pos, "paragraphEnd");
  21. for (var end = pos.line + 1, last = cm.lastLine(); end <= last; ++end) {
  22. var line = cm.getLine(end);
  23. if (endRE && endRE.test(line)) { ++end; break; }
  24. if (!/\S/.test(line)) break;
  25. }
  26. return {from: start, to: end};
  27. }
  28. function findBreakPoint(text, column, wrapOn, killTrailingSpace, forceBreak) {
  29. var at = column
  30. while (at < text.length && text.charAt(at) == " ") at++
  31. for (; at > 0; --at)
  32. if (wrapOn.test(text.slice(at - 1, at + 1))) break;
  33. if (!forceBreak && at <= text.match(/^[ \t]*/)[0].length) {
  34. // didn't find a break point before column, in non-forceBreak mode try to
  35. // find one after 'column'.
  36. for (at = column + 1; at < text.length - 1; ++at) {
  37. if (wrapOn.test(text.slice(at - 1, at + 1))) break;
  38. }
  39. }
  40. for (var first = true;; first = false) {
  41. var endOfText = at;
  42. if (killTrailingSpace)
  43. while (text.charAt(endOfText - 1) == " ") --endOfText;
  44. if (endOfText == 0 && first) at = column;
  45. else return {from: endOfText, to: at};
  46. }
  47. }
  48. function wrapRange(cm, from, to, options) {
  49. from = cm.clipPos(from); to = cm.clipPos(to);
  50. var column = options.column || 80;
  51. var wrapOn = options.wrapOn || /\s\S|-[^\.\d]/;
  52. var forceBreak = options.forceBreak !== false;
  53. var killTrailing = options.killTrailingSpace !== false;
  54. var changes = [], curLine = "", curNo = from.line;
  55. var lines = cm.getRange(from, to, false);
  56. if (!lines.length) return null;
  57. var leadingSpace = lines[0].match(/^[ \t]*/)[0];
  58. if (leadingSpace.length >= column) column = leadingSpace.length + 1
  59. for (var i = 0; i < lines.length; ++i) {
  60. var text = lines[i], oldLen = curLine.length, spaceInserted = 0;
  61. if (curLine && text && !wrapOn.test(curLine.charAt(curLine.length - 1) + text.charAt(0))) {
  62. curLine += " ";
  63. spaceInserted = 1;
  64. }
  65. var spaceTrimmed = "";
  66. if (i) {
  67. spaceTrimmed = text.match(/^\s*/)[0];
  68. text = text.slice(spaceTrimmed.length);
  69. }
  70. curLine += text;
  71. if (i) {
  72. var firstBreak = curLine.length > column && leadingSpace == spaceTrimmed &&
  73. findBreakPoint(curLine, column, wrapOn, killTrailing, forceBreak);
  74. // If this isn't broken, or is broken at a different point, remove old break
  75. if (!firstBreak || firstBreak.from != oldLen || firstBreak.to != oldLen + spaceInserted) {
  76. changes.push({text: [spaceInserted ? " " : ""],
  77. from: Pos(curNo, oldLen),
  78. to: Pos(curNo + 1, spaceTrimmed.length)});
  79. } else {
  80. curLine = leadingSpace + text;
  81. ++curNo;
  82. }
  83. }
  84. while (curLine.length > column) {
  85. var bp = findBreakPoint(curLine, column, wrapOn, killTrailing, forceBreak);
  86. if (bp.from != bp.to ||
  87. forceBreak && leadingSpace !== curLine.slice(0, bp.to)) {
  88. changes.push({text: ["", leadingSpace],
  89. from: Pos(curNo, bp.from),
  90. to: Pos(curNo, bp.to)});
  91. curLine = leadingSpace + curLine.slice(bp.to);
  92. ++curNo;
  93. } else {
  94. break;
  95. }
  96. }
  97. }
  98. if (changes.length) cm.operation(function() {
  99. for (var i = 0; i < changes.length; ++i) {
  100. var change = changes[i];
  101. if (change.text || CodeMirror.cmpPos(change.from, change.to))
  102. cm.replaceRange(change.text, change.from, change.to);
  103. }
  104. });
  105. return changes.length ? {from: changes[0].from, to: CodeMirror.changeEnd(changes[changes.length - 1])} : null;
  106. }
  107. CodeMirror.defineExtension("wrapParagraph", function(pos, options) {
  108. options = options || {};
  109. if (!pos) pos = this.getCursor();
  110. var para = findParagraph(this, pos, options);
  111. return wrapRange(this, Pos(para.from, 0), Pos(para.to - 1), options);
  112. });
  113. CodeMirror.commands.wrapLines = function(cm) {
  114. cm.operation(function() {
  115. var ranges = cm.listSelections(), at = cm.lastLine() + 1;
  116. for (var i = ranges.length - 1; i >= 0; i--) {
  117. var range = ranges[i], span;
  118. if (range.empty()) {
  119. var para = findParagraph(cm, range.head, {});
  120. span = {from: Pos(para.from, 0), to: Pos(para.to - 1)};
  121. } else {
  122. span = {from: range.from(), to: range.to()};
  123. }
  124. if (span.to.line >= at) continue;
  125. at = span.from.line;
  126. wrapRange(cm, span.from, span.to, {});
  127. }
  128. });
  129. };
  130. CodeMirror.defineExtension("wrapRange", function(from, to, options) {
  131. return wrapRange(this, from, to, options || {});
  132. });
  133. CodeMirror.defineExtension("wrapParagraphsInRange", function(from, to, options) {
  134. options = options || {};
  135. var cm = this, paras = [];
  136. for (var line = from.line; line <= to.line;) {
  137. var para = findParagraph(cm, Pos(line, 0), options);
  138. paras.push(para);
  139. line = para.to;
  140. }
  141. var madeChange = false;
  142. if (paras.length) cm.operation(function() {
  143. for (var i = paras.length - 1; i >= 0; --i)
  144. madeChange = madeChange || wrapRange(cm, Pos(paras[i].from, 0), Pos(paras[i].to - 1), options);
  145. });
  146. return madeChange;
  147. });
  148. });