Skip to content

Commit

Permalink
Ignore unsafe wraps
Browse files Browse the repository at this point in the history
  • Loading branch information
Egorand committed Oct 11, 2024
1 parent dd1ba29 commit 15f7aeb
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ internal class CodeWriter(
if (!first) {
if ((kdoc || comment) && trailingNewline) {
emitIndentation()
out.appendNonWrapping(if (kdoc) " *" else "//")
out.append(if (kdoc) " *" else "//")
}
out.newline()
trailingNewline = true
Expand All @@ -606,9 +606,9 @@ internal class CodeWriter(
if (trailingNewline) {
emitIndentation()
if (kdoc) {
out.appendNonWrapping(" * ")
out.append(" * ")
} else if (comment) {
out.appendNonWrapping("// ")
out.append("// ")
}
}

Expand All @@ -627,7 +627,7 @@ internal class CodeWriter(

private fun emitIndentation() {
for (j in 0..<indentLevel) {
out.appendNonWrapping(indent)
out.append(indent)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal class LineWrapper(
while (pos < s.length) {
when (s[pos]) {
'' -> {
// Each space starts a new empty segment.
// Each wrapping space starts a new empty segment.
this.indentLevel = indentLevel
this.linePrefix = linePrefix
segments += ""
Expand All @@ -66,7 +66,7 @@ internal class LineWrapper(
pos++
}

'·', ' ' -> {
' ', '·' -> {
// Render · as a non-breaking space.
segments[segments.size - 1] += " "
pos++
Expand Down Expand Up @@ -105,8 +105,6 @@ internal class LineWrapper(
}

private fun emitCurrentLine() {
foldUnsafeBreaks()

var start = 0
var columnCount = segments[0].length

Expand Down Expand Up @@ -150,26 +148,7 @@ internal class LineWrapper(
}
}

/**
* Any segment that starts with '+' or '-' can't have a break preceding it. Combine it with the
* preceding segment. Note that this doesn't apply to the first segment.
*/
private fun foldUnsafeBreaks() {
var i = 1
while (i < segments.size) {
val segment = segments[i]
if (UNSAFE_LINE_START.matches(segment)) {
segments[i - 1] = segments[i - 1] + " " + segments[i]
segments.removeAt(i)
if (i > 1) i--
} else {
i++
}
}
}

companion object {
private val UNSAFE_LINE_START = Regex("\\s*[-+].*")
private val SPECIAL_CHARACTERS = " \n·♢".toCharArray()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,34 +143,6 @@ class LineWrapperTest {
)
}

@Test fun noWrapPrecedingUnaryPlus() {
val out = StringBuffer()
val lineWrapper = LineWrapper(out, " ", 10)
lineWrapper.append("a♢+♢b♢♢♢♢♢♢♢+♢c", indentLevel = 2)
lineWrapper.close()
assertThat(out.toString()).isEqualTo(
"""
|a +
| b +
| c
""".trimMargin(),
)
}

@Test fun noWrapPrecedingUnaryMinus() {
val out = StringBuffer()
val lineWrapper = LineWrapper(out, " ", 10)
lineWrapper.append("a♢-♢b♢♢♢♢♢♢♢-♢c", indentLevel = 2)
lineWrapper.close()
assertThat(out.toString()).isEqualTo(
"""
|a -
| b -
| c
""".trimMargin(),
)
}

@Test fun appendNonWrapping() {
val out = StringBuffer()
val lineWrapper = LineWrapper(out, " ", 10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,30 +144,6 @@ class LineWrappingTest {
)
}

@Test fun wrappingWhitespacePrecedingUnaryOperatorsDoesNotWrap() {
val wrapMe = FunSpec.builder("wrapMe")
.addStatement("val aaaaaa =♢%S♢+1", "x".repeat(80))
.addStatement("val bbbbbb =♢%S♢+1", "x".repeat(81))
.addStatement("val cccccc =♢%S♢-1", "x".repeat(80))
.addStatement("val dddddd =♢%S♢-1", "x".repeat(81))
.build()
assertThat(toString(wrapMe)).isEqualTo(
"""
|package com.squareup.tacos
|
|public fun wrapMe() {
| val aaaaaa = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +1
| val bbbbbb =
| "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +1
| val cccccc = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -1
| val dddddd =
| "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -1
|}
|
""".trimMargin(),
)
}

@Test fun parameterWrapping() {
val funSpecBuilder = FunSpec.builder("call")
funSpecBuilder.addCode("«call(")
Expand Down

0 comments on commit 15f7aeb

Please sign in to comment.