Skip to content

Commit

Permalink
Consolidate Escaping.kt into one class
Browse files Browse the repository at this point in the history
Summary: Some more refactoring, this time to group all the functions in this file under one class to make it easier to keep track of the static helpers.

Reviewed By: strulovich

Differential Revision: D33146489

fbshipit-source-id: a8ed82738aca0266f5772624fb04128d1075c4d3
  • Loading branch information
davidtorosyan authored and facebook-github-bot committed Dec 16, 2021
1 parent 24e89f9 commit b7b74c2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 36 deletions.
4 changes: 2 additions & 2 deletions core/src/main/java/com/facebook/ktfmt/format/Formatter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import com.facebook.ktfmt.format.FormattingOptions.Style.DROPBOX
import com.facebook.ktfmt.format.FormattingOptions.Style.GOOGLE
import com.facebook.ktfmt.format.RedundantElementRemover.dropRedundantElements
import com.facebook.ktfmt.format.WhitespaceTombstones.indexOfWhitespaceTombstone
import com.facebook.ktfmt.kdoc.Escaping
import com.facebook.ktfmt.kdoc.KDocCommentsHelper
import com.facebook.ktfmt.kdoc.indexOfCommentEscapeSequences
import com.google.common.collect.ImmutableList
import com.google.common.collect.Range
import com.google.googlejavaformat.Doc
Expand Down Expand Up @@ -121,7 +121,7 @@ object Formatter {
private fun checkEscapeSequences(code: String) {
var index = code.indexOfWhitespaceTombstone()
if (index == -1) {
index = indexOfCommentEscapeSequences(code)
index = Escaping.indexOfCommentEscapeSequences(code)
}
if (index != -1) {
throw ParseError(
Expand Down
53 changes: 28 additions & 25 deletions core/src/main/java/com/facebook/ktfmt/kdoc/Escaping.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,38 @@

package com.facebook.ktfmt.kdoc

private const val SLASH_STAR_ESCAPE = "\u0004\u0005"
object Escaping {

private const val STAR_SLASH_ESCAPE = "\u0005\u0004"
private const val SLASH_STAR_ESCAPE = "\u0004\u0005"

fun indexOfCommentEscapeSequences(s: String) =
s.indexOfAny(listOf(SLASH_STAR_ESCAPE, STAR_SLASH_ESCAPE))
private const val STAR_SLASH_ESCAPE = "\u0005\u0004"

/**
* kotlin-compiler's KDoc lexer doesn't correctly handle nested slash-star comments, so we escape
* them into tombstones, format, then unescape.
*/
fun escapeKDoc(s: String): String {
val startMarkerIndex = s.indexOf("/*")
val endMarkerIndex = s.lastIndexOf("*/")
fun indexOfCommentEscapeSequences(s: String) =
s.indexOfAny(listOf(SLASH_STAR_ESCAPE, STAR_SLASH_ESCAPE))

/**
* kotlin-compiler's KDoc lexer doesn't correctly handle nested slash-star comments, so we escape
* them into tombstones, format, then unescape.
*/
fun escapeKDoc(s: String): String {
val startMarkerIndex = s.indexOf("/*")
val endMarkerIndex = s.lastIndexOf("*/")

if (startMarkerIndex == -1 || endMarkerIndex == -1) {
throw RuntimeException("KDoc with no /** and/or */")
}

if (startMarkerIndex == -1 || endMarkerIndex == -1) {
throw RuntimeException("KDoc with no /** and/or */")
return s.substring(0, startMarkerIndex + 3) +
s.substring(startMarkerIndex + 3, endMarkerIndex)
.replace("/*", SLASH_STAR_ESCAPE)
.replace("*/", STAR_SLASH_ESCAPE) +
s.substring(endMarkerIndex)
}

return s.substring(0, startMarkerIndex + 3) +
s.substring(startMarkerIndex + 3, endMarkerIndex)
.replace("/*", SLASH_STAR_ESCAPE)
.replace("*/", STAR_SLASH_ESCAPE) +
s.substring(endMarkerIndex)
/**
*
* See [escapeKDoc].
*/
fun unescapeKDoc(s: String): String =
s.replace(SLASH_STAR_ESCAPE, "/*").replace(STAR_SLASH_ESCAPE, "*/")
}

/**
*
* See [escapeKDoc].
*/
fun unescapeKDoc(s: String): String =
s.replace(SLASH_STAR_ESCAPE, "/*").replace(STAR_SLASH_ESCAPE, "*/")
4 changes: 2 additions & 2 deletions core/src/main/java/com/facebook/ktfmt/kdoc/KDocFormatter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ object KDocFormatter {
* start and end with the same characters.
*/
fun formatKDoc(input: String, blockIndent: Int, maxLineLength: Int): String {
val escapedInput = escapeKDoc(input)
val escapedInput = Escaping.escapeKDoc(input)
val kDocLexer = KDocLexer()
kDocLexer.start(escapedInput)
val tokens = mutableListOf<KDocToken>()
Expand Down Expand Up @@ -138,7 +138,7 @@ object KDocFormatter {
BEGIN_KDOC -> output.writeBeginJavadoc()
END_KDOC -> {
output.writeEndJavadoc()
return unescapeKDoc(output.toString())
return Escaping.unescapeKDoc(output.toString())
}
LIST_ITEM_OPEN_TAG -> output.writeListItemOpen(token)
PRE_OPEN_TAG -> output.writePreOpen(token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class EscapingKtTest {
class EscapingTest {

@Test
fun `escaping kdoc`() {
assertThat(escapeKDoc("/** foo */")).isEqualTo("/** foo */")
assertThat(escapeKDoc("/*** foo */")).isEqualTo("/*** foo */")
assertThat(escapeKDoc("/** * foo */")).isEqualTo("/** * foo */")
assertThat(escapeKDoc("/** /* foo */")).isEqualTo("/** \u0004\u0005 foo */")
assertThat(escapeKDoc("/** /* foo */ */")).isEqualTo("/** \u0004\u0005 foo \u0005\u0004 */")
assertThat(Escaping.escapeKDoc("/** foo */")).isEqualTo("/** foo */")
assertThat(Escaping.escapeKDoc("/*** foo */")).isEqualTo("/*** foo */")
assertThat(Escaping.escapeKDoc("/** * foo */")).isEqualTo("/** * foo */")
assertThat(Escaping.escapeKDoc("/** /* foo */")).isEqualTo("/** \u0004\u0005 foo */")
assertThat(Escaping.escapeKDoc("/** /* foo */ */"))
.isEqualTo("/** \u0004\u0005 foo \u0005\u0004 */")

assertThat(escapeKDoc("/* /* foo */ */")).isEqualTo("/* \u0004\u0005 foo \u0005\u0004 */")
assertThat(Escaping.escapeKDoc("/* /* foo */ */"))
.isEqualTo("/* \u0004\u0005 foo \u0005\u0004 */")
}
}

0 comments on commit b7b74c2

Please sign in to comment.