From 0682bcc8d5d5f03cebfbb9d14c3ffa7dcc3e0e6d Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Thu, 2 Jul 2020 00:54:59 -0500 Subject: [PATCH] [hardwrap addon] introduce forceBreak --- addon/wrap/hardwrap.js | 30 ++++++++++++++++++++++-------- doc/manual.html | 5 +++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/addon/wrap/hardwrap.js b/addon/wrap/hardwrap.js index 29cc15f01f..2259e0e11e 100644 --- a/addon/wrap/hardwrap.js +++ b/addon/wrap/hardwrap.js @@ -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) @@ -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); @@ -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 ? " " : ""], @@ -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() { diff --git a/doc/manual.html b/doc/manual.html index e779008384..8008002a40 100644 --- a/doc/manual.html +++ b/doc/manual.html @@ -3135,6 +3135,11 @@

Addons

killTrailingSpace: boolean
Whether trailing space caused by wrapping should be preserved, or deleted. Defaults to true.
+
forceBreak: boolean
+
If set to true forces a break at column in the case + when no wrapOn pattern is found in the range. If set to + false allows line to overflow the column limit if no + wrapOn pattern found. Defaults to true.
A demo of the addon is available here.