Skip to content

Commit 0682bcc

Browse files
kaushikcfdmarijnh
authored andcommitted
[hardwrap addon] introduce forceBreak
1 parent b5ce22f commit 0682bcc

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

addon/wrap/hardwrap.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,20 @@
2929
return {from: start, to: end};
3030
}
3131

32-
function findBreakPoint(text, column, wrapOn, killTrailingSpace) {
32+
function findBreakPoint(text, column, wrapOn, killTrailingSpace, forceBreak) {
3333
var at = column
3434
while (at < text.length && text.charAt(at) == " ") at++
3535
for (; at > 0; --at)
3636
if (wrapOn.test(text.slice(at - 1, at + 1))) break;
37+
38+
if ((at == 0) && (!forceBreak)) {
39+
// didn't find a break point before column, in non-forceBreak mode try to
40+
// find one after 'column'.
41+
for (at = column+1; at < text.length-1; ++at) {
42+
if (wrapOn.test(text.slice(at - 1, at + 1))) break;
43+
}
44+
}
45+
3746
for (var first = true;; first = false) {
3847
var endOfText = at;
3948
if (killTrailingSpace)
@@ -47,6 +56,7 @@
4756
from = cm.clipPos(from); to = cm.clipPos(to);
4857
var column = options.column || 80;
4958
var wrapOn = options.wrapOn || /\s\S|-[^\.\d]/;
59+
var forceBreak = options.forceBreak !== false;
5060
var killTrailing = options.killTrailingSpace !== false;
5161
var changes = [], curLine = "", curNo = from.line;
5262
var lines = cm.getRange(from, to, false);
@@ -68,7 +78,7 @@
6878
curLine += text;
6979
if (i) {
7080
var firstBreak = curLine.length > column && leadingSpace == spaceTrimmed &&
71-
findBreakPoint(curLine, column, wrapOn, killTrailing);
81+
findBreakPoint(curLine, column, wrapOn, killTrailing, forceBreak);
7282
// If this isn't broken, or is broken at a different point, remove old break
7383
if (!firstBreak || firstBreak.from != oldLen || firstBreak.to != oldLen + spaceInserted) {
7484
changes.push({text: [spaceInserted ? " " : ""],
@@ -80,12 +90,16 @@
8090
}
8191
}
8292
while (curLine.length > column) {
83-
var bp = findBreakPoint(curLine, column, wrapOn, killTrailing);
84-
changes.push({text: ["", leadingSpace],
85-
from: Pos(curNo, bp.from),
86-
to: Pos(curNo, bp.to)});
87-
curLine = leadingSpace + curLine.slice(bp.to);
88-
++curNo;
93+
var bp = findBreakPoint(curLine, column, wrapOn, killTrailing, forceBreak);
94+
if ((bp.from != bp.to) || (forceBreak)) {
95+
changes.push({text: ["", leadingSpace],
96+
from: Pos(curNo, bp.from),
97+
to: Pos(curNo, bp.to)});
98+
curLine = leadingSpace + curLine.slice(bp.to);
99+
++curNo;
100+
} else {
101+
break;
102+
}
89103
}
90104
}
91105
if (changes.length) cm.operation(function() {

doc/manual.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3135,6 +3135,11 @@ <h2 id="addons">Addons</h2>
31353135
<dt><code><strong>killTrailingSpace</strong>: boolean</code></dt>
31363136
<dd>Whether trailing space caused by wrapping should be
31373137
preserved, or deleted. Defaults to true.</dd>
3138+
<dt><code><strong>forceBreak</strong>: boolean</code></dt>
3139+
<dd>If set to true forces a break at <code>column</code> in the case
3140+
when no <code>wrapOn</code> pattern is found in the range. If set to
3141+
false allows line to overflow the <code>column</code> limit if no
3142+
<code>wrapOn</code> pattern found. Defaults to true.</dd>
31383143
</dl>
31393144
A demo of the addon is available <a href="../demo/hardwrap.html">here</a>.
31403145
</dd>

0 commit comments

Comments
 (0)