fix: clear closed editor file context#280
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Refactors ReferencedFileService editor-close handling to clear the “current referenced file” more reliably, and adds a unit test covering the new behavior.
Changes:
- Update
partClosedlogic to clear the current referenced file when either the current file is closed or there are no open editors. - Extract helper methods (
isCurrentReferencedFile,hasNoOpenEditors,clearCurrentReferencedFile) for reuse and readability. - Add a JUnit/Mockito test validating that closing the current referenced file clears service state.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| com.microsoft.copilot.eclipse.ui/src/com/microsoft/copilot/eclipse/ui/chat/services/ReferencedFileService.java | Refactors close-handling and adds helper methods to decide when to clear current referenced file/selection. |
| com.microsoft.copilot.eclipse.ui.test/src/com/microsoft/copilot/eclipse/ui/chat/services/ReferencedFileServiceTest.java | Adds a regression test for clearing current file when the closed editor matches the current referenced file. |
|
Hi @nanookclaw Thank you for your contribution, there are some copilot review comments, could you first check if they are valid? |
|
Checked the Copilot review comments and they were valid. I pushed
Verification:
|
|
Thank you. Please mark those comments as resolved once you addressed them |
jdneo
left a comment
There was a problem hiding this comment.
Hi @nanookclaw
Thank you for the contribution and sorry for the late reply. Left some comments.
| currentSelectionObservable.setValue(null); | ||
| }); | ||
| boolean closedCurrentFile = isCurrentReferencedFile(partRef); | ||
| boolean noOpenEditors = hasNoOpenEditors(); |
There was a problem hiding this comment.
Curious about the no open editors check here.
From code, seems like closedCurrentFile is enough, why we have this check?
| /** | ||
| * Sets the current file for tests. | ||
| */ | ||
| protected void setCurrentFileForTest(IFile file) { |
There was a problem hiding this comment.
Instead of introduce a method for testing purpose only, can we achieve the same purpose by firing a part-open/activate event on the captured listener before firing partClosed in test?
Summary
Fixes #277.
When an editor closes,
ReferencedFileServicenow checks whether the closed editor is the file currently represented in Copilot Chat. If it is, the service clears both the current-file observable and current-selection observable immediately. This keeps the existing fallback behavior for the final-editor-close case, but no longer depends only on the workbench editor reference count, which can still include an editor while focus has moved to a non-editor view.The regression test captures the part listener registered by
ReferencedFileService, seeds the current file observable, simulates closing that same editor while another editor reference is still reported by the active page, and asserts the chat current file is cleared.Testing
git diff --check./mvnw -pl com.microsoft.copilot.eclipse.terminal.api,com.microsoft.copilot.eclipse.core,com.microsoft.copilot.eclipse.ui,com.microsoft.copilot.eclipse.ui.test -am -Dtest=ReferencedFileServiceTest testI also tried the same module set with
verify, but it stops in packaging becausecom.microsoft.copilot.eclipse.core/build.propertiesexpectscopilot-agent/dist/, which is not present in this checkout.