@@ -10,27 +10,26 @@ import org.javacs.kt.position.range
10
10
import org.javacs.kt.util.partitionAroundLast
11
11
import com.intellij.openapi.util.TextRange
12
12
13
-
14
13
fun resolveMain (file : CompiledFile ): Map <String ,Any > {
15
14
val parsedFile = file.parse.copy() as KtFile
16
-
17
- val mainFunction = findTopLevelMainFunction(parsedFile)
18
- if (null != mainFunction) {
15
+
16
+ findTopLevelMainFunction(parsedFile)?.let { mainFunction ->
19
17
// the KtFiles name is weird. Full path. This causes the class to have full path in name as well. Correcting to top level only
20
18
parsedFile.name = parsedFile.name.partitionAroundLast(" /" ).second.substring(1 )
21
19
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
+ )
24
24
}
25
25
26
- val companionMain = findCompanionObjectMain(parsedFile)
27
- if (null != companionMain) {
26
+ findCompanionObjectMain(parsedFile)?.let { companionMain ->
28
27
return mapOf (
29
28
" mainClass" to (companionMain.first ? : " " ),
30
29
" range" to range(file.content, companionMain.second)
31
30
)
32
31
}
33
-
32
+
34
33
return emptyMap()
35
34
}
36
35
@@ -42,21 +41,25 @@ private fun findTopLevelMainFunction(file: KtFile): Pair<String?, TextRange>? =
42
41
}
43
42
44
43
// 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
+ }
50
51
}
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)
58
65
}
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