[hardwrap addon] introduce forceBreak

This commit is contained in:
Kaushik Kulkarni 2020-07-02 00:54:59 -05:00 committed by Marijn Haverbeke
parent b5ce22f1e3
commit 0682bcc8d5
2 changed files with 27 additions and 8 deletions

View File

@ -29,11 +29,20 @@
return {from: start, to: end}; return {from: start, to: end};
} }
function findBreakPoint(text, column, wrapOn, killTrailingSpace) { function findBreakPoint(text, column, wrapOn, killTrailingSpace, forceBreak) {
var at = column var at = column
while (at < text.length && text.charAt(at) == " ") at++ while (at < text.length && text.charAt(at) == " ") at++
for (; at > 0; --at) for (; at > 0; --at)
if (wrapOn.test(text.slice(at - 1, at + 1))) break; 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) { for (var first = true;; first = false) {
var endOfText = at; var endOfText = at;
if (killTrailingSpace) if (killTrailingSpace)
@ -47,6 +56,7 @@
from = cm.clipPos(from); to = cm.clipPos(to); from = cm.clipPos(from); to = cm.clipPos(to);
var column = options.column || 80; var column = options.column || 80;
var wrapOn = options.wrapOn || /\s\S|-[^\.\d]/; var wrapOn = options.wrapOn || /\s\S|-[^\.\d]/;
var forceBreak = options.forceBreak !== false;
var killTrailing = options.killTrailingSpace !== false; var killTrailing = options.killTrailingSpace !== false;
var changes = [], curLine = "", curNo = from.line; var changes = [], curLine = "", curNo = from.line;
var lines = cm.getRange(from, to, false); var lines = cm.getRange(from, to, false);
@ -68,7 +78,7 @@
curLine += text; curLine += text;
if (i) { if (i) {
var firstBreak = curLine.length > column && leadingSpace == spaceTrimmed && 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 this isn't broken, or is broken at a different point, remove old break
if (!firstBreak || firstBreak.from != oldLen || firstBreak.to != oldLen + spaceInserted) { if (!firstBreak || firstBreak.from != oldLen || firstBreak.to != oldLen + spaceInserted) {
changes.push({text: [spaceInserted ? " " : ""], changes.push({text: [spaceInserted ? " " : ""],
@ -80,12 +90,16 @@
} }
} }
while (curLine.length > column) { while (curLine.length > column) {
var bp = findBreakPoint(curLine, column, wrapOn, killTrailing); var bp = findBreakPoint(curLine, column, wrapOn, killTrailing, forceBreak);
changes.push({text: ["", leadingSpace], if ((bp.from != bp.to) || (forceBreak)) {
from: Pos(curNo, bp.from), changes.push({text: ["", leadingSpace],
to: Pos(curNo, bp.to)}); from: Pos(curNo, bp.from),
curLine = leadingSpace + curLine.slice(bp.to); to: Pos(curNo, bp.to)});
++curNo; curLine = leadingSpace + curLine.slice(bp.to);
++curNo;
} else {
break;
}
} }
} }
if (changes.length) cm.operation(function() { if (changes.length) cm.operation(function() {

View File

@ -3135,6 +3135,11 @@ editor.setOption("extraKeys", {
<dt><code><strong>killTrailingSpace</strong>: boolean</code></dt> <dt><code><strong>killTrailingSpace</strong>: boolean</code></dt>
<dd>Whether trailing space caused by wrapping should be <dd>Whether trailing space caused by wrapping should be
preserved, or deleted. Defaults to true.</dd> 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> </dl>
A demo of the addon is available <a href="../demo/hardwrap.html">here</a>. A demo of the addon is available <a href="../demo/hardwrap.html">here</a>.
</dd> </dd>