/
customEditor.ts
194 lines (155 loc) · 6.67 KB
/
customEditor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { distinct } from 'vs/base/common/arrays';
import { Event } from 'vs/base/common/event';
import * as glob from 'vs/base/common/glob';
import { IDisposable, IReference } from 'vs/base/common/lifecycle';
import { Schemas } from 'vs/base/common/network';
import { posix } from 'vs/base/common/path';
import { basename } from 'vs/base/common/resources';
import { URI } from 'vs/base/common/uri';
import { RawContextKey } from 'vs/platform/contextkey/common/contextkey';
import { ITextEditorOptions } from 'vs/platform/editor/common/editor';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService';
import * as nls from 'vs/nls';
import { IEditorPane, IRevertOptions, ISaveOptions } from 'vs/workbench/common/editor';
export const ICustomEditorService = createDecorator<ICustomEditorService>('customEditorService');
export const CONTEXT_ACTIVE_CUSTOM_EDITOR_ID = new RawContextKey<string>('activeCustomEditorId', '', {
type: 'string',
description: nls.localize('context.customEditor', "The viewType of the currently active custom editor."),
});
export const CONTEXT_FOCUSED_CUSTOM_EDITOR_IS_EDITABLE = new RawContextKey<boolean>('focusedCustomEditorIsEditable', false);
export interface CustomEditorCapabilities {
readonly supportsMultipleEditorsPerDocument?: boolean;
}
export interface ICustomEditorService {
_serviceBrand: any;
readonly models: ICustomEditorModelManager;
getCustomEditor(viewType: string): CustomEditorInfo | undefined;
getAllCustomEditors(resource: URI): CustomEditorInfoCollection;
getContributedCustomEditors(resource: URI): CustomEditorInfoCollection;
getUserConfiguredCustomEditors(resource: URI): CustomEditorInfoCollection;
openWith(resource: URI, customEditorViewType: string, options?: ITextEditorOptions, group?: IEditorGroup): Promise<IEditorPane | undefined>;
registerCustomEditorCapabilities(viewType: string, options: CustomEditorCapabilities): IDisposable;
getCustomEditorCapabilities(viewType: string): CustomEditorCapabilities | undefined
}
export interface ICustomEditorModelManager {
get(resource: URI, viewType: string): Promise<ICustomEditorModel | undefined>;
tryRetain(resource: URI, viewType: string): Promise<IReference<ICustomEditorModel>> | undefined;
add(resource: URI, viewType: string, model: Promise<ICustomEditorModel>): Promise<IReference<ICustomEditorModel>>;
disposeAllModelsForView(viewType: string): void;
}
export interface ICustomEditorModel extends IDisposable {
readonly viewType: string;
readonly resource: URI;
readonly backupId: string | undefined;
isEditable(): boolean;
isOnReadonlyFileSystem(): boolean;
isOrphaned(): boolean;
readonly onDidChangeOrphaned: Event<void>;
isDirty(): boolean;
readonly onDidChangeDirty: Event<void>;
revert(options?: IRevertOptions): Promise<void>;
saveCustomEditor(options?: ISaveOptions): Promise<URI | undefined>;
saveCustomEditorAs(resource: URI, targetResource: URI, currentOptions?: ISaveOptions): Promise<boolean>;
}
export const enum CustomEditorPriority {
default = 'default',
builtin = 'builtin',
option = 'option',
}
export interface CustomEditorSelector {
readonly filenamePattern?: string;
}
export interface CustomEditorDescriptor {
readonly id: string;
readonly displayName: string;
readonly providerDisplayName: string;
readonly priority: CustomEditorPriority;
readonly selector: readonly CustomEditorSelector[];
}
export class CustomEditorInfo implements CustomEditorDescriptor {
private static readonly excludedSchemes = new Set([
Schemas.extension,
Schemas.webviewPanel,
]);
public readonly id: string;
public readonly displayName: string;
public readonly providerDisplayName: string;
public readonly priority: CustomEditorPriority;
public readonly selector: readonly CustomEditorSelector[];
constructor(descriptor: CustomEditorDescriptor) {
this.id = descriptor.id;
this.displayName = descriptor.displayName;
this.providerDisplayName = descriptor.providerDisplayName;
this.priority = descriptor.priority;
this.selector = descriptor.selector;
}
matches(resource: URI): boolean {
return this.selector.some(selector => CustomEditorInfo.selectorMatches(selector, resource));
}
static selectorMatches(selector: CustomEditorSelector, resource: URI): boolean {
if (CustomEditorInfo.excludedSchemes.has(resource.scheme)) {
return false;
}
if (selector.filenamePattern) {
const matchOnPath = selector.filenamePattern.indexOf(posix.sep) >= 0;
const target = matchOnPath ? resource.path : basename(resource);
if (glob.match(selector.filenamePattern.toLowerCase(), target.toLowerCase())) {
return true;
}
}
return false;
}
}
export class CustomEditorInfoCollection {
public readonly allEditors: readonly CustomEditorInfo[];
constructor(
editors: readonly CustomEditorInfo[],
) {
this.allEditors = distinct(editors, editor => editor.id);
}
public get length(): number { return this.allEditors.length; }
/**
* Find the single default editor to use (if any) by looking at the editor's priority and the
* other contributed editors.
*/
public get defaultEditor(): CustomEditorInfo | undefined {
return this.allEditors.find(editor => {
switch (editor.priority) {
case CustomEditorPriority.default:
case CustomEditorPriority.builtin:
// A default editor must have higher priority than all other contributed editors.
return this.allEditors.every(otherEditor =>
otherEditor === editor || isLowerPriority(otherEditor, editor));
default:
return false;
}
});
}
/**
* Find the best available editor to use.
*
* Unlike the `defaultEditor`, a bestAvailableEditor can exist even if there are other editors with
* the same priority.
*/
public get bestAvailableEditor(): CustomEditorInfo | undefined {
const editors = Array.from(this.allEditors).sort((a, b) => {
return priorityToRank(a.priority) - priorityToRank(b.priority);
});
return editors[0];
}
}
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;
}
}