Skip to content

Commit

Permalink
Improve word wrap robustness when preserving whitespace
Browse files Browse the repository at this point in the history
v1.11.2

Fixes potential crash when editing a long line of input with multi-line
input buffer rendering enabled
  • Loading branch information
dhleong committed Feb 28, 2019
1 parent 3efab5e commit 4b174e4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ ext.deps = [

ext.config = [
name: 'Judo',
version: '1.11.1',
version: '1.11.2',
]
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ internal inline fun FlavorableCharSequence.forEachRenderedLine(
block: (start: Int, end: Int) -> Unit
) {
var len = length
if (len == 0 || len == 1 && this[0] == '\n') {
if (len == 0 || (len == 1 && this[0] == '\n')) {
block(0, 0)
return
}
Expand Down Expand Up @@ -137,9 +137,16 @@ internal inline fun FlavorableCharSequence.forEachRenderedLine(

// word wrap right on a boundary
(
wordWrap
&& inWord
&& (i == len || Character.isWhitespace(codePointAt(i)))
wordWrap && (
(
inWord && (
i == len || Character.isWhitespace(codePointAt(i))
)
) || (
// if preserving whitespace, it's okay to not be "in a word"
preserveWhitespace && (i == len || !inWord)
)
)
) -> {
block(lineStart, i)
lineStart = i
Expand Down
24 changes: 24 additions & 0 deletions jline/src/test/kotlin/net/dhleong/judo/jline/LineSplittingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,28 @@ class LineSplittingTest {
)
}
}


@Test fun `Split line with preserved whitespace at limit`() {
val s = FlavorableStringBuilder.withDefaultFlavor(
"Kaylee Frye "
)
assertInWindow(width = 12, wordWrap = true, preserveWhitespace = true) {
s.hasRenderedLines(
"Kaylee Frye "
)
}
}

@Test fun `Split line with preserved whitespace at limit and text just after`() {
val s = FlavorableStringBuilder.withDefaultFlavor(
"Kaylee Frye a"
)
assertInWindow(width = 12, wordWrap = true, preserveWhitespace = true) {
s.hasRenderedLines(
"Kaylee Frye ",
"a"
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ class TerminalInputLineHelperTest {
}
}

@Test fun `fit with word wrap and space right at width`() {
windowWidth = 10
settings[MAX_INPUT_LINES] = 4
settings[WORD_WRAP] = true

val text = "The quick "
renderer.typeMultiAndFit(text,
expected = listOf(
"The quick ",
""
) to (1 to 0))
}

private fun InputLine.updateInputLine(text: String, cursor: Int) {
line = FlavorableStringBuilder.withDefaultFlavor(text)
cursorIndex = cursor
Expand Down

0 comments on commit 4b174e4

Please sign in to comment.