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