Adding builtin priority for custom editors
This commit is contained in:
parent
89e4d3eddc
commit
f462eab969
|
@ -24,6 +24,7 @@
|
|||
{
|
||||
"viewType": "imagePreview.previewEditor",
|
||||
"displayName": "%webviewEditors.displayName%",
|
||||
"priority": "builtin",
|
||||
"selector": [
|
||||
{
|
||||
"filenamePattern": "*.{jpg,jpe,jpeg,png,bmp,gif,ico,tga,webp}",
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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'
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ export interface ICustomEditorService {
|
|||
|
||||
export const enum CustomEditorPriority {
|
||||
default = 'default',
|
||||
builtin = 'builtin',
|
||||
option = 'option',
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue