From 5577ad7d5108c2f0b41e0afb90516fc147ab70d5 Mon Sep 17 00:00:00 2001
From: Kirill Starkov
Date: Fri, 20 Dec 2024 00:19:39 +0800
Subject: [PATCH] fix completion action on win
---
.../com/smallcloud/refactai/Initializer.kt | 38 -------------------
.../RefactAICompletionProvider.kt | 2 +
.../refactai/listeners/AcceptAction.kt | 12 ++++--
.../listeners/AcceptActionPromoter.kt | 12 ++++--
.../GenerateGitCommitMessageAction.kt | 4 +-
src/main/resources/META-INF/plugin.xml | 16 ++++----
6 files changed, 28 insertions(+), 56 deletions(-)
diff --git a/src/main/kotlin/com/smallcloud/refactai/Initializer.kt b/src/main/kotlin/com/smallcloud/refactai/Initializer.kt
index 64620cee..ce32be75 100644
--- a/src/main/kotlin/com/smallcloud/refactai/Initializer.kt
+++ b/src/main/kotlin/com/smallcloud/refactai/Initializer.kt
@@ -40,44 +40,6 @@ class Initializer : ProjectActivity, Disposable {
PluginInstaller.addStateListener(UninstallListener())
UpdateChecker.instance
- ApplicationManager.getApplication()
- .messageBus
- .connect(PluginState.instance)
- .subscribe(KeymapManagerListener.TOPIC, object : KeymapManagerListener {
- override fun shortcutsChanged(
- keymap: Keymap,
- actionIds: MutableCollection,
- fromSettings: Boolean
- ) {
- if (Thread.currentThread().stackTrace.count { it.className.startsWith("com.smallcloud.refactai.Initializer") } > 1) {
- return
- }
- for (id in actionIds) {
- if (!listOf(IdeActions.ACTION_INSERT_INLINE_COMPLETION, ACTION_ID_).contains(id)) {
- continue
- }
- val shortcuts = keymap.getShortcuts(id)
- if (id == IdeActions.ACTION_INSERT_INLINE_COMPLETION) {
- keymap.removeAllActionShortcuts(ACTION_ID_)
- for (shortcut in shortcuts) {
- keymap.addShortcut(
- ACTION_ID_,
- shortcut
- )
- }
- } else if (id == ACTION_ID_) {
- keymap.removeAllActionShortcuts(IdeActions.ACTION_INSERT_INLINE_COMPLETION)
- for (shortcut in shortcuts) {
- keymap.addShortcut(
- IdeActions.ACTION_INSERT_INLINE_COMPLETION,
- shortcut
- )
- }
- }
- }
- }
- })
-
ApplicationManager.getApplication().getService(CloudMessageService::class.java)
if (!isJcefCanStart()) {
emitInfo(RefactAIBundle.message("notifications.chatCanNotStartWarning"), false)
diff --git a/src/main/kotlin/com/smallcloud/refactai/codecompletion/RefactAICompletionProvider.kt b/src/main/kotlin/com/smallcloud/refactai/codecompletion/RefactAICompletionProvider.kt
index 79169966..ae5e3605 100644
--- a/src/main/kotlin/com/smallcloud/refactai/codecompletion/RefactAICompletionProvider.kt
+++ b/src/main/kotlin/com/smallcloud/refactai/codecompletion/RefactAICompletionProvider.kt
@@ -52,6 +52,7 @@ import kotlin.time.Duration.Companion.milliseconds
import com.smallcloud.refactai.io.InferenceGlobalContext.Companion.instance as InferenceGlobalContext
val EditorRefactLastSnippetTelemetryIdKey = Key.create("refact.snippetTelemetryId")
+val EditorRefactLastCompletionIsMultilineKey = Key.create("refact.lastCompletion.isMultiline")
private class Default : InlineCompletionSuggestionUpdateManager.Adapter {
override fun onDocumentChange(
@@ -257,6 +258,7 @@ class RefactAICompletionProvider : DebouncedInlineCompletionProvider() {
delay(2)
}
EditorRefactLastSnippetTelemetryIdKey[request.editor] = completion.snippetTelemetryId
+ EditorRefactLastCompletionIsMultilineKey[request.editor] = completion.multiline
}
}
awaitClose()
diff --git a/src/main/kotlin/com/smallcloud/refactai/listeners/AcceptAction.kt b/src/main/kotlin/com/smallcloud/refactai/listeners/AcceptAction.kt
index ff03f200..fbcaed7d 100644
--- a/src/main/kotlin/com/smallcloud/refactai/listeners/AcceptAction.kt
+++ b/src/main/kotlin/com/smallcloud/refactai/listeners/AcceptAction.kt
@@ -12,6 +12,7 @@ import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.actionSystem.EditorAction
import com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler
import com.smallcloud.refactai.Resources
+import com.smallcloud.refactai.codecompletion.EditorRefactLastCompletionIsMultilineKey
import com.smallcloud.refactai.codecompletion.EditorRefactLastSnippetTelemetryIdKey
import com.smallcloud.refactai.codecompletion.InlineCompletionGrayTextElementCustom
import com.smallcloud.refactai.modes.ModeProvider
@@ -19,14 +20,14 @@ import com.smallcloud.refactai.statistic.UsageStats
const val ACTION_ID_ = "TabPressedAction"
-class TabPressedAction : EditorAction(InlineCompletionHandler()), ActionToIgnore {
+class TabPressedAction : EditorAction(InsertInlineCompletionHandler()), ActionToIgnore {
val ACTION_ID = ACTION_ID_
init {
this.templatePresentation.icon = Resources.Icons.LOGO_RED_16x16
}
- class InlineCompletionHandler : EditorWriteActionHandler() {
+ class InsertInlineCompletionHandler : EditorWriteActionHandler() {
override fun executeWriteAction(editor: Editor, caret: Caret?, dataContext: DataContext) {
Logger.getInstance("RefactTabPressedAction").debug("executeWriteAction")
val provider = ModeProvider.getOrCreateModeProvider(editor)
@@ -35,6 +36,7 @@ class TabPressedAction : EditorAction(InlineCompletionHandler()), ActionToIgnore
EditorRefactLastSnippetTelemetryIdKey[editor]?.also {
editor.project?.service()?.snippetAccepted(it)
EditorRefactLastSnippetTelemetryIdKey[editor] = null
+ EditorRefactLastCompletionIsMultilineKey[editor] = null
}
} else {
provider.onTabPressed(editor, caret, dataContext)
@@ -51,8 +53,10 @@ class TabPressedAction : EditorAction(InlineCompletionHandler()), ActionToIgnore
val ctx = InlineCompletionContext.getOrNull(editor) ?: return false
if (ctx.state.elements.size != 1) return false
val elem = ctx.state.elements.first()
- if (elem !is InlineCompletionGrayTextElementCustom.Presentable) return false
- return elem.delta == caret.logicalPosition.column
+ val isMultiline = EditorRefactLastCompletionIsMultilineKey[editor]
+ if (isMultiline && elem is InlineCompletionGrayTextElementCustom.Presentable)
+ return elem.delta == caret.logicalPosition.column
+ return true
} else {
return ModeProvider.getOrCreateModeProvider(editor).modeInActiveState()
}
diff --git a/src/main/kotlin/com/smallcloud/refactai/listeners/AcceptActionPromoter.kt b/src/main/kotlin/com/smallcloud/refactai/listeners/AcceptActionPromoter.kt
index f525988d..ffa46641 100644
--- a/src/main/kotlin/com/smallcloud/refactai/listeners/AcceptActionPromoter.kt
+++ b/src/main/kotlin/com/smallcloud/refactai/listeners/AcceptActionPromoter.kt
@@ -1,5 +1,6 @@
package com.smallcloud.refactai.listeners
+import com.intellij.codeInsight.inline.completion.session.InlineCompletionContext
import com.intellij.openapi.actionSystem.ActionPromoter
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.CommonDataKeys
@@ -10,9 +11,12 @@ class AcceptActionsPromoter : ActionPromoter {
private fun getEditor(dataContext: DataContext): Editor? {
return CommonDataKeys.EDITOR.getData(dataContext)
}
- override fun promote(actions: MutableList, context: DataContext): MutableList {
- if (getEditor(context) == null)
- return actions.toMutableList()
- return actions.filterIsInstance().toMutableList()
+ override fun promote(actions: MutableList, context: DataContext): List {
+ val editor = getEditor(context) ?: return emptyList()
+ if (InlineCompletionContext.getOrNull(editor) == null) {
+ return emptyList()
+ }
+ actions.filterIsInstance().takeIf { it.isNotEmpty() }?.let { return it }
+ return emptyList()
}
}
\ No newline at end of file
diff --git a/src/main/kotlin/com/smallcloud/refactai/listeners/GenerateGitCommitMessageAction.kt b/src/main/kotlin/com/smallcloud/refactai/listeners/GenerateGitCommitMessageAction.kt
index 33e51cdb..140ea980 100644
--- a/src/main/kotlin/com/smallcloud/refactai/listeners/GenerateGitCommitMessageAction.kt
+++ b/src/main/kotlin/com/smallcloud/refactai/listeners/GenerateGitCommitMessageAction.kt
@@ -37,7 +37,7 @@ class GenerateGitCommitMessageAction : AnAction(
val lspService =
event.project?.service() ?: return@invokeLater
- val isEnabled = lspService.isWorking && (commitWorkflowUi.getIncludedChanges().isNotEmpty() && commitWorkflowUi.getIncludedUnversionedFiles().isNotEmpty())
+ val isEnabled = lspService.isWorking && (commitWorkflowUi.getIncludedChanges().isNotEmpty() || commitWorkflowUi.getIncludedUnversionedFiles().isNotEmpty())
event.presentation.isEnabled = isEnabled
event.presentation.text = if (lspService.isWorking) {
@@ -54,7 +54,6 @@ class GenerateGitCommitMessageAction : AnAction(
return
}
-
val gitDiff = getDiff(event, project) ?: return
val commitWorkflowUi = event.getData(VcsDataKeys.COMMIT_WORKFLOW_UI)
if (commitWorkflowUi != null) {
@@ -87,7 +86,6 @@ class GenerateGitCommitMessageAction : AnAction(
try {
val includedChanges = commitWorkflowUi.getIncludedChanges()
- commitWorkflowUi.getIncludedUnversionedFiles()
val filePatches = IdeaTextPatchBuilder.buildPatch(
project, includedChanges, projectFileVcsRoot.toNioPath(), false, true
)
diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml
index 05ef895b..4bb36ad9 100644
--- a/src/main/resources/META-INF/plugin.xml
+++ b/src/main/resources/META-INF/plugin.xml
@@ -148,9 +148,11 @@ integrated into a single package that follows your privacy settings.
+
-
-
-
-
-
-
+
+
+
+
bundles.RefactAI