From 15df78c16f789ee141397e8ed9722eea4fb568c8 Mon Sep 17 00:00:00 2001 From: Oleg Kizeev Date: Sun, 28 Apr 2024 12:52:55 +0700 Subject: [PATCH] Version idea2024 and above --- .idea/kotlinc.xml | 2 +- build.gradle.kts | 31 +++-- gradle.properties | 4 +- gradle/libs.versions.toml | 8 +- .../slint/ideaplugin/ide/actions/LspAction.kt | 23 ---- .../ideaplugin/ide/actions/PreviewAction.kt | 19 ++-- .../ide/actions/PreviewComponentAction.kt | 27 +++-- .../formatter/SlintLspFormattingService.kt | 13 +-- .../ideaplugin/ide/lsp/LspLanguageClient.kt | 107 +----------------- .../ide/lsp/SlintLspServerDescriptor.kt | 2 +- .../ide/lsp/requests/PreviewMessageRequest.kt | 23 ---- .../ide/services/SlintServerService.kt | 31 ++++- src/main/resources/META-INF/plugin.xml | 6 +- 13 files changed, 83 insertions(+), 213 deletions(-) delete mode 100644 src/main/kotlin/dev/slint/ideaplugin/ide/actions/LspAction.kt delete mode 100644 src/main/kotlin/dev/slint/ideaplugin/ide/lsp/requests/PreviewMessageRequest.kt diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml index f8467b4..fe63bb6 100644 --- a/.idea/kotlinc.xml +++ b/.idea/kotlinc.xml @@ -1,6 +1,6 @@ - \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index fd1a08f..f876bba 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -66,13 +66,12 @@ changelog { tasks { generateLexer { sourceFile = file("src/main/grammars/SlintLexer.flex") - targetDir = "src/gen/dev/slint/ideaplugin/lang/lexer" - targetClass = "_SlintLexer" + targetOutputDir = file("src/gen/dev/slint/ideaplugin/lang/lexer") purgeOldFiles = true } generateParser { sourceFile = file("src/main/grammars/SlintParser.bnf") - targetRoot = "src/gen" + targetRootOutputDir = file("src/gen") pathToParser = "dev/slint/ideaplugin/lang/parser/SlintParser.java" pathToPsiRoot = "dev/slint/ideaplugin/lang/psi" purgeOldFiles = true @@ -137,21 +136,21 @@ tasks { channels = properties("pluginVersion").map { listOf(it.split('-').getOrElse(1) { "default" }.split('.').first()) } } - task("downloadSlintLspVscodePlugin", type = Download::class) { - src("https://Slint.gallery.vsassets.io/_apis/public/gallery/publisher/Slint/extension/slint/${slintLspVersion}/assetbyname/Microsoft.VisualStudio.Services.VSIXPackage") - dest("${project.buildDir}/tmp/slint-${slintLspVersion}-vscode-plugin.zip") - onlyIfModified(true) - } - - task("extractSlintLspVscodePlugin", type = Copy::class) { - dependsOn("downloadSlintLspVscodePlugin") - from(zipTree("${project.buildDir}/tmp/slint-${slintLspVersion}-vscode-plugin.zip")) { - destinationDir = file("${project.buildDir}/tmp/slint-vscode-plugin") - } - } +// task("downloadSlintLspVscodePlugin", type = Download::class) { +// src("https://Slint.gallery.vsassets.io/_apis/public/gallery/publisher/Slint/extension/slint/${slintLspVersion}/assetbyname/Microsoft.VisualStudio.Services.VSIXPackage") +// dest("${project.buildDir}/tmp/slint-${slintLspVersion}-vscode-plugin.zip") +// onlyIfModified(true) +// } +// +// task("extractSlintLspVscodePlugin", type = Copy::class) { +// dependsOn("downloadSlintLspVscodePlugin") +// from(zipTree("${project.buildDir}/tmp/slint-${slintLspVersion}-vscode-plugin.zip")) { +// destinationDir = file("${project.buildDir}/tmp/slint-vscode-plugin") +// } +// } prepareSandbox { - dependsOn("extractSlintLspVscodePlugin") + // dependsOn("extractSlintLspVscodePlugin") from("${project.buildDir}/tmp/slint-vscode-plugin/extension/bin") { into("${intellij.pluginName.get()}/language-server/bin") } diff --git a/gradle.properties b/gradle.properties index 42d9206..70716d5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,8 +12,8 @@ pluginName = SlintPlugin pluginVersion = 1.0.2 platformType = IU # platformType = CL -platformVersion = 232-EAP-SNAPSHOT -pluginSinceBuild = 232 +platformVersion = 241-EAP-SNAPSHOT +pluginSinceBuild = 241 pluginUntilBuild = platformPlugins = jvmVersion = 17 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 7d7f27e..e6e0795 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -3,10 +3,10 @@ annotations = "24.0.1" # plugins -kotlin = "1.9.10" +kotlin = "1.9.23" changelog = "2.2.0" -intellij = "1.16.0" -grammarkit = "2022.3.1" +intellij = "1.17.2" +grammarkit = "2022.3.2.2" download = "5.6.0" #gradleIntelliJPlugin = "1.15.0" @@ -21,4 +21,4 @@ kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } changelog = { id = "org.jetbrains.changelog", version.ref = "changelog" } intellij = { id = "org.jetbrains.intellij", version.ref = "intellij" } grammarkit = { id = "org.jetbrains.grammarkit", version.ref = "grammarkit" } -download = { id = "de.undercouch.download", version.ref = "download"} \ No newline at end of file +download = { id = "de.undercouch.download", version.ref = "download"} diff --git a/src/main/kotlin/dev/slint/ideaplugin/ide/actions/LspAction.kt b/src/main/kotlin/dev/slint/ideaplugin/ide/actions/LspAction.kt deleted file mode 100644 index 47b6ace..0000000 --- a/src/main/kotlin/dev/slint/ideaplugin/ide/actions/LspAction.kt +++ /dev/null @@ -1,23 +0,0 @@ -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.platform.lsp.api.LspServer -import dev.slint.ideaplugin.ide.services.SlintServerService -import javax.swing.Icon - -abstract class LspAction(text: String, description: String?, icon: Icon?) : AnAction(text, description, icon) { - override fun actionPerformed(e: AnActionEvent) { - val project = e.project ?: return - - val servers = project.service().getServers() - if (servers.isEmpty()) { - return - } - - actionPerformed(e, servers) - } - - abstract fun actionPerformed(e: AnActionEvent, servers: List) -} \ No newline at end of file diff --git a/src/main/kotlin/dev/slint/ideaplugin/ide/actions/PreviewAction.kt b/src/main/kotlin/dev/slint/ideaplugin/ide/actions/PreviewAction.kt index ef97d0b..a641efd 100644 --- a/src/main/kotlin/dev/slint/ideaplugin/ide/actions/PreviewAction.kt +++ b/src/main/kotlin/dev/slint/ideaplugin/ide/actions/PreviewAction.kt @@ -1,29 +1,28 @@ package dev.slint.ideaplugin.ide.actions -import com.intellij.icons.AllIcons -import com.intellij.notification.Notification +import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.CommonDataKeys import com.intellij.openapi.actionSystem.CommonDataKeys.VIRTUAL_FILE -import com.intellij.platform.lsp.api.LspServer -import dev.slint.ideaplugin.ide.lsp.requests.PreviewMessageRequest +import com.intellij.openapi.components.service +import dev.slint.ideaplugin.ide.services.SlintServerService import dev.slint.ideaplugin.lang.SlintLanguage import kotlin.io.path.Path -internal class PreviewAction(private val notification: Notification? = null) : - LspAction("Show All Preview", null, AllIcons.Actions.Preview) { +internal class PreviewAction : AnAction() { + override fun update(e: AnActionEvent) { val psiFile = e.getData(CommonDataKeys.PSI_FILE) ?: return e.presentation.isEnabledAndVisible = psiFile.language.isKindOf(SlintLanguage.INSTANCE) } - override fun actionPerformed(e: AnActionEvent, servers: List) { + override fun actionPerformed(e: AnActionEvent) { val virtualFile = e.getData(VIRTUAL_FILE) ?: return val uriFile = Path(virtualFile.path).toUri() - val request = PreviewMessageRequest(servers.first(), uriFile.toString(), "") - servers.first().requestExecutor.sendRequestSync(request) + val project = e.project ?: return + val slintServerService = project.service() - notification?.expire() + slintServerService.previewComponent(uriFile.toString(), "") } } \ No newline at end of file diff --git a/src/main/kotlin/dev/slint/ideaplugin/ide/actions/PreviewComponentAction.kt b/src/main/kotlin/dev/slint/ideaplugin/ide/actions/PreviewComponentAction.kt index 647d1e7..52201c3 100644 --- a/src/main/kotlin/dev/slint/ideaplugin/ide/actions/PreviewComponentAction.kt +++ b/src/main/kotlin/dev/slint/ideaplugin/ide/actions/PreviewComponentAction.kt @@ -1,25 +1,24 @@ package dev.slint.ideaplugin.ide.actions import com.intellij.icons.AllIcons -import com.intellij.notification.Notification +import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent -import com.intellij.openapi.actionSystem.CommonDataKeys -import com.intellij.platform.lsp.api.LspServer -import dev.slint.ideaplugin.ide.lsp.requests.PreviewMessageRequest +import com.intellij.openapi.actionSystem.CommonDataKeys.VIRTUAL_FILE +import com.intellij.openapi.components.service +import dev.slint.ideaplugin.ide.services.SlintServerService import kotlin.io.path.Path -internal class PreviewComponentAction( - private val componentName: String, - private val notification: Notification? = null, -) : - LspAction("Show Component Preview", null, AllIcons.Actions.ShowCode) { - override fun actionPerformed(e: AnActionEvent, servers: List) { - val virtualFile = e.getData(CommonDataKeys.VIRTUAL_FILE) ?: return + +internal class PreviewComponentAction(private val componentName: String) : + AnAction("Show Component Preview", null, AllIcons.Actions.ShowCode) { + + override fun actionPerformed(e: AnActionEvent) { + val virtualFile = e.getData(VIRTUAL_FILE) ?: return val uriFile = Path(virtualFile.path).toUri() - val request = PreviewMessageRequest(servers.first(), uriFile.toString(), componentName) - servers.first().requestExecutor.sendRequestSync(request) + val project = e.project ?: return + val slintServerService = project.service() - notification?.expire() + slintServerService.previewComponent(uriFile.toString(), componentName) } } \ No newline at end of file diff --git a/src/main/kotlin/dev/slint/ideaplugin/ide/formatter/SlintLspFormattingService.kt b/src/main/kotlin/dev/slint/ideaplugin/ide/formatter/SlintLspFormattingService.kt index c042cbf..88b3bcb 100644 --- a/src/main/kotlin/dev/slint/ideaplugin/ide/formatter/SlintLspFormattingService.kt +++ b/src/main/kotlin/dev/slint/ideaplugin/ide/formatter/SlintLspFormattingService.kt @@ -28,19 +28,15 @@ class SlintLspFormattingService : AsyncDocumentFormattingService() { val uriFile = Path(file.path).toUri() - val params = DocumentFormattingParams( - TextDocumentIdentifier(uriFile.toString()), - FormattingOptions(4, true) - ) - - val server = project.service().getServers().firstOrNull() ?: return null + val slintServerService = project.service() val fileEditorManager = FileEditorManager.getInstance(project) val editor = fileEditorManager.selectedTextEditor ?: return null return object : FormattingTask { override fun run() { - val edits = server.lsp4jServer.textDocumentService.formatting(params).join() - edits.forEach { textEdit -> + val edits = slintServerService.formatting(uriFile.toString(), 4, true) + + edits?.forEach { textEdit -> WriteCommandAction.runWriteCommandAction(project) { FileEditorService.applyTextChanges(textEdit, editor) } @@ -50,7 +46,6 @@ class SlintLspFormattingService : AsyncDocumentFormattingService() { override fun cancel(): Boolean { return true } - } } diff --git a/src/main/kotlin/dev/slint/ideaplugin/ide/lsp/LspLanguageClient.kt b/src/main/kotlin/dev/slint/ideaplugin/ide/lsp/LspLanguageClient.kt index 580e2c4..eab7bb6 100644 --- a/src/main/kotlin/dev/slint/ideaplugin/ide/lsp/LspLanguageClient.kt +++ b/src/main/kotlin/dev/slint/ideaplugin/ide/lsp/LspLanguageClient.kt @@ -1,116 +1,11 @@ package dev.slint.ideaplugin.ide.lsp -import com.intellij.openapi.components.service -import com.intellij.openapi.project.Project import com.intellij.platform.lsp.api.Lsp4jClient import com.intellij.platform.lsp.api.LspServerNotificationsHandler -import dev.slint.ideaplugin.ide.services.FileEditorService -import org.eclipse.lsp4j.* import org.eclipse.lsp4j.jsonrpc.services.JsonNotification -import java.util.concurrent.CompletableFuture -class LspLanguageClient(project: Project) : - Lsp4jClient(LspServerNotificationsMiddleware(project)) { +class LspLanguageClient(handler: LspServerNotificationsHandler) : Lsp4jClient(handler) { @JsonNotification("experimental/serverStatus") fun serverStatus(status: Any?) { } -} - - -class LspServerNotificationsMiddleware( - project: Project -) : LspServerNotificationsHandler { - private val fileEditorService = project.service() - - override fun applyEdit(params: ApplyWorkspaceEditParams): CompletableFuture { - fileEditorService.applyEdit(params) - return CompletableFuture.supplyAsync { - ApplyWorkspaceEditResponse(true) - } - } - - override fun configuration(params: ConfigurationParams): CompletableFuture> { - return CompletableFuture.supplyAsync { - null - } - } - - override fun createProgress(params: WorkDoneProgressCreateParams): CompletableFuture { - return CompletableFuture.supplyAsync { - null - } - } - - override fun logMessage(params: MessageParams) {} - - override fun logTrace(params: LogTraceParams) {} - - override fun notifyProgress(params: ProgressParams) {} - - override fun publishDiagnostics(params: PublishDiagnosticsParams) {} - - override fun refreshCodeLenses(): CompletableFuture { - return CompletableFuture.supplyAsync { - null - } - } - - override fun refreshDiagnostics(): CompletableFuture { - return CompletableFuture.supplyAsync { - null - } - } - - override fun refreshInlayHints(): CompletableFuture { - return CompletableFuture.supplyAsync { - null - } - } - - override fun refreshInlineValues(): CompletableFuture { - return CompletableFuture.supplyAsync { - null - } - } - - override fun refreshSemanticTokens(): CompletableFuture { - return CompletableFuture.supplyAsync { - null - } - } - - override fun registerCapability(params: RegistrationParams): CompletableFuture { - return CompletableFuture.supplyAsync { - null - } - } - - override fun showDocument(params: ShowDocumentParams): CompletableFuture { - fileEditorService.showDocument(params) - return CompletableFuture.supplyAsync { - ShowDocumentResult(true) - } - } - - override fun showMessage(params: MessageParams) {} - - override fun showMessageRequest(params: ShowMessageRequestParams): CompletableFuture { - return CompletableFuture.supplyAsync { - null - } - } - - override fun telemetryEvent(`object`: Any) {} - - override fun unregisterCapability(params: UnregistrationParams): CompletableFuture { - return CompletableFuture.supplyAsync { - null - } - } - - override fun workspaceFolders(): CompletableFuture> { - return CompletableFuture.supplyAsync { - null - } - } } \ No newline at end of file diff --git a/src/main/kotlin/dev/slint/ideaplugin/ide/lsp/SlintLspServerDescriptor.kt b/src/main/kotlin/dev/slint/ideaplugin/ide/lsp/SlintLspServerDescriptor.kt index ba0710e..2b0574f 100644 --- a/src/main/kotlin/dev/slint/ideaplugin/ide/lsp/SlintLspServerDescriptor.kt +++ b/src/main/kotlin/dev/slint/ideaplugin/ide/lsp/SlintLspServerDescriptor.kt @@ -22,7 +22,7 @@ class SlintLspServerDescriptor(project: Project) : ProjectWideLspServerDescripto override fun createInitializationOptions(): Any = SlintSettingsState.getInstance().lspSettings - override fun createLsp4jClient(handler: LspServerNotificationsHandler): Lsp4jClient = LspLanguageClient(project) + override fun createLsp4jClient(handler: LspServerNotificationsHandler): Lsp4jClient = LspLanguageClient(handler) override fun startServerProcess(): OSProcessHandler = ServerProcessHandler.addListeners(super.startServerProcess(), project) diff --git a/src/main/kotlin/dev/slint/ideaplugin/ide/lsp/requests/PreviewMessageRequest.kt b/src/main/kotlin/dev/slint/ideaplugin/ide/lsp/requests/PreviewMessageRequest.kt deleted file mode 100644 index b32a169..0000000 --- a/src/main/kotlin/dev/slint/ideaplugin/ide/lsp/requests/PreviewMessageRequest.kt +++ /dev/null @@ -1,23 +0,0 @@ -package dev.slint.ideaplugin.ide.lsp.requests - -import com.intellij.platform.lsp.api.LspServer -import com.intellij.platform.lsp.api.requests.LspRequest -import org.eclipse.lsp4j.ExecuteCommandParams -import java.util.concurrent.CompletableFuture - -class PreviewMessageRequest(private val server: LspServer, private val path: String, private val component: String) : - LspRequest(server) { - override fun preprocessResponse(serverResponse: Any): Any { - return serverResponse - } - - override fun sendRequest(): CompletableFuture { - return server.lsp4jServer.workspaceService - .executeCommand( - ExecuteCommandParams( - "slint/showPreview", - listOf(path, component) - ) - ) - } -} \ No newline at end of file diff --git a/src/main/kotlin/dev/slint/ideaplugin/ide/services/SlintServerService.kt b/src/main/kotlin/dev/slint/ideaplugin/ide/services/SlintServerService.kt index 730bbcf..0519c2f 100644 --- a/src/main/kotlin/dev/slint/ideaplugin/ide/services/SlintServerService.kt +++ b/src/main/kotlin/dev/slint/ideaplugin/ide/services/SlintServerService.kt @@ -6,9 +6,10 @@ 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.SlintLanguageServer import dev.slint.ideaplugin.ide.lsp.SlintLspServerSupportProvider +import org.eclipse.lsp4j.* @Service(Service.Level.PROJECT) @Suppress("UnstableApiUsage") @@ -36,11 +37,35 @@ class SlintServerService(private val project: Project) { notifyRestart() } - fun getServers(): List { + fun previewComponent(path: String, component: String) { + val server = getActiveServer() ?: return + + server.sendRequestSync { + it.workspaceService.executeCommand( + ExecuteCommandParams( + "slint/showPreview", + listOf(path, component) + ) + ) + } + } + + fun formatting(path: String, tabSize: Int, insertSpaces: Boolean): List? { + 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.filter { it.lsp4jServer is SlintLanguageServer } + return servers.firstOrNull { it.state == LspServerState.Running } } private fun notifyRun() { diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 17a8e3b..d50432f 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -73,7 +73,11 @@ - +