Skip to content

Commit cd01576

Browse files
committed
Clean up and finalized companion object main resolving. Also adding
test resource that was not added earlier.
1 parent 29d2c96 commit cd01576

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import org.jetbrains.kotlin.psi.KtFile
55
import org.jetbrains.kotlin.psi.KtNamedFunction
66
import org.jetbrains.kotlin.psi.KtClass
77
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
118
import org.javacs.kt.CompiledFile
12-
import org.javacs.kt.LOG
139
import org.javacs.kt.position.range
1410
import org.javacs.kt.util.partitionAroundLast
1511
import com.intellij.openapi.util.TextRange
@@ -29,7 +25,6 @@ fun resolveMain(file: CompiledFile): Map<String,Any> {
2925

3026
val companionMain = findCompanionObjectMain(parsedFile)
3127
if(null != companionMain) {
32-
// TODO: any way we should handle the jvmname stuff here?
3328
return mapOf(
3429
"mainClass" to (companionMain.first ?: ""),
3530
"range" to range(file.content, companionMain.second)
@@ -39,15 +34,13 @@ fun resolveMain(file: CompiledFile): Map<String,Any> {
3934
return emptyMap()
4035
}
4136

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)
4338
private fun findTopLevelMainFunction(file: KtFile): Pair<String?, TextRange>? = file.declarations.find {
44-
// TODO: any validations on arguments
4539
it is KtNamedFunction && "main" == it.name
4640
}?.let {
4741
Pair(it.name, it.textRangeInParent)
4842
}
4943

50-
// TODO: can this and the previous be merged in any way? or is this approach the cleanest?
5144
// finds a top level class that contains a companion object with a main function inside
5245
private fun findCompanionObjectMain(file: KtFile): Pair<String?, TextRange>? = file.declarations.flatMap { topLevelDeclaration ->
5346
if(topLevelDeclaration is KtClass) {
@@ -58,16 +51,12 @@ private fun findCompanionObjectMain(file: KtFile): Pair<String?, TextRange>? = f
5851
}.flatMap { companionObject ->
5952
companionObject.body?.children?.toList() ?: emptyList()
6053
}.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")) {
6555
companionObjectInternal
6656
} else {
6757
null
6858
}
6959
}.firstOrNull()?.let {
7060
// 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..
7261
Pair((it.parent.parent.parent.parent as KtClass).fqName?.toString(), it.textRange)
7362
}

server/src/test/kotlin/org/javacs/kt/ResolveMainCommandTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class CompanionObjectMainResolve : SingleFileTestFixture("resolvemain", "Compani
7070
assertNotNull(commandResult)
7171
val mainInfo = commandResult as Map<String, Any>
7272
assertEquals("test.my.companion.SweetPotato", mainInfo["mainClass"])
73-
assertEquals(Range(Position(9, 8), Position(11, 9)), mainInfo["range"])
73+
assertEquals(Range(Position(8, 8), Position(11, 9)), mainInfo["range"])
7474
assertEquals(root.toString(), mainInfo["projectRoot"])
7575
}
7676
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test.my.companion
2+
3+
val SOME_GLOBAL_CONSTANT = 42
4+
5+
fun multiplyByOne(num: Int) = num*1
6+
7+
class SweetPotato {
8+
companion object {
9+
@JvmStatic
10+
fun main() {
11+
println("42 multiplied by 1: ${multiplyByOne(42)}")
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)