From 738d421f12faefbc63fdf08442b3025bd3388ed4 Mon Sep 17 00:00:00 2001 From: alashchev17 Date: Mon, 28 Oct 2024 15:32:24 +0100 Subject: [PATCH 1/2] fix: open chat instruction selects range of code in active editor --- .../refactai/code_lens/CodeLensAction.kt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/kotlin/com/smallcloud/refactai/code_lens/CodeLensAction.kt b/src/main/kotlin/com/smallcloud/refactai/code_lens/CodeLensAction.kt index 06050b17..c55e3732 100644 --- a/src/main/kotlin/com/smallcloud/refactai/code_lens/CodeLensAction.kt +++ b/src/main/kotlin/com/smallcloud/refactai/code_lens/CodeLensAction.kt @@ -9,6 +9,7 @@ import com.intellij.openapi.wm.ToolWindowManager import com.smallcloud.refactai.Resources import com.smallcloud.refactai.panes.RefactAIToolboxPaneFactory import kotlin.io.path.relativeTo +import kotlinx.coroutines.* class CodeLensAction( private val editor: Editor, @@ -34,17 +35,35 @@ class CodeLensAction( }.minBy { it.toString().length } } + return contentMsg .replace("%CURRENT_FILE%", relativePath?.toString() ?: filePath.toString()) .replace("%CURSOR_LINE%", line1.toString()) .replace("%CODE_SELECTION%", text) } + @OptIn(DelicateCoroutinesApi::class) fun actionPerformed() { val chat = editor.project?.let { ToolWindowManager.getInstance(it).getToolWindow("Refact") } chat?.activate { RefactAIToolboxPaneFactory.chat?.requestFocus() RefactAIToolboxPaneFactory.chat?.executeCodeLensCommand(formatMessage(), sendImmediately, openNewTab) } + + // If content is empty, then it's "Open Chat" instruction, selecting range of code in active tab + if (contentMsg.isEmpty()) { + GlobalScope.launch { + delay(500) + withContext(Dispatchers.Main) { + val pos1 = LogicalPosition(line1, 0) + val pos2 = LogicalPosition(line2, editor.document.getLineEndOffset(line2)) + + editor.selectionModel.setSelection( + editor.logicalPositionToOffset(pos1), + editor.logicalPositionToOffset(pos2) + ) + } + } + } } } \ No newline at end of file From c8b4dc36be651707101dd6e00900a957adbac8a3 Mon Sep 17 00:00:00 2001 From: alashchev17 Date: Mon, 28 Oct 2024 16:02:05 +0100 Subject: [PATCH 2/2] fix: removal of delicate api, using CoroutineScope instead --- .../refactai/code_lens/CodeLensAction.kt | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/com/smallcloud/refactai/code_lens/CodeLensAction.kt b/src/main/kotlin/com/smallcloud/refactai/code_lens/CodeLensAction.kt index c55e3732..6dd4eab1 100644 --- a/src/main/kotlin/com/smallcloud/refactai/code_lens/CodeLensAction.kt +++ b/src/main/kotlin/com/smallcloud/refactai/code_lens/CodeLensAction.kt @@ -42,7 +42,6 @@ class CodeLensAction( .replace("%CODE_SELECTION%", text) } - @OptIn(DelicateCoroutinesApi::class) fun actionPerformed() { val chat = editor.project?.let { ToolWindowManager.getInstance(it).getToolWindow("Refact") } chat?.activate { @@ -52,17 +51,15 @@ class CodeLensAction( // If content is empty, then it's "Open Chat" instruction, selecting range of code in active tab if (contentMsg.isEmpty()) { - GlobalScope.launch { + CoroutineScope(Dispatchers.Main).launch { delay(500) - withContext(Dispatchers.Main) { - val pos1 = LogicalPosition(line1, 0) - val pos2 = LogicalPosition(line2, editor.document.getLineEndOffset(line2)) + val pos1 = LogicalPosition(line1, 0) + val pos2 = LogicalPosition(line2, editor.document.getLineEndOffset(line2)) - editor.selectionModel.setSelection( - editor.logicalPositionToOffset(pos1), - editor.logicalPositionToOffset(pos2) - ) - } + editor.selectionModel.setSelection( + editor.logicalPositionToOffset(pos1), + editor.logicalPositionToOffset(pos2) + ) } } }