Skip to content

Commit

Permalink
fixes #560
Browse files Browse the repository at this point in the history
  • Loading branch information
raamcosta committed Jan 14, 2024
1 parent a8fa8b2 commit 917e381
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.google.devtools.ksp.symbol.KSValueParameter
import com.google.devtools.ksp.symbol.NonExistLocation
import com.ramcosta.composedestinations.codegen.commons.IllegalDestinationsSetup
import com.ramcosta.composedestinations.codegen.commons.removeFromTo
import com.ramcosta.composedestinations.codegen.facades.Logger
import com.ramcosta.composedestinations.codegen.model.DefaultValue
import java.io.File

Expand Down Expand Up @@ -71,11 +72,19 @@ object DefaultParameterValueReader {
): DefaultValue {

var result = auxText
if (!result.firstParenthesisIsClosing() && result.contains("(")) {
// ':' 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 }

if (result.firstParenthesisIsOpening() && // if first parenthesis is "(" then it is not closing list of function params
result.contains("(") && // we have a "(" and it's before a ")"
result.indexOf('(') < (indexOfNextParam ?: result.lastIndex) // "(" is before next param if it exists
) {
if (indexOfNextParam != null) {
result = result.removeRange(indexOfNextParam, result.length)
}
result = result.defaultValueCodeWithFunctionCalls()
} else {
val index = result.indexOfFirst { it == ' ' || it == ',' || it == '\n' || it == ')' }

if (index != -1)
result = result.removeRange(index, result.length)
}
Expand All @@ -92,7 +101,7 @@ object DefaultParameterValueReader {
if (result.length - importableAux.length > 2) {
//we detected a function call with args, we can't resolve this
throw IllegalDestinationsSetup("Navigation arguments using function calls with parameters as their default value " +
"are not currently supported (near: '$auxText')")
"are not currently supported (near: '$auxText')")
}

val importable = importableAux.split(".")[0]
Expand Down Expand Up @@ -130,11 +139,11 @@ object DefaultParameterValueReader {
}
}

private fun String.firstParenthesisIsClosing(): Boolean {
private fun String.firstParenthesisIsOpening(): Boolean {
val indexOfFirstOpening = this.indexOfFirst { it == '(' }
val indexOfFirstClosing = this.indexOfFirst { it == ')' }

return indexOfFirstClosing < indexOfFirstOpening
return indexOfFirstClosing >= indexOfFirstOpening
}

private fun String.defaultValueCodeWithFunctionCalls(): String {
Expand Down Expand Up @@ -191,8 +200,10 @@ fun KSValueParameter.getDefaultValue(resolver: Resolver): DefaultValue? {
"Are you using navArgsDelegate with code from different module?")
}

Logger.instance.info("getDefaultValue | name = ${name!!.asString()} type = $type")
val fileLocation = location as FileLocation
val (line, imports) = File(fileLocation.filePath).readLineAndImports(fileLocation.lineNumber)
val (lines, imports) = File(fileLocation.filePath)
.readLinesAndImports(fileLocation.lineNumber, fileLocation.lineNumber + 10)

return DefaultParameterValueReader.readDefaultValue(
{ pckg, name ->
Expand All @@ -204,12 +215,12 @@ fun KSValueParameter.getDefaultValue(resolver: Resolver): DefaultValue? {
}
}.getOrNull()
},
line,
lines.joinToString("").also { Logger.instance.info("getDefaultValue | line = $it") },
this.containingFile!!.packageName.asString(),
imports,
name!!.asString(),
type.toString()
)
).also { Logger.instance.info("getDefaultValue | Result = $it") }
}

class ResolvedSymbol(val isAccessible: Boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,44 +47,41 @@ inline fun <reified T> KSAnnotation.findArgumentValue(name: String): T? {
return arguments.find { it.name?.asString() == name }?.value as T?
}

fun File.readLineAndImports(lineNumber: Int): Pair<String, List<String>> {
fun File.readLine(lineNumber: Int): String {
val bufferedReader = BufferedReader(InputStreamReader(FileInputStream(this), Charsets.UTF_8))
return bufferedReader
.useLines { lines: Sequence<String> ->
val firstNLines = lines.take(lineNumber)

val iterator = firstNLines.iterator()
var line = iterator.next()
val importsList = mutableListOf<String>()
while (iterator.hasNext()) {
line = iterator.next()
if (line.startsWith("import")) {
importsList.add(line.removePrefix("import "))
}
}

line to importsList
lines
.take(lineNumber)
.last()
}
}

fun File.readLine(lineNumber: Int): String {
fun File.readLines(startLineNumber: Int, endLineNumber: Int): List<String> {
val bufferedReader = BufferedReader(InputStreamReader(FileInputStream(this), Charsets.UTF_8))
return bufferedReader
.useLines { lines: Sequence<String> ->
lines
.take(lineNumber)
.last()
val linesList = lines
.take(endLineNumber)
.toList()
linesList
.takeLast(linesList.size - (startLineNumber - 1))
}
}

fun File.readLines(startLineNumber: Int, endLineNumber: Int): List<String> {
fun File.readLinesAndImports(startLineNumber: Int, endLineNumber: Int): Pair<List<String>, List<String>> {
val bufferedReader = BufferedReader(InputStreamReader(FileInputStream(this), Charsets.UTF_8))
return bufferedReader
.useLines { lines: Sequence<String> ->
lines
val linesList = lines
.take(endLineNumber)
.toList()
.takeLast(endLineNumber - (startLineNumber - 1))

val linesRes = linesList.takeLast(linesList.size - (startLineNumber - 1))
val imports = linesList.filter { it.startsWith("import") }
.map { it.removePrefix("import ") }

linesRes to imports
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,42 @@ class DefaultParameterValueReaderTest {
argType = "String",
expected = DefaultValue("\"multiple, words string\"")
),
TestCase(
lineText = """
cena: Class<Any> = Any::class.java,
""",
argName = "cena",
argType = "Class",
expected = DefaultValue("Any::class.java")
),
TestCase(
lineText = """
arg1: String = "multiple, words string",
arg2: String = "doesn't matter"
""",
argName = "arg1",
argType = "String",
expected = DefaultValue("\"multiple, words string\"")
),
TestCase(
lineText = """
thingsWithNavTypeSerializer: Things? = null,
serializableExample: SerializableExample? = SerializableExample(),
""",
argName = "thingsWithNavTypeSerializer",
argType = "Things",
expected = DefaultValue("null")
),
TestCase(
lineText = """
stuff3: ArrayList<Color>? = arrayListOf(),
stuff4: SerializableExampleWithNavTypeSerializer? = SerializableExampleWithNavTypeSerializer(),
""",
argName = "stuff3",
argType = "ArrayList",
imports = emptyList(),
expected = DefaultValue("arrayListOf()")
),
TestCase(
lineText = "arg1: String = \"multiple, \\\"words string\", arg2: String = \"doesn't matter\"",
argName = "arg1",
Expand Down

0 comments on commit 917e381

Please sign in to comment.