Skip to content

Commit

Permalink
Fixes one more case for default value parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
raamcosta committed Jan 17, 2024
1 parent c04eb38 commit 6ea4ab7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ object DefaultParameterValueReader {
): DefaultValue {

var result = auxText
val indexOfFinalClosingParenthesis = result.indexOfFinalClosingParenthesis()
if (indexOfFinalClosingParenthesis != null) {
result = result.removeRange(indexOfFinalClosingParenthesis, result.length)
}

// ':' means its another parameter (I think.. I don't know what other meaning a ':' would have here..)
val indexOfNextParam = result.indexOfFirst { it == ':' }.takeIf { it != -1 }

Expand All @@ -83,13 +88,12 @@ object DefaultParameterValueReader {
) {
if (indexOfNextParam != null) {
result = result.removeRange(indexOfNextParam, result.length)
} else {
val indexOfFinalClosingParenthesis = result.indexOfFinalClosingParenthesis()
if (indexOfFinalClosingParenthesis != null) {
result = result.removeRange(indexOfFinalClosingParenthesis, result.length)
}
}
result = result.defaultValueCodeWithFunctionCalls()

val commaIndex = result.indexOfLast { it == ',' }
if (commaIndex != -1) {
result = result.removeRange(commaIndex, result.length)
}
} else {
val index = result.indexOfFirst { it == ' ' || it == ',' || it == ')' }
if (index != -1)
Expand Down Expand Up @@ -171,35 +175,6 @@ private fun String.indexOfFinalClosingParenthesis(): Int? {
return null
}

private fun String.defaultValueCodeWithFunctionCalls(): String {
var idx = 0

while (true) {
val indexOfOpen = this.indexOf('(', idx)
if (indexOfOpen == -1) break

idx = this.indexOf(')', indexOfOpen)

if (idx == -1) {
error("unexpected: did not find a ')' for previous '('")
}
}

if (idx < this.lastIndex) {
idx++
val textToConsider = this.removeRange(0, idx)
val indexFinish = textToConsider.indexOfLast { it == ',' }
var idxFromRemove = idx

if (indexFinish != -1) {
idxFromRemove += indexFinish
return this.removeRange(idxFromRemove, this.length)
}
}

return this
}

@OptIn(KspExperimental::class)
fun KSValueParameter.getDefaultValue(resolver: Resolver): DefaultValue? {
if (!hasDefault) return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ class DefaultParameterValueReaderTest {
argType = "SearchConfiguration",
expected = DefaultValue("SearchConfiguration()")
),
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",
argType = "AppliedSearchFilters",
expected = DefaultValue("AppliedSearchFilters()")
),
TestCase(
srcCodeText = """
val appliedFilters: AppliedSearchFilters = AppliedSearchFilters(),
Expand Down

0 comments on commit 6ea4ab7

Please sign in to comment.