Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ private const val BYTES_PER_PART = BYTES_PER_LINE * LINES_PER_PART
// This makes huge bytecode files and can easily hit the compiler's internal
// code size limits (error "code to large"). String literals are apparently
// embedded raw, which is what we want.
fun encodeFileDescriptor(fileDescriptorProto: FileDescriptorProto): List<List<String>> {
val parts = mutableListOf<MutableList<String>>()
fun encodeFileDescriptor(fileDescriptorProto: FileDescriptorProto): List<String> {
val parts = mutableListOf<String>()
val bytes = fileDescriptorProto.toByteArray()
for (i in bytes.indices step BYTES_PER_LINE) {
if (i % BYTES_PER_PART == 0) {
parts.add(mutableListOf())
parts.add("")
}
parts.last().add(escape(bytes.asSequence().drop(i).take(BYTES_PER_LINE)))
parts[parts.size - 1] += escape(bytes.asSequence().drop(i).take(BYTES_PER_LINE))
}
return parts
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,11 @@ private constructor(
return FileDescriptorInfo(type, properties)
}

private fun descriptorLines() =
fileDescriptorParts()
.map { arrayParts ->
arrayParts
.map { CodeBlock.of("\"%L\"", it) }
.joinToCode(" +\n")
}
.joinToCode(",\n")
private fun descriptorLines(): CodeBlock {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • This looks a lot like String.embed in KotlinPoetUtils. Is that usable here?
  • If we're just concatenating all the file descriptor parts, is there any need for it to be a list of strings? Should it just return a CodeBlock?

val sb = StringBuilder()
fileDescriptorParts().forEach(sb::append)
return CodeBlock.of("\"%L\"", sb.toString().bindSpaces())
}

private fun fileDescriptorParts() =
encodeFileDescriptor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,11 @@ private class MessageGenerator(
} else {
buildCodeBlock {
add("return \"%L(\" +\n", msg.className.simpleName)
toStringLines(properties).forEach(::add)
if (properties.size < 100) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this getting around a problem? How does this (or its absence) interact with the ktlint integration we use to clean up generated code?

toStringLines(properties).forEach(::add)
} else {
add(toStringLine(properties))
}
add(unknownFieldsToString(prefix = ", "))
}
}
Expand All @@ -226,6 +230,12 @@ private class MessageGenerator(
prop.name
)
}

private fun toStringLine(properties: List<PropertyInfo>): CodeBlock {
val string = "\"" + properties.joinToString(", ") { "%N=\$%N" } + "\" +\n"
val names = properties.flatMap { listOf(it, it) }
return CodeBlock.of(string.bindSpaces(), *names.map { it.name }.toTypedArray())
}
}

fun formatDoc(lines: List<String>) =
Expand Down
Loading