diff --git a/src/main/kotlin/com/smallcloud/refactai/Initializer.kt b/src/main/kotlin/com/smallcloud/refactai/Initializer.kt index 9529d800..64620cee 100644 --- a/src/main/kotlin/com/smallcloud/refactai/Initializer.kt +++ b/src/main/kotlin/com/smallcloud/refactai/Initializer.kt @@ -2,12 +2,16 @@ package com.smallcloud.refactai import com.intellij.ide.plugins.PluginInstaller import com.intellij.openapi.Disposable +import com.intellij.openapi.actionSystem.IdeActions import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.application.invokeLater import com.intellij.openapi.diagnostic.Logger +import com.intellij.openapi.keymap.Keymap +import com.intellij.openapi.keymap.KeymapManagerListener import com.intellij.openapi.project.Project import com.intellij.openapi.startup.ProjectActivity import com.smallcloud.refactai.io.CloudMessageService +import com.smallcloud.refactai.listeners.ACTION_ID_ import com.smallcloud.refactai.listeners.UninstallListener import com.smallcloud.refactai.lsp.LSPActiveDocNotifierService import com.smallcloud.refactai.lsp.LSPProcessHolder.Companion.initialize @@ -21,7 +25,6 @@ import com.smallcloud.refactai.utils.isJcefCanStart import java.util.concurrent.atomic.AtomicBoolean import com.smallcloud.refactai.lsp.LSPProcessHolder.Companion.getInstance as getLSPProcessHolder - class Initializer : ProjectActivity, Disposable { override suspend fun execute(project: Project) { val shouldInitialize = !(initialized.getAndSet(true) || ApplicationManager.getApplication().isUnitTestMode) @@ -36,6 +39,45 @@ class Initializer : ProjectActivity, Disposable { notificationStartup() 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/listeners/AcceptAction.kt b/src/main/kotlin/com/smallcloud/refactai/listeners/AcceptAction.kt index e84fcbe5..7956f5a3 100644 --- a/src/main/kotlin/com/smallcloud/refactai/listeners/AcceptAction.kt +++ b/src/main/kotlin/com/smallcloud/refactai/listeners/AcceptAction.kt @@ -27,7 +27,9 @@ class TabPressedAction : EditorAction(InlineCompletionHandler()), ActionToIgnore override fun executeWriteAction(editor: Editor, caret: Caret?, dataContext: DataContext) { Logger.getInstance("RefactTabPressedAction").debug("executeWriteAction") val provider = ModeProvider.getOrCreateModeProvider(editor) - if (!provider.isInCompletionMode()) { + if (provider.isInCompletionMode()) { + InlineCompletion.getHandlerOrNull(editor)?.insert() + } else { provider.onTabPressed(editor, caret, dataContext) } } @@ -38,10 +40,15 @@ class TabPressedAction : EditorAction(InlineCompletionHandler()), ActionToIgnore dataContext: DataContext ): Boolean { val provider = ModeProvider.getOrCreateModeProvider(editor) - if (!provider.isInCompletionMode()) { + if (provider.isInCompletionMode()) { + 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 + } else { return ModeProvider.getOrCreateModeProvider(editor).modeInActiveState() } - return false } } } diff --git a/src/main/kotlin/com/smallcloud/refactai/listeners/AcceptActionPromoter.kt b/src/main/kotlin/com/smallcloud/refactai/listeners/AcceptActionPromoter.kt index fcd2c971..f525988d 100644 --- a/src/main/kotlin/com/smallcloud/refactai/listeners/AcceptActionPromoter.kt +++ b/src/main/kotlin/com/smallcloud/refactai/listeners/AcceptActionPromoter.kt @@ -5,18 +5,14 @@ import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.CommonDataKeys import com.intellij.openapi.actionSystem.DataContext import com.intellij.openapi.editor.Editor -import com.smallcloud.refactai.modes.ModeProvider class AcceptActionsPromoter : ActionPromoter { private fun getEditor(dataContext: DataContext): Editor? { return CommonDataKeys.EDITOR.getData(dataContext) } override fun promote(actions: MutableList, context: DataContext): MutableList { - val editor = getEditor(context) ?: return actions.toMutableList() - val provider = ModeProvider.getOrCreateModeProvider(editor) - if (!provider.isInCompletionMode()) { - return actions.filterIsInstance().toMutableList() - } - return actions.toMutableList() + if (getEditor(context) == null) + return actions.toMutableList() + return actions.filterIsInstance().toMutableList() } } \ No newline at end of file