@@ -5,11 +5,7 @@ import org.jetbrains.kotlin.psi.KtFile
5
5
import org.jetbrains.kotlin.psi.KtNamedFunction
6
6
import org.jetbrains.kotlin.psi.KtClass
7
7
import org.jetbrains.kotlin.psi.KtObjectDeclaration
8
- import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
9
- import org.jetbrains.kotlin.psi.KtClassOrObject
10
- import org.jetbrains.kotlin.container.topologicalSort
11
8
import org.javacs.kt.CompiledFile
12
- import org.javacs.kt.LOG
13
9
import org.javacs.kt.position.range
14
10
import org.javacs.kt.util.partitionAroundLast
15
11
import com.intellij.openapi.util.TextRange
@@ -29,7 +25,6 @@ fun resolveMain(file: CompiledFile): Map<String,Any> {
29
25
30
26
val companionMain = findCompanionObjectMain(parsedFile)
31
27
if (null != companionMain) {
32
- // TODO: any way we should handle the jvmname stuff here?
33
28
return mapOf (
34
29
" mainClass" to (companionMain.first ? : " " ),
35
30
" range" to range(file.content, companionMain.second)
@@ -39,15 +34,13 @@ fun resolveMain(file: CompiledFile): Map<String,Any> {
39
34
return emptyMap()
40
35
}
41
36
42
- // only one allowed (so invalid syntax files will not show any main methods)
37
+ // only one main method allowed top level in a file (so invalid syntax files will not show any main methods)
43
38
private fun findTopLevelMainFunction (file : KtFile ): Pair <String ?, TextRange >? = file.declarations.find {
44
- // TODO: any validations on arguments
45
39
it is KtNamedFunction && " main" == it.name
46
40
}?.let {
47
41
Pair (it.name, it.textRangeInParent)
48
42
}
49
43
50
- // TODO: can this and the previous be merged in any way? or is this approach the cleanest?
51
44
// finds a top level class that contains a companion object with a main function inside
52
45
private fun findCompanionObjectMain (file : KtFile ): Pair <String ?, TextRange >? = file.declarations.flatMap { topLevelDeclaration ->
53
46
if (topLevelDeclaration is KtClass ) {
@@ -58,16 +51,12 @@ private fun findCompanionObjectMain(file: KtFile): Pair<String?, TextRange>? = f
58
51
}.flatMap { companionObject ->
59
52
companionObject.body?.children?.toList() ? : emptyList()
60
53
}.mapNotNull { companionObjectInternal ->
61
- if (companionObjectInternal is KtNamedFunction && " main" == companionObjectInternal.name) { // && companionObjectInternal.annotations.any {
62
- // LOG.info("Annotation!! {}", it.name)
63
- // "JvmStatic" == it.name
64
- // }
54
+ if (companionObjectInternal is KtNamedFunction && " main" == companionObjectInternal.name && companionObjectInternal.text.startsWith(" @JvmStatic" )) {
65
55
companionObjectInternal
66
56
} else {
67
57
null
68
58
}
69
59
}.firstOrNull()?.let {
70
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)
71
- // TODO: should we correct textRange start line manually? includes the JvmStatic annotation if present..
72
61
Pair ((it.parent.parent.parent.parent as KtClass ).fqName?.toString(), it.textRange)
73
62
}
0 commit comments