diff --git a/src/main/kotlin/com/sourcegraph/cody/config/CodySettingsFileChangeListener.kt b/src/main/kotlin/com/sourcegraph/cody/config/CodySettingsFileChangeListener.kt index 9c3964031c..99adf606fe 100644 --- a/src/main/kotlin/com/sourcegraph/cody/config/CodySettingsFileChangeListener.kt +++ b/src/main/kotlin/com/sourcegraph/cody/config/CodySettingsFileChangeListener.kt @@ -1,16 +1,21 @@ package com.sourcegraph.cody.config import com.intellij.openapi.editor.Document -import com.intellij.openapi.fileEditor.FileDocumentManager import com.intellij.openapi.fileEditor.FileDocumentManagerListener import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.LocalFileSystem import com.sourcegraph.cody.agent.CodyAgentService import com.sourcegraph.config.ConfigUtil +import com.sourcegraph.utils.CodyEditorUtil class CodySettingsFileChangeListener(private val project: Project) : FileDocumentManagerListener { override fun beforeDocumentSaving(document: Document) { - val currentFile = FileDocumentManager.getInstance().getFile(document) + val editor = CodyEditorUtil.getEditorForDocument(document) ?: return + if (editor.project != project) { + return + } + + val currentFile = editor.virtualFile val configFile = LocalFileSystem.getInstance() .refreshAndFindFileByNioFile(ConfigUtil.getSettingsFile(project)) diff --git a/src/main/kotlin/com/sourcegraph/cody/initialization/PostStartupActivity.kt b/src/main/kotlin/com/sourcegraph/cody/initialization/PostStartupActivity.kt index 8aa3151974..e8cd6d64b5 100644 --- a/src/main/kotlin/com/sourcegraph/cody/initialization/PostStartupActivity.kt +++ b/src/main/kotlin/com/sourcegraph/cody/initialization/PostStartupActivity.kt @@ -47,8 +47,11 @@ class PostStartupActivity : ProjectActivity { CodyStatusService.resetApplication(project) - val multicaster = EditorFactory.getInstance().eventMulticaster as EditorEventMulticasterEx val disposable = CodyAgentService.getInstance(project) + + // WARNING: All listeners should check if an event they are receiving is matching project + // they were created for. Otherwise, we risk propagating events to the wrong agent instance. + val multicaster = EditorFactory.getInstance().eventMulticaster as EditorEventMulticasterEx multicaster.addFocusChangeListener(CodyFocusChangeListener(project), disposable) multicaster.addCaretListener(CodyCaretListener(project), disposable) multicaster.addSelectionListener(CodySelectionListener(project), disposable) diff --git a/src/main/kotlin/com/sourcegraph/cody/listeners/CodyCaretListener.kt b/src/main/kotlin/com/sourcegraph/cody/listeners/CodyCaretListener.kt index 8cf7aad81f..72a436c51e 100644 --- a/src/main/kotlin/com/sourcegraph/cody/listeners/CodyCaretListener.kt +++ b/src/main/kotlin/com/sourcegraph/cody/listeners/CodyCaretListener.kt @@ -15,7 +15,9 @@ import com.sourcegraph.utils.CodyEditorUtil class CodyCaretListener(val project: Project) : CaretListener { override fun caretPositionChanged(e: CaretEvent) { - if (!ConfigUtil.isCodyEnabled() || e.editor.editorKind != EditorKind.MAIN_EDITOR) { + if (!ConfigUtil.isCodyEnabled() || + e.editor.editorKind != EditorKind.MAIN_EDITOR || + e.editor.project != project) { return } diff --git a/src/main/kotlin/com/sourcegraph/cody/listeners/CodyDocumentListener.kt b/src/main/kotlin/com/sourcegraph/cody/listeners/CodyDocumentListener.kt index ef55bf57a1..54848c0169 100644 --- a/src/main/kotlin/com/sourcegraph/cody/listeners/CodyDocumentListener.kt +++ b/src/main/kotlin/com/sourcegraph/cody/listeners/CodyDocumentListener.kt @@ -46,6 +46,10 @@ class CodyDocumentListener(val project: Project) : BulkAwareDocumentListener { private fun handleDocumentEvent(event: DocumentEvent) { val editor = CodyEditorUtil.getEditorForDocument(event.document) ?: return + if (editor.project != project) { + return + } + logCodeCopyPastedFromChat(event) CodyAutocompleteManager.instance.clearAutocompleteSuggestions(editor) diff --git a/src/main/kotlin/com/sourcegraph/cody/listeners/CodyFocusChangeListener.kt b/src/main/kotlin/com/sourcegraph/cody/listeners/CodyFocusChangeListener.kt index ee445ce459..642282905d 100644 --- a/src/main/kotlin/com/sourcegraph/cody/listeners/CodyFocusChangeListener.kt +++ b/src/main/kotlin/com/sourcegraph/cody/listeners/CodyFocusChangeListener.kt @@ -12,6 +12,10 @@ import com.sourcegraph.cody.ignore.IgnoreOracle class CodyFocusChangeListener(val project: Project) : FocusChangeListener { override fun focusGained(editor: Editor) { + if (editor.project != project) { + return + } + ProtocolTextDocumentExt.fromEditor(editor)?.let { textDocument -> EditorChangesBus.documentChanged(project, textDocument) CodyAgentService.withAgent(project) { agent: CodyAgent ->