Skip to content

Commit

Permalink
fix completion action on win
Browse files Browse the repository at this point in the history
  • Loading branch information
reymondzzzz committed Dec 19, 2024
1 parent 9932ac6 commit 5577ad7
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 56 deletions.
38 changes: 0 additions & 38 deletions src/main/kotlin/com/smallcloud/refactai/Initializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import kotlin.time.Duration.Companion.milliseconds
import com.smallcloud.refactai.io.InferenceGlobalContext.Companion.instance as InferenceGlobalContext

val EditorRefactLastSnippetTelemetryIdKey = Key.create<Int>("refact.snippetTelemetryId")
val EditorRefactLastCompletionIsMultilineKey = Key.create<Boolean>("refact.lastCompletion.isMultiline")

private class Default : InlineCompletionSuggestionUpdateManager.Adapter {
override fun onDocumentChange(
Expand Down Expand Up @@ -257,6 +258,7 @@ class RefactAICompletionProvider : DebouncedInlineCompletionProvider() {
delay(2)
}
EditorRefactLastSnippetTelemetryIdKey[request.editor] = completion.snippetTelemetryId
EditorRefactLastCompletionIsMultilineKey[request.editor] = completion.multiline
}
}
awaitClose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ 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
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)
Expand All @@ -35,6 +36,7 @@ class TabPressedAction : EditorAction(InlineCompletionHandler()), ActionToIgnore
EditorRefactLastSnippetTelemetryIdKey[editor]?.also {
editor.project?.service<UsageStats>()?.snippetAccepted(it)
EditorRefactLastSnippetTelemetryIdKey[editor] = null
EditorRefactLastCompletionIsMultilineKey[editor] = null
}
} else {
provider.onTabPressed(editor, caret, dataContext)
Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,9 +11,12 @@ 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> {
if (getEditor(context) == null)
return actions.toMutableList()
return actions.filterIsInstance<TabPressedAction>().toMutableList()
override fun promote(actions: MutableList<out AnAction>, context: DataContext): List<AnAction> {
val editor = getEditor(context) ?: return emptyList()
if (InlineCompletionContext.getOrNull(editor) == null) {
return emptyList()
}
actions.filterIsInstance<TabPressedAction>().takeIf { it.isNotEmpty() }?.let { return it }
return emptyList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class GenerateGitCommitMessageAction : AnAction(
val lspService =
event.project?.service<com.smallcloud.refactai.lsp.LSPProcessHolder>() ?: 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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -87,7 +86,6 @@ class GenerateGitCommitMessageAction : AnAction(

try {
val includedChanges = commitWorkflowUi.getIncludedChanges()
commitWorkflowUi.getIncludedUnversionedFiles()
val filePatches = IdeaTextPatchBuilder.buildPatch(
project, includedChanges, projectFileVcsRoot.toNioPath(), false, true
)
Expand Down
16 changes: 9 additions & 7 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ integrated into a single package that follows your privacy settings.</p>
</extensions>
<actions>
<action class="com.smallcloud.refactai.listeners.TabPressedAction"
id="TabPressedAction"
id="InsertInlineCompletionAction"
text="Accept Inline Completion"
overrides="true"
description="Refact AI Accept Inline Completion">
<add-to-group group-id="InlineCompletion" anchor="first"/>
<keyboard-shortcut first-keystroke="TAB" keymap="$default"/>
</action>
<action class="com.smallcloud.refactai.listeners.CancelPressedAction"
Expand Down Expand Up @@ -182,12 +184,12 @@ integrated into a single package that follows your privacy settings.</p>
<separator/>
</group>
<!-- Temporary disable; JB has broken vcs plugin in 2024.3 -->
<!-- <group id="RefactAI.GenerateGitCommitMessageGroup">-->
<!-- <add-to-group group-id="Vcs.MessageActionGroup" anchor="first"/>-->
<!-- <action-->
<!-- id="RefactAIGenerateGitCommitMessage"-->
<!-- class="com.smallcloud.refactai.listeners.GenerateGitCommitMessageAction" />-->
<!-- </group>-->
<group id="RefactAI.GenerateGitCommitMessageGroup">
<add-to-group group-id="Vcs.MessageActionGroup" anchor="first"/>
<action
id="RefactAIGenerateGitCommitMessage"
class="com.smallcloud.refactai.listeners.GenerateGitCommitMessageAction" />
</group>
</actions>
<resource-bundle>bundles.RefactAI</resource-bundle>
<applicationListeners>
Expand Down

0 comments on commit 5577ad7

Please sign in to comment.