@@ -7,6 +7,7 @@ import com.intellij.codeInsight.inline.completion.elements.InlineCompletionSkipT
77import com.intellij.codeInsight.inline.completion.session.InlineCompletionSession
88import com.intellij.codeInsight.inline.completion.suggestion.InlineCompletionSuggestion
99import com.intellij.codeInsight.inline.completion.suggestion.InlineCompletionVariant
10+ import com.intellij.codeWithMe.ClientId
1011import com.intellij.openapi.application.EDT
1112import com.intellij.openapi.application.ModalityState
1213import 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 )
214216internal 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 }
0 commit comments