hardwrap.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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) {
  29. for (var at = column; at > 0; --at)
  30. if (wrapOn.test(text.slice(at - 1, at + 1))) break;
  31. if (at == 0) at = column;
  32. var endOfText = at;
  33. if (killTrailingSpace)
  34. while (text.charAt(endOfText - 1) == " ") --endOfText;
  35. return {from: endOfText, to: at};
  36. }
  37. function wrapRange(cm, from, to, options) {
  38. from = cm.clipPos(from); to = cm.clipPos(to);
  39. var column = options.column || 80;
  40. var wrapOn = options.wrapOn || /\s\S|-[^\.\d]/;
  41. var killTrailing = options.killTrailingSpace !== false;
  42. var changes = [], curLine = "", curNo = from.line;
  43. var lines = cm.getRange(from, to, false);
  44. if (!lines.length) return null;
  45. var leadingSpace = lines[0].match(/^[ \t]*/)[0];
  46. for (var i = 0; i < lines.length; ++i) {
  47. var text = lines[i], oldLen = curLine.length, spaceInserted = 0;
  48. if (curLine && text && !wrapOn.test(curLine.charAt(curLine.length - 1) + text.charAt(0))) {
  49. curLine += " ";
  50. spaceInserted = 1;
  51. }
  52. var spaceTrimmed = "";
  53. if (i) {
  54. spaceTrimmed = text.match(/^\s*/)[0];
  55. text = text.slice(spaceTrimmed.length);
  56. }
  57. curLine += text;
  58. if (i) {
  59. var firstBreak = curLine.length > column && leadingSpace == spaceTrimmed &&
  60. findBreakPoint(curLine, column, wrapOn, killTrailing);
  61. // If this isn't broken, or is broken at a different point, remove old break
  62. if (!firstBreak || firstBreak.from != oldLen || firstBreak.to != oldLen + spaceInserted) {
  63. changes.push({text: [spaceInserted ? " " : ""],
  64. from: Pos(curNo, oldLen),
  65. to: Pos(curNo + 1, spaceTrimmed.length)});
  66. } else {
  67. curLine = leadingSpace + text;
  68. ++curNo;
  69. }
  70. }
  71. while (curLine.length > column) {
  72. var bp = findBreakPoint(curLine, column, wrapOn, killTrailing);
  73. changes.push({text: ["", leadingSpace],
  74. from: Pos(curNo, bp.from),
  75. to: Pos(curNo, bp.to)});
  76. curLine = leadingSpace + curLine.slice(bp.to);
  77. ++curNo;
  78. }
  79. }
  80. if (changes.length) cm.operation(function() {
  81. for (var i = 0; i < changes.length; ++i) {
  82. var change = changes[i];
  83. cm.replaceRange(change.text, change.from, change.to);
  84. }
  85. });
  86. return changes.length ? {from: changes[0].from, to: CodeMirror.changeEnd(changes[changes.length - 1])} : null;
  87. }
  88. CodeMirror.defineExtension("wrapParagraph", function(pos, options) {
  89. options = options || {};
  90. if (!pos) pos = this.getCursor();
  91. var para = findParagraph(this, pos, options);
  92. return wrapRange(this, Pos(para.from, 0), Pos(para.to - 1), options);
  93. });
  94. CodeMirror.commands.wrapLines = function(cm) {
  95. cm.operation(function() {
  96. var ranges = cm.listSelections(), at = cm.lastLine() + 1;
  97. for (var i = ranges.length - 1; i >= 0; i--) {
  98. var range = ranges[i], span;
  99. if (range.empty()) {
  100. var para = findParagraph(cm, range.head, {});
  101. span = {from: Pos(para.from, 0), to: Pos(para.to - 1)};
  102. } else {
  103. span = {from: range.from(), to: range.to()};
  104. }
  105. if (span.to.line >= at) continue;
  106. at = span.from.line;
  107. wrapRange(cm, span.from, span.to, {});
  108. }
  109. });
  110. };
  111. CodeMirror.defineExtension("wrapRange", function(from, to, options) {
  112. return wrapRange(this, from, to, options || {});
  113. });
  114. CodeMirror.defineExtension("wrapParagraphsInRange", function(from, to, options) {
  115. options = options || {};
  116. var cm = this, paras = [];
  117. for (var line = from.line; line <= to.line;) {
  118. var para = findParagraph(cm, Pos(line, 0), options);
  119. paras.push(para);
  120. line = para.to;
  121. }
  122. var madeChange = false;
  123. if (paras.length) cm.operation(function() {
  124. for (var i = paras.length - 1; i >= 0; --i)
  125. madeChange = madeChange || wrapRange(cm, Pos(paras[i].from, 0), Pos(paras[i].to - 1), options);
  126. });
  127. return madeChange;
  128. });
  129. });