Skip to content

fix: clear closed editor file context#280

Open
nanookclaw wants to merge 1 commit into
microsoft:mainfrom
nanookclaw:fix/clear-closed-file-context
Open

fix: clear closed editor file context#280
nanookclaw wants to merge 1 commit into
microsoft:mainfrom
nanookclaw:fix/clear-closed-file-context

Conversation

@nanookclaw
Copy link
Copy Markdown

Summary

Fixes #277.

When an editor closes, ReferencedFileService now 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 test

I also tried the same module set with verify, but it stops in packaging because com.microsoft.copilot.eclipse.core/build.properties expects copilot-agent/dist/, which is not present in this checkout.

Copilot AI review requested due to automatic review settings June 5, 2026 20:59
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 partClosed logic 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The Copilot Chat view retains file context even after the file is closed

2 participants