-
Notifications
You must be signed in to change notification settings - Fork 48
fix: clear closed editor file context #280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nanookclaw
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
nanookclaw:fix/clear-closed-file-context
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+271
−7
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
168 changes: 168 additions & 0 deletions
168
...ui.test/src/com/microsoft/copilot/eclipse/ui/chat/services/ReferencedFileServiceTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| package com.microsoft.copilot.eclipse.ui.chat.services; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
| import static org.mockito.Mockito.mockStatic; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import org.eclipse.core.resources.IFile; | ||
| import org.eclipse.ui.IEditorInput; | ||
| import org.eclipse.ui.IEditorPart; | ||
| import org.eclipse.ui.IEditorReference; | ||
| import org.eclipse.ui.IFileEditorInput; | ||
| import org.eclipse.ui.IPartListener2; | ||
| import org.eclipse.ui.IPartService; | ||
| import org.eclipse.ui.IWorkbench; | ||
| import org.eclipse.ui.IWorkbenchPage; | ||
| import org.eclipse.ui.IWorkbenchWindow; | ||
| import org.eclipse.ui.PlatformUI; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.Mock; | ||
| import org.mockito.MockedStatic; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import com.microsoft.copilot.eclipse.ui.utils.UiUtils; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class ReferencedFileServiceTest { | ||
|
|
||
| @Mock | ||
| private IWorkbench workbench; | ||
|
|
||
| @Mock | ||
| private IWorkbenchWindow window; | ||
|
|
||
| @Mock | ||
| private IPartService partService; | ||
|
|
||
| @Mock | ||
| private IWorkbenchPage activePage; | ||
|
|
||
| @Mock | ||
| private IWorkbenchPage otherPage; | ||
|
|
||
| @Mock | ||
| private IEditorReference closedEditorReference; | ||
|
|
||
| @Mock | ||
| private IEditorReference remainingEditorReference; | ||
|
|
||
| @Mock | ||
| private IEditorPart closedEditor; | ||
|
|
||
| @Mock | ||
| private IFileEditorInput closedEditorInput; | ||
|
|
||
| @Mock | ||
| private IEditorInput nonFileEditorInput; | ||
|
|
||
| @Mock | ||
| private IFile currentFile; | ||
|
|
||
| @Test | ||
| void partClosed_WhenClosedEditorIsCurrentFileAndEditorReferencesRemain_ShouldClearCurrentFile() | ||
| throws Exception { | ||
| try (MockedStatic<PlatformUI> platformUi = mockStatic(PlatformUI.class); | ||
| MockedStatic<UiUtils> uiUtils = mockStatic(UiUtils.class)) { | ||
| platformUi.when(PlatformUI::getWorkbench).thenReturn(workbench); | ||
| when(workbench.getWorkbenchWindows()).thenReturn(new IWorkbenchWindow[] { window }); | ||
| when(window.getPartService()).thenReturn(partService); | ||
|
|
||
| TestReferencedFileService service = new TestReferencedFileService(); | ||
| try { | ||
| IPartListener2 listener = getRegisteredPartListener(); | ||
| when(closedEditorReference.getEditorInput()).thenReturn(nonFileEditorInput); | ||
| when(closedEditorReference.getPart(false)).thenReturn(closedEditor); | ||
| uiUtils.when(() -> UiUtils.getFileFromEditorPart(closedEditor)).thenReturn(currentFile); | ||
| when(window.getPages()).thenReturn(new IWorkbenchPage[] { activePage }); | ||
| when(activePage.getEditorReferences()).thenReturn(new IEditorReference[] { remainingEditorReference }); | ||
|
|
||
| service.setCurrentFile(currentFile); | ||
| assertSame(currentFile, service.getCurrentFile()); | ||
|
|
||
| listener.partClosed(closedEditorReference); | ||
|
|
||
| assertNull(service.getCurrentFile()); | ||
| } finally { | ||
| service.dispose(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void partClosed_WhenEditorPartIsDisposedButReferenceInputIsCurrentFile_ShouldClearCurrentFile() | ||
| throws Exception { | ||
| try (MockedStatic<PlatformUI> platformUi = mockStatic(PlatformUI.class)) { | ||
| platformUi.when(PlatformUI::getWorkbench).thenReturn(workbench); | ||
| when(workbench.getWorkbenchWindows()).thenReturn(new IWorkbenchWindow[] { window }); | ||
| when(window.getPartService()).thenReturn(partService); | ||
|
|
||
| TestReferencedFileService service = new TestReferencedFileService(); | ||
| try { | ||
| IPartListener2 listener = getRegisteredPartListener(); | ||
| when(closedEditorReference.getEditorInput()).thenReturn(closedEditorInput); | ||
| when(closedEditorInput.getFile()).thenReturn(currentFile); | ||
| when(window.getPages()).thenReturn(new IWorkbenchPage[] { activePage }); | ||
| when(activePage.getEditorReferences()).thenReturn(new IEditorReference[] { remainingEditorReference }); | ||
|
|
||
| service.setCurrentFile(currentFile); | ||
| assertSame(currentFile, service.getCurrentFile()); | ||
|
|
||
| listener.partClosed(closedEditorReference); | ||
|
|
||
| assertNull(service.getCurrentFile()); | ||
| } finally { | ||
| service.dispose(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void partClosed_WhenActivePageHasNoEditorsButAnotherPageHasEditors_ShouldKeepCurrentFile() | ||
| throws Exception { | ||
| try (MockedStatic<PlatformUI> platformUi = mockStatic(PlatformUI.class); | ||
| MockedStatic<UiUtils> uiUtils = mockStatic(UiUtils.class)) { | ||
| platformUi.when(PlatformUI::getWorkbench).thenReturn(workbench); | ||
| when(workbench.getWorkbenchWindows()).thenReturn(new IWorkbenchWindow[] { window }); | ||
| when(window.getPartService()).thenReturn(partService); | ||
|
|
||
| TestReferencedFileService service = new TestReferencedFileService(); | ||
| try { | ||
| IPartListener2 listener = getRegisteredPartListener(); | ||
| when(closedEditorReference.getEditorInput()).thenReturn(nonFileEditorInput); | ||
| when(closedEditorReference.getPart(false)).thenReturn(closedEditor); | ||
| uiUtils.when(() -> UiUtils.getFileFromEditorPart(closedEditor)).thenReturn(null); | ||
| when(window.getPages()).thenReturn(new IWorkbenchPage[] { activePage, otherPage }); | ||
| when(activePage.getEditorReferences()).thenReturn(new IEditorReference[0]); | ||
| when(otherPage.getEditorReferences()).thenReturn(new IEditorReference[] { remainingEditorReference }); | ||
|
|
||
| service.setCurrentFile(currentFile); | ||
| assertSame(currentFile, service.getCurrentFile()); | ||
|
|
||
| listener.partClosed(closedEditorReference); | ||
|
|
||
| assertSame(currentFile, service.getCurrentFile()); | ||
| } finally { | ||
| service.dispose(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private IPartListener2 getRegisteredPartListener() { | ||
| ArgumentCaptor<IPartListener2> listenerCaptor = ArgumentCaptor.forClass(IPartListener2.class); | ||
| verify(partService).addPartListener(listenerCaptor.capture()); | ||
| return listenerCaptor.getValue(); | ||
| } | ||
|
|
||
| private static class TestReferencedFileService extends ReferencedFileService { | ||
| void setCurrentFile(IFile file) { | ||
| setCurrentFileForTest(file); | ||
| } | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,13 +26,19 @@ | |
| import org.eclipse.lsp4e.LSPEclipseUtils; | ||
| import org.eclipse.lsp4j.Position; | ||
| import org.eclipse.lsp4j.Range; | ||
| import org.eclipse.ui.IEditorInput; | ||
| import org.eclipse.ui.IEditorPart; | ||
| import org.eclipse.ui.IEditorReference; | ||
| import org.eclipse.ui.IFileEditorInput; | ||
| import org.eclipse.ui.IPartListener2; | ||
| import org.eclipse.ui.IWorkbench; | ||
| import org.eclipse.ui.IWorkbenchPage; | ||
| import org.eclipse.ui.IWorkbenchPart; | ||
| import org.eclipse.ui.IWorkbenchPartReference; | ||
| import org.eclipse.ui.IWorkbenchWindow; | ||
| import org.eclipse.ui.PartInitException; | ||
| import org.eclipse.ui.PlatformUI; | ||
| import org.eclipse.ui.ide.ResourceUtil; | ||
| import org.eclipse.ui.texteditor.ITextEditor; | ||
|
|
||
| import com.microsoft.copilot.eclipse.core.Constants; | ||
|
|
@@ -105,12 +111,10 @@ public void partBroughtToTop(IWorkbenchPartReference partRef) { | |
|
|
||
| @Override | ||
| public void partClosed(IWorkbenchPartReference partRef) { | ||
| IWorkbenchPage page = UiUtils.getActivePage(); | ||
| if (page == null || page.getEditorReferences().length == 0) { | ||
| ensureRealm(() -> { | ||
| currentFileObservable.setValue(null); | ||
| currentSelectionObservable.setValue(null); | ||
| }); | ||
| boolean closedCurrentFile = isCurrentReferencedFile(partRef); | ||
| boolean noOpenEditors = hasNoOpenEditors(); | ||
| if (closedCurrentFile || noOpenEditors) { | ||
| clearCurrentReferencedFile(); | ||
| } | ||
| untrackSelectionInEditor(partRef); | ||
| } | ||
|
|
@@ -479,6 +483,98 @@ private void updateCurrentReferencedFile(IWorkbenchPartReference partRef) { | |
| } | ||
| } | ||
|
|
||
| private boolean isCurrentReferencedFile(IWorkbenchPartReference partRef) { | ||
| IFile closedFile = getFileFromPartReference(partRef); | ||
| if (closedFile == null) { | ||
| return false; | ||
| } | ||
|
|
||
| AtomicReference<IFile> currentFile = new AtomicReference<>(); | ||
| ensureRealm(() -> currentFile.set(currentFileObservable.getValue())); | ||
| return closedFile.equals(currentFile.get()); | ||
| } | ||
|
nanookclaw marked this conversation as resolved.
|
||
|
|
||
| private IFile getFileFromPartReference(IWorkbenchPartReference partRef) { | ||
| if (partRef instanceof IEditorReference editorReference) { | ||
| IFile file = getFileFromEditorReference(editorReference); | ||
| if (file != null) { | ||
| return file; | ||
| } | ||
| } | ||
|
|
||
| IWorkbenchPart part = partRef.getPart(false); | ||
| if (part instanceof IEditorPart editorPart) { | ||
| return UiUtils.getFileFromEditorPart(editorPart); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private IFile getFileFromEditorReference(IEditorReference editorReference) { | ||
| try { | ||
| return getFileFromEditorInput(editorReference.getEditorInput()); | ||
| } catch (PartInitException e) { | ||
| CopilotCore.LOGGER.error("Failed to get editor input from part reference", e); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private IFile getFileFromEditorInput(IEditorInput input) { | ||
| if (input instanceof IFileEditorInput fileEditorInput) { | ||
| return fileEditorInput.getFile(); | ||
| } | ||
| return ResourceUtil.getFile(input); | ||
| } | ||
|
|
||
| private boolean hasNoOpenEditors() { | ||
| IWorkbench workbench = PlatformUI.getWorkbench(); | ||
| if (workbench == null) { | ||
| return true; | ||
| } | ||
|
|
||
| IWorkbenchWindow[] windows = workbench.getWorkbenchWindows(); | ||
| if (windows == null || windows.length == 0) { | ||
| return true; | ||
| } | ||
|
|
||
| for (IWorkbenchWindow window : windows) { | ||
| if (window == null) { | ||
| continue; | ||
| } | ||
|
|
||
| IWorkbenchPage[] pages = window.getPages(); | ||
| if (pages == null || pages.length == 0) { | ||
| IWorkbenchPage activePage = window.getActivePage(); | ||
| pages = activePage == null ? new IWorkbenchPage[0] : new IWorkbenchPage[] { activePage }; | ||
| } | ||
|
|
||
| for (IWorkbenchPage page : pages) { | ||
| if (page == null) { | ||
| continue; | ||
| } | ||
|
|
||
| IEditorReference[] editorReferences = page.getEditorReferences(); | ||
| if (editorReferences != null && editorReferences.length > 0) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
nanookclaw marked this conversation as resolved.
|
||
|
|
||
| private void clearCurrentReferencedFile() { | ||
| ensureRealm(() -> { | ||
| currentFileObservable.setValue(null); | ||
| currentSelectionObservable.setValue(null); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the current file for tests. | ||
| */ | ||
| protected void setCurrentFileForTest(IFile file) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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? |
||
| ensureRealm(() -> currentFileObservable.setValue(file)); | ||
| } | ||
|
|
||
| private void updateCurrentReferencedFile(IEditorPart editorPart) { | ||
| if (editorPart == null) { | ||
| updateObservable(currentFileObservable, null); | ||
|
|
@@ -529,4 +625,4 @@ private void unregisterPartListener() { | |
| } | ||
| } | ||
|
|
||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious about the no open editors check here.
From code, seems like
closedCurrentFileis enough, why we have this check?