Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kizeevov committed May 24, 2024
1 parent 91a36c3 commit 586e2c0
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 349 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.intellij.platform.lsp.api.Lsp4jClient
import com.intellij.platform.lsp.api.LspServerNotificationsHandler
import org.eclipse.lsp4j.jsonrpc.services.JsonNotification

class SlintLanguageClient(
class SlintLspClient(
serverNotificationsHandler: LspServerNotificationsHandler
) : Lsp4jClient(serverNotificationsHandler) {
@JsonNotification("experimental/serverStatus")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.slint.ideaplugin.ide.lsp

import com.intellij.openapi.vfs.VirtualFile
import com.intellij.platform.lsp.api.customization.LspFormattingSupport
import dev.slint.ideaplugin.lang.SlintFileType

@Suppress("UnstableApiUsage")
class SlintLspFormattingSupport : LspFormattingSupport() {
override fun shouldFormatThisFileExclusivelyByServer(
file: VirtualFile,
ideCanFormatThisFileItself: Boolean,
serverExplicitlyWantsToFormatThisFile: Boolean
) = file.fileType == SlintFileType
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import org.eclipse.lsp4j.jsonrpc.services.JsonRequest
import org.eclipse.lsp4j.services.LanguageServer
import java.util.concurrent.CompletableFuture

interface SlintLanguageServer : LanguageServer {
interface SlintLspServer : LanguageServer {
@JsonRequest("slint/load_file")
fun loadFile(path: String): CompletableFuture<String>
}
}

Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package dev.slint.ideaplugin.ide.lsp

import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.execution.process.OSProcessHandler
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.platform.lsp.api.Lsp4jClient
import com.intellij.platform.lsp.api.LspServerListener
import com.intellij.platform.lsp.api.LspServerNotificationsHandler
import com.intellij.platform.lsp.api.ProjectWideLspServerDescriptor
import com.intellij.platform.lsp.api.*
import com.intellij.platform.lsp.api.customization.LspCompletionSupport
import com.intellij.platform.lsp.api.customization.LspFormattingSupport
import dev.slint.ideaplugin.ide.settings.SlintSettingsState
import dev.slint.ideaplugin.lang.SlintFileType
import org.eclipse.lsp4j.services.LanguageServer
Expand All @@ -22,13 +19,10 @@ class SlintLspServerDescriptor(project: Project) : ProjectWideLspServerDescripto

override fun createInitializationOptions(): Any = SlintSettingsState.getInstance().lspSettings

// Suppression of the verifier plugin warning
// override fun createLsp4jClient(handler: LspServerNotificationsHandler): Lsp4jClient = LspLanguageClient(handler)
override fun createLsp4jClient(handler: LspServerNotificationsHandler): Lsp4jClient = SlintLspClient(handler)

override fun startServerProcess(): OSProcessHandler =
ServerProcessHandler.addListeners(super.startServerProcess(), project)

override val lsp4jServerClass: Class<out LanguageServer> = SlintLanguageServer::class.java
override val lsp4jServerClass: Class<out LanguageServer> = SlintLspServer::class.java
override val lspServerListener: LspServerListener = SlintLspServerListener(project)
override val lspCompletionSupport: LspCompletionSupport = SlintLspCompletionSupport()
override val lspFormattingSupport: LspFormattingSupport = SlintLspFormattingSupport()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package dev.slint.ideaplugin.ide.lsp

import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.platform.lsp.api.LspServer
import com.intellij.platform.lsp.api.LspServerSupportProvider
import com.intellij.platform.lsp.api.lsWidget.LspServerWidgetItem
import dev.slint.ideaplugin.SlintIcons
import dev.slint.ideaplugin.ide.settings.SlintSettingsConfigurable
import dev.slint.ideaplugin.lang.SlintFileType

@Suppress("UnstableApiUsage")
Expand All @@ -15,4 +19,7 @@ class SlintLspServerSupportProvider : LspServerSupportProvider {
if (file.fileType != SlintFileType) return
serverStarter.ensureServerStarted(SlintLspServerDescriptor(project))
}

override fun createLspServerWidgetItem(lspServer: LspServer, currentFile: VirtualFile?): LspServerWidgetItem =
LspServerWidgetItem(lspServer, currentFile, SlintIcons.SLINT, SlintSettingsConfigurable::class.java)
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,19 @@
package dev.slint.ideaplugin.ide.services

import com.intellij.notification.NotificationGroupManager
import com.intellij.notification.NotificationType
import com.intellij.openapi.components.Service
import com.intellij.openapi.project.Project
import com.intellij.platform.lsp.api.LspServer
import com.intellij.platform.lsp.api.LspServerManager
import com.intellij.platform.lsp.api.LspServerState
import dev.slint.ideaplugin.SlintBundle
import dev.slint.ideaplugin.ide.lsp.SlintLspServerSupportProvider
import org.eclipse.lsp4j.*

@Service(Service.Level.PROJECT)
@Suppress("UnstableApiUsage")
class SlintServerService(private val project: Project) {
private var status = Status.TERMINATING

val isRunning get() = status == Status.RUNNING

val isRestarting get() = status == Status.RESTARTING

fun setRunningStatus() {
status = Status.RUNNING
}

fun setTerminatingStatus() {
status = Status.TERMINATING
notifyTerminating()
}

fun restartServer() {
LspServerManager.getInstance(project)
.stopAndRestartIfNeeded(SlintLspServerSupportProvider::class.java)

status = Status.RESTARTING
notifyRestart()
}

fun previewComponent(path: String, component: String) {
Expand All @@ -50,60 +29,10 @@ class SlintServerService(private val project: Project) {
}
}

fun formatting(path: String, tabSize: Int, insertSpaces: Boolean): List<TextEdit>? {
val server = getActiveServer() ?: return null

val params = DocumentFormattingParams(
TextDocumentIdentifier(path),
FormattingOptions(tabSize, insertSpaces)
)

return server.sendRequestSync { it.textDocumentService.formatting(params) }
}

private fun getActiveServer(): LspServer? {
val servers = LspServerManager.getInstance(project)
.getServersForProvider(SlintLspServerSupportProvider::class.java)

return servers.firstOrNull { it.state == LspServerState.Running }
}

private fun notifyRun() {
NotificationGroupManager.getInstance()
.getNotificationGroup("Slint")
.createNotification(
SlintBundle.message("slint.language.server.is.running"),
"",
NotificationType.INFORMATION
)
.notify(project)
}

private fun notifyRestart() {
NotificationGroupManager.getInstance()
.getNotificationGroup("Slint")
.createNotification(
SlintBundle.message("slint.language.server.restarted"),
"",
NotificationType.INFORMATION
)
.notify(project)
}

private fun notifyTerminating() {
NotificationGroupManager.getInstance()
.getNotificationGroup("Slint")
.createNotification(
SlintBundle.message("slint.language.server.is.stopped"),
"",
NotificationType.WARNING
)
.notify(project)
}

private enum class Status {
RUNNING,
RESTARTING,
TERMINATING
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ data class SlintLspSettings(
var noToolbar: Boolean = false,
var includePaths: MutableList<String> = mutableListOf(),
var useExternalLsp: Boolean = false,
var isRestartLsp: Boolean = true,
)

enum class SlintStyle {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,6 @@ class SlintSettingsComponent(lspSettings: SlintLspSettings) {
.onApply { pathsTablePanel.onApply(lspSettings.includePaths) }
.onReset { pathsTablePanel.onReset(lspSettings.includePaths) }
}
row {
checkBox("Restart LSP server")
.comment(
"Restarting the LSP server after a crash or shutdown. " +
"If there are critical errors in the LSP server, the server will restart indefinitely." +
"<br>Manually restart the server: 'Tools -> Restart Slint LSP'"
)
.bindSelected(lspSettings::isRestartLsp)
}
}
group("Preview") {
row {
Expand Down
Loading

0 comments on commit 586e2c0

Please sign in to comment.