Skip to content

Commit d9d7b6a

Browse files
committed
Moved main resolve logic to protocol extensions instead of commands
1 parent 2f46b72 commit d9d7b6a

File tree

6 files changed

+44
-46
lines changed

6 files changed

+44
-46
lines changed

server/src/main/kotlin/org/javacs/kt/KotlinLanguageServer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class KotlinLanguageServer : LanguageServer, LanguageClientAware, Closeable {
3030

3131
private val textDocuments = KotlinTextDocumentService(sourceFiles, sourcePath, config, tempDirectory, uriContentProvider, classPath)
3232
private val workspaces = KotlinWorkspaceService(sourceFiles, sourcePath, classPath, textDocuments, config)
33-
private val protocolExtensions = KotlinProtocolExtensionService(uriContentProvider, classPath)
33+
private val protocolExtensions = KotlinProtocolExtensionService(uriContentProvider, classPath, sourcePath)
3434

3535
private lateinit var client: LanguageClient
3636

server/src/main/kotlin/org/javacs/kt/KotlinProtocolExtensionService.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package org.javacs.kt
33
import org.eclipse.lsp4j.*
44
import org.javacs.kt.util.AsyncExecutor
55
import org.javacs.kt.util.parseURI
6+
import org.javacs.kt.resolve.resolveMain
67
import java.util.concurrent.CompletableFuture
8+
import java.nio.file.Paths
79

810
class KotlinProtocolExtensionService(
911
private val uriContentProvider: URIContentProvider,
10-
private val cp: CompilerClassPath
12+
private val cp: CompilerClassPath,
13+
private val sp: SourcePath
1114
) : KotlinProtocolExtensions {
1215
private val async = AsyncExecutor()
1316

@@ -18,4 +21,22 @@ class KotlinProtocolExtensionService(
1821
override fun buildOutputLocation(): CompletableFuture<String?> = async.compute {
1922
cp.outputDirectory.absolutePath
2023
}
24+
25+
override fun mainClass(textDocument: TextDocumentIdentifier): CompletableFuture<Map<String, Any?>> = async.compute {
26+
val fileUri = parseURI(textDocument.uri)
27+
val filePath = Paths.get(fileUri)
28+
29+
// we find the longest one in case both the root and submodule are included
30+
val workspacePath = cp.workspaceRoots.filter {
31+
filePath.startsWith(it)
32+
}.map {
33+
it.toString()
34+
}.maxByOrNull(String::length) ?: ""
35+
36+
val compiledFile = sp.currentVersion(fileUri)
37+
38+
resolveMain(compiledFile) + mapOf(
39+
"projectRoot" to workspacePath
40+
)
41+
}
2142
}

server/src/main/kotlin/org/javacs/kt/KotlinProtocolExtensions.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ interface KotlinProtocolExtensions {
1212

1313
@JsonRequest
1414
fun buildOutputLocation(): CompletableFuture<String?>
15+
16+
@JsonRequest
17+
fun mainClass(textDocument: TextDocumentIdentifier): CompletableFuture<Map<String, Any?>>
1518
}

server/src/main/kotlin/org/javacs/kt/KotlinWorkspaceService.kt

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ import org.eclipse.lsp4j.services.LanguageClientAware
88
import org.eclipse.lsp4j.jsonrpc.messages.Either
99
import org.javacs.kt.symbols.workspaceSymbols
1010
import org.javacs.kt.command.JAVA_TO_KOTLIN_COMMAND
11-
import org.javacs.kt.command.RESOLVE_MAIN
1211
import org.javacs.kt.j2k.convertJavaToKotlin
1312
import org.javacs.kt.KotlinTextDocumentService
1413
import org.javacs.kt.position.extractRange
1514
import org.javacs.kt.util.filePath
1615
import org.javacs.kt.util.parseURI
17-
import org.javacs.kt.util.AsyncExecutor
1816
import org.javacs.kt.resolve.resolveMain
1917
import java.net.URI
2018
import java.nio.file.Paths
@@ -32,9 +30,7 @@ class KotlinWorkspaceService(
3230
) : WorkspaceService, LanguageClientAware {
3331
private val gson = Gson()
3432
private var languageClient: LanguageClient? = null
35-
36-
private val async = AsyncExecutor()
37-
33+
3834
override fun connect(client: LanguageClient): Unit {
3935
languageClient = client
4036
}
@@ -58,26 +54,6 @@ class KotlinWorkspaceService(
5854
)
5955
)))))
6056
}
61-
62-
RESOLVE_MAIN -> {
63-
val fileUri = parseURI(gson.fromJson(args[0] as JsonElement, String::class.java))
64-
val filePath = Paths.get(fileUri)
65-
66-
// we find the longest one in case both the root and submodule are included
67-
val workspacePath = cp.workspaceRoots.filter {
68-
filePath.startsWith(it)
69-
}.map {
70-
it.toString()
71-
}.maxByOrNull(String::length) ?: ""
72-
73-
val compiledFile = sp.currentVersion(fileUri)
74-
75-
return async.compute {
76-
resolveMain(compiledFile) + mapOf(
77-
"projectRoot" to workspacePath
78-
)
79-
}
80-
}
8157
}
8258

8359
return CompletableFuture.completedFuture(null)
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package org.javacs.kt.command
22

33
const val JAVA_TO_KOTLIN_COMMAND = "convertJavaToKotlin"
4-
const val RESOLVE_MAIN = "resolveMain"
54

