-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathmonaco-editor-model.ts
697 lines (604 loc) · 25.7 KB
/
monaco-editor-model.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
// *****************************************************************************
// Copyright (C) 2017 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { Position, Range, TextDocumentSaveReason } from '@theia/core/shared/vscode-languageserver-protocol';
import { TextEditorDocument, EncodingMode, FindMatchesOptions, FindMatch, EditorPreferences } from '@theia/editor/lib/browser';
import { DisposableCollection, Disposable } from '@theia/core/lib/common/disposable';
import { Emitter, Event } from '@theia/core/lib/common/event';
import { CancellationTokenSource, CancellationToken } from '@theia/core/lib/common/cancellation';
import { Resource, ResourceError, ResourceVersion } from '@theia/core/lib/common/resource';
import { Saveable, SaveOptions } from '@theia/core/lib/browser/saveable';
import { MonacoToProtocolConverter } from './monaco-to-protocol-converter';
import { ProtocolToMonacoConverter } from './protocol-to-monaco-converter';
import { ILogger, Loggable, Log } from '@theia/core/lib/common/logger';
import { IIdentifiedSingleEditOperation, ITextBufferFactory, ITextModel, ITextSnapshot } from '@theia/monaco-editor-core/esm/vs/editor/common/model';
import { IResolvedTextEditorModel } from '@theia/monaco-editor-core/esm/vs/editor/common/services/resolverService';
import * as monaco from '@theia/monaco-editor-core';
import { StandaloneServices } from '@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneServices';
import { ILanguageService } from '@theia/monaco-editor-core/esm/vs/editor/common/languages/language';
import { IModelService } from '@theia/monaco-editor-core/esm/vs/editor/common/services/model';
import { createTextBufferFactoryFromStream } from '@theia/monaco-editor-core/esm/vs/editor/common/model/textModel';
import { editorGeneratedPreferenceProperties } from '@theia/editor/lib/browser/editor-generated-preference-schema';
import { MarkdownString } from '@theia/core/lib/common/markdown-rendering';
import { BinaryBuffer } from '@theia/core/lib/common/buffer';
export {
TextDocumentSaveReason
};
export interface WillSaveMonacoModelEvent {
readonly model: MonacoEditorModel;
readonly reason: TextDocumentSaveReason;
readonly options?: SaveOptions;
waitUntil(thenable: Thenable<IIdentifiedSingleEditOperation[]>): void;
}
export interface MonacoModelContentChangedEvent {
readonly model: MonacoEditorModel;
readonly contentChanges: MonacoTextDocumentContentChange[];
}
export interface MonacoTextDocumentContentChange {
readonly range: Range;
readonly rangeOffset: number;
readonly rangeLength: number;
readonly text: string;
}
export class MonacoEditorModel implements IResolvedTextEditorModel, TextEditorDocument {
suppressOpenEditorWhenDirty = false;
lineNumbersMinChars = 3;
/* @deprecated there is no general save timeout, each participant should introduce a sensible timeout */
readonly onWillSaveLoopTimeOut = 1500;
protected bufferSavedVersionId: number;
protected model: ITextModel;
protected readonly resolveModel: Promise<void>;
protected readonly toDispose = new DisposableCollection();
protected readonly toDisposeOnAutoSave = new DisposableCollection();
protected readonly onDidChangeContentEmitter = new Emitter<MonacoModelContentChangedEvent>();
readonly onDidChangeContent = this.onDidChangeContentEmitter.event;
get onContentChanged(): Event<void> {
return (listener, thisArgs, disposables) => this.onDidChangeContent(() => listener(), thisArgs, disposables);
}
protected readonly onDidSaveModelEmitter = new Emitter<ITextModel>();
readonly onDidSaveModel = this.onDidSaveModelEmitter.event;
protected readonly onWillSaveModelEmitter = new Emitter<WillSaveMonacoModelEvent>();
readonly onWillSaveModel = this.onWillSaveModelEmitter.event;
protected readonly onDidChangeValidEmitter = new Emitter<void>();
readonly onDidChangeValid = this.onDidChangeValidEmitter.event;
protected readonly onDidChangeEncodingEmitter = new Emitter<string>();
readonly onDidChangeEncoding = this.onDidChangeEncodingEmitter.event;
readonly onDidChangeReadOnly: Event<boolean | MarkdownString> = this.resource.onDidChangeReadOnly ?? Event.None;
private preferredEncoding: string | undefined;
private contentEncoding: string | undefined;
protected resourceVersion: ResourceVersion | undefined;
constructor(
protected readonly resource: Resource,
protected readonly m2p: MonacoToProtocolConverter,
protected readonly p2m: ProtocolToMonacoConverter,
protected readonly logger?: ILogger,
protected readonly editorPreferences?: EditorPreferences
) {
this.toDispose.push(resource);
this.toDispose.push(this.toDisposeOnAutoSave);
this.toDispose.push(this.onDidChangeContentEmitter);
this.toDispose.push(this.onDidSaveModelEmitter);
this.toDispose.push(this.onWillSaveModelEmitter);
this.toDispose.push(this.onDirtyChangedEmitter);
this.toDispose.push(this.onDidChangeValidEmitter);
this.toDispose.push(Disposable.create(() => this.cancelSave()));
this.toDispose.push(Disposable.create(() => this.cancelSync()));
this.resolveModel = this.readContents().then(
content => this.initialize(content || '')
);
}
undo(): void {
this.model.undo();
}
redo(): void {
this.model.redo();
}
dispose(): void {
this.toDispose.dispose();
}
isDisposed(): boolean {
return this.toDispose.disposed;
}
resolve(): Promise<void> {
return this.resolveModel;
}
isResolved(): boolean {
return Boolean(this.model);
}
setEncoding(encoding: string, mode: EncodingMode): Promise<void> {
if (mode === EncodingMode.Decode && this.dirty) {
return Promise.resolve();
}
if (!this.setPreferredEncoding(encoding)) {
return Promise.resolve();
}
if (mode === EncodingMode.Decode) {
return this.sync();
}
return this.scheduleSave(TextDocumentSaveReason.Manual, this.cancelSave(), true);
}
getEncoding(): string | undefined {
return this.preferredEncoding || this.contentEncoding;
}
protected setPreferredEncoding(encoding: string): boolean {
if (encoding === this.preferredEncoding || (!this.preferredEncoding && encoding === this.contentEncoding)) {
return false;
}
this.preferredEncoding = encoding;
this.onDidChangeEncodingEmitter.fire(encoding);
return true;
}
protected updateContentEncoding(): void {
const contentEncoding = this.resource.encoding;
if (!contentEncoding || this.contentEncoding === contentEncoding) {
return;
}
this.contentEncoding = contentEncoding;
if (!this.preferredEncoding) {
this.onDidChangeEncodingEmitter.fire(contentEncoding);
}
}
/**
* #### Important
* Only this method can create an instance of `monaco.editor.IModel`,
* there should not be other calls to `monaco.editor.createModel`.
*/
protected initialize(value: string | ITextBufferFactory): void {
if (!this.toDispose.disposed) {
const uri = monaco.Uri.parse(this.resource.uri.toString());
let firstLine;
if (typeof value === 'string') {
firstLine = value;
const firstLF = value.indexOf('\n');
if (firstLF !== -1) {
firstLine = value.substring(0, firstLF);
}
} else {
firstLine = value.getFirstLineText(1000);
}
const languageSelection = StandaloneServices.get(ILanguageService).createByFilepathOrFirstLine(uri, firstLine);
this.model = StandaloneServices.get(IModelService).createModel(value, languageSelection, uri);
this.resourceVersion = this.resource.version;
this.setDirty(this._dirty || (!!this.resource.initiallyDirty));
this.updateSavedVersionId();
this.toDispose.push(this.model);
this.toDispose.push(this.model.onDidChangeContent(event => this.fireDidChangeContent(event)));
if (this.resource.onDidChangeContents) {
this.toDispose.push(this.resource.onDidChangeContents(() => this.sync()));
}
}
}
/**
* Use `valid` to access it.
* Use `setValid` to mutate it.
*/
protected _valid = false;
/**
* Whether it is possible to load content from the underlying resource.
*/
get valid(): boolean {
return this._valid;
}
protected setValid(valid: boolean): void {
if (valid === this._valid) {
return;
}
this._valid = valid;
this.onDidChangeValidEmitter.fire(undefined);
}
protected _dirty = false;
get dirty(): boolean {
return this._dirty;
}
protected setDirty(dirty: boolean): void {
if (dirty === this._dirty) {
return;
}
this._dirty = dirty;
if (dirty === false) {
this.updateSavedVersionId();
}
this.onDirtyChangedEmitter.fire(undefined);
}
private updateSavedVersionId(): void {
this.bufferSavedVersionId = this.model.getAlternativeVersionId();
}
protected readonly onDirtyChangedEmitter = new Emitter<void>();
get onDirtyChanged(): Event<void> {
return this.onDirtyChangedEmitter.event;
}
get uri(): string {
return this.resource.uri.toString();
}
get autosaveable(): boolean | undefined {
return this.resource.autosaveable;
}
protected _languageId: string | undefined;
get languageId(): string {
return this._languageId !== undefined ? this._languageId : this.model.getLanguageId();
}
getLanguageId(): string | undefined {
return this.languageId;
}
/**
* It's a hack to dispatch close notification with an old language id; don't use it.
*/
setLanguageId(languageId: string | undefined): void {
this._languageId = languageId;
}
get version(): number {
return this.model.getVersionId();
}
/**
* Return selected text by Range or all text by default
*/
getText(range?: Range): string {
if (!range) {
return this.model.getValue();
} else {
return this.model.getValueInRange(this.p2m.asRange(range));
}
}
positionAt(offset: number): Position {
const { lineNumber, column } = this.model.getPositionAt(offset);
return this.m2p.asPosition(lineNumber, column);
}
offsetAt(position: Position): number {
return this.model.getOffsetAt(this.p2m.asPosition(position));
}
get lineCount(): number {
return this.model.getLineCount();
}
/**
* Retrieves a line in a text document expressed as a one-based position.
*/
getLineContent(lineNumber: number): string {
return this.model.getLineContent(lineNumber);
}
getLineMaxColumn(lineNumber: number): number {
return this.model.getLineMaxColumn(lineNumber);
}
toValidPosition(position: Position): Position {
const { lineNumber, column } = this.model.validatePosition(this.p2m.asPosition(position));
return this.m2p.asPosition(lineNumber, column);
}
toValidRange(range: Range): Range {
return this.m2p.asRange(this.model.validateRange(this.p2m.asRange(range)));
}
get readOnly(): boolean | MarkdownString {
return this.resource.readOnly ?? false;
}
isReadonly(): boolean | MarkdownString {
return this.readOnly;
}
get onDispose(): monaco.IEvent<void> {
return this.toDispose.onDispose;
}
get onWillDispose(): Event<void> {
return this.toDispose.onDispose;
}
// We have a TypeScript problem here. There is a const enum `DefaultEndOfLine` used for ITextModel and a non-const redeclaration of that enum in the public API in
// Monaco.editor. The values will be the same, but TS won't accept that the two enums are equivalent, so it says these types are irreconcilable.
get textEditorModel(): monaco.editor.ITextModel & ITextModel {
// @ts-expect-error ts(2322)
return this.model;
}
/**
* Find all matches in an editor for the given options.
* @param options the options for finding matches.
*
* @returns the list of matches.
*/
findMatches(options: FindMatchesOptions): FindMatch[] {
const wordSeparators = this.editorPreferences?.['editor.wordSeparators'] ?? editorGeneratedPreferenceProperties['editor.wordSeparators'].default as string;
const results: monaco.editor.FindMatch[] = this.model.findMatches(
options.searchString,
false,
options.isRegex,
options.matchCase,
// eslint-disable-next-line no-null/no-null
options.matchWholeWord ? wordSeparators : null,
true,
options.limitResultCount
);
const extractedMatches: FindMatch[] = [];
results.forEach(r => {
if (r.matches) {
extractedMatches.push({
matches: r.matches,
range: Range.create(r.range.startLineNumber, r.range.startColumn, r.range.endLineNumber, r.range.endColumn)
});
}
});
return extractedMatches;
}
async load(): Promise<MonacoEditorModel> {
await this.resolveModel;
return this;
}
save(options?: SaveOptions): Promise<void> {
return this.scheduleSave(options?.saveReason ?? TextDocumentSaveReason.Manual, undefined, undefined, options);
}
protected pendingOperation = Promise.resolve();
protected async run(operation: () => Promise<void>): Promise<void> {
if (this.toDispose.disposed) {
return;
}
return this.pendingOperation = this.pendingOperation.then(async () => {
try {
await operation();
} catch (e) {
console.error(e);
}
});
}
protected syncCancellationTokenSource = new CancellationTokenSource();
protected cancelSync(): CancellationToken {
this.trace(log => log('MonacoEditorModel.cancelSync'));
this.syncCancellationTokenSource.cancel();
this.syncCancellationTokenSource = new CancellationTokenSource();
return this.syncCancellationTokenSource.token;
}
async sync(): Promise<void> {
const token = this.cancelSync();
return this.run(() => this.doSync(token));
}
protected async doSync(token: CancellationToken): Promise<void> {
this.trace(log => log('MonacoEditorModel.doSync - enter'));
if (token.isCancellationRequested) {
this.trace(log => log('MonacoEditorModel.doSync - exit - cancelled'));
return;
}
const value = await this.readContents();
if (value === undefined) {
this.trace(log => log('MonacoEditorModel.doSync - exit - resource not found'));
return;
}
if (token.isCancellationRequested) {
this.trace(log => log('MonacoEditorModel.doSync - exit - cancelled while looking for a resource'));
return;
}
if (this._dirty) {
this.trace(log => log('MonacoEditorModel.doSync - exit - pending dirty changes'));
return;
}
this.resourceVersion = this.resource.version;
this.updateModel(() => StandaloneServices.get(IModelService).updateModel(this.model, value), {
ignoreDirty: true,
ignoreContentChanges: true
});
this.trace(log => log('MonacoEditorModel.doSync - exit'));
}
protected async readContents(): Promise<string | ITextBufferFactory | undefined> {
try {
const options = { encoding: this.getEncoding() };
const content = await (this.resource.readStream ? this.resource.readStream(options) : this.resource.readContents(options));
let value;
if (typeof content === 'string') {
value = content;
} else {
value = createTextBufferFactoryFromStream(content);
}
this.updateContentEncoding();
this.setValid(true);
return value;
} catch (e) {
this.setValid(false);
if (ResourceError.NotFound.is(e)) {
return undefined;
}
throw e;
}
}
protected ignoreDirtyEdits = false;
protected markAsDirty(): void {
this.trace(log => log('MonacoEditorModel.markAsDirty - enter'));
if (this.ignoreDirtyEdits) {
this.trace(log => log('MonacoEditorModel.markAsDirty - exit - ignoring dirty changes enabled'));
return;
}
this.cancelSync();
this.setDirty(true);
this.trace(log => log('MonacoEditorModel.markAsDirty - exit'));
}
protected saveCancellationTokenSource = new CancellationTokenSource();
protected cancelSave(): CancellationToken {
this.trace(log => log('MonacoEditorModel.cancelSave'));
this.saveCancellationTokenSource.cancel();
this.saveCancellationTokenSource = new CancellationTokenSource();
return this.saveCancellationTokenSource.token;
}
protected scheduleSave(reason: TextDocumentSaveReason, token: CancellationToken = this.cancelSave(), overwriteEncoding?: boolean, options?: SaveOptions): Promise<void> {
return this.run(() => this.doSave(reason, token, overwriteEncoding, options));
}
protected ignoreContentChanges = false;
protected readonly contentChanges: MonacoTextDocumentContentChange[] = [];
protected pushContentChanges(contentChanges: MonacoTextDocumentContentChange[]): void {
if (!this.ignoreContentChanges) {
this.contentChanges.push(...contentChanges);
}
}
protected fireDidChangeContent(event: monaco.editor.IModelContentChangedEvent): void {
this.trace(log => log(`MonacoEditorModel.fireDidChangeContent - enter - ${JSON.stringify(event, undefined, 2)}`));
if (this.model.getAlternativeVersionId() === this.bufferSavedVersionId) {
this.setDirty(false);
} else {
this.markAsDirty();
}
const changeContentEvent = this.asContentChangedEvent(event);
this.onDidChangeContentEmitter.fire(changeContentEvent);
this.pushContentChanges(changeContentEvent.contentChanges);
this.trace(log => log('MonacoEditorModel.fireDidChangeContent - exit'));
}
protected asContentChangedEvent(event: monaco.editor.IModelContentChangedEvent): MonacoModelContentChangedEvent {
const contentChanges = event.changes.map(change => this.asTextDocumentContentChangeEvent(change));
return { model: this, contentChanges };
}
protected asTextDocumentContentChangeEvent(change: monaco.editor.IModelContentChange): MonacoTextDocumentContentChange {
const range = this.m2p.asRange(change.range);
const rangeOffset = change.rangeOffset;
const rangeLength = change.rangeLength;
const text = change.text;
return { range, rangeOffset, rangeLength, text };
}
protected applyEdits(
operations: monaco.editor.IIdentifiedSingleEditOperation[],
options?: Partial<MonacoEditorModel.ApplyEditsOptions>
): void {
return this.updateModel(() => this.model.applyEdits(operations), options);
}
protected updateModel<T>(doUpdate: () => T, options?: Partial<MonacoEditorModel.ApplyEditsOptions>): T {
const resolvedOptions: MonacoEditorModel.ApplyEditsOptions = {
ignoreDirty: false,
ignoreContentChanges: false,
...options
};
const { ignoreDirtyEdits, ignoreContentChanges } = this;
this.ignoreDirtyEdits = resolvedOptions.ignoreDirty;
this.ignoreContentChanges = resolvedOptions.ignoreContentChanges;
try {
return doUpdate();
} finally {
this.ignoreDirtyEdits = ignoreDirtyEdits;
this.ignoreContentChanges = ignoreContentChanges;
}
}
protected async doSave(reason: TextDocumentSaveReason, token: CancellationToken, overwriteEncoding?: boolean, options?: SaveOptions): Promise<void> {
if (token.isCancellationRequested || !this.resource.saveContents) {
return;
}
await this.fireWillSaveModel(reason, token, options);
if (token.isCancellationRequested) {
return;
}
const changes = [...this.contentChanges];
if ((changes.length === 0 && !this.resource.initiallyDirty) && !overwriteEncoding && reason !== TextDocumentSaveReason.Manual) {
return;
}
const contentLength = this.model.getValueLength();
const content = this.model.getValue();
try {
const encoding = this.getEncoding();
const version = this.resourceVersion;
await Resource.save(this.resource, { changes, content, contentLength, options: { encoding, overwriteEncoding, version } }, token);
this.contentChanges.splice(0, changes.length);
this.resourceVersion = this.resource.version;
this.updateContentEncoding();
this.setValid(true);
if (token.isCancellationRequested) {
return;
}
this.setDirty(false);
this.fireDidSaveModel();
} catch (e) {
if (!ResourceError.OutOfSync.is(e)) {
throw e;
}
}
}
protected async fireWillSaveModel(reason: TextDocumentSaveReason, token: CancellationToken, options?: SaveOptions): Promise<void> {
type EditContributor = Thenable<monaco.editor.IIdentifiedSingleEditOperation[]>;
const firing = this.onWillSaveModelEmitter.sequence(async listener => {
if (token.isCancellationRequested) {
return false;
}
const waitables: EditContributor[] = [];
const { version } = this;
const event = {
model: this, reason, options,
waitUntil: (thenable: EditContributor) => {
if (Object.isFrozen(waitables)) {
throw new Error('waitUntil cannot be called asynchronously.');
}
waitables.push(thenable);
}
};
// Fire.
try {
listener(event);
} catch (err) {
console.error(err);
return true;
}
// Asynchronous calls to `waitUntil` should fail.
Object.freeze(waitables);
// Wait for all promises.
const edits = await Promise.all(waitables).then(allOperations =>
([] as monaco.editor.IIdentifiedSingleEditOperation[]).concat(...allOperations)
);
if (token.isCancellationRequested) {
return false;
}
// In a perfect world, we should only apply edits if document is clean.
if (version !== this.version) {
console.error('onWillSave listeners should provide edits, not directly alter the document.');
}
// Finally apply edits provided by this listener before firing the next.
if (edits && edits.length > 0) {
this.applyEdits(edits, {
ignoreDirty: true,
});
}
return true;
});
try {
await firing;
} catch (e) {
console.error(e);
}
}
protected fireDidSaveModel(): void {
this.onDidSaveModelEmitter.fire(this.model);
}
async revert(options?: Saveable.RevertOptions): Promise<void> {
this.trace(log => log('MonacoEditorModel.revert - enter'));
this.cancelSave();
const soft = options && options.soft;
if (soft !== true) {
const dirty = this._dirty;
this._dirty = false;
try {
await this.sync();
} finally {
this._dirty = dirty;
}
}
this.setDirty(false);
this.trace(log => log('MonacoEditorModel.revert - exit'));
}
createSnapshot(preserveBOM?: boolean): ITextSnapshot {
return { read: () => this.model.getValue(undefined, preserveBOM) };
}
applySnapshot(snapshot: Saveable.Snapshot): void {
const value = Saveable.Snapshot.read(snapshot) ?? '';
this.model.setValue(value);
}
async serialize(): Promise<BinaryBuffer> {
return BinaryBuffer.fromString(this.model.getValue());
}
protected trace(loggable: Loggable): void {
if (this.logger) {
this.logger.debug((log: Log) =>
loggable((message, ...params) => log(message, ...params, this.resource.uri.toString(true)))
);
}
}
}
export namespace MonacoEditorModel {
export interface ApplyEditsOptions {
ignoreDirty: boolean
ignoreContentChanges: boolean
}
}