Skip to content

Commit bd9de76

Browse files
committed
fix(completion): execute each inline acceptance once
Let the inserting client own split-mode command execution. A forwarded Tab may select a backend session after the acknowledgement consumed an earlier matching offer; executing from both paths ran one command twice. Refs #76
1 parent b51b021 commit bd9de76

3 files changed

Lines changed: 77 additions & 7 deletions

File tree

‎src/runtime/backend/src/main/kotlin/me/helvert/codebridge/language/CodeBridgeInlineCompletions.kt‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.intellij.codeInsight.inline.completion.elements.InlineCompletionSkipT
77
import com.intellij.codeInsight.inline.completion.session.InlineCompletionSession
88
import com.intellij.codeInsight.inline.completion.suggestion.InlineCompletionSuggestion
99
import com.intellij.codeInsight.inline.completion.suggestion.InlineCompletionVariant
10+
import com.intellij.codeWithMe.ClientId
1011
import com.intellij.openapi.application.EDT
1112
import com.intellij.openapi.application.ModalityState
1213
import com.intellij.openapi.application.asContextElement
@@ -208,7 +209,8 @@ internal class CodeBridgeInlineCompletionProvider : DebouncedInlineCompletionPro
208209
* that request reaches the backend before the Tab, it ends the backend's session as typed over,
209210
* and the Tab finds nothing to accept. So what a suggestion hidden that way offered is kept a little
210211
* while, and the client says which item it inserted ([accepted], from
211-
* `CodeBridgeInlineCompletionAcceptance`). Whichever way it is heard, a command runs once.
212+
* `CodeBridgeInlineCompletionAcceptance`). For remote editors only that acknowledgement executes
213+
* the command: the forwarded Tab may select a backend session as well, in either order.
212214
*/
213215
@Service(Service.Level.PROJECT)
214216
internal class CodeBridgeInlineCompletionLists(private val project: Project, private val scope: CoroutineScope) {
@@ -237,6 +239,7 @@ internal class CodeBridgeInlineCompletionLists(private val project: Project, pri
237239
offered: List<CodeBridgeInlineCompletionProvider.Offered>
238240
) {
239241
val holder = editor as? UserDataHolderEx ?: return free(answers)
242+
holder.putUserData(CLIENT_INSERTS, ClientId.currentOrNull?.let { it != ClientId.localId } == true)
240243
listen(editor, holder)
241244
holder.putUserDataIfAbsent(HELD, ConcurrentHashMap.newKeySet()) += answers.map { Held(it.handle, it.answer.id) }
242245
holder.putUserData(OFFERED_IN, offered.filter { it.item.command != null })
@@ -249,7 +252,7 @@ internal class CodeBridgeInlineCompletionLists(private val project: Project, pri
249252
/**
250253
* Runs the command of the item a client inserted, which left its document [length] characters
251254
* long, hashing to [hash]. Waits a little for the backend to hide the suggestion, which this
252-
* call can overtake. Nothing, when it was hidden as accepted: that ran the command already.
255+
* call can overtake. A local editor's accepted session already ran its command and left no offer.
253256
*/
254257
suspend fun accepted(length: Int, hash: Int) {
255258
val (hidden, offered) = withTimeoutOrNull(SYNC_MILLIS.milliseconds) {
@@ -319,14 +322,17 @@ internal class CodeBridgeInlineCompletionLists(private val project: Project, pri
319322
session?.takeIf { selected && it.provider.id == CodeBridgeInlineCompletionProvider.ID }
320323
?.let(::offered)
321324
val command = accepted?.item?.command
322-
if (command != null) {
325+
if (command != null && editor.getUserData(CLIENT_INSERTS) != true) {
323326
// What was left in this document before can no longer be what the client inserts.
324327
left.filter { it.document == editor.document }.forEach { if (left.remove(it)) release(it.lists) }
325328
runAfterInsertion(editor.document, command) { release(lists) }
326329
return
327330
}
328-
if (selected || offered.isEmpty() || editor.isDisposed) return release(lists)
329-
val hidden = Left(editor, offered, lists)
331+
// A remote client inserts the text and acknowledges it itself. Its forwarded Tab can also
332+
// select the backend session, so that notification must not execute a second command.
333+
val available = if (selected) listOfNotNull(accepted) else offered
334+
if (available.isEmpty() || editor.isDisposed) return release(lists)
335+
val hidden = Left(editor, available, lists)
330336
left += hidden
331337
scope.launch {
332338
delay(LEFT_MILLIS.milliseconds)
@@ -397,6 +403,7 @@ internal class CodeBridgeInlineCompletionLists(private val project: Project, pri
397403
Key.create("codebridge.inlineCompletion.offeredIn")
398404
private val LISTENED: Key<Any> = Key.create("codebridge.inlineCompletion.listened")
399405
private val RELEASED: Key<Boolean> = Key.create("codebridge.inlineCompletion.released")
406+
private val CLIENT_INSERTS: Key<Boolean> = Key.create("codebridge.inlineCompletion.clientInserts")
400407

401408
fun getInstance(project: Project): CodeBridgeInlineCompletionLists = project.service()
402409
}

‎src/runtime/backend/src/test/kotlin/me/helvert/codebridge/ide/InlineCompletionTest.kt‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ package me.helvert.codebridge.ide
22

33
import com.intellij.codeInsight.inline.completion.InlineCompletionProvider
44
import com.intellij.codeInsight.inline.completion.testInlineCompletion
5+
import com.intellij.codeInsight.inline.completion.session.InlineCompletionSession
6+
import com.intellij.codeWithMe.ClientId
57
import com.intellij.openapi.components.service
68
import com.intellij.openapi.fileEditor.FileEditorManager
79
import com.intellij.openapi.vfs.LocalFileSystem
810
import kotlinx.coroutines.runBlocking
11+
import kotlinx.coroutines.async
12+
import kotlinx.coroutines.coroutineScope
13+
import kotlinx.coroutines.CoroutineStart
914
import kotlinx.serialization.json.Json
1015
import kotlinx.serialization.json.jsonObject
1116
import me.helvert.codebridge.exthost.ExtensionHostDocuments
@@ -104,6 +109,63 @@ class InlineCompletionTest : EditorFixture() {
104109
assertEquals(1, calls.count { it.first == EXECUTE }, calls.map { it.first }.toString())
105110
}
106111

112+
@Test
113+
fun `a remote backend selection waits for the inserting client's acknowledgement`() {
114+
open()
115+
fixture.testInlineCompletion {
116+
callInlineCompletion()
117+
delay()
118+
useClientInsertion()
119+
insert()
120+
}
121+
assertTrue(calls.none { it.first == EXECUTE }, calls.map { it.first }.toString())
122+
runBlocking { CodeBridgeInlineCompletionLists.getInstance(fixture.project).accepted(ACCEPTED.length, ACCEPTED.hashCode()) }
123+
awaitFreed()
124+
assertEquals(1, calls.count { it.first == EXECUTE }, calls.map { it.first }.toString())
125+
runBlocking { CodeBridgeInlineCompletionLists.getInstance(fixture.project).accepted(ACCEPTED.length, ACCEPTED.hashCode()) }
126+
assertEquals(1, calls.count { it.first == EXECUTE }, calls.map { it.first }.toString())
127+
}
128+
129+
@Test
130+
fun `a client acknowledgement overtaking the forwarded Tab runs its command once`() {
131+
open()
132+
fixture.testInlineCompletion {
133+
callInlineCompletion()
134+
delay()
135+
useClientInsertion()
136+
// An earlier request offered the same text before the client restarted its session.
137+
escape()
138+
callInlineCompletion()
139+
delay()
140+
useClientInsertion()
141+
coroutineScope {
142+
// Consume the older hidden offer before the forwarded Tab selects the current
143+
// backend session. Both notifications describe the same client insertion.
144+
val acknowledgement = async(start = CoroutineStart.UNDISPATCHED) {
145+
CodeBridgeInlineCompletionLists.getInstance(fixture.project)
146+
.accepted(ACCEPTED.length, ACCEPTED.hashCode())
147+
}
148+
insert()
149+
acknowledgement.await()
150+
}
151+
}
152+
awaitFreed()
153+
assertEquals(1, calls.count { it.first == EXECUTE }, calls.map { it.first }.toString())
154+
}
155+
156+
/** The light fixture has no remote session; retain its real offer as a remote request does. */
157+
private fun useClientInsertion() {
158+
val session = requireNotNull(InlineCompletionSession.getOrNull(fixture.editor))
159+
val offered = requireNotNull(
160+
session.context.getUserData(CodeBridgeInlineCompletionProvider.OFFERED)
161+
?: session.capture()?.activeVariant?.data?.getUserData(CodeBridgeInlineCompletionProvider.OFFERED)
162+
)
163+
ClientId.withClientId(ClientId("inline-test-client")) {
164+
CodeBridgeInlineCompletionLists.getInstance(fixture.project)
165+
.hold(fixture.editor, emptyList(), listOf(offered))
166+
}
167+
}
168+
107169
@Test
108170
fun `dismissing frees the list without running the command`() {
109171
open()

‎src/runtime/shared/src/main/kotlin/me/helvert/codebridge/completion/CodeBridgeInlineCompletionAcceptance.kt‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ import com.intellij.openapi.editor.Editor
1414
* A split-mode client inserts the suggestion itself and forwards the Tab, which the backend runs
1515
* on its own copy of the session. The client's request for the next suggestion can reach the
1616
* backend first and end that copy, and the Tab then accepts nothing there. Registered for the
17-
* frontend only; the backend matches the document against what each item would have left, and
18-
* runs a command at most once however it hears of it.
17+
* frontend only; the backend matches the document against what each item would have left. For a
18+
* remote editor this acknowledgement owns command execution; its forwarded Tab only retains the
19+
* selected offer until the acknowledgement arrives.
1920
*/
2021
internal class CodeBridgeInlineCompletionAcceptance : InlineCompletionInstallListener {
2122

0 commit comments

Comments
 (0)