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