From cddf705c0544278556ab6ec9db72105a0d813a46 Mon Sep 17 00:00:00 2001 From: Knut Funkel Date: Wed, 18 Sep 2024 10:51:22 +0200 Subject: [PATCH] feat: send ai fix feedback --- .../io/snyk/plugin/ui/jcef/ApplyFixHandler.kt | 8 +++++--- .../ui/toolwindow/panels/JCEFDescriptionPanel.kt | 4 +++- .../snyk/common/lsp/LanguageServerWrapper.kt | 16 +++++++++++++++- .../kotlin/snyk/common/lsp/commands/Commands.kt | 1 + 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/io/snyk/plugin/ui/jcef/ApplyFixHandler.kt b/src/main/kotlin/io/snyk/plugin/ui/jcef/ApplyFixHandler.kt index 18a5ebbc9..8e221be58 100644 --- a/src/main/kotlin/io/snyk/plugin/ui/jcef/ApplyFixHandler.kt +++ b/src/main/kotlin/io/snyk/plugin/ui/jcef/ApplyFixHandler.kt @@ -55,9 +55,10 @@ class ApplyFixHandler(private val project: Project) { val applyFixQuery = JBCefJSQuery.create(jbCefBrowser) applyFixQuery.addHandler { value -> - val params = value.split("|@", limit = 2) - val filePath = params[0] // Path to the file that needs to be patched - val patch = params[1] // The patch we received from LS + val params = value.split("|@", limit = 3) + val fixId = params[0] // Path to the file that needs to be patched + val filePath = params[1] // Path to the file that needs to be patched + val patch = params[2] // The patch we received from LS // Avoid blocking the UI thread runAsync { @@ -86,6 +87,7 @@ class ApplyFixHandler(private val project: Project) { } } + return@addHandler JBCefJSQuery.Response("success") } diff --git a/src/main/kotlin/io/snyk/plugin/ui/toolwindow/panels/JCEFDescriptionPanel.kt b/src/main/kotlin/io/snyk/plugin/ui/toolwindow/panels/JCEFDescriptionPanel.kt index 555b40455..ed703ce9c 100644 --- a/src/main/kotlin/io/snyk/plugin/ui/toolwindow/panels/JCEFDescriptionPanel.kt +++ b/src/main/kotlin/io/snyk/plugin/ui/toolwindow/panels/JCEFDescriptionPanel.kt @@ -299,11 +299,13 @@ class SuggestionDescriptionPanelFromLS( if (!fixes.length) return; const currentFix = fixes[diffSelectedIndex]; + const fixId = currentFix.fixId; + console.log('currentFix', currentFix); const filePath = getFilePathFromFix(currentFix); const patch = currentFix.unifiedDiffsPerFile[filePath]; - window.applyFixQuery(filePath + '|@' + patch); + window.applyFixQuery(fixId + '|@' + filePath + '|@' + patch); // Following VSCode logic, the steps are: // 1. Read the current file content. diff --git a/src/main/kotlin/snyk/common/lsp/LanguageServerWrapper.kt b/src/main/kotlin/snyk/common/lsp/LanguageServerWrapper.kt index 8a414d228..96157e614 100644 --- a/src/main/kotlin/snyk/common/lsp/LanguageServerWrapper.kt +++ b/src/main/kotlin/snyk/common/lsp/LanguageServerWrapper.kt @@ -2,7 +2,6 @@ package snyk.common.lsp import com.google.gson.Gson import com.google.gson.annotations.SerializedName -import com.google.gson.reflect.TypeToken import com.intellij.openapi.Disposable import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.components.service @@ -50,6 +49,7 @@ import org.jetbrains.concurrency.runAsync import snyk.common.EnvironmentHelper import snyk.common.getEndpointUrl import snyk.common.lsp.commands.COMMAND_CODE_FIX_DIFFS +import snyk.common.lsp.commands.COMMAND_CODE_SUBMIT_FIX_FEEDBACK import snyk.common.lsp.commands.COMMAND_COPY_AUTH_LINK import snyk.common.lsp.commands.COMMAND_EXECUTE_CLI import snyk.common.lsp.commands.COMMAND_GET_ACTIVE_USER @@ -555,6 +555,20 @@ class LanguageServerWrapper( } + fun submitAutofixFeedbackCommand(fixId: String, feedback: String) { + if (!ensureLanguageServerInitialized()) return + + try { + val param = ExecuteCommandParams() + param.command = COMMAND_CODE_SUBMIT_FIX_FEEDBACK + param.arguments = listOf(fixId, feedback) + languageServer.workspaceService.executeCommand(param) + } catch (ignored: Exception) { + // do nothing to not break UX for analytics + } + } + + private fun ensureLanguageServerProtocolVersion(project: Project) { val protocolVersion = initializeResult?.serverInfo?.version pluginSettings().currentLSProtocolVersion = protocolVersion?.toIntOrNull() diff --git a/src/main/kotlin/snyk/common/lsp/commands/Commands.kt b/src/main/kotlin/snyk/common/lsp/commands/Commands.kt index 854c789bd..3a8affa41 100644 --- a/src/main/kotlin/snyk/common/lsp/commands/Commands.kt +++ b/src/main/kotlin/snyk/common/lsp/commands/Commands.kt @@ -10,3 +10,4 @@ internal const val COMMAND_LOGOUT = "snyk.logout" internal const val COMMAND_GET_SETTINGS_SAST_ENABLED = "snyk.getSettingsSastEnabled" internal const val COMMAND_COPY_AUTH_LINK = "snyk.copyAuthLink" internal const val COMMAND_CODE_FIX_DIFFS = "snyk.code.fixDiffs" +internal const val COMMAND_CODE_SUBMIT_FIX_FEEDBACK = "snyk.code.submitFixFeedback"