Skip to content

Commit 0c259c2

Browse files
authored
fix: Update compare editor handling to async. (#1622)
1 parent f19aa4d commit 0c259c2

2 files changed

Lines changed: 47 additions & 23 deletions

File tree

com.microsoft.copilot.eclipse.ui/src/com/microsoft/copilot/eclipse/ui/chat/tools/EditFileTool.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Map;
1111
import java.util.concurrent.CompletableFuture;
1212

13+
import org.eclipse.compare.CompareEditorInput;
1314
import org.eclipse.core.resources.IFile;
1415
import org.eclipse.core.resources.IResource;
1516
import org.eclipse.core.runtime.CoreException;
@@ -212,12 +213,15 @@ public void onUndoAllChanges(List<IFile> files) throws CoreException, IOExceptio
212213

213214
@Override
214215
public void onViewDiff(IFile file) {
215-
// Check if the file is already open in a compare editor and bring it to the top if is exists
216-
if (compareEditorInputMap.containsKey(file) && bringCompareEditorToTop(compareEditorInputMap.get(file))) {
217-
return;
216+
CompareEditorInput input = compareEditorInputMap.get(file);
217+
if (input != null) {
218+
if (isCompareEditorOpen(input)) {
219+
bringCompareEditorToTop(input);
220+
return;
221+
}
222+
// Compare editor was closed by the user, remove stale entry and recreate
223+
compareEditorInputMap.remove(file);
218224
}
219-
// If the compare editor is created but closed, remove it from the map and create a new one
220-
compareEditorInputMap.remove(file);
221225
compareStringWithFile(fileContentCache.get(file), file);
222226
}
223227

com.microsoft.copilot.eclipse.ui/src/com/microsoft/copilot/eclipse/ui/chat/tools/FileToolBase.java

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,13 @@ protected void compareStringWithFile(String originalFileContent, IFile file) {
113113
try {
114114
CompareEditorInput input = createCompareEditorInput(originalFileContent, file);
115115
input.run(new NullProgressMonitor());
116-
117-
// TODO: Add a progress monitor to show the progress of the operation input.run(new NullProgressMonitor());
118116
compareEditorInputMap.put(file, input);
119-
SwtUtils.invokeOnDisplayThread(() -> {
120-
CompareUI.openCompareEditor(input);
117+
// TODO: Add a progress monitor to show the progress of the operation input.run(new NullProgressMonitor());
118+
SwtUtils.invokeOnDisplayThreadAsync(() -> {
119+
CompareEditorInput compareEditorInput = compareEditorInputMap.get(file);
120+
if (compareEditorInput != null) {
121+
CompareUI.openCompareEditor(compareEditorInput);
122+
}
121123
});
122124
} catch (InvocationTargetException | InterruptedException e) {
123125
CopilotCore.LOGGER.error("Error opening compare editor", e);
@@ -138,15 +140,18 @@ protected void updateOrCreateCompareStringWithFile(String fileContent, IFile fil
138140
CompareEditorInput input = compareEditorInputMap.get(file);
139141
if (input != null) {
140142
if (fileContent.equals(fileContentCache.get(file))) {
141-
SwtUtils.invokeOnDisplayThread(() -> {
143+
SwtUtils.invokeOnDisplayThreadAsync(() -> {
142144
CompareUI.reuseCompareEditor(input, (IReusableEditor) getCompareEditor(input));
143145
});
144146
} else {
145147
CompareEditorInput newInput = createCompareEditorInput(fileContent, file);
146-
SwtUtils.invokeOnDisplayThread(() -> {
147-
CompareUI.reuseCompareEditor(newInput, (IReusableEditor) getCompareEditor(input));
148-
});
149148
compareEditorInputMap.put(file, newInput);
149+
SwtUtils.invokeOnDisplayThreadAsync(() -> {
150+
CompareEditorInput compareEditorInput = compareEditorInputMap.get(file);
151+
if (compareEditorInput != null) {
152+
CompareUI.reuseCompareEditor(compareEditorInput, (IReusableEditor) getCompareEditor(compareEditorInput));
153+
}
154+
});
150155
}
151156
bringCompareEditorToTop(input);
152157
} else {
@@ -156,8 +161,8 @@ protected void updateOrCreateCompareStringWithFile(String fileContent, IFile fil
156161
}
157162

158163
/**
159-
* Refreshes the compare editor for the given file only if it is already open.
160-
* Does not open a new editor or steal focus.
164+
* Refreshes the compare editor for the given file only if it is already open. Does not open a new editor or steal
165+
* focus.
161166
*
162167
* @param fileContent The original file content to compare against.
163168
* @param file The file whose compare editor should be refreshed.
@@ -172,8 +177,15 @@ protected void refreshCompareEditorIfOpen(String fileContent, IFile file) {
172177
compareEditorInputMap.put(file, newInput);
173178
SwtUtils.invokeOnDisplayThreadAsync(() -> {
174179
IEditorPart editor = getCompareEditor(input);
175-
if (editor != null) {
176-
CompareUI.reuseCompareEditor(newInput, (IReusableEditor) editor);
180+
if (editor == null) {
181+
// If the compare editor is closed, remove the input from the map and skip refreshing.
182+
compareEditorInputMap.remove(file);
183+
return;
184+
} else {
185+
CompareEditorInput compareEditorInput = compareEditorInputMap.get(file);
186+
if (compareEditorInput != null) {
187+
CompareUI.reuseCompareEditor(compareEditorInput, (IReusableEditor) editor);
188+
}
177189
}
178190
});
179191
}
@@ -183,19 +195,27 @@ protected void refreshCompareEditorIfOpen(String fileContent, IFile file) {
183195
* Brings the compare editor to the top of the workbench.
184196
*
185197
* @param input The CompareEditorInput to be brought to the top.
186-
* @return true if the editor was successfully brought to the top, false otherwise.
187198
*/
188-
protected boolean bringCompareEditorToTop(CompareEditorInput input) {
189-
AtomicReference<Boolean> ref = new AtomicReference<>(false);
190-
SwtUtils.invokeOnDisplayThread(() -> {
199+
protected void bringCompareEditorToTop(CompareEditorInput input) {
200+
SwtUtils.invokeOnDisplayThreadAsync(() -> {
191201
IWorkbenchPage page = UiUtils.getActivePage();
192202
IEditorPart editor = getCompareEditor(input);
193203
if (editor != null) {
194204
page.bringToTop(editor);
195-
ref.set(true);
196205
}
197206
});
198-
return ref.get();
207+
}
208+
209+
/**
210+
* Checks whether the compare editor for the given input is still open.
211+
*
212+
* @param input The CompareEditorInput to check.
213+
* @return true if the editor is open, false otherwise.
214+
*/
215+
protected boolean isCompareEditorOpen(CompareEditorInput input) {
216+
AtomicReference<Boolean> isOpen = new AtomicReference<>(false);
217+
SwtUtils.invokeOnDisplayThread(() -> isOpen.set(getCompareEditor(input) != null));
218+
return isOpen.get();
199219
}
200220

201221
private IEditorPart getCompareEditor(CompareEditorInput input) {

0 commit comments

Comments
 (0)