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

146 lines
5.4KB

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