fix: clear closed editor file context#280
Open
nanookclaw wants to merge 1 commit into
Open
Conversation
Contributor
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. |
Comment on lines
+480
to
+494
| private boolean isCurrentReferencedFile(IWorkbenchPartReference partRef) { | ||
| IWorkbenchPart part = partRef.getPart(false); | ||
| if (!(part instanceof IEditorPart editorPart)) { | ||
| return false; | ||
| } | ||
|
|
||
| IFile closedFile = UiUtils.getFileFromEditorPart(editorPart); | ||
| if (closedFile == null) { | ||
| return false; | ||
| } | ||
|
|
||
| AtomicReference<IFile> currentFile = new AtomicReference<>(); | ||
| ensureRealm(() -> currentFile.set(currentFileObservable.getValue())); | ||
| return closedFile.equals(currentFile.get()); | ||
| } |
Comment on lines
+496
to
+499
| private boolean hasNoOpenEditors() { | ||
| IWorkbenchPage page = UiUtils.getActivePage(); | ||
| return page == null || page.getEditorReferences().length == 0; | ||
| } |
Comment on lines
+98
to
+109
| private static void setCurrentFile(ReferencedFileService service, IFile file) throws Exception { | ||
| Object currentFileObservable = getField(service, "currentFileObservable"); | ||
| runOnDisplayThread(() -> invokeSetValue(currentFileObservable, file)); | ||
| } | ||
|
|
||
| private static Object getField(Object target, String fieldName) throws NoSuchFieldException, | ||
| IllegalAccessException { | ||
| Field field = target.getClass().getDeclaredField(fieldName); | ||
| field.setAccessible(true); | ||
| return field.get(target); | ||
| } | ||
|
|
Comment on lines
+118
to
+125
| private static void invokeSetValue(Object observable, Object value) { | ||
| try { | ||
| Method setValue = observable.getClass().getMethod("setValue", Object.class); | ||
| setValue.invoke(observable, value); | ||
| } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { | ||
| throw new IllegalStateException("Failed to set observable value", e); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.