-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatic restart of the LSP-server if it crashed or shutdown
- Loading branch information
Showing
14 changed files
with
418 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/dev/slint/ideaplugin/ide/actions/RestartLspAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package dev.slint.ideaplugin.ide.actions | ||
|
||
import com.intellij.openapi.actionSystem.AnAction | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.intellij.openapi.components.service | ||
import com.intellij.openapi.project.DumbAware | ||
import dev.slint.ideaplugin.ide.services.SlintServerService | ||
|
||
class RestartLspAction: AnAction(), DumbAware { | ||
override fun actionPerformed(e: AnActionEvent) { | ||
val project = e.project ?: return | ||
val slintServerService = project.service<SlintServerService>() | ||
slintServerService.restartServer() | ||
} | ||
|
||
companion object { | ||
const val ID: String = "Slint.RestartLspAction" | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/main/kotlin/dev/slint/ideaplugin/ide/lsp/CommandLineHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package dev.slint.ideaplugin.ide.lsp | ||
|
||
import com.intellij.execution.configurations.GeneralCommandLine | ||
import com.intellij.ide.plugins.PluginManager | ||
import com.intellij.openapi.extensions.PluginId | ||
import com.intellij.openapi.util.SystemInfo | ||
import dev.slint.ideaplugin.ide.settings.SlintBackend | ||
import dev.slint.ideaplugin.ide.settings.SlintSettingsState | ||
import dev.slint.ideaplugin.ide.settings.SlintStyle | ||
import java.nio.file.Path | ||
|
||
object CommandLineHandler { | ||
fun createCommandLine(): GeneralCommandLine { | ||
val settingState = SlintSettingsState.getInstance().lspSettings | ||
|
||
val parameters = mutableListOf<String>() | ||
if (settingState.args.isNotEmpty()) { | ||
val args = settingState.args.split("\\s+".toRegex()) | ||
parameters.addAll(args) | ||
} | ||
|
||
if (settingState.includePaths.isNotEmpty()) { | ||
parameters.add("-I") | ||
settingState.includePaths.forEach { | ||
parameters.add("'${it}'") | ||
} | ||
} | ||
|
||
if (settingState.backend != SlintBackend.DEFAULT) { | ||
parameters.add("--backend") | ||
parameters.add(settingState.backend.toString()) | ||
} | ||
|
||
if (settingState.style != SlintStyle.DEFAULT) { | ||
parameters.add("--style") | ||
parameters.add(settingState.style.toString()) | ||
} | ||
|
||
if (settingState.noToolbar) { | ||
parameters.add("--no-toolbar") | ||
} | ||
|
||
val path = if (settingState.useExternalLsp) { | ||
settingState.path | ||
} else { | ||
getEmbeddedLspPath().toString() | ||
} | ||
|
||
return GeneralCommandLine(path).apply { | ||
addParameters(parameters) | ||
withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE) | ||
withCharset(Charsets.UTF_8) | ||
} | ||
} | ||
|
||
private fun getEmbeddedLspPath(): Path? { | ||
val pluginPath = PluginManager | ||
.getInstance() | ||
.findEnabledPlugin(PluginId.getId(dev.slint.ideaplugin.SLINT_PLUGIN_ID)) | ||
?.pluginPath | ||
|
||
val programName: String | ||
|
||
if (SystemInfo.isMac) { | ||
programName = "Slint Live Preview.app/Contents/MacOS/slint-lsp" | ||
} else if (SystemInfo.isLinux) { | ||
programName = when (SystemInfo.OS_ARCH) { | ||
"x64" -> { | ||
"slint-lsp-x86_64-unknown-linux-gnu" | ||
} | ||
|
||
"arm" -> { | ||
"slint-lsp-armv7-unknown-linux-gnueabihf" | ||
} | ||
|
||
"arm64" -> { | ||
"slint-lsp-aarch64-unknown-linux-gnu" | ||
} | ||
|
||
else -> { | ||
return null | ||
} | ||
} | ||
} else if (SystemInfo.isWindows) { | ||
programName = "slint-lsp-x86_64-pc-windows-msvc.exe" | ||
} else { | ||
return null | ||
} | ||
|
||
return pluginPath | ||
?.resolve("language-server/bin") | ||
?.resolve(programName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/main/kotlin/dev/slint/ideaplugin/ide/lsp/ServerProcessHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package dev.slint.ideaplugin.ide.lsp | ||
|
||
import com.intellij.execution.process.OSProcessHandler | ||
import com.intellij.execution.process.ProcessAdapter | ||
import com.intellij.execution.process.ProcessEvent | ||
import com.intellij.openapi.components.service | ||
import com.intellij.openapi.project.Project | ||
import dev.slint.ideaplugin.ide.services.SlintServerService | ||
import dev.slint.ideaplugin.ide.settings.SlintSettingsState | ||
import java.util.* | ||
import kotlin.concurrent.schedule | ||
|
||
object ServerProcessHandler { | ||
fun addListeners(handler: OSProcessHandler, project: Project): OSProcessHandler { | ||
handler.addProcessListener(object : ProcessAdapter() { | ||
override fun processTerminated(event: ProcessEvent) { | ||
super.processTerminated(event) | ||
|
||
val slintServerService = project.service<SlintServerService>() | ||
if (slintServerService.isRestarting) { | ||
return | ||
} | ||
|
||
slintServerService.setTerminatingStatus() | ||
|
||
val settingState = SlintSettingsState.getInstance().lspSettings | ||
if (!settingState.isRestartLsp) { | ||
return | ||
} | ||
|
||
Timer().schedule(3000) { | ||
slintServerService.restartServer() | ||
} | ||
} | ||
|
||
override fun processNotStarted() { | ||
super.processNotStarted() | ||
|
||
val slintServerService = project.service<SlintServerService>() | ||
slintServerService.setTerminatingStatus() | ||
} | ||
|
||
override fun startNotified(event: ProcessEvent) { | ||
super.startNotified(event) | ||
|
||
val slintServerService = project.service<SlintServerService>() | ||
slintServerService.setRunningStatus() | ||
} | ||
}) | ||
|
||
return handler | ||
} | ||
} | ||
|
||
fun startServerProcessHandler(handler: OSProcessHandler, project: Project): OSProcessHandler { | ||
handler.addProcessListener(object : ProcessAdapter() { | ||
override fun processTerminated(event: ProcessEvent) { | ||
super.processTerminated(event) | ||
|
||
val slintServerService = project.service<SlintServerService>() | ||
if (slintServerService.isRestarting) { | ||
return | ||
} | ||
|
||
slintServerService.setTerminatingStatus() | ||
|
||
val settingState = SlintSettingsState.getInstance().lspSettings | ||
if (!settingState.isRestartLsp) { | ||
return | ||
} | ||
|
||
Timer().schedule(3000) { | ||
slintServerService.restartServer() | ||
} | ||
} | ||
|
||
override fun processNotStarted() { | ||
super.processNotStarted() | ||
|
||
val slintServerService = project.service<SlintServerService>() | ||
slintServerService.setTerminatingStatus() | ||
} | ||
|
||
override fun startNotified(event: ProcessEvent) { | ||
super.startNotified(event) | ||
|
||
val slintServerService = project.service<SlintServerService>() | ||
slintServerService.setRunningStatus() | ||
} | ||
}) | ||
|
||
return handler | ||
} |
Oops, something went wrong.