Skip to content

Commit

Permalink
fix completion
Browse files Browse the repository at this point in the history
  • Loading branch information
reymondzzzz committed Dec 4, 2024
1 parent 19db9fa commit 167bc2c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
44 changes: 43 additions & 1 deletion src/main/kotlin/com/smallcloud/refactai/Initializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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<String?>,
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)
Expand Down
13 changes: 10 additions & 3 deletions src/main/kotlin/com/smallcloud/refactai/listeners/AcceptAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<out AnAction>, context: DataContext): MutableList<AnAction> {
val editor = getEditor(context) ?: return actions.toMutableList()
val provider = ModeProvider.getOrCreateModeProvider(editor)
if (!provider.isInCompletionMode()) {
return actions.filterIsInstance<TabPressedAction>().toMutableList()
}
return actions.toMutableList()
if (getEditor(context) == null)
return actions.toMutableList()
return actions.filterIsInstance<TabPressedAction>().toMutableList()
}
}

0 comments on commit 167bc2c

Please sign in to comment.