Skip to content

Commit

Permalink
Move fun wrap() to 'commons' package.
Browse files Browse the repository at this point in the history
  • Loading branch information
OLarionova-HORIS committed Mar 25, 2024
1 parent 41e7392 commit 77cc64b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2024. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/

package org.jetbrains.letsPlot.commons.formatting.string

fun wrap(text: String, lengthLimit: Int, countLimit: Int = -1): String {
if (text.length <= lengthLimit || text.contains("\n")) {
return text
}

return text.split(" ")
.let { words ->
val lines = mutableListOf(mutableListOf<String>())
words.forEach { word ->
val freeSpace =
lengthLimit - lines.last().let { line -> line.sumOf(String::length) + line.size }
.coerceAtMost(lengthLimit)
when {
freeSpace >= word.length -> lines.last().add(word)
word.length <= lengthLimit -> lines.add(mutableListOf(word))
else -> {
lines.last().takeIf { freeSpace > 0 }?.add(word.take(freeSpace))
word.drop(freeSpace)
.chunked(lengthLimit)
.forEach {
lines.add(mutableListOf<String>(it))
}
}
}
}
lines
}
.joinToString(separator = "\n", limit = countLimit) {
it.joinToString(separator = " ")
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
/*
* Copyright (c) 2019. JetBrains s.r.o.
* Copyright (c) 2024. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/

package org.jetbrains.letsPlot.core.plot.builder.assemble
package org.jetbrains.letsPlot.commons.formatting.string

import org.assertj.core.api.Assertions.assertThat
import kotlin.test.Test

class LegendAssemblerTest {
class WordWrapperTest {

@Test
fun skipWrappingForShortText() {
assertThat(LegendAssembler.wrap("abc", 7))
assertThat(wrap("abc", 7))
.isEqualTo("abc")
assertThat(LegendAssembler.wrap("abcdefg", 7))
assertThat(wrap("abcdefg", 7))
.isEqualTo("abcdefg")
assertThat(LegendAssembler.wrap("ab cd e", 7))
assertThat(wrap("ab cd e", 7))
.isEqualTo("ab cd e")
}

@Test
fun skipWrappingIfTextHaveDividers() {
assertThat(
LegendAssembler.wrap(
wrap(
"Lorem ipsum dolor sit amet,\n"
+ "consectetur adipiscing elit.", 7, 7
)
Expand All @@ -33,7 +33,7 @@ class LegendAssemblerTest {

@Test
fun wrapWordsSimple() {
assertThat(LegendAssembler.wrap("Lorem ipsum dolor sit amet", 10))
assertThat(wrap("Lorem ipsum dolor sit amet", 10))
.isEqualTo(
"""
|Lorem
Expand All @@ -42,7 +42,7 @@ class LegendAssemblerTest {
|amet
""".trimMargin()
)
assertThat(LegendAssembler.wrap("Lorem ipsum dolor sit amet, consectetur adipiscing elit", 15))
assertThat(wrap("Lorem ipsum dolor sit amet, consectetur adipiscing elit", 15))
.isEqualTo(
"""
|Lorem ipsum
Expand All @@ -55,7 +55,7 @@ class LegendAssemblerTest {

@Test
fun wrapWithoutSpaces() {
assertThat(LegendAssembler.wrap("abcdefghijklmnopqrstuvwxyz", 7))
assertThat(wrap("abcdefghijklmnopqrstuvwxyz", 7))
.isEqualTo(
"""
|abcdefg
Expand All @@ -68,7 +68,7 @@ class LegendAssemblerTest {

@Test
fun wrapWithLimitOverflow() {
assertThat(LegendAssembler.wrap("abcdefghijklmnopqrstuvwxyz", 7, 3))
assertThat(wrap("abcdefghijklmnopqrstuvwxyz", 7, 3))
.isEqualTo(
"""
|abcdefg
Expand All @@ -77,7 +77,7 @@ class LegendAssemblerTest {
|...
""".trimMargin()
)
assertThat(LegendAssembler.wrap("abcde fghijk lmno pqrstuv", 7, 3))
assertThat(wrap("abcde fghijk lmno pqrstuv", 7, 3))
.isEqualTo(
"""
|abcde
Expand All @@ -90,7 +90,7 @@ class LegendAssemblerTest {

@Test
fun wrapLongWords() {
assertThat(LegendAssembler.wrap("amet, consectetur adipiscing elit", 11))
assertThat(wrap("amet, consectetur adipiscing elit", 11))
.isEqualTo(
"""
|amet,
Expand All @@ -102,7 +102,7 @@ class LegendAssemblerTest {

//If long word stay after space character, what have position index greater than string.length / 3,
//then long word will be wrap at new line
assertThat(LegendAssembler.wrap("Lorem ipsum abcdefghijklmnopqrstuvwxyz, abcdefghijklmnopqrstuvwxyz elit.", 20))
assertThat(wrap("Lorem ipsum abcdefghijklmnopqrstuvwxyz, abcdefghijklmnopqrstuvwxyz elit.", 20))
.isEqualTo(
"""
|Lorem ipsum abcdefgh
Expand All @@ -113,7 +113,7 @@ class LegendAssemblerTest {
)
//If long word stay after space character, what have position index lower than string.length / 3,
//then long word will be cut and wrap at current line
assertThat(LegendAssembler.wrap("Lorem abcdefghijklmnopqrstuvwxyz, abcdefghijklmnopqrstuvwxyz elit.", 20))
assertThat(wrap("Lorem abcdefghijklmnopqrstuvwxyz, abcdefghijklmnopqrstuvwxyz elit.", 20))
.isEqualTo(
"""
|Lorem abcdefghijklmn
Expand All @@ -124,7 +124,7 @@ class LegendAssemblerTest {
)

assertThat(
LegendAssembler.wrap(
wrap(
"XYABAACYYDBASXXYAUAOKXYABAACYYXYABAACYYDBASXXYAUAOKDBASXXYAUAOK a scale ASSDASDASDASDASDASDASDASDASDAS benzimidazole QWQWQWQWQWEQEQEQWQWQEQEQWQWQEQ",
30
)
Expand All @@ -141,7 +141,7 @@ class LegendAssemblerTest {
)

assertThat(
LegendAssembler.wrap(
wrap(
"XYABAACYYDBASXXYAUAOKXYABAACYYXYABAACYYDBASXXYAUAOKDBASXXYAUAOK a scale ASSDASDASDASDASDASDASDASDASDASASDAS benzimidazole QWQWQWQWQWEQEQEQWQWQEQEQWQWQEQQWQWQWQWQWQWQW",
30
)
Expand All @@ -157,7 +157,7 @@ class LegendAssemblerTest {
""".trimMargin()
)
assertThat(
LegendAssembler.wrap(
wrap(
"XYABAACYYDBASXXYAUAOKXYABAACYYXYABAACYYDBASXXYAUAOKDBASXXYAUAOK a scale ASSDASDASDASDASDASDASDASDASDASASDAS benzimidazole QWQWQWQWQWEQEQEQWQWQEQEQWQWQEQQWQWQWQWQWQWQW",
31
)
Expand All @@ -173,7 +173,7 @@ class LegendAssemblerTest {
""".trimMargin()
)
assertThat(
LegendAssembler.wrap(
wrap(
"XYABAACYYDBASXXYAUAOKXYABAACYYXYABAACYYDBASXXYAUAOKDBASXXYAUAOK a scale ASSDASDASDASDASDASDASDASDASDASASDAS benzimidazole QWQWQWQWQWEQEQEQWQWQEQEQWQWQEQQWQWQWQWQWQWQW",
31
)
Expand All @@ -193,7 +193,7 @@ class LegendAssemblerTest {
@Test
fun wrapLongWordsWhenLineLengthEqualLimit() {
assertThat(
LegendAssembler.wrap(
wrap(
"XYABAACYYDBASXXYAUAOKXYABAACYYXYABAACYYDBASXXYAUAOKDBASXXYAUAOK a scale ASSDASDASDASDASDASDASDASDASDASASDA benzimidazole QWQWQWQWQWEQEQEQWQWQEQEQWQWQEQQWQWQWQWQWQWQW",
30
)
Expand All @@ -213,7 +213,7 @@ class LegendAssemblerTest {
@Test
fun wrapRealText() {
assertThat(
LegendAssembler.wrap(
wrap(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, " +
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, " +
"quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute " +
Expand All @@ -233,7 +233,7 @@ class LegendAssemblerTest {
""".trimMargin()
)
assertThat(
LegendAssembler.wrap(
wrap(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, " +
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, " +
"quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute " +
Expand All @@ -253,7 +253,7 @@ class LegendAssemblerTest {
""".trimMargin()
)
assertThat(
LegendAssembler.wrap(
wrap(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, " +
"https://github.com/JetBrains/lets-plot/issues/315, quis nostrud exercitation " +
"ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.jetbrains.letsPlot.core.plot.builder.assemble

import org.jetbrains.letsPlot.commons.formatting.string.wrap
import org.jetbrains.letsPlot.commons.geometry.DoubleVector
import org.jetbrains.letsPlot.commons.values.Color
import org.jetbrains.letsPlot.core.FeatureSwitch
Expand Down Expand Up @@ -161,38 +162,6 @@ class LegendAssembler(
companion object {
private const val DEBUG_DRAWING = FeatureSwitch.LEGEND_DEBUG_DRAWING

fun wrap(text: String, lengthLimit: Int, countLimit: Int = -1): String {
if (text.length <= lengthLimit || text.contains("\n")) {
return text
}

return text.split(" ")
.let { words ->
val lines = mutableListOf(mutableListOf<String>())
words.forEach { word ->
val freeSpace =
lengthLimit - lines.last().let { line -> line.sumOf(String::length) + line.size }
.coerceAtMost(lengthLimit)
when {
freeSpace >= word.length -> lines.last().add(word)
word.length <= lengthLimit -> lines.add(mutableListOf(word))
else -> {
lines.last().takeIf { freeSpace > 0 }?.add(word.take(freeSpace))
word.drop(freeSpace)
.chunked(lengthLimit)
.forEach {
lines.add(mutableListOf<String>(it))
}
}
}
}
lines
}
.joinToString(separator = "\n", limit = countLimit) {
it.joinToString(separator = " ")
}
}

fun createLegendSpec(
title: String,
breaks: List<LegendBreak>,
Expand Down

0 comments on commit 77cc64b

Please sign in to comment.