Skip to content

Commit

Permalink
Fix incorrect cross-project event propagation (#2662)
Browse files Browse the repository at this point in the history
## Changes

Due to incorrect implementation of event listeners, if more than one
project was open, events like caret change were propagated between
editors. This PR fixes it.

## Test plan

Test separately with four different options ([opened file | active file
| caret position | selected text]):

1. Open two projects in IntelliJ.
2. Change [opened file | active file | caret position | selected text]
for the first project
3. Change [opened file | active file | caret position | selected text]
for the second project
4. Files set as context in the Cody chat should be different and match
files activated on the previous steps.
  • Loading branch information
pkukielka authored Nov 18, 2024
1 parent dcfc2d3 commit f9e8198
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down

0 comments on commit f9e8198

Please sign in to comment.