diff --git a/extensions/image-preview/package.json b/extensions/image-preview/package.json index 7dd872ae1bc9e96f89312d9d1d24aa8162a70d31..f90152f7bc04f1da2afd0c17f4ef87e1bb8f56ec 100644 --- a/extensions/image-preview/package.json +++ b/extensions/image-preview/package.json @@ -24,6 +24,7 @@ { "viewType": "imagePreview.previewEditor", "displayName": "%webviewEditors.displayName%", + "priority": "builtin", "selector": [ { "filenamePattern": "*.{jpg,jpe,jpeg,png,bmp,gif,ico,tga,webp}", diff --git a/src/vs/workbench/contrib/customEditor/browser/customEditors.ts b/src/vs/workbench/contrib/customEditor/browser/customEditors.ts index 36eb48e2e48fcce099f806434122ad2dec699d57..a1bd70c18f057c51f730881fcbdc511d9aa926af 100644 --- a/src/vs/workbench/contrib/customEditor/browser/customEditors.ts +++ b/src/vs/workbench/contrib/customEditor/browser/customEditors.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { coalesce, distinct } from 'vs/base/common/arrays'; +import { coalesce, distinct, mergeSort, find } from 'vs/base/common/arrays'; import * as glob from 'vs/base/common/glob'; import { UnownedDisposable, Disposable } from 'vs/base/common/lifecycle'; import { Schemas } from 'vs/base/common/network'; @@ -272,10 +272,19 @@ export class CustomEditorContribution implements IWorkbenchContribution { return; } - const defaultEditors = contributedEditors.filter(editor => editor.priority === CustomEditorPriority.default); - if (defaultEditors.length === 1) { + // Find the single default editor to use (if any) by looking at the editor's priority and the + // other contributed editors. + const defaultEditor = find(contributedEditors, editor => { + if (editor.priority !== CustomEditorPriority.default && editor.priority !== CustomEditorPriority.builtin) { + return false; + } + return contributedEditors.every(otherEditor => + otherEditor === editor || isLowerPriority(otherEditor, editor)); + }); + + if (defaultEditor) { return { - override: this.customEditorService.openWith(resource, defaultEditors[0].id, options, group), + override: this.customEditorService.openWith(resource, defaultEditor.id, options, group), }; } } @@ -326,15 +335,20 @@ export class CustomEditorContribution implements IWorkbenchContribution { return undefined; } - const editors = distinct([ - ...this.customEditorService.getUserConfiguredCustomEditors(resource), - ...this.customEditorService.getContributedCustomEditors(resource), - ], editor => editor.id); + // Prefer default editors in the diff editor case but ultimatly always take the first editor + const editors = mergeSort( + distinct([ + ...this.customEditorService.getUserConfiguredCustomEditors(resource), + ...this.customEditorService.getContributedCustomEditors(resource), + ], editor => editor.id), + (a, b) => { + return priorityToRank(a.priority) - priorityToRank(b.priority); + }); if (!editors.length) { return undefined; } - // Always prefer the first editor in the diff editor case + return this.customEditorService.createInput(resource, editors[0].id, group, { customClasses }); }; @@ -354,6 +368,18 @@ export class CustomEditorContribution implements IWorkbenchContribution { } } +function isLowerPriority(otherEditor: CustomEditorInfo, editor: CustomEditorInfo): unknown { + return priorityToRank(otherEditor.priority) < priorityToRank(editor.priority); +} + +function priorityToRank(priority: CustomEditorPriority): number { + switch (priority) { + case CustomEditorPriority.default: return 3; + case CustomEditorPriority.builtin: return 2; + case CustomEditorPriority.option: return 1; + } +} + function matches(selector: CustomEditorSelector, resource: URI): boolean { if (resource.scheme === Schemas.data) { if (!selector.mime) { diff --git a/src/vs/workbench/contrib/customEditor/browser/extensionPoint.ts b/src/vs/workbench/contrib/customEditor/browser/extensionPoint.ts index 0ab4cbd7514bfd1830ba095800e1ef1ff4f3b9cd..5e169d1b2c41de1631e6464a81329756cc0571bb 100644 --- a/src/vs/workbench/contrib/customEditor/browser/extensionPoint.ts +++ b/src/vs/workbench/contrib/customEditor/browser/extensionPoint.ts @@ -65,11 +65,13 @@ const webviewEditorsContribution: IJSONSchema = { description: nls.localize('contributes.priority', 'Controls when the custom editor is used. May be overridden by users.'), enum: [ CustomEditorPriority.default, - CustomEditorPriority.option + CustomEditorPriority.option, + CustomEditorPriority.builtin, ], enumDescriptions: [ nls.localize('contributes.priority.default', 'Editor is automatically used for a resource if no other default custom editors are registered for it.'), nls.localize('contributes.priority.option', 'Editor is not automatically used but can be selected by a user.'), + nls.localize('contributes.priority.builtin', 'Editor automatically used if no other `default` or `builtin` editors are registered for the resource.'), ], default: 'default' } diff --git a/src/vs/workbench/contrib/customEditor/common/customEditor.ts b/src/vs/workbench/contrib/customEditor/common/customEditor.ts index f635727192f73a9f0239262eebbf1a95f33cd556..a0bda2e0764a3246650d5432299815407272010a 100644 --- a/src/vs/workbench/contrib/customEditor/common/customEditor.ts +++ b/src/vs/workbench/contrib/customEditor/common/customEditor.ts @@ -28,6 +28,7 @@ export interface ICustomEditorService { export const enum CustomEditorPriority { default = 'default', + builtin = 'builtin', option = 'option', }