@@ -240,23 +240,25 @@ export class CustomEditorService extends Disposable implements ICustomEditorServ
240240 options ?: IEditorOptions ,
241241 group ?: IEditorGroup
242242 ) : Promise < IEditor | undefined > {
243- if ( group ) {
244- const existingEditors = group . editors . filter ( editor => editor . getResource ( ) && isEqual ( editor . getResource ( ) , resource ) ) ;
245- if ( existingEditors . length ) {
246- const existing = existingEditors [ 0 ] ;
247- if ( ! input . matches ( existing ) ) {
248- await this . editorService . replaceEditors ( [ {
249- editor : existing ,
250- replacement : input ,
251- options : options ? EditorOptions . create ( options ) : undefined ,
252- } ] , group ) ;
253-
254- if ( existing instanceof CustomFileEditorInput ) {
255- existing . dispose ( ) ;
256- }
243+ const targetGroup = group || this . editorGroupService . activeGroup ;
244+
245+ // Try to replace existing editors for resource
246+ const existingEditors = targetGroup . editors . filter ( editor => editor . getResource ( ) && isEqual ( editor . getResource ( ) , resource ) ) ;
247+ if ( existingEditors . length ) {
248+ const existing = existingEditors [ 0 ] ;
249+ if ( ! input . matches ( existing ) ) {
250+ await this . editorService . replaceEditors ( [ {
251+ editor : existing ,
252+ replacement : input ,
253+ options : options ? EditorOptions . create ( options ) : undefined ,
254+ } ] , targetGroup ) ;
255+
256+ if ( existing instanceof CustomFileEditorInput ) {
257+ existing . dispose ( ) ;
257258 }
258259 }
259260 }
261+
260262 return this . editorService . openEditor ( input , options , group ) ;
261263 }
262264
@@ -345,6 +347,17 @@ export class CustomEditorContribution implements IWorkbenchContribution {
345347 options : ITextEditorOptions | undefined ,
346348 group : IEditorGroup
347349 ) : IOpenEditorOverride | undefined {
350+ // Check to see if there already an editor for the resource in the group.
351+ // If there is, we want to open that instead of creating a new editor.
352+ // This ensures that we preserve whatever state the editor was previously in
353+ // when the user switches back to it.
354+ const existingEditorForResource = group . editors . find ( editor => isEqual ( resource , editor . getResource ( ) ) ) ;
355+ if ( existingEditorForResource ) {
356+ return {
357+ override : this . editorService . openEditor ( existingEditorForResource , { ...options , ignoreOverrides : true } , group )
358+ } ;
359+ }
360+
348361 const userConfiguredEditors = this . customEditorService . getUserConfiguredCustomEditors ( resource ) ;
349362 if ( userConfiguredEditors . length ) {
350363 return {
0 commit comments