Skip to content

Commit

Permalink
fix(intellij): fix trigger for inline completion (#2560)
Browse files Browse the repository at this point in the history
* fix(intellij): fix trigger completion action priority.

* fix(intellij): fix auto dismiss for manually trigger.
  • Loading branch information
icycodes authored Jul 1, 2024
1 parent 7c30d74 commit 1d3805c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<InlineCompletionService>()
val editor = e.getRequiredData(CommonDataKeys.EDITOR)
Expand All @@ -24,4 +25,6 @@ class TriggerInlineCompletion : AnAction() {
override fun getActionUpdateThread(): ActionUpdateThread {
return ActionUpdateThread.BGT
}

override val priority: Int = 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConnectionService>().getServerAsync()

Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -404,7 +401,7 @@ class InlineCompletionService(private val project: Project) : Disposable {
override fun dispose() {
dismiss()
unregisterAutoTriggerListener()
settingsMessageBusConnection.dispose()
messageBusConnection.dispose()
}

interface Listener {
Expand Down

0 comments on commit 1d3805c

Please sign in to comment.