65
val ALL_COMMANDS = listOf(
76
JAVA_TO_KOTLIN_COMMAND,
8-
RESOLVE_MAIN
97
)

server/src/test/kotlin/org/javacs/kt/ResolveMainCommandTest.kt renamed to server/src/test/kotlin/org/javacs/kt/ResolveMainTest.kt

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ import com.google.gson.Gson
44
import org.eclipse.lsp4j.ExecuteCommandParams
55
import org.eclipse.lsp4j.Position
66
import org.eclipse.lsp4j.Range
7+
import org.eclipse.lsp4j.TextDocumentIdentifier
78
import org.junit.Test
89
import org.junit.Assert.assertEquals
910
import org.junit.Assert.assertNotNull
1011
import org.junit.Assert.assertNull
11-
import org.javacs.kt.command.RESOLVE_MAIN
1212

1313
class NoMainResolve : SingleFileTestFixture("resolvemain", "NoMain.kt") {
1414
@Test
1515
fun `Should not find any main class info`() {
1616
val root = testResourcesRoot().resolve(workspaceRoot)
17-
val executeCommandParams = ExecuteCommandParams(RESOLVE_MAIN, listOf(Gson().toJsonTree(root.resolve(file).toUri().toString(), )))
17+
val fileUri = root.resolve(file).toUri().toString()
1818

19-
val commandResult = languageServer.workspaceService.executeCommand(executeCommandParams).get()
19+
val result = languageServer.getProtocolExtensionService().mainClass(TextDocumentIdentifier(fileUri)).get()
2020

21-
assertNotNull(commandResult)
22-
val mainInfo = commandResult as Map<String, String>
21+
assertNotNull(result)
22+
val mainInfo = result as Map<String, String>
2323
assertNull(mainInfo["mainClass"])
2424
assertEquals(root.toString(), mainInfo["projectRoot"])
2525
}
@@ -30,12 +30,12 @@ class SimpleMainResolve : SingleFileTestFixture("resolvemain", "Simple.kt") {
3030
@Test
3131
fun `Should resolve correct main class of simple file`() {
3232
val root = testResourcesRoot().resolve(workspaceRoot)
33-
val executeCommandParams = ExecuteCommandParams(RESOLVE_MAIN, listOf(Gson().toJsonTree(root.resolve(file).toUri().toString())))
33+
val fileUri = root.resolve(file).toUri().toString()
3434

35-
val commandResult = languageServer.workspaceService.executeCommand(executeCommandParams).get()
35+
val result = languageServer.getProtocolExtensionService().mainClass(TextDocumentIdentifier(fileUri)).get()
3636

37-
assertNotNull(commandResult)
38-
val mainInfo = commandResult as Map<String, Any>
37+
assertNotNull(result)
38+
val mainInfo = result as Map<String, Any>
3939
assertEquals("test.SimpleKt", mainInfo["mainClass"])
4040
assertEquals(Range(Position(2, 0), Position(4, 1)), mainInfo["range"])
4141
assertEquals(root.toString(), mainInfo["projectRoot"])
@@ -47,12 +47,12 @@ class JvmNameAnnotationMainResolve : SingleFileTestFixture("resolvemain", "JvmNa
4747
@Test
4848
fun `Should resolve correct main class of file annotated with JvmName`() {
4949
val root = testResourcesRoot().resolve(workspaceRoot)
50-
val executeCommandParams = ExecuteCommandParams(RESOLVE_MAIN, listOf(Gson().toJsonTree(root.resolve(file).toUri().toString())))
50+
val fileUri = root.resolve(file).toUri().toString()
5151

52-
val commandResult = languageServer.workspaceService.executeCommand(executeCommandParams).get()
52+
val result = languageServer.getProtocolExtensionService().mainClass(TextDocumentIdentifier(fileUri)).get()
5353

54-
assertNotNull(commandResult)
55-
val mainInfo = commandResult as Map<String, Any>
54+
assertNotNull(result)
55+
val mainInfo = result as Map<String, Any>
5656
assertEquals("com.mypackage.name.Potato", mainInfo["mainClass"])
5757
assertEquals(Range(Position(5, 0), Position(7, 1)), mainInfo["range"])
5858
assertEquals(root.toString(), mainInfo["projectRoot"])
@@ -63,12 +63,12 @@ class CompanionObjectMainResolve : SingleFileTestFixture("resolvemain", "Compani
6363
@Test
6464
fun `Should resolve correct main class of main function inside companion object`() {
6565
val root = testResourcesRoot().resolve(workspaceRoot)
66-
val executeCommandParams = ExecuteCommandParams(RESOLVE_MAIN, listOf(Gson().toJsonTree(root.resolve(file).toUri().toString())))
66+
val fileUri = root.resolve(file).toUri().toString()
6767

68-
val commandResult = languageServer.workspaceService.executeCommand(executeCommandParams).get()
68+
val result = languageServer.getProtocolExtensionService().mainClass(TextDocumentIdentifier(fileUri)).get()
6969

70-
assertNotNull(commandResult)
71-
val mainInfo = commandResult as Map<String, Any>
70+
assertNotNull(result)
71+
val mainInfo = result as Map<String, Any>
7272
assertEquals("test.my.companion.SweetPotato", mainInfo["mainClass"])
7373
assertEquals(Range(Position(8, 8), Position(11, 9)), mainInfo["range"])
7474
assertEquals(root.toString(), mainInfo["projectRoot"])

0 commit comments

Comments
 (0)