Skip to content

Commit

Permalink
(#144) Simplify and clean up the startup a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Oct 20, 2023
1 parent 1e6c420 commit f584fd4
Showing 1 changed file with 8 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class LanguageServerEndpoint(

fun start() {
coroutineScope.launchNow {
checkStarted()
ensureStarted()
val capabilities = getServerCapabilities()
if (capabilities != null) {
//todo move it to LanguageHostConnectionManager (notify it when server initialized)
Expand All @@ -108,7 +108,7 @@ class LanguageServerEndpoint(
val uri = VfsUtil.toUri(File(file.path))
connectedEditors.computeIfAbsent(uri) {
coroutineScope.async {
checkStarted()
ensureStarted()
val capabilities = getServerCapabilities()
if (capabilities != null) {
LOG.runAndLogException {
Expand Down Expand Up @@ -147,7 +147,7 @@ class LanguageServerEndpoint(
}

private suspend fun getServerCapabilities(): ServerCapabilities? {
checkStarted()
ensureStarted()
val initialization = synchronized(serverInitializationLock) { serverInitialization }
return initialization?.await()?.capabilities
}
Expand Down Expand Up @@ -201,19 +201,13 @@ class LanguageServerEndpoint(
}
}

private suspend fun checkStarted() {
synchronized(serverInitializationLock) {
if (serverInitialization == null) {
serverInitialization = scheduleStart()
}
}

LOG.runAndLogException {
val initialization = synchronized(serverInitializationLock) {
serverInitialization
private suspend fun ensureStarted() {
val initialization = synchronized(serverInitializationLock) {
serverInitialization ?: scheduleStart().also {
serverInitialization = it
}
initialization?.await()
}
initialization.await()
}

private fun scheduleStart(): Deferred<InitializeResult?> {
Expand Down

0 comments on commit f584fd4

Please sign in to comment.