Skip to content

Commit 5d2088c

Browse files
Context key availableEditorIds for diff editors (microsoft#250198)
1 parent 8ce742c commit 5d2088c

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

src/vs/workbench/common/contextkeys.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { Schemas } from '../../base/common/network.js';
1515
import { EditorInput } from './editor/editorInput.js';
1616
import { IEditorResolverService } from '../services/editor/common/editorResolverService.js';
1717
import { DEFAULT_EDITOR_ASSOCIATION } from './editor.js';
18+
import { DiffEditorInput } from './editor/diffEditorInput.js';
1819

1920
//#region < --- Workbench --- >
2021

@@ -303,13 +304,30 @@ export function applyAvailableEditorIds(contextKey: IContextKey<string>, editor:
303304
return;
304305
}
305306

306-
const editorResource = editor.resource;
307-
if (editorResource?.scheme === Schemas.untitled && editor.editorId !== DEFAULT_EDITOR_ASSOCIATION.id) {
308-
// Non text editor untitled files cannot be easily serialized between extensions
309-
// so instead we disable this context key to prevent common commands that act on the active editor
310-
contextKey.set('');
311-
} else {
312-
const editors = editorResource ? editorResolverService.getEditors(editorResource).map(editor => editor.id) : [];
313-
contextKey.set(editors.join(','));
307+
const editors = getAvailableEditorIds(editor, editorResolverService);
308+
contextKey.set(editors.join(','));
309+
}
310+
311+
function getAvailableEditorIds(editor: EditorInput, editorResolverService: IEditorResolverService): string[] {
312+
// Non text editor untitled files cannot be easily serialized between
313+
// extensions so instead we disable this context key to prevent common
314+
// commands that act on the active editor.
315+
if (editor.resource?.scheme === Schemas.untitled && editor.editorId !== DEFAULT_EDITOR_ASSOCIATION.id) {
316+
return [];
317+
}
318+
319+
// Diff editors. The original and modified resources of a diff editor
320+
// *should* be the same, but calculate the set intersection just to be safe.
321+
if (editor instanceof DiffEditorInput) {
322+
const original = getAvailableEditorIds(editor.original, editorResolverService);
323+
const modified = new Set(getAvailableEditorIds(editor.modified, editorResolverService));
324+
return original.filter(editor => modified.has(editor));
325+
}
326+
327+
// Normal editors.
328+
if (editor.resource) {
329+
return editorResolverService.getEditors(editor.resource).map(editor => editor.id);
314330
}
331+
332+
return [];
315333
}

0 commit comments

Comments
 (0)