From 1d3805c3cf1ebdcf7b3fb29ac8bdb45e3745f88d Mon Sep 17 00:00:00 2001 From: Zhiming Ma Date: Tue, 2 Jul 2024 07:05:28 +0800 Subject: [PATCH] fix(intellij): fix trigger for inline completion (#2560) * fix(intellij): fix trigger completion action priority. * fix(intellij): fix auto dismiss for manually trigger. --- .../actions/TriggerInlineCompletion.kt | 5 ++- .../completion/InlineCompletionService.kt | 43 +++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/actions/TriggerInlineCompletion.kt b/clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/actions/TriggerInlineCompletion.kt index 08f25c3b1f32..77dba3afa0f4 100644 --- a/clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/actions/TriggerInlineCompletion.kt +++ b/clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/actions/TriggerInlineCompletion.kt @@ -5,10 +5,11 @@ import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.CommonDataKeys import com.intellij.openapi.components.service +import com.tabbyml.intellijtabby.actionPromoter.HasPriority import com.tabbyml.intellijtabby.completion.InlineCompletionService -class TriggerInlineCompletion : AnAction() { +class TriggerInlineCompletion : AnAction(), HasPriority { override fun actionPerformed(e: AnActionEvent) { val inlineCompletionService = e.getRequiredData(CommonDataKeys.PROJECT).service() val editor = e.getRequiredData(CommonDataKeys.EDITOR) @@ -24,4 +25,6 @@ class TriggerInlineCompletion : AnAction() { override fun getActionUpdateThread(): ActionUpdateThread { return ActionUpdateThread.BGT } + + override val priority: Int = 1 } diff --git a/clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/completion/InlineCompletionService.kt b/clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/completion/InlineCompletionService.kt index 3ae647c7981c..c2565af45727 100644 --- a/clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/completion/InlineCompletionService.kt +++ b/clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/completion/InlineCompletionService.kt @@ -37,7 +37,8 @@ import org.eclipse.lsp4j.TextDocumentIdentifier class InlineCompletionService(private val project: Project) : Disposable { private val logger = Logger.getInstance(InlineCompletionService::class.java) private val publisher = project.messageBus.syncPublisher(Listener.TOPIC) - private val settingsMessageBusConnection = project.messageBus.connect() + private val messageBusConnection = project.messageBus.connect() + private val editorManager = FileEditorManager.getInstance(project) private val scope = CoroutineScope(Dispatchers.IO) private suspend fun getServer() = project.service().getServerAsync() @@ -132,7 +133,7 @@ class InlineCompletionService(private val project: Project) : Disposable { if (triggerMode == SettingsState.TriggerMode.AUTOMATIC) { registerAutoTriggerListener() } - settingsMessageBusConnection.subscribe(SettingsService.Listener.TOPIC, object : SettingsService.Listener { + messageBusConnection.subscribe(SettingsService.Listener.TOPIC, object : SettingsService.Listener { override fun settingsChanged(settings: SettingsService.Settings) { logger.debug("TriggerMode updated: ${settings.completionTriggerMode}") if (settings.completionTriggerMode == SettingsState.TriggerMode.AUTOMATIC) { @@ -142,24 +143,7 @@ class InlineCompletionService(private val project: Project) : Disposable { } } }) - } - - private var autoTriggerMessageBusConnection: MessageBusConnection? = null - private fun registerAutoTriggerListener() { - logger.debug("Register AutoTrigger listener.") - val connection = project.messageBus.connect() - autoTriggerMessageBusConnection = connection - val editorManager = FileEditorManager.getInstance(project) - - connection.subscribe(DocumentListener.TOPIC, object : DocumentListener { - override fun documentChanged(document: Document, editor: Editor, event: DocumentEvent) { - if (editorManager.selectedTextEditor == editor) { - provideInlineCompletion(editor, event.offset + event.newFragment.length) - } - } - }) - - connection.subscribe(CaretListener.TOPIC, object : CaretListener { + messageBusConnection.subscribe(CaretListener.TOPIC, object : CaretListener { override fun caretPositionChanged(editor: Editor, event: CaretEvent) { if (editorManager.selectedTextEditor == editor) { val offset = editor.caretModel.offset @@ -171,14 +155,27 @@ class InlineCompletionService(private val project: Project) : Disposable { } } }) - - connection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, object : FileEditorManagerListener { + messageBusConnection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, object : FileEditorManagerListener { override fun selectionChanged(event: FileEditorManagerEvent) { dismiss() } }) } + private var autoTriggerMessageBusConnection: MessageBusConnection? = null + private fun registerAutoTriggerListener() { + logger.debug("Register AutoTrigger listener.") + val connection = project.messageBus.connect() + autoTriggerMessageBusConnection = connection + connection.subscribe(DocumentListener.TOPIC, object : DocumentListener { + override fun documentChanged(document: Document, editor: Editor, event: DocumentEvent) { + if (editorManager.selectedTextEditor == editor) { + provideInlineCompletion(editor, event.offset + event.newFragment.length) + } + } + }) + } + private fun unregisterAutoTriggerListener() { logger.debug("Unregister AutoTrigger listener.") autoTriggerMessageBusConnection?.dispose() @@ -404,7 +401,7 @@ class InlineCompletionService(private val project: Project) : Disposable { override fun dispose() { dismiss() unregisterAutoTriggerListener() - settingsMessageBusConnection.dispose() + messageBusConnection.dispose() } interface Listener {