Skip to content

Commit eb1a418

Browse files
authored
Merge pull request #267 from fwcd/progress
Emit progress notifications
2 parents 0ded408 + e165f60 commit eb1a418

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

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

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.eclipse.lsp4j.services.LanguageClientAware
88
import org.eclipse.lsp4j.services.LanguageServer
99
import org.javacs.kt.commands.ALL_COMMANDS
1010
import org.javacs.kt.externalsources.JarClassContentProvider
11+
import org.javacs.kt.util.AsyncExecutor
1112
import org.javacs.kt.util.TemporaryDirectory
1213
import org.javacs.kt.util.parseURI
1314
import java.net.URI
@@ -29,8 +30,12 @@ class KotlinLanguageServer : LanguageServer, LanguageClientAware, Closeable {
2930
private val workspaces = KotlinWorkspaceService(sourceFiles, sourcePath, classPath, textDocuments, config)
3031
private val protocolExtensions = KotlinProtocolExtensionService(uriContentProvider)
3132

33+
private lateinit var client: LanguageClient
34+
private val async = AsyncExecutor()
35+
3236
override fun connect(client: LanguageClient) {
33-
connectLoggingBackend(client)
37+
this.client = client
38+
connectLoggingBackend()
3439

3540
workspaces.connect(client)
3641
textDocuments.connect(client)
@@ -45,7 +50,7 @@ class KotlinLanguageServer : LanguageServer, LanguageClientAware, Closeable {
4550
@JsonDelegate
4651
fun getProtocolExtensionService(): KotlinProtocolExtensions = protocolExtensions
4752

48-
override fun initialize(params: InitializeParams): CompletableFuture<InitializeResult> {
53+
override fun initialize(params: InitializeParams): CompletableFuture<InitializeResult> = async.compute {
4954
val serverCapabilities = ServerCapabilities()
5055
serverCapabilities.setTextDocumentSync(TextDocumentSyncKind.Incremental)
5156
serverCapabilities.workspace = WorkspaceServerCapabilities()
@@ -67,22 +72,52 @@ class KotlinLanguageServer : LanguageServer, LanguageClientAware, Closeable {
6772
val clientCapabilities = params.capabilities
6873
config.completion.snippets.enabled = clientCapabilities?.textDocument?.completion?.completionItem?.snippetSupport ?: false
6974

70-
if (params.rootUri != null) {
71-
LOG.info("Adding workspace {} to source path", params.rootUri)
75+
val folders = params.workspaceFolders
76+
77+
fun reportProgress(notification: WorkDoneProgressNotification) {
78+
params.workDoneToken?.let {
79+
client.notifyProgress(ProgressParams(it, notification))
80+
}
81+
}
82+
83+
reportProgress(WorkDoneProgressBegin().apply {
84+
title = "Adding Kotlin workspace folders"
85+
percentage = 0
86+
})
7287

73-
val root = Paths.get(parseURI(params.rootUri))
88+
folders.forEachIndexed { i, folder ->
89+
LOG.info("Adding workspace folder {}", folder.name)
90+
val progressPrefix = "[${i + 1}/${folders.size}] ${folder.name}"
91+
val progressPercent = (100 * i) / folders.size
7492

93+
reportProgress(WorkDoneProgressReport().apply {
94+
message = "$progressPrefix: Updating source path"
95+
percentage = progressPercent
96+
})
97+
98+
val root = Paths.get(parseURI(folder.uri))
7599
sourceFiles.addWorkspaceRoot(root)
100+
101+
reportProgress(WorkDoneProgressReport().apply {
102+
message = "$progressPrefix: Updating class path"
103+
percentage = progressPercent
104+
})
105+
76106
val refreshed = classPath.addWorkspaceRoot(root)
77107
if (refreshed) {
108+
reportProgress(WorkDoneProgressReport().apply {
109+
message = "$progressPrefix: Refreshing source path"
110+
percentage = progressPercent
111+
})
112+
78113
sourcePath.refresh()
79114
}
80115
}
81116

82-
return completedFuture(InitializeResult(serverCapabilities))
117+
InitializeResult(serverCapabilities)
83118
}
84119

85-
private fun connectLoggingBackend(client: LanguageClient) {
120+
private fun connectLoggingBackend() {
86121
val backend: (LogMessage) -> Unit = {
87122
client.logMessage(MessageParams().apply {
88123
type = it.level.toLSPMessageType()
@@ -104,6 +139,7 @@ class KotlinLanguageServer : LanguageServer, LanguageClientAware, Closeable {
104139
textDocumentService.close()
105140
classPath.close()
106141
tempDirectory.close()
142+
async.shutdown(awaitTermination = true)
107143
}
108144

109145
override fun shutdown(): CompletableFuture<Any> {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ abstract class LanguageServerTestFixture(relativeWorkspaceRoot: String) : Langua
3232
}
3333
}
3434

35-
init.rootUri = workspaceRoot.toUri().toString()
36-
languageServer.initialize(init)
35+
init.workspaceFolders = listOf(WorkspaceFolder().apply {
36+
name = workspaceRoot.fileName.toString()
37+
uri = workspaceRoot.toUri().toString()
38+
})
3739
languageServer.connect(this)
40+
languageServer.initialize(init).join()
3841

3942
return languageServer
4043
}

0 commit comments

Comments
 (0)