-
Notifications
You must be signed in to change notification settings - Fork 30.9k
/
Copy pathuntitledTextEditorService.ts
264 lines (208 loc) · 9.57 KB
/
untitledTextEditorService.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { URI } from 'vs/base/common/uri';
import { createDecorator, IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { UntitledTextEditorModel, IUntitledTextEditorModel } from 'vs/workbench/services/untitled/common/untitledTextEditorModel';
import { IFilesConfiguration } from 'vs/platform/files/common/files';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { Event, Emitter } from 'vs/base/common/event';
import { ResourceMap } from 'vs/base/common/map';
import { Schemas } from 'vs/base/common/network';
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
export const IUntitledTextEditorService = createDecorator<IUntitledTextEditorService>('untitledTextEditorService');
export interface INewUntitledTextEditorOptions {
/**
* Initial value of the untitled editor. An untitled editor with initial
* value is dirty right from the beginning.
*/
initialValue?: string;
/**
* Preferred language mode to use when saving the untitled editor.
*/
mode?: string;
/**
* Preferred encoding to use when saving the untitled editor.
*/
encoding?: string;
}
export interface IExistingUntitledTextEditorOptions extends INewUntitledTextEditorOptions {
/**
* A resource to identify the untitled editor to create or return
* if already existing.
*
* Note: the resource will not be used unless the scheme is `untitled`.
*/
untitledResource?: URI;
}
export interface INewUntitledTextEditorWithAssociatedResourceOptions extends INewUntitledTextEditorOptions {
/**
* Resource components to associate with the untitled editor. When saving
* the untitled editor, the associated components will be used and the user
* is not being asked to provide a file path.
*
* Note: currently it is not possible to specify the `scheme` to use. The
* untitled editor will saved to the default local or remote resource.
*/
associatedResource?: { authority: string; path: string; query: string; fragment: string; }
}
type IInternalUntitledTextEditorOptions = IExistingUntitledTextEditorOptions & INewUntitledTextEditorWithAssociatedResourceOptions;
export interface IUntitledTextEditorModelManager {
/**
* Events for when untitled text editors change (e.g. getting dirty, saved or reverted).
*/
readonly onDidChangeDirty: Event<IUntitledTextEditorModel>;
/**
* Events for when untitled text editor encodings change.
*/
readonly onDidChangeEncoding: Event<IUntitledTextEditorModel>;
/**
* Events for when untitled text editor labels change.
*/
readonly onDidChangeLabel: Event<IUntitledTextEditorModel>;
/**
* Events for when untitled text editors are about to be disposed.
*/
readonly onWillDispose: Event<IUntitledTextEditorModel>;
/**
* Creates a new untitled editor model with the provided options. If the `untitledResource`
* property is provided and the untitled editor exists, it will return that existing
* instance instead of creating a new one.
*/
create(options?: INewUntitledTextEditorOptions): IUntitledTextEditorModel;
create(options?: INewUntitledTextEditorWithAssociatedResourceOptions): IUntitledTextEditorModel;
create(options?: IExistingUntitledTextEditorOptions): IUntitledTextEditorModel;
/**
* Returns an existing untitled editor model if already created before.
*/
get(resource: URI): IUntitledTextEditorModel | undefined;
/**
* Returns the value of the untitled editor, undefined if none exists
* @param resource The URI of the untitled file
* @returns The content, or undefined
*/
getValue(resource: URI): string | undefined;
/**
* Resolves an untitled editor model from the provided options. If the `untitledResource`
* property is provided and the untitled editor exists, it will return that existing
* instance instead of creating a new one.
*/
resolve(options?: INewUntitledTextEditorOptions): Promise<IUntitledTextEditorModel>;
resolve(options?: INewUntitledTextEditorWithAssociatedResourceOptions): Promise<IUntitledTextEditorModel>;
resolve(options?: IExistingUntitledTextEditorOptions): Promise<IUntitledTextEditorModel>;
}
export interface IUntitledTextEditorService extends IUntitledTextEditorModelManager {
readonly _serviceBrand: undefined;
}
export class UntitledTextEditorService extends Disposable implements IUntitledTextEditorService {
declare readonly _serviceBrand: undefined;
private readonly _onDidChangeDirty = this._register(new Emitter<IUntitledTextEditorModel>());
readonly onDidChangeDirty = this._onDidChangeDirty.event;
private readonly _onDidChangeEncoding = this._register(new Emitter<IUntitledTextEditorModel>());
readonly onDidChangeEncoding = this._onDidChangeEncoding.event;
private readonly _onWillDispose = this._register(new Emitter<IUntitledTextEditorModel>());
readonly onWillDispose = this._onWillDispose.event;
private readonly _onDidChangeLabel = this._register(new Emitter<IUntitledTextEditorModel>());
readonly onDidChangeLabel = this._onDidChangeLabel.event;
private readonly mapResourceToModel = new ResourceMap<UntitledTextEditorModel>();
constructor(
@IInstantiationService private readonly instantiationService: IInstantiationService,
@IConfigurationService private readonly configurationService: IConfigurationService
) {
super();
}
get(resource: URI): UntitledTextEditorModel | undefined {
return this.mapResourceToModel.get(resource);
}
getValue(resource: URI): string | undefined {
return this.get(resource)?.textEditorModel?.getValue();
}
async resolve(options?: IInternalUntitledTextEditorOptions): Promise<UntitledTextEditorModel> {
const model = this.doCreateOrGet(options);
await model.resolve();
return model;
}
create(options?: IInternalUntitledTextEditorOptions): UntitledTextEditorModel {
return this.doCreateOrGet(options);
}
private doCreateOrGet(options: IInternalUntitledTextEditorOptions = Object.create(null)): UntitledTextEditorModel {
const massagedOptions = this.massageOptions(options);
// Return existing instance if asked for it
if (massagedOptions.untitledResource && this.mapResourceToModel.has(massagedOptions.untitledResource)) {
return this.mapResourceToModel.get(massagedOptions.untitledResource)!;
}
// Create new instance otherwise
return this.doCreate(massagedOptions);
}
private massageOptions(options: IInternalUntitledTextEditorOptions): IInternalUntitledTextEditorOptions {
const massagedOptions: IInternalUntitledTextEditorOptions = Object.create(null);
// Figure out associated and untitled resource
if (options.associatedResource) {
massagedOptions.untitledResource = URI.from({
scheme: Schemas.untitled,
authority: options.associatedResource.authority,
fragment: options.associatedResource.fragment,
path: options.associatedResource.path,
query: options.associatedResource.query
});
massagedOptions.associatedResource = options.associatedResource;
} else {
if (options.untitledResource?.scheme === Schemas.untitled) {
massagedOptions.untitledResource = options.untitledResource;
}
}
// Language mode
if (options.mode) {
massagedOptions.mode = options.mode;
} else if (!massagedOptions.associatedResource) {
const configuration = this.configurationService.getValue<IFilesConfiguration>();
if (configuration.files?.defaultLanguage) {
massagedOptions.mode = configuration.files.defaultLanguage;
}
}
// Take over encoding and initial value
massagedOptions.encoding = options.encoding;
massagedOptions.initialValue = options.initialValue;
return massagedOptions;
}
private doCreate(options: IInternalUntitledTextEditorOptions): UntitledTextEditorModel {
// Create a new untitled resource if none is provided
let untitledResource = options.untitledResource;
if (!untitledResource) {
let counter = 1;
do {
untitledResource = URI.from({ scheme: Schemas.untitled, path: `Untitled-${counter}` });
counter++;
} while (this.mapResourceToModel.has(untitledResource));
}
// Create new model with provided options
const model = this._register(this.instantiationService.createInstance(UntitledTextEditorModel, untitledResource, !!options.associatedResource, options.initialValue, options.mode, options.encoding));
this.registerModel(model);
return model;
}
private registerModel(model: UntitledTextEditorModel): void {
// Install model listeners
const modelListeners = new DisposableStore();
modelListeners.add(model.onDidChangeDirty(() => this._onDidChangeDirty.fire(model)));
modelListeners.add(model.onDidChangeName(() => this._onDidChangeLabel.fire(model)));
modelListeners.add(model.onDidChangeEncoding(() => this._onDidChangeEncoding.fire(model)));
modelListeners.add(model.onWillDispose(() => this._onWillDispose.fire(model)));
// Remove from cache on dispose
Event.once(model.onWillDispose)(() => {
// Registry
this.mapResourceToModel.delete(model.resource);
// Listeners
modelListeners.dispose();
});
// Add to cache
this.mapResourceToModel.set(model.resource, model);
// If the model is dirty right from the beginning,
// make sure to emit this as an event
if (model.isDirty()) {
this._onDidChangeDirty.fire(model);
}
}
}
registerSingleton(IUntitledTextEditorService, UntitledTextEditorService, true);