Skip to content

Commit

Permalink
fixes issue with comments after nav arg with default value
Browse files Browse the repository at this point in the history
  • Loading branch information
raamcosta committed Jan 26, 2024
1 parent 6ea4ab7 commit d0dda36
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ object DefaultParameterValueReader {
argName: String,
argType: String,
): DefaultValue {
var auxText = srcCodeLines.joinToString("") { it.trim() }
var auxText = srcCodeLines
.map {
it.removeLineComments()
}
.joinToString("") { it.trim() }
.removeMultilineComments()

Logger.instance.info("getDefaultValue | src code line = $auxText")

Expand All @@ -46,12 +51,34 @@ object DefaultParameterValueReader {
auxText = auxText.removeRange(0, index)

return if (auxText.startsWith("\"")) {
if (auxText.contains("\"\"\"")) {
throw IllegalDestinationsSetup("Multiline string literals are not supported as navigation argument defaults (near: '$auxText'")
}
DefaultValue(stringLiteralValue(auxText))
} else {
importedDefaultValue(resolver, auxText, packageName, imports)
}
}

private fun String.removeMultilineComments(): String {
val idxOfMultiLineComment = this.indexOf("/*")
return if (idxOfMultiLineComment != -1 && !this.isInsideString(idxOfMultiLineComment)) {
this.removeFromTo("/*", "*/")
} else {
this
}
}

private fun String.removeLineComments(): String {
val idxOfLineComment = this.indexOf("//")
return if (idxOfLineComment != -1 && !this.isInsideString(idxOfLineComment)) {
this.replaceAfter("//", "")
.removeSuffix("//")
} else {
this
}
}

private fun stringLiteralValue(auxText: String): String {
var finalText = auxText
val splits = finalText.split("\"")
Expand Down Expand Up @@ -150,6 +177,23 @@ object DefaultParameterValueReader {
}
}

private fun String.isInsideString(idxToCheck: Int): Boolean {
var isInsideString = false

for (i in indices) {
when (this[i]) {
'"' -> isInsideString = !isInsideString
else -> {
if (i == idxToCheck) {
return isInsideString
}
}
}
}

return isInsideString
}

private fun String.firstParenthesisIsOpening(): Boolean {
val indexOfFirstOpening = this.indexOfFirst { it == '(' }
val indexOfFirstClosing = this.indexOfFirst { it == ')' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ class DefaultParameterValueReaderTest {
argType = "SearchConfiguration",
expected = DefaultValue("SearchConfiguration()")
),
TestCase(
srcCodeText = """
val myProperty: Boolean = false
// This is an awesome (usually) property.
""",
argName = "myProperty",
argType = "Boolean",
expected = DefaultValue("false")
),
TestCase(
srcCodeText = """
val myProperty: Boolean = false
/* This is an awesome (usually)
*
* property. */
""",
argName = "myProperty",
argType = "Boolean",
expected = DefaultValue("false")
),
TestCase(
srcCodeText = """val appliedFilters: AppliedSearchFilters = AppliedSearchFilters(),)@Preview@Composableprivate fun SearchScreenPreview(@PreviewParameter(PoiListPreviewParameterProvider::class, limit = 1) poiList: ImmutableList,) {OcmPreview {SearchScreenContent(poiCallbacks = PoiCallbacks(null, Origin.Deals, LocalFocusManager.current),""",
argName = "appliedFilters",
Expand Down

0 comments on commit d0dda36

Please sign in to comment.