Skip to content

Commit 29f0a9f

Browse files
committed
Reformat ResolveMain with some smaller stylistic changes
1 parent d23bab1 commit 29f0a9f

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

server/src/main/kotlin/org/javacs/kt/resolve/ResolveMain.kt

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,26 @@ import org.javacs.kt.position.range
1010
import org.javacs.kt.util.partitionAroundLast
1111
import com.intellij.openapi.util.TextRange
1212

13-
1413
fun resolveMain(file: CompiledFile): Map<String,Any> {
1514
val parsedFile = file.parse.copy() as KtFile
16-
17-
val mainFunction = findTopLevelMainFunction(parsedFile)
18-
if(null != mainFunction) {
15+
16+
findTopLevelMainFunction(parsedFile)?.let { mainFunction ->
1917
// the KtFiles name is weird. Full path. This causes the class to have full path in name as well. Correcting to top level only
2018
parsedFile.name = parsedFile.name.partitionAroundLast("/").second.substring(1)
2119

22-
return mapOf("mainClass" to JvmFileClassUtil.getFileClassInfoNoResolve(parsedFile).facadeClassFqName.asString(),
23-
"range" to range(file.content, mainFunction.second))
20+
return mapOf(
21+
"mainClass" to JvmFileClassUtil.getFileClassInfoNoResolve(parsedFile).facadeClassFqName.asString(),
22+
"range" to range(file.content, mainFunction.second)
23+
)
2424
}
2525

26-
val companionMain = findCompanionObjectMain(parsedFile)
27-
if(null != companionMain) {
26+
findCompanionObjectMain(parsedFile)?.let { companionMain ->
2827
return mapOf(
2928
"mainClass" to (companionMain.first ?: ""),
3029
"range" to range(file.content, companionMain.second)
3130
)
3231
}
33-
32+
3433
return emptyMap()
3534
}
3635

@@ -42,21 +41,25 @@ private fun findTopLevelMainFunction(file: KtFile): Pair<String?, TextRange>? =
4241
}
4342

4443
// finds a top level class that contains a companion object with a main function inside
45-
private fun findCompanionObjectMain(file: KtFile): Pair<String?, TextRange>? = file.declarations.flatMap { topLevelDeclaration ->
46-
if(topLevelDeclaration is KtClass) {
47-
topLevelDeclaration.companionObjects
48-
} else {
49-
emptyList<KtObjectDeclaration>()
44+
private fun findCompanionObjectMain(file: KtFile): Pair<String?, TextRange>? = file.declarations
45+
.flatMap { topLevelDeclaration ->
46+
if (topLevelDeclaration is KtClass) {
47+
topLevelDeclaration.companionObjects
48+
} else {
49+
emptyList<KtObjectDeclaration>()
50+
}
5051
}
51-
}.flatMap { companionObject ->
52-
companionObject.body?.children?.toList() ?: emptyList()
53-
}.mapNotNull { companionObjectInternal ->
54-
if(companionObjectInternal is KtNamedFunction && "main" == companionObjectInternal.name && companionObjectInternal.text.startsWith("@JvmStatic")) {
55-
companionObjectInternal
56-
} else {
57-
null
52+
.flatMap { companionObject ->
53+
companionObject.body?.children?.toList() ?: emptyList()
54+
}
55+
.mapNotNull { companionObjectInternal ->
56+
companionObjectInternal.takeIf {
57+
companionObjectInternal is KtNamedFunction
58+
&& "main" == companionObjectInternal.name
59+
&& companionObjectInternal.text.startsWith("@JvmStatic")
60+
}
61+
}
62+
.firstOrNull()?.let {
63+
// a little ugly, but because of success of the above, we know that "it" has 4 layers of parent objects (child of companion object body, companion object body, companion object, outer class)
64+
Pair((it.parent.parent.parent.parent as KtClass).fqName?.toString(), it.textRange)
5865
}
59-
}.firstOrNull()?.let {
60-
// a little ugly, but because of success of the above, we know that "it" has 4 layers of parent objects (child of companion object body, companion object body, companion object, outer class)
61-
Pair((it.parent.parent.parent.parent as KtClass).fqName?.toString(), it.textRange)
62-
}

0 commit comments

Comments
 (0)