Skip to content

Commit

Permalink
[hardwrap addon] introduce forceBreak
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushikcfd authored and marijnh committed Jul 8, 2020
1 parent b5ce22f commit 0682bcc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
30 changes: 22 additions & 8 deletions addon/wrap/hardwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,20 @@
return {from: start, to: end};
}

function findBreakPoint(text, column, wrapOn, killTrailingSpace) {
function findBreakPoint(text, column, wrapOn, killTrailingSpace, forceBreak) {
var at = column
while (at < text.length && text.charAt(at) == " ") at++
for (; at > 0; --at)
if (wrapOn.test(text.slice(at - 1, at + 1))) break;

if ((at == 0) && (!forceBreak)) {
// didn't find a break point before column, in non-forceBreak mode try to
// find one after 'column'.
for (at = column+1; at < text.length-1; ++at) {
if (wrapOn.test(text.slice(at - 1, at + 1))) break;
}
}

for (var first = true;; first = false) {
var endOfText = at;
if (killTrailingSpace)
Expand All @@ -47,6 +56,7 @@
from = cm.clipPos(from); to = cm.clipPos(to);
var column = options.column || 80;
var wrapOn = options.wrapOn || /\s\S|-[^\.\d]/;
var forceBreak = options.forceBreak !== false;
var killTrailing = options.killTrailingSpace !== false;
var changes = [], curLine = "", curNo = from.line;
var lines = cm.getRange(from, to, false);
Expand All @@ -68,7 +78,7 @@
curLine += text;
if (i) {
var firstBreak = curLine.length > column && leadingSpace == spaceTrimmed &&
findBreakPoint(curLine, column, wrapOn, killTrailing);
findBreakPoint(curLine, column, wrapOn, killTrailing, forceBreak);
// If this isn't broken, or is broken at a different point, remove old break
if (!firstBreak || firstBreak.from != oldLen || firstBreak.to != oldLen + spaceInserted) {
changes.push({text: [spaceInserted ? " " : ""],
Expand All @@ -80,12 +90,16 @@
}
}
while (curLine.length > column) {
var bp = findBreakPoint(curLine, column, wrapOn, killTrailing);
changes.push({text: ["", leadingSpace],
from: Pos(curNo, bp.from),
to: Pos(curNo, bp.to)});
curLine = leadingSpace + curLine.slice(bp.to);
++curNo;
var bp = findBreakPoint(curLine, column, wrapOn, killTrailing, forceBreak);
if ((bp.from != bp.to) || (forceBreak)) {
changes.push({text: ["", leadingSpace],
from: Pos(curNo, bp.from),
to: Pos(curNo, bp.to)});
curLine = leadingSpace + curLine.slice(bp.to);
++curNo;
} else {
break;
}
}
}
if (changes.length) cm.operation(function() {
Expand Down
5 changes: 5 additions & 0 deletions doc/manual.html
Original file line number Diff line number Diff line change
Expand Up @@ -3135,6 +3135,11 @@ <h2 id="addons">Addons</h2>
<dt><code><strong>killTrailingSpace</strong>: boolean</code></dt>
<dd>Whether trailing space caused by wrapping should be
preserved, or deleted. Defaults to true.</dd>
<dt><code><strong>forceBreak</strong>: boolean</code></dt>
<dd>If set to true forces a break at <code>column</code> in the case
when no <code>wrapOn</code> pattern is found in the range. If set to
false allows line to overflow the <code>column</code> limit if no
<code>wrapOn</code> pattern found. Defaults to true.</dd>
</dl>
A demo of the addon is available <a href="../demo/hardwrap.html">here</a>.
</dd>
Expand Down

0 comments on commit 0682bcc

Please sign in to comment.