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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Event } from 'vs/base/common/event';
import { Registry } from 'vs/platform/registry/common/platform';
import { ResourceMap } from 'vs/base/common/map';
import { createDecorator, IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IEditorFactoryRegistry, IFileEditorInput, IUntypedEditorInput, IUntypedFileEditorInput, EditorExtensions, isResourceDiffEditorInput, isResourceSideBySideEditorInput, IUntitledTextResourceEditorInput, DEFAULT_EDITOR_ASSOCIATION } from 'vs/workbench/common/editor';
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
import { INewUntitledTextEditorOptions, IUntitledTextEditorService } from 'vs/workbench/services/untitled/common/untitledTextEditorService';
import { Schemas } from 'vs/base/common/network';
import { DiffEditorInput } from 'vs/workbench/common/editor/diffEditorInput';
import { SideBySideEditorInput } from 'vs/workbench/common/editor/sideBySideEditorInput';
import { TextResourceEditorInput } from 'vs/workbench/common/editor/textResourceEditorInput';
import { UntitledTextEditorInput } from 'vs/workbench/services/untitled/common/untitledTextEditorInput';
import { IUntitledTextEditorModel } from 'vs/workbench/services/untitled/common/untitledTextEditorModel';
import { basename } from 'vs/base/common/resources';
import { URI } from 'vs/base/common/uri';
import { IUriIdentityService } from 'vs/platform/uriIdentity/common/uriIdentity';
import { IFileService } from 'vs/platform/files/common/files';
import { IEditorResolverService, RegisteredEditorPriority } from 'vs/workbench/services/editor/common/editorResolverService';
import { Disposable } from 'vs/base/common/lifecycle';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
export const ITextEditorService = createDecorator<ITextEditorService>('textEditorService');
export interface ITextEditorService {
readonly _serviceBrand: undefined;
/**
* A way to create text editor inputs from an untyped editor input. Depending
* on the passed in input this will be:
* - a `IFileEditorInput` for file resources
* - a `UntitledEditorInput` for untitled resources
* - a `TextResourceEditorInput` for virtual resources
*
* @param input the untyped editor input to create a typed input from
*/
createTextEditor(input: IUntypedEditorInput): EditorInput;
createTextEditor(input: IUntypedFileEditorInput): IFileEditorInput;
}
export class TextEditorService extends Disposable implements ITextEditorService {
declare readonly _serviceBrand: undefined;
private readonly editorInputCache = new ResourceMap<TextResourceEditorInput | IFileEditorInput | UntitledTextEditorInput>();
private readonly fileEditorFactory = Registry.as<IEditorFactoryRegistry>(EditorExtensions.EditorFactory).getFileEditorFactory();
constructor(
@IUntitledTextEditorService private readonly untitledTextEditorService: IUntitledTextEditorService,
@IInstantiationService private readonly instantiationService: IInstantiationService,
@IUriIdentityService private readonly uriIdentityService: IUriIdentityService,
@IFileService private readonly fileService: IFileService,
@IEditorResolverService private readonly editorResolverService: IEditorResolverService
) {
super();
// Register the default editor to the editor resolver
// service so that it shows up in the editors picker
this.registerDefaultEditor();
}
private registerDefaultEditor(): void {
this._register(this.editorResolverService.registerEditor(
'*',
{
id: DEFAULT_EDITOR_ASSOCIATION.id,
label: DEFAULT_EDITOR_ASSOCIATION.displayName,
detail: DEFAULT_EDITOR_ASSOCIATION.providerDisplayName,
priority: RegisteredEditorPriority.builtin
},
{},
editor => ({ editor: this.createTextEditor(editor) }),
untitledEditor => ({ editor: this.createTextEditor(untitledEditor) }),
diffEditor => ({ editor: this.createTextEditor(diffEditor) })
));
}
createTextEditor(input: IUntypedEditorInput): EditorInput;
createTextEditor(input: IUntypedFileEditorInput): IFileEditorInput;
createTextEditor(input: IUntypedEditorInput | IUntypedFileEditorInput): EditorInput | IFileEditorInput {
// Diff Editor Support
if (isResourceDiffEditorInput(input)) {
const original = this.createTextEditor({ ...input.original });
const modified = this.createTextEditor({ ...input.modified });
return this.instantiationService.createInstance(DiffEditorInput, input.label, input.description, original, modified, undefined);
}
// Side by Side Editor Support
if (isResourceSideBySideEditorInput(input)) {
const primary = this.createTextEditor({ ...input.primary });
const secondary = this.createTextEditor({ ...input.secondary });
return this.instantiationService.createInstance(SideBySideEditorInput, input.label, input.description, secondary, primary);
}
// Untitled text file support
const untitledInput = input as IUntitledTextResourceEditorInput;
if (untitledInput.forceUntitled || !untitledInput.resource || (untitledInput.resource.scheme === Schemas.untitled)) {
const untitledOptions: Partial<INewUntitledTextEditorOptions> = {
languageId: untitledInput.languageId,
initialValue: untitledInput.contents,
encoding: untitledInput.encoding
};
// Untitled resource: use as hint for an existing untitled editor
let untitledModel: IUntitledTextEditorModel;
if (untitledInput.resource?.scheme === Schemas.untitled) {
untitledModel = this.untitledTextEditorService.create({ untitledResource: untitledInput.resource, ...untitledOptions });
}
// Other resource: use as hint for associated filepath
else {
untitledModel = this.untitledTextEditorService.create({ associatedResource: untitledInput.resource, ...untitledOptions });
}
return this.createOrGetCached(untitledModel.resource, () => {
// Factory function for new untitled editor
const input = this.instantiationService.createInstance(UntitledTextEditorInput, untitledModel);
// We dispose the untitled model once the editor
// is being disposed. Even though we may have not
// created the model initially, the lifecycle for
// untitled is tightly coupled with the editor
// lifecycle for now.
Event.once(input.onWillDispose)(() => untitledModel.dispose());
return input;
});
}
// Text File/Resource Editor Support
const textResourceEditorInput = input as IUntypedFileEditorInput;
if (textResourceEditorInput.resource instanceof URI) {
// Derive the label from the path if not provided explicitly
const label = textResourceEditorInput.label || basename(textResourceEditorInput.resource);
// We keep track of the preferred resource this input is to be created
// with but it may be different from the canonical resource (see below)
const preferredResource = textResourceEditorInput.resource;
// From this moment on, only operate on the canonical resource
// to ensure we reduce the chance of opening the same resource
// with different resource forms (e.g. path casing on Windows)
const canonicalResource = this.uriIdentityService.asCanonicalUri(preferredResource);
return this.createOrGetCached(canonicalResource, () => {
// File
if (textResourceEditorInput.forceFile || this.fileService.hasProvider(canonicalResource)) {
return this.fileEditorFactory.createFileEditor(canonicalResource, preferredResource, textResourceEditorInput.label, textResourceEditorInput.description, textResourceEditorInput.encoding, textResourceEditorInput.languageId, textResourceEditorInput.contents, this.instantiationService);
}
// Resource
return this.instantiationService.createInstance(TextResourceEditorInput, canonicalResource, textResourceEditorInput.label, textResourceEditorInput.description, textResourceEditorInput.languageId, textResourceEditorInput.contents);
}, cachedInput => {
// Untitled
if (cachedInput instanceof UntitledTextEditorInput) {
return;
}
// Files
else if (!(cachedInput instanceof TextResourceEditorInput)) {
cachedInput.setPreferredResource(preferredResource);
if (textResourceEditorInput.label) {
cachedInput.setPreferredName(textResourceEditorInput.label);
}
if (textResourceEditorInput.description) {
cachedInput.setPreferredDescription(textResourceEditorInput.description);
}
if (textResourceEditorInput.encoding) {
cachedInput.setPreferredEncoding(textResourceEditorInput.encoding);
}
if (textResourceEditorInput.languageId) {
cachedInput.setPreferredLanguageId(textResourceEditorInput.languageId);
}
if (typeof textResourceEditorInput.contents === 'string') {
cachedInput.setPreferredContents(textResourceEditorInput.contents);
}
}
// Resources
else {
if (label) {
cachedInput.setName(label);
}
if (textResourceEditorInput.description) {
cachedInput.setDescription(textResourceEditorInput.description);
}
if (textResourceEditorInput.languageId) {
cachedInput.setPreferredLanguageId(textResourceEditorInput.languageId);
}
if (typeof textResourceEditorInput.contents === 'string') {
cachedInput.setPreferredContents(textResourceEditorInput.contents);
}
}
});
}
throw new Error(`ITextEditorService: Unable to create texteditor from ${JSON.stringify(input)}`);
}
private createOrGetCached(
resource: URI,
factoryFn: () => TextResourceEditorInput | IFileEditorInput | UntitledTextEditorInput,
cachedFn?: (input: TextResourceEditorInput | IFileEditorInput | UntitledTextEditorInput) => void
): TextResourceEditorInput | IFileEditorInput | UntitledTextEditorInput {
// Return early if already cached
let input = this.editorInputCache.get(resource);
if (input) {
cachedFn?.(input);
return input;
}
// Otherwise create and add to cache
input = factoryFn();
this.editorInputCache.set(resource, input);
Event.once(input.onWillDispose)(() => this.editorInputCache.delete(resource));
return input;
}
}
registerSingleton(ITextEditorService, TextEditorService, false /* do not change: https://github.com/microsoft/vscode/issues/137675 */);
|