202
this.options = options;
203
}
205
>
206
>
export interface IResourceEncodings {
207
>
getPreferredReadEncoding(resource: URI): Promise<IResourceEncoding>;
208
>
getPreferredWriteEncoding(resource: URI, preferredEncoding?: string): Promise<IResourceEncoding>;
209
>
}
210
>
211
>
export interface IResourceEncoding {
212
>
readonly encoding: string;
213
>
readonly hasBOM: boolean;
214
>
}
215
>
216
>
/**
217
>
* The save error handler can be installed on the text file editor model to install code that executes when save errors occur.
218
>
*/
219
>
export interface ISaveErrorHandler {
220
>
221
>
/**
222
>
* Called whenever a save fails.
223
>
*/
224
>
onSaveError(error: Error, model: ITextFileEditorModel, options: ITextFileSaveAsOptions): void;
225
>
}
226
>
227
>
/**
228
>
* States the text file editor model can be in.
229
>
*/
230
>
export const enum TextFileEditorModelState {
231
>
232
>
/**
233
>
* A model is saved.
234
>
*/
235
>
SAVED,
236
>
237
>
/**
238
>
* A model is dirty.
239
>
*/
240
>
DIRTY,
241
>
242
>
/**
243
>
* A model is currently being saved but this operation has not completed yet.
244
>
*/
245
>
PENDING_SAVE,
246
>
247
>
/**
248
>
* A model is in conflict mode when changes cannot be saved because the
249
>
* underlying file has changed. Models in conflict mode are always dirty.
250
>
*/
251
>
CONFLICT,
252
>
253
>
/**
254
>
* A model is in orphan state when the underlying file has been deleted.
255
>
*/
256
>
ORPHAN,
257
>
258
>
/**
259
>
* Any error that happens during a save that is not causing the CONFLICT state.
260
>
* Models in error mode are always dirty.
261
>
*/
262
>
ERROR
263
>
}
264
>
265
>
export const enum TextFileResolveReason {
266
>
EDITOR = 1,
267
>
REFERENCE = 2,
268
>
OTHER = 3
269
>
}
270
>
271
>
interface IBaseTextFileContent extends IBaseFileStatWithMetadata {
272
>
273
>
/**
274
>
* The encoding of the content if known.
275
>
*/
276
>
readonly encoding: string;
277
>
}
278
>
279
>
export interface ITextFileContent extends IBaseTextFileContent {
280
>
281
>
/**
282
>
* The content of a text file.
283
>
*/
284
>
readonly value: string;
285
>
}
286
>
287
>
export interface ITextFileStreamContent extends IBaseTextFileContent {
288
>
289
>
/**
290
>
* The line grouped content of a text file.
291
>
*/
292
>
readonly value: ITextBufferFactory;
293
>
}
294
>
295
>
export interface ITextFileEditorModelResolveOrCreateOptions extends ITextFileResolveOptions {
296
>
297
>
/**
298
>
* The language id to use for the model text content.
299
>
*/
300
>
readonly languageId?: string;
301
>
302
>
/**
303
>
* The encoding to use when resolving the model text content.
304
>
*/
305
>
readonly encoding?: string;
306
>
307
>
/**
308
>
* If the model was already resolved before, allows to trigger
309
>
* a reload of it to fetch the latest contents.
310
>
*/
311
>
readonly reload?: {
312
>
313
>
/**
314
>
* Controls whether the reload happens in the background
315
>
* or whether `resolve` will await the reload to happen.
316
>
*/
317
>
readonly async: boolean;
318
>
};
319
>
}
320
>
321
>
export interface ITextFileSaveEvent extends ITextFileEditorModelSaveEvent {
322
>
323
>
/**
324
>
* The model that was saved.
325
>
*/
326
>
readonly model: ITextFileEditorModel;
327
>
}
328
>
329
>
export interface ITextFileResolveEvent {
330
>
331
>
/**
332
>
* The model that was resolved.
333
>
*/
334
>
readonly model: ITextFileEditorModel;
335
>
336
>
/**
337
>
* The reason why the model was resolved.
338
>
*/
339
>
readonly reason: TextFileResolveReason;
340
>
}
341
>
342
>
export interface ITextFileSaveParticipantContext {
343
>
344
>
/**
345
>
* The reason why the save was triggered.
346
>
*/
347
>
readonly reason: SaveReason;
348
>
349
>
/**
350
>
* Only applies to when a text file was saved as, for
351
>
* example when starting with untitled and saving. This
352
>
* provides access to the initial resource the text
353
>
* file had before.
354
>
*/
355
>
readonly savedFrom?: URI;
356
>
}
357
>
358
>
export interface ITextFileSaveParticipant {
359
>
360
>
/**
361
>
* The ordinal number which determines the order of participation.
362
>
* Lower values mean to participant sooner
363
>
*/
364
>
readonly ordinal?: number;
365
>
366
>
/**
367
>
* Participate in a save of a model. Allows to change the model
368
>
* before it is being saved to disk.
369
>
*/
370
>
participate(
371
>
model: ITextFileEditorModel,
372
>
context: ITextFileSaveParticipantContext,
373
>
progress: IProgress<IProgressStep>,
374
>
token: CancellationToken
375
>
): Promise<void>;
376
>
}
377
>
378
>
export interface ITextFileEditorModelManager {
379
>
380
>
readonly onDidCreate: Event<ITextFileEditorModel>;
381
>
readonly onDidResolve: Event<ITextFileResolveEvent>;
382
>
readonly onDidChangeDirty: Event<ITextFileEditorModel>;
383
>
readonly onDidChangeReadonly: Event<ITextFileEditorModel>;
384
>
readonly onDidRemove: Event<URI>;
385
>
readonly onDidChangeOrphaned: Event<ITextFileEditorModel>;
386
>
readonly onDidChangeEncoding: Event<ITextFileEditorModel>;
387
>
readonly onDidSaveError: Event<ITextFileEditorModel>;
388
>
readonly onDidSave: Event<ITextFileSaveEvent>;
389
>
readonly onDidRevert: Event<ITextFileEditorModel>;
390
>
391
>
/**
392
>
* Access to all text file editor models in memory.
393
>
*/
394
>
readonly models: ITextFileEditorModel[];
395
>
396
>
/**
397
>
* Allows to configure the error handler that is called on save errors.
398
>
*/
399
>
saveErrorHandler: ISaveErrorHandler;
400
>
401
>
/**
402
>
* Returns the text file editor model for the provided resource
403
>
* or undefined if none.
404
>
*/
405
>
get(resource: URI): ITextFileEditorModel | undefined;
406
>
407
>
/**
408
>
* Allows to resolve a text file model from disk.
409
>
*/
410
>
resolve(resource: URI, options?: ITextFileEditorModelResolveOrCreateOptions): Promise<ITextFileEditorModel>;
411
>
412
>
/**
413
>
* Adds a participant for saving text file models.
414
>
*/
415
>
addSaveParticipant(participant: ITextFileSaveParticipant): IDisposable;
416
>
417
>
/**
418
>
* Runs the registered save participants on the provided model.
419
>
*/
420
>
runSaveParticipants(model: ITextFileEditorModel, context: ITextFileSaveParticipantContext, progress: IProgress<IProgressStep>, token: CancellationToken): Promise<void>;
421
>
422
>
/**
423
>
* Waits for the model to be ready to be disposed. There may be conditions
424
>
* under which the model cannot be disposed, e.g. when it is dirty. Once the
425
>
* promise is settled, it is safe to dispose the model.
426
>
*/
427
>
canDispose(model: ITextFileEditorModel): true | Promise<true>;
428
>
}
429
>
430
>
export interface ITextFileSaveOptions extends ISaveOptions {
431
>
432
>
/**
433
>
* Save the file with an attempt to unlock it.
434
>
*/
435
>
readonly writeUnlock?: boolean;
436
>
437
>
/**
438
>
* Save the file with elevated privileges.
439
>
*
440
>
* Note: This may not be supported in all environments.
441
>
*/
442
>
readonly writeElevated?: boolean;
443
>
444
>
/**
445
>
* Allows to write to a file even if it has been modified on disk.
446
>
*/
447
>
readonly ignoreModifiedSince?: boolean;
448
>
449
>
/**
450
>
* If set, will bubble up the error to the caller instead of handling it.
451
>
*/
452
>
readonly ignoreErrorHandler?: boolean;
453
>
}
454
>
455
>
export interface ITextFileSaveAsOptions extends ITextFileSaveOptions {
456
>
457
>
/**
458
>
* Optional URI of the resource the text file is saved from if known.
459
>
*/
460
>
readonly from?: URI;
461
>
462
>
/**
463
>
* Optional URI to use as suggested file path to save as.
464
>
*/
465
>
readonly suggestedTarget?: URI;
466
>
}
467
>
468
>
export interface ITextFileResolveOptions {
469
>
470
>
/**
471
>
* The contents to use for the model if known. If not
472
>
* provided, the contents will be retrieved from the
473
>
* underlying resource or backup if present.
474
>
*/
475
>
readonly contents?: ITextBufferFactory;
476
>
477
>
/**
478
>
* Go to file bypassing any cache of the model if any.
479
>
*/
480
>
readonly forceReadFromFile?: boolean;
481
>
482
>
/**
483
>
* Allow to resolve a model even if we think it is a binary file.
484
>
*/
485
>
readonly allowBinary?: boolean;
486
>
487
>
/**
488
>
* Context why the model is being resolved.
489
>
*/
490
>
readonly reason?: TextFileResolveReason;
491
>
492
>
/**
493
>
* If provided, the size of the file will be checked against the limits
494
>
* and an error will be thrown if any limit is exceeded.
495
>
*/
496
>
readonly limits?: IFileReadLimits;
497
>
}
498
>
499
>
export const enum EncodingMode {
500
>
501
>
/**
502
>
* Instructs the encoding support to encode the object with the provided encoding
503
>
*/
504
>
Encode,
505
>
506
>
/**
507
>
* Instructs the encoding support to decode the object with the provided encoding
508
>
*/
509
>
Decode
510
>
}
511
>
512
>
export interface IEncodingSupport {
513
>
514
>
/**
515
>
* Gets the encoding of the object if known.
516
>
*/
517
>
getEncoding(): string | undefined;
518
>
519
>
/**
520
>
* Sets the encoding for the object for saving.
521
>
*/
522
>
setEncoding(encoding: string, mode: EncodingMode): Promise<void>;
523
>
}
524
>
525
>
export interface ILanguageSupport {
526
>
527
>
/**
528
>
* Sets the language id of the object.
529
>
*/
530
>
setLanguageId(languageId: string, source?: string): void;
531
>
}
532
>
533
>
export interface ITextFileEditorModelSaveEvent extends IWorkingCopySaveEvent {
534
>
535
>
/**
536
>
* The resolved stat from the save operation.
537
>
*/
538
>
readonly stat: IFileStatWithMetadata;
539
>
}
540
>
541
>
export interface ITextFileEditorModel extends ITextEditorModel, IEncodingSupport, ILanguageSupport, IWorkingCopy {
542
>
543
>
readonly onDidSave: Event<ITextFileEditorModelSaveEvent>;
544
>
readonly onDidSaveError: Event<void>;
545
>
readonly onDidChangeOrphaned: Event<void>;
546
>
readonly onDidChangeReadonly: Event<void>;
547
>
readonly onDidChangeEncoding: Event<void>;
548
>
549
>
hasState(state: TextFileEditorModelState): boolean;
550
>
joinState(state: TextFileEditorModelState.PENDING_SAVE): Promise<void>;
551
>
552
>
updatePreferredEncoding(encoding: string | undefined): void;
553
>
554
>
save(options?: ITextFileSaveAsOptions): Promise<boolean>;
555
>
revert(options?: IRevertOptions): Promise<void>;
556
>
557
>
resolve(options?: ITextFileResolveOptions): Promise<void>;
558
>
559
>
isDirty(): this is IResolvedTextFileEditorModel;
560
>
561
>
getLanguageId(): string | undefined;
562
>
563
>
isResolved(): this is IResolvedTextFileEditorModel;
564
>
}
565
>
566
>
export function isTextFileEditorModel(model: ITextEditorModel): model is ITextFileEditorModel {
567
const candidate = model as ITextFileEditorModel;
568
569
return areFunctions(candidate.setEncoding, candidate.getEncoding, candidate.save, candidate.revert, candidate.isDirty, candidate.getLanguageId);
570
}
572
>
export interface IResolvedTextFileEditorModel extends ITextFileEditorModel {
573
>
574
>
readonly textEditorModel: ITextModel;
575
>
576
>
createSnapshot(): ITextSnapshot;
577
>
}
578
>
579
>
export function snapshotToString(snapshot: ITextSnapshot): string {
580
const chunks: string[] = [];
581