ext-hardwrap.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. define("ace/ext/hardwrap",["require","exports","module","ace/range","ace/editor","ace/config"], function(require, exports, module) {
  2. "use strict";
  3. var Range = require("../range").Range;
  4. function hardWrap(editor, options) {
  5. var max = options.column || editor.getOption("printMarginColumn");
  6. var allowMerge = options.allowMerge != false;
  7. var row = Math.min(options.startRow, options.endRow);
  8. var endRow = Math.max(options.startRow, options.endRow);
  9. var session = editor.session;
  10. while (row <= endRow) {
  11. var line = session.getLine(row);
  12. if (line.length > max) {
  13. var space = findSpace(line, max, 5);
  14. if (space) {
  15. var indentation = /^\s*/.exec(line)[0];
  16. session.replace(new Range(row,space.start,row,space.end), "\n" + indentation);
  17. }
  18. endRow++;
  19. } else if (allowMerge && /\S/.test(line) && row != endRow) {
  20. var nextLine = session.getLine(row + 1);
  21. if (nextLine && /\S/.test(nextLine)) {
  22. var trimmedLine = line.replace(/\s+$/, "");
  23. var trimmedNextLine = nextLine.replace(/^\s+/, "");
  24. var mergedLine = trimmedLine + " " + trimmedNextLine;
  25. var space = findSpace(mergedLine, max, 5);
  26. if (space && space.start > trimmedLine.length || mergedLine.length < max) {
  27. var replaceRange = new Range(row,trimmedLine.length,row + 1,nextLine.length - trimmedNextLine.length);
  28. session.replace(replaceRange, " ");
  29. row--;
  30. endRow--;
  31. } else if (trimmedLine.length < line.length) {
  32. session.remove(new Range(row, trimmedLine.length, row, line.length));
  33. }
  34. }
  35. }
  36. row++;
  37. }
  38. function findSpace(line, max, min) {
  39. if (line.length < max)
  40. return;
  41. var before = line.slice(0, max);
  42. var after = line.slice(max);
  43. var spaceAfter = /^(?:(\s+)|(\S+)(\s+))/.exec(after);
  44. var spaceBefore = /(?:(\s+)|(\s+)(\S+))$/.exec(before);
  45. var start = 0;
  46. var end = 0;
  47. if (spaceBefore && !spaceBefore[2]) {
  48. start = max - spaceBefore[1].length;
  49. end = max;
  50. }
  51. if (spaceAfter && !spaceAfter[2]) {
  52. if (!start)
  53. start = max;
  54. end = max + spaceAfter[1].length;
  55. }
  56. if (start) {
  57. return {
  58. start: start,
  59. end: end
  60. };
  61. }
  62. if (spaceBefore && spaceBefore[2] && spaceBefore.index > min) {
  63. return {
  64. start: spaceBefore.index,
  65. end: spaceBefore.index + spaceBefore[2].length
  66. };
  67. }
  68. if (spaceAfter && spaceAfter[2]) {
  69. start = max + spaceAfter[2].length;
  70. return {
  71. start: start,
  72. end: start + spaceAfter[3].length
  73. };
  74. }
  75. }
  76. }
  77. function wrapAfterInput(e) {
  78. if (e.command.name == "insertstring" && /\S/.test(e.args)) {
  79. var editor = e.editor;
  80. var cursor = editor.selection.cursor;
  81. if (cursor.column <= editor.renderer.$printMarginColumn) return;
  82. var lastDelta = editor.session.$undoManager.$lastDelta;
  83. hardWrap(editor, {
  84. startRow: cursor.row, endRow: cursor.row,
  85. allowMerge: false
  86. });
  87. if (lastDelta != editor.session.$undoManager.$lastDelta)
  88. editor.session.markUndoGroup();
  89. }
  90. }
  91. var Editor = require("../editor").Editor;
  92. require("../config").defineOptions(Editor.prototype, "editor", {
  93. hardWrap: {
  94. set: function(val) {
  95. if (val) {
  96. this.commands.on("afterExec", wrapAfterInput);
  97. } else {
  98. this.commands.off("afterExec", wrapAfterInput);
  99. }
  100. },
  101. value: false
  102. }
  103. });
  104. exports.hardWrap = hardWrap;
  105. }); (function() {
  106. window.require(["ace/ext/hardwrap"], function(m) {
  107. if (typeof module == "object" && typeof exports == "object" && module) {
  108. module.exports = m;
  109. }
  110. });
  111. })();