-
Notifications
You must be signed in to change notification settings - Fork 29.5k
/
textFileEditorModel.ts
1210 lines (966 loc) · 43.3 KB
/
textFileEditorModel.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { localize } from '../../../../nls.js';
import { Emitter } from '../../../../base/common/event.js';
import { URI } from '../../../../base/common/uri.js';
import { mark } from '../../../../base/common/performance.js';
import { assertIsDefined } from '../../../../base/common/types.js';
import { EncodingMode, ITextFileService, TextFileEditorModelState, ITextFileEditorModel, ITextFileStreamContent, ITextFileResolveOptions, IResolvedTextFileEditorModel, TextFileResolveReason, ITextFileEditorModelSaveEvent, ITextFileSaveAsOptions } from './textfiles.js';
import { IRevertOptions, SaveReason, SaveSourceRegistry } from '../../../common/editor.js';
import { BaseTextEditorModel } from '../../../common/editor/textEditorModel.js';
import { IWorkingCopyBackupService, IResolvedWorkingCopyBackup } from '../../workingCopy/common/workingCopyBackup.js';
import { IFileService, FileOperationError, FileOperationResult, FileChangesEvent, FileChangeType, IFileStatWithMetadata, ETAG_DISABLED, NotModifiedSinceFileOperationError } from '../../../../platform/files/common/files.js';
import { ILanguageService } from '../../../../editor/common/languages/language.js';
import { IModelService } from '../../../../editor/common/services/model.js';
import { timeout, TaskSequentializer } from '../../../../base/common/async.js';
import { ITextBufferFactory, ITextModel } from '../../../../editor/common/model.js';
import { ILogService } from '../../../../platform/log/common/log.js';
import { basename } from '../../../../base/common/path.js';
import { IWorkingCopyService } from '../../workingCopy/common/workingCopyService.js';
import { IWorkingCopyBackup, WorkingCopyCapabilities, NO_TYPE_ID, IWorkingCopyBackupMeta } from '../../workingCopy/common/workingCopy.js';
import { IFilesConfigurationService } from '../../filesConfiguration/common/filesConfigurationService.js';
import { ILabelService } from '../../../../platform/label/common/label.js';
import { CancellationToken, CancellationTokenSource } from '../../../../base/common/cancellation.js';
import { UTF16be, UTF16le, UTF8, UTF8_with_bom } from './encoding.js';
import { createTextBufferFactoryFromStream } from '../../../../editor/common/model/textModel.js';
import { ILanguageDetectionService } from '../../languageDetection/common/languageDetectionWorkerService.js';
import { IPathService } from '../../path/common/pathService.js';
import { extUri } from '../../../../base/common/resources.js';
import { IAccessibilityService } from '../../../../platform/accessibility/common/accessibility.js';
import { PLAINTEXT_LANGUAGE_ID } from '../../../../editor/common/languages/modesRegistry.js';
import { IExtensionService } from '../../extensions/common/extensions.js';
import { IMarkdownString } from '../../../../base/common/htmlContent.js';
import { IProgress, IProgressService, IProgressStep, ProgressLocation } from '../../../../platform/progress/common/progress.js';
import { isCancellationError } from '../../../../base/common/errors.js';
interface IBackupMetaData extends IWorkingCopyBackupMeta {
mtime: number;
ctime: number;
size: number;
etag: string;
orphaned: boolean;
}
/**
* The text file editor model listens to changes to its underlying code editor model and saves these changes through the file service back to the disk.
*/
export class TextFileEditorModel extends BaseTextEditorModel implements ITextFileEditorModel {
private static readonly TEXTFILE_SAVE_ENCODING_SOURCE = SaveSourceRegistry.registerSource('textFileEncoding.source', localize('textFileCreate.source', "File Encoding Changed"));
//#region Events
private readonly _onDidChangeContent = this._register(new Emitter<void>());
readonly onDidChangeContent = this._onDidChangeContent.event;
private readonly _onDidResolve = this._register(new Emitter<TextFileResolveReason>());
readonly onDidResolve = this._onDidResolve.event;
private readonly _onDidChangeDirty = this._register(new Emitter<void>());
readonly onDidChangeDirty = this._onDidChangeDirty.event;
private readonly _onDidSaveError = this._register(new Emitter<void>());
readonly onDidSaveError = this._onDidSaveError.event;
private readonly _onDidSave = this._register(new Emitter<ITextFileEditorModelSaveEvent>());
readonly onDidSave = this._onDidSave.event;
private readonly _onDidRevert = this._register(new Emitter<void>());
readonly onDidRevert = this._onDidRevert.event;
private readonly _onDidChangeEncoding = this._register(new Emitter<void>());
readonly onDidChangeEncoding = this._onDidChangeEncoding.event;
private readonly _onDidChangeOrphaned = this._register(new Emitter<void>());
readonly onDidChangeOrphaned = this._onDidChangeOrphaned.event;
private readonly _onDidChangeReadonly = this._register(new Emitter<void>());
readonly onDidChangeReadonly = this._onDidChangeReadonly.event;
//#endregion
readonly typeId = NO_TYPE_ID; // IMPORTANT: never change this to not break existing assumptions (e.g. backups)
readonly capabilities = WorkingCopyCapabilities.None;
readonly name = basename(this.labelService.getUriLabel(this.resource));
private resourceHasExtension: boolean = !!extUri.extname(this.resource);
private contentEncoding: string | undefined; // encoding as reported from disk
private versionId = 0;
private bufferSavedVersionId: number | undefined;
private ignoreDirtyOnModelContentChange = false;
private ignoreSaveFromSaveParticipants = false;
private static readonly UNDO_REDO_SAVE_PARTICIPANTS_AUTO_SAVE_THROTTLE_THRESHOLD = 500;
private lastModelContentChangeFromUndoRedo: number | undefined = undefined;
lastResolvedFileStat: IFileStatWithMetadata | undefined; // !!! DO NOT MARK PRIVATE! USED IN TESTS !!!
private readonly saveSequentializer = new TaskSequentializer();
private dirty = false;
private inConflictMode = false;
private inOrphanMode = false;
private inErrorMode = false;
constructor(
readonly resource: URI,
private preferredEncoding: string | undefined, // encoding as chosen by the user
private preferredLanguageId: string | undefined, // language id as chosen by the user
@ILanguageService languageService: ILanguageService,
@IModelService modelService: IModelService,
@IFileService private readonly fileService: IFileService,
@ITextFileService private readonly textFileService: ITextFileService,
@IWorkingCopyBackupService private readonly workingCopyBackupService: IWorkingCopyBackupService,
@ILogService private readonly logService: ILogService,
@IWorkingCopyService private readonly workingCopyService: IWorkingCopyService,
@IFilesConfigurationService private readonly filesConfigurationService: IFilesConfigurationService,
@ILabelService private readonly labelService: ILabelService,
@ILanguageDetectionService languageDetectionService: ILanguageDetectionService,
@IAccessibilityService accessibilityService: IAccessibilityService,
@IPathService private readonly pathService: IPathService,
@IExtensionService private readonly extensionService: IExtensionService,
@IProgressService private readonly progressService: IProgressService
) {
super(modelService, languageService, languageDetectionService, accessibilityService);
// Make known to working copy service
this._register(this.workingCopyService.registerWorkingCopy(this));
this.registerListeners();
}
private registerListeners(): void {
this._register(this.fileService.onDidFilesChange(e => this.onDidFilesChange(e)));
this._register(this.filesConfigurationService.onDidChangeFilesAssociation(() => this.onDidChangeFilesAssociation()));
this._register(this.filesConfigurationService.onDidChangeReadonly(() => this._onDidChangeReadonly.fire()));
}
private async onDidFilesChange(e: FileChangesEvent): Promise<void> {
let fileEventImpactsModel = false;
let newInOrphanModeGuess: boolean | undefined;
// If we are currently orphaned, we check if the model file was added back
if (this.inOrphanMode) {
const modelFileAdded = e.contains(this.resource, FileChangeType.ADDED);
if (modelFileAdded) {
newInOrphanModeGuess = false;
fileEventImpactsModel = true;
}
}
// Otherwise we check if the model file was deleted
else {
const modelFileDeleted = e.contains(this.resource, FileChangeType.DELETED);
if (modelFileDeleted) {
newInOrphanModeGuess = true;
fileEventImpactsModel = true;
}
}
if (fileEventImpactsModel && this.inOrphanMode !== newInOrphanModeGuess) {
let newInOrphanModeValidated: boolean = false;
if (newInOrphanModeGuess) {
// We have received reports of users seeing delete events even though the file still
// exists (network shares issue: https://github.com/microsoft/vscode/issues/13665).
// Since we do not want to mark the model as orphaned, we have to check if the
// file is really gone and not just a faulty file event.
await timeout(100, CancellationToken.None);
if (this.isDisposed()) {
newInOrphanModeValidated = true;
} else {
const exists = await this.fileService.exists(this.resource);
newInOrphanModeValidated = !exists;
}
}
if (this.inOrphanMode !== newInOrphanModeValidated && !this.isDisposed()) {
this.setOrphaned(newInOrphanModeValidated);
}
}
}
private setOrphaned(orphaned: boolean): void {
if (this.inOrphanMode !== orphaned) {
this.inOrphanMode = orphaned;
this._onDidChangeOrphaned.fire();
}
}
private onDidChangeFilesAssociation(): void {
if (!this.isResolved()) {
return;
}
const firstLineText = this.getFirstLineText(this.textEditorModel);
const languageSelection = this.getOrCreateLanguage(this.resource, this.languageService, this.preferredLanguageId, firstLineText);
this.textEditorModel.setLanguage(languageSelection);
}
override setLanguageId(languageId: string, source?: string): void {
super.setLanguageId(languageId, source);
this.preferredLanguageId = languageId;
}
//#region Backup
async backup(token: CancellationToken): Promise<IWorkingCopyBackup> {
// Fill in metadata if we are resolved
let meta: IBackupMetaData | undefined = undefined;
if (this.lastResolvedFileStat) {
meta = {
mtime: this.lastResolvedFileStat.mtime,
ctime: this.lastResolvedFileStat.ctime,
size: this.lastResolvedFileStat.size,
etag: this.lastResolvedFileStat.etag,
orphaned: this.inOrphanMode
};
}
// Fill in content the same way we would do when
// saving the file via the text file service
// encoding support (hardcode UTF-8)
const content = await this.textFileService.getEncodedReadable(this.resource, this.createSnapshot() ?? undefined, { encoding: UTF8 });
return { meta, content };
}
//#endregion
//#region Revert
async revert(options?: IRevertOptions): Promise<void> {
if (!this.isResolved()) {
return;
}
// Unset flags
const wasDirty = this.dirty;
const undo = this.doSetDirty(false);
// Force read from disk unless reverting soft
const softUndo = options?.soft;
if (!softUndo) {
try {
await this.forceResolveFromFile();
} catch (error) {
// FileNotFound means the file got deleted meanwhile, so ignore it
if ((<FileOperationError>error).fileOperationResult !== FileOperationResult.FILE_NOT_FOUND) {
// Set flags back to previous values, we are still dirty if revert failed
undo();
throw error;
}
}
}
// Emit file change event
this._onDidRevert.fire();
// Emit dirty change event
if (wasDirty) {
this._onDidChangeDirty.fire();
}
}
//#endregion
//#region Resolve
override async resolve(options?: ITextFileResolveOptions): Promise<void> {
this.trace('resolve() - enter');
mark('code/willResolveTextFileEditorModel');
// Return early if we are disposed
if (this.isDisposed()) {
this.trace('resolve() - exit - without resolving because model is disposed');
return;
}
// Unless there are explicit contents provided, it is important that we do not
// resolve a model that is dirty or is in the process of saving to prevent data
// loss.
if (!options?.contents && (this.dirty || this.saveSequentializer.isRunning())) {
this.trace('resolve() - exit - without resolving because model is dirty or being saved');
return;
}
// Resolve either from backup or from file
await this.doResolve(options);
mark('code/didResolveTextFileEditorModel');
}
private async doResolve(options?: ITextFileResolveOptions): Promise<void> {
// First check if we have contents to use for the model
if (options?.contents) {
return this.resolveFromBuffer(options.contents, options);
}
// Second, check if we have a backup to resolve from (only for new models)
const isNewModel = !this.isResolved();
if (isNewModel) {
const resolvedFromBackup = await this.resolveFromBackup(options);
if (resolvedFromBackup) {
return;
}
}
// Finally, resolve from file resource
return this.resolveFromFile(options);
}
private async resolveFromBuffer(buffer: ITextBufferFactory, options?: ITextFileResolveOptions): Promise<void> {
this.trace('resolveFromBuffer()');
// Try to resolve metdata from disk
let mtime: number;
let ctime: number;
let size: number;
let etag: string;
try {
const metadata = await this.fileService.stat(this.resource);
mtime = metadata.mtime;
ctime = metadata.ctime;
size = metadata.size;
etag = metadata.etag;
// Clear orphaned state when resolving was successful
this.setOrphaned(false);
} catch (error) {
// Put some fallback values in error case
mtime = Date.now();
ctime = Date.now();
size = 0;
etag = ETAG_DISABLED;
// Apply orphaned state based on error code
this.setOrphaned(error.fileOperationResult === FileOperationResult.FILE_NOT_FOUND);
}
const preferredEncoding = await this.textFileService.encoding.getPreferredWriteEncoding(this.resource, this.preferredEncoding);
// Resolve with buffer
this.resolveFromContent({
resource: this.resource,
name: this.name,
mtime,
ctime,
size,
etag,
value: buffer,
encoding: preferredEncoding.encoding,
readonly: false,
locked: false
}, true /* dirty (resolved from buffer) */, options);
}
private async resolveFromBackup(options?: ITextFileResolveOptions): Promise<boolean> {
// Resolve backup if any
const backup = await this.workingCopyBackupService.resolve<IBackupMetaData>(this);
// Resolve preferred encoding if we need it
let encoding = UTF8;
if (backup) {
encoding = (await this.textFileService.encoding.getPreferredWriteEncoding(this.resource, this.preferredEncoding)).encoding;
}
// Abort if someone else managed to resolve the model by now
const isNewModel = !this.isResolved();
if (!isNewModel) {
this.trace('resolveFromBackup() - exit - without resolving because previously new model got created meanwhile');
return true; // imply that resolving has happened in another operation
}
// Try to resolve from backup if we have any
if (backup) {
await this.doResolveFromBackup(backup, encoding, options);
return true;
}
// Otherwise signal back that resolving did not happen
return false;
}
private async doResolveFromBackup(backup: IResolvedWorkingCopyBackup<IBackupMetaData>, encoding: string, options?: ITextFileResolveOptions): Promise<void> {
this.trace('doResolveFromBackup()');
// Resolve with backup
this.resolveFromContent({
resource: this.resource,
name: this.name,
mtime: backup.meta ? backup.meta.mtime : Date.now(),
ctime: backup.meta ? backup.meta.ctime : Date.now(),
size: backup.meta ? backup.meta.size : 0,
etag: backup.meta ? backup.meta.etag : ETAG_DISABLED, // etag disabled if unknown!
value: await createTextBufferFactoryFromStream(await this.textFileService.getDecodedStream(this.resource, backup.value, { encoding: UTF8 })),
encoding,
readonly: false,
locked: false
}, true /* dirty (resolved from backup) */, options);
// Restore orphaned flag based on state
if (backup.meta?.orphaned) {
this.setOrphaned(true);
}
}
private async resolveFromFile(options?: ITextFileResolveOptions): Promise<void> {
this.trace('resolveFromFile()');
const forceReadFromFile = options?.forceReadFromFile;
const allowBinary = this.isResolved() /* always allow if we resolved previously */ || options?.allowBinary;
// Decide on etag
let etag: string | undefined;
if (forceReadFromFile) {
etag = ETAG_DISABLED; // disable ETag if we enforce to read from disk
} else if (this.lastResolvedFileStat) {
etag = this.lastResolvedFileStat.etag; // otherwise respect etag to support caching
}
// Remember current version before doing any long running operation
// to ensure we are not changing a model that was changed meanwhile
const currentVersionId = this.versionId;
// Resolve Content
try {
const content = await this.textFileService.readStream(this.resource, {
acceptTextOnly: !allowBinary,
etag,
encoding: this.preferredEncoding,
limits: options?.limits
});
// Clear orphaned state when resolving was successful
this.setOrphaned(false);
// Return early if the model content has changed
// meanwhile to prevent loosing any changes
if (currentVersionId !== this.versionId) {
this.trace('resolveFromFile() - exit - without resolving because model content changed');
return;
}
return this.resolveFromContent(content, false /* not dirty (resolved from file) */, options);
} catch (error) {
const result = error.fileOperationResult;
// Apply orphaned state based on error code
this.setOrphaned(result === FileOperationResult.FILE_NOT_FOUND);
// NotModified status is expected and can be handled gracefully
// if we are resolved. We still want to update our last resolved
// stat to e.g. detect changes to the file's readonly state
if (this.isResolved() && result === FileOperationResult.FILE_NOT_MODIFIED_SINCE) {
if (error instanceof NotModifiedSinceFileOperationError) {
this.updateLastResolvedFileStat(error.stat);
}
return;
}
// Unless we are forced to read from the file, Ignore when a model has been resolved once
// and the file was deleted meanwhile. Since we already have the model resolved, we can return
// to this state and update the orphaned flag to indicate that this model has no version on
// disk anymore.
if (this.isResolved() && result === FileOperationResult.FILE_NOT_FOUND && !forceReadFromFile) {
return;
}
// Otherwise bubble up the error
throw error;
}
}
private resolveFromContent(content: ITextFileStreamContent, dirty: boolean, options?: ITextFileResolveOptions): void {
this.trace('resolveFromContent() - enter');
// Return early if we are disposed
if (this.isDisposed()) {
this.trace('resolveFromContent() - exit - because model is disposed');
return;
}
// Update our resolved disk stat model
this.updateLastResolvedFileStat({
resource: this.resource,
name: content.name,
mtime: content.mtime,
ctime: content.ctime,
size: content.size,
etag: content.etag,
readonly: content.readonly,
locked: content.locked,
isFile: true,
isDirectory: false,
isSymbolicLink: false,
children: undefined
});
// Keep the original encoding to not loose it when saving
const oldEncoding = this.contentEncoding;
this.contentEncoding = content.encoding;
// Handle events if encoding changed
if (this.preferredEncoding) {
this.updatePreferredEncoding(this.contentEncoding); // make sure to reflect the real encoding of the file (never out of sync)
} else if (oldEncoding !== this.contentEncoding) {
this._onDidChangeEncoding.fire();
}
// Update Existing Model
if (this.textEditorModel) {
this.doUpdateTextModel(content.value);
}
// Create New Model
else {
this.doCreateTextModel(content.resource, content.value);
}
// Update model dirty flag. This is very important to call
// in both cases of dirty or not because it conditionally
// updates the `bufferSavedVersionId` to determine the
// version when to consider the model as saved again (e.g.
// when undoing back to the saved state)
this.setDirty(!!dirty);
// Emit as event
this._onDidResolve.fire(options?.reason ?? TextFileResolveReason.OTHER);
}
private doCreateTextModel(resource: URI, value: ITextBufferFactory): void {
this.trace('doCreateTextModel()');
// Create model
const textModel = this.createTextEditorModel(value, resource, this.preferredLanguageId);
// Model Listeners
this.installModelListeners(textModel);
// Detect language from content
this.autoDetectLanguage();
}
private doUpdateTextModel(value: ITextBufferFactory): void {
this.trace('doUpdateTextModel()');
// Update model value in a block that ignores content change events for dirty tracking
this.ignoreDirtyOnModelContentChange = true;
try {
this.updateTextEditorModel(value, this.preferredLanguageId);
} finally {
this.ignoreDirtyOnModelContentChange = false;
}
}
protected override installModelListeners(model: ITextModel): void {
// See https://github.com/microsoft/vscode/issues/30189
// This code has been extracted to a different method because it caused a memory leak
// where `value` was captured in the content change listener closure scope.
this._register(model.onDidChangeContent(e => this.onModelContentChanged(model, e.isUndoing || e.isRedoing)));
this._register(model.onDidChangeLanguage(() => this.onMaybeShouldChangeEncoding())); // detect possible encoding change via language specific settings
super.installModelListeners(model);
}
private onModelContentChanged(model: ITextModel, isUndoingOrRedoing: boolean): void {
this.trace(`onModelContentChanged() - enter`);
// In any case increment the version id because it tracks the textual content state of the model at all times
this.versionId++;
this.trace(`onModelContentChanged() - new versionId ${this.versionId}`);
// Remember when the user changed the model through a undo/redo operation.
// We need this information to throttle save participants to fix
// https://github.com/microsoft/vscode/issues/102542
if (isUndoingOrRedoing) {
this.lastModelContentChangeFromUndoRedo = Date.now();
}
// We mark check for a dirty-state change upon model content change, unless:
// - explicitly instructed to ignore it (e.g. from model.resolve())
// - the model is readonly (in that case we never assume the change was done by the user)
if (!this.ignoreDirtyOnModelContentChange && !this.isReadonly()) {
// The contents changed as a matter of Undo and the version reached matches the saved one
// In this case we clear the dirty flag and emit a SAVED event to indicate this state.
if (model.getAlternativeVersionId() === this.bufferSavedVersionId) {
this.trace('onModelContentChanged() - model content changed back to last saved version');
// Clear flags
const wasDirty = this.dirty;
this.setDirty(false);
// Emit revert event if we were dirty
if (wasDirty) {
this._onDidRevert.fire();
}
}
// Otherwise the content has changed and we signal this as becoming dirty
else {
this.trace('onModelContentChanged() - model content changed and marked as dirty');
// Mark as dirty
this.setDirty(true);
}
}
// Emit as event
this._onDidChangeContent.fire();
// Detect language from content
this.autoDetectLanguage();
}
protected override async autoDetectLanguage(): Promise<void> {
// Wait to be ready to detect language
await this.extensionService?.whenInstalledExtensionsRegistered();
// Only perform language detection conditionally
const languageId = this.getLanguageId();
if (
this.resource.scheme === this.pathService.defaultUriScheme && // make sure to not detect language for non-user visible documents
(!languageId || languageId === PLAINTEXT_LANGUAGE_ID) && // only run on files with plaintext language set or no language set at all
!this.resourceHasExtension // only run if this particular file doesn't have an extension
) {
return super.autoDetectLanguage();
}
}
private async forceResolveFromFile(): Promise<void> {
if (this.isDisposed()) {
return; // return early when the model is invalid
}
// We go through the text file service to make
// sure this kind of `resolve` is properly
// running in sequence with any other running
// `resolve` if any, including subsequent runs
// that are triggered right after.
await this.textFileService.files.resolve(this.resource, {
reload: { async: false },
forceReadFromFile: true
});
}
//#endregion
//#region Dirty
isDirty(): this is IResolvedTextFileEditorModel {
return this.dirty;
}
isModified(): boolean {
return this.isDirty();
}
setDirty(dirty: boolean): void {
if (!this.isResolved()) {
return; // only resolved models can be marked dirty
}
// Track dirty state and version id
const wasDirty = this.dirty;
this.doSetDirty(dirty);
// Emit as Event if dirty changed
if (dirty !== wasDirty) {
this._onDidChangeDirty.fire();
}
}
private doSetDirty(dirty: boolean): () => void {
const wasDirty = this.dirty;
const wasInConflictMode = this.inConflictMode;
const wasInErrorMode = this.inErrorMode;
const oldBufferSavedVersionId = this.bufferSavedVersionId;
if (!dirty) {
this.dirty = false;
this.inConflictMode = false;
this.inErrorMode = false;
this.updateSavedVersionId();
} else {
this.dirty = true;
}
// Return function to revert this call
return () => {
this.dirty = wasDirty;
this.inConflictMode = wasInConflictMode;
this.inErrorMode = wasInErrorMode;
this.bufferSavedVersionId = oldBufferSavedVersionId;
};
}
//#endregion
//#region Save
async save(options: ITextFileSaveAsOptions = Object.create(null)): Promise<boolean> {
if (!this.isResolved()) {
return false;
}
if (this.isReadonly()) {
this.trace('save() - ignoring request for readonly resource');
return false; // if model is readonly we do not attempt to save at all
}
if (
(this.hasState(TextFileEditorModelState.CONFLICT) || this.hasState(TextFileEditorModelState.ERROR)) &&
(options.reason === SaveReason.AUTO || options.reason === SaveReason.FOCUS_CHANGE || options.reason === SaveReason.WINDOW_CHANGE)
) {
this.trace('save() - ignoring auto save request for model that is in conflict or error');
return false; // if model is in save conflict or error, do not save unless save reason is explicit
}
// Actually do save and log
this.trace('save() - enter');
await this.doSave(options);
this.trace('save() - exit');
return this.hasState(TextFileEditorModelState.SAVED);
}
private async doSave(options: ITextFileSaveAsOptions): Promise<void> {
if (typeof options.reason !== 'number') {
options.reason = SaveReason.EXPLICIT;
}
const versionId = this.versionId;
this.trace(`doSave(${versionId}) - enter with versionId ${versionId}`);
// Return early if saved from within save participant to break recursion
//
// Scenario: a save participant triggers a save() on the model
if (this.ignoreSaveFromSaveParticipants) {
this.trace(`doSave(${versionId}) - exit - refusing to save() recursively from save participant`);
return;
}
// Lookup any running save for this versionId and return it if found
//
// Scenario: user invoked the save action multiple times quickly for the same contents
// while the save was not yet finished to disk
//
if (this.saveSequentializer.isRunning(versionId)) {
this.trace(`doSave(${versionId}) - exit - found a running save for versionId ${versionId}`);
return this.saveSequentializer.running;
}
// Return early if not dirty (unless forced)
//
// Scenario: user invoked save action even though the model is not dirty
if (!options.force && !this.dirty) {
this.trace(`doSave(${versionId}) - exit - because not dirty and/or versionId is different (this.isDirty: ${this.dirty}, this.versionId: ${this.versionId})`);
return;
}
// Return if currently saving by storing this save request as the next save that should happen.
// Never ever must 2 saves execute at the same time because this can lead to dirty writes and race conditions.
//
// Scenario A: auto save was triggered and is currently busy saving to disk. this takes long enough that another auto save
// kicks in.
// Scenario B: save is very slow (e.g. network share) and the user manages to change the buffer and trigger another save
// while the first save has not returned yet.
//
if (this.saveSequentializer.isRunning()) {
this.trace(`doSave(${versionId}) - exit - because busy saving`);
// Indicate to the save sequentializer that we want to
// cancel the running operation so that ours can run
// before the running one finishes.
// Currently this will try to cancel running save
// participants but never a running save.
this.saveSequentializer.cancelRunning();
// Queue this as the upcoming save and return
return this.saveSequentializer.queue(() => this.doSave(options));
}
// Push all edit operations to the undo stack so that the user has a chance to
// Ctrl+Z back to the saved version.
if (this.isResolved()) {
this.textEditorModel.pushStackElement();
}
const saveCancellation = new CancellationTokenSource();
return this.progressService.withProgress({
title: localize('saveParticipants', "Saving '{0}'", this.name),
location: ProgressLocation.Window,
cancellable: true,
delay: this.isDirty() ? 3000 : 5000
}, progress => {
return this.doSaveSequential(versionId, options, progress, saveCancellation);
}, () => {
saveCancellation.cancel();
}).finally(() => {
saveCancellation.dispose();
});
}
private doSaveSequential(versionId: number, options: ITextFileSaveAsOptions, progress: IProgress<IProgressStep>, saveCancellation: CancellationTokenSource): Promise<void> {
return this.saveSequentializer.run(versionId, (async () => {
// A save participant can still change the model now and since we are so close to saving
// we do not want to trigger another auto save or similar, so we block this
// In addition we update our version right after in case it changed because of a model change
//
// Save participants can also be skipped through API.
if (this.isResolved() && !options.skipSaveParticipants) {
try {
// Measure the time it took from the last undo/redo operation to this save. If this
// time is below `UNDO_REDO_SAVE_PARTICIPANTS_THROTTLE_THRESHOLD`, we make sure to
// delay the save participant for the remaining time if the reason is auto save.
//
// This fixes the following issue:
// - the user has configured auto save with delay of 100ms or shorter
// - the user has a save participant enabled that modifies the file on each save
// - the user types into the file and the file gets saved
// - the user triggers undo operation
// - this will undo the save participant change but trigger the save participant right after
// - the user has no chance to undo over the save participant
//
// Reported as: https://github.com/microsoft/vscode/issues/102542
if (options.reason === SaveReason.AUTO && typeof this.lastModelContentChangeFromUndoRedo === 'number') {
const timeFromUndoRedoToSave = Date.now() - this.lastModelContentChangeFromUndoRedo;
if (timeFromUndoRedoToSave < TextFileEditorModel.UNDO_REDO_SAVE_PARTICIPANTS_AUTO_SAVE_THROTTLE_THRESHOLD) {
await timeout(TextFileEditorModel.UNDO_REDO_SAVE_PARTICIPANTS_AUTO_SAVE_THROTTLE_THRESHOLD - timeFromUndoRedoToSave);
}
}
// Run save participants unless save was cancelled meanwhile
if (!saveCancellation.token.isCancellationRequested) {
this.ignoreSaveFromSaveParticipants = true;
try {
await this.textFileService.files.runSaveParticipants(this, { reason: options.reason ?? SaveReason.EXPLICIT, savedFrom: options.from }, progress, saveCancellation.token);
} catch (err) {
if (isCancellationError(err) && !saveCancellation.token.isCancellationRequested) {
// participant wants to cancel this operation
saveCancellation.cancel();
}
} finally {
this.ignoreSaveFromSaveParticipants = false;
}
}
} catch (error) {
this.logService.error(`[text file model] runSaveParticipants(${versionId}) - resulted in an error: ${error.toString()}`, this.resource.toString());
}
}
// It is possible that a subsequent save is cancelling this
// running save. As such we return early when we detect that
// However, we do not pass the token into the file service
// because that is an atomic operation currently without
// cancellation support, so we dispose the cancellation if
// it was not cancelled yet.
if (saveCancellation.token.isCancellationRequested) {
return;
} else {
saveCancellation.dispose();
}
// We have to protect against being disposed at this point. It could be that the save() operation
// was triggerd followed by a dispose() operation right after without waiting. Typically we cannot
// be disposed if we are dirty, but if we are not dirty, save() and dispose() can still be triggered
// one after the other without waiting for the save() to complete. If we are disposed(), we risk
// saving contents to disk that are stale (see https://github.com/microsoft/vscode/issues/50942).
// To fix this issue, we will not store the contents to disk when we got disposed.
if (this.isDisposed()) {
return;
}
// We require a resolved model from this point on, since we are about to write data to disk.
if (!this.isResolved()) {
return;
}
// update versionId with its new value (if pre-save changes happened)
versionId = this.versionId;
// Clear error flag since we are trying to save again
this.inErrorMode = false;
// Save to Disk. We mark the save operation as currently running with
// the latest versionId because it might have changed from a save
// participant triggering
progress.report({ message: localize('saveTextFile', "Writing into file...") });
this.trace(`doSave(${versionId}) - before write()`);
const lastResolvedFileStat = assertIsDefined(this.lastResolvedFileStat);
const resolvedTextFileEditorModel = this;
return this.saveSequentializer.run(versionId, (async () => {
try {
const stat = await this.textFileService.write(lastResolvedFileStat.resource, resolvedTextFileEditorModel.createSnapshot(), {
mtime: lastResolvedFileStat.mtime,
encoding: this.getEncoding(),
etag: (options.ignoreModifiedSince || !this.filesConfigurationService.preventSaveConflicts(lastResolvedFileStat.resource, resolvedTextFileEditorModel.getLanguageId())) ? ETAG_DISABLED : lastResolvedFileStat.etag,
unlock: options.writeUnlock,
writeElevated: options.writeElevated
});
this.handleSaveSuccess(stat, versionId, options);
} catch (error) {
this.handleSaveError(error, versionId, options);
}
})());
})(), () => saveCancellation.cancel());
}
private handleSaveSuccess(stat: IFileStatWithMetadata, versionId: number, options: ITextFileSaveAsOptions): void {
// Updated resolved stat with updated stat
this.updateLastResolvedFileStat(stat);
// Update dirty state unless model has changed meanwhile
if (versionId === this.versionId) {
this.trace(`handleSaveSuccess(${versionId}) - setting dirty to false because versionId did not change`);
this.setDirty(false);
} else {
this.trace(`handleSaveSuccess(${versionId}) - not setting dirty to false because versionId did change meanwhile`);
}
// Update orphan state given save was successful
this.setOrphaned(false);
// Emit Save Event
this._onDidSave.fire({ reason: options.reason, stat, source: options.source });
}
private handleSaveError(error: Error, versionId: number, options: ITextFileSaveAsOptions): void {
(options.ignoreErrorHandler ? this.logService.trace : this.logService.error).apply(this.logService, [`[text file model] handleSaveError(${versionId}) - exit - resulted in a save error: ${error.toString()}`, this.resource.toString()]);
// Return early if the save() call was made asking to
// handle the save error itself.
if (options.ignoreErrorHandler) {
throw error;
}
// In any case of an error, we mark the model as dirty to prevent data loss
// It could be possible that the write corrupted the file on disk (e.g. when
// an error happened after truncating the file) and as such we want to preserve
// the model contents to prevent data loss.
this.setDirty(true);
// Flag as error state in the model
this.inErrorMode = true;
// Look out for a save conflict
if ((<FileOperationError>error).fileOperationResult === FileOperationResult.FILE_MODIFIED_SINCE) {
this.inConflictMode = true;
}
// Show to user
this.textFileService.files.saveErrorHandler.onSaveError(error, this, options);
// Emit as event
this._onDidSaveError.fire();
}
private updateSavedVersionId(): void {
// we remember the models alternate version id to remember when the version
// of the model matches with the saved version on disk. we need to keep this
// in order to find out if the model changed back to a saved version (e.g.
// when undoing long enough to reach to a version that is saved and then to