@@ -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