diff --git a/src/main/java/org/nixos/idea/lsp/NixLspServerDescriptor.java b/src/main/java/org/nixos/idea/lsp/NixLspServerDescriptor.java deleted file mode 100644 index b73bd6f7..00000000 --- a/src/main/java/org/nixos/idea/lsp/NixLspServerDescriptor.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.nixos.idea.lsp; - -import com.intellij.execution.ExecutionException; -import com.intellij.execution.configurations.GeneralCommandLine; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.platform.lsp.api.ProjectWideLspServerDescriptor; -import com.intellij.util.execution.ParametersListUtil; -import org.jetbrains.annotations.NotNull; -import org.nixos.idea.file.NixFileType; - -import java.util.List; - -@SuppressWarnings("UnstableApiUsage") -final class NixLspServerDescriptor extends ProjectWideLspServerDescriptor { - - private final String myCommand; - - NixLspServerDescriptor(@NotNull Project project, NixLspSettings settings) { - super(project, "Nix"); - myCommand = settings.getCommand(); - } - - @Override - public @NotNull GeneralCommandLine createCommandLine() throws ExecutionException { - List argv = ParametersListUtil.parse(myCommand, false, true); - return new GeneralCommandLine(argv); - } - - @Override - public boolean isSupportedFile(@NotNull VirtualFile virtualFile) { - return virtualFile.getFileType() == NixFileType.INSTANCE; - } -} diff --git a/src/main/java/org/nixos/idea/lsp/NixLspServerDescriptor.kt b/src/main/java/org/nixos/idea/lsp/NixLspServerDescriptor.kt new file mode 100644 index 00000000..2e5ae79f --- /dev/null +++ b/src/main/java/org/nixos/idea/lsp/NixLspServerDescriptor.kt @@ -0,0 +1,54 @@ +package org.nixos.idea.lsp + +import com.google.gson.JsonObject +import com.intellij.execution.ExecutionException +import com.intellij.execution.configurations.GeneralCommandLine +import com.intellij.ide.BrowserUtil +import com.intellij.openapi.editor.Editor +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.ProjectWideLspServerDescriptor +import com.intellij.platform.lsp.api.customization.LspCodeActionsSupport +import com.intellij.platform.lsp.api.customization.LspCustomization +import com.intellij.platform.lsp.api.customization.LspIntentionAction +import com.intellij.psi.PsiFile +import com.intellij.util.execution.ParametersListUtil +import org.eclipse.lsp4j.CodeAction +import org.eclipse.lsp4j.WorkspaceEdit +import org.nixos.idea.file.NixFileType + +internal class NixLspServerDescriptor(project: Project, private var settings: NixLspSettings) : + ProjectWideLspServerDescriptor(project, "Nix") { + + @Throws(ExecutionException::class) + override fun createCommandLine(): GeneralCommandLine { + val argv = ParametersListUtil.parse(settings.command, false, true) + return GeneralCommandLine(argv) + } + + override fun isSupportedFile(file: VirtualFile): Boolean { + return file.fileType === NixFileType.INSTANCE + } + + override val lspCustomization: LspCustomization = object : LspCustomization() { + override val codeActionsCustomizer = object : LspCodeActionsSupport() { + private fun getNoogleUrl(codeAction: CodeAction): String? = + (codeAction.data as? JsonObject)?.get("noogleUrl")?.asString + + override fun createIntentionAction(lspServer: LspServer, codeAction: CodeAction): LspIntentionAction? { + if (getNoogleUrl(codeAction) != null) { + /* set an empty edit to prevent the IDE from calling + the LSP server and automatically opening the url */ + codeAction.edit = WorkspaceEdit() + return object : LspIntentionAction(lspServer, codeAction) { + override fun invoke(project: Project, editor: Editor, psiFile: PsiFile) { + BrowserUtil.open(getNoogleUrl(codeAction)!!) + } + } + } + return super.createIntentionAction(lspServer, codeAction) + } + } + } +}