Skip to content

Commit 22b2bd4

Browse files
author
Benjamin Pasero
committed
hot exit - implement support for metadata
1 parent fc99587 commit 22b2bd4

11 files changed

Lines changed: 407 additions & 120 deletions

File tree

src/vs/workbench/browser/dnd.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export class ResourcesDropHandler {
240240
return this.backupFileService.resolveBackupContent(droppedDirtyEditor.backupResource!).then(content => {
241241

242242
// Set the contents of to the resource to the target
243-
return this.backupFileService.backupResource(droppedDirtyEditor.resource, content.create(this.getDefaultEOL()).createSnapshot(true));
243+
return this.backupFileService.backupResource(droppedDirtyEditor.resource, content.value.create(this.getDefaultEOL()).createSnapshot(true));
244244
}).then(() => false, () => false /* ignore any error */);
245245
}
246246

src/vs/workbench/browser/nodeless.simpleservices.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { URI } from 'vs/base/common/uri';
7-
import { IBackupFileService } from 'vs/workbench/services/backup/common/backup';
8-
import { ITextBufferFactory, ITextSnapshot } from 'vs/editor/common/model';
7+
import { IBackupFileService, IResolvedBackup } from 'vs/workbench/services/backup/common/backup';
8+
import { ITextSnapshot } from 'vs/editor/common/model';
99
import { createTextBufferFactoryFromSnapshot } from 'vs/editor/common/model/textModel';
1010
import { keys, ResourceMap } from 'vs/base/common/map';
1111
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
@@ -86,17 +86,17 @@ export class SimpleBackupFileService implements IBackupFileService {
8686
return Promise.resolve(undefined);
8787
}
8888

89-
backupResource(resource: URI, content: ITextSnapshot, versionId?: number): Promise<void> {
89+
backupResource<T extends object>(resource: URI, content: ITextSnapshot, versionId?: number, meta?: T): Promise<void> {
9090
const backupResource = this.toBackupResource(resource);
9191
this.backups.set(backupResource.toString(), content);
9292

9393
return Promise.resolve();
9494
}
9595

96-
resolveBackupContent(backupResource: URI): Promise<ITextBufferFactory> {
96+
resolveBackupContent<T extends object>(backupResource: URI): Promise<IResolvedBackup<T>> {
9797
const snapshot = this.backups.get(backupResource.toString());
9898
if (snapshot) {
99-
return Promise.resolve(createTextBufferFactoryFromSnapshot(snapshot));
99+
return Promise.resolve({ value: createTextBufferFactoryFromSnapshot(snapshot) });
100100
}
101101

102102
return Promise.reject('Unexpected backup resource to resolve');

src/vs/workbench/common/editor/untitledEditorModel.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ export class UntitledEditorModel extends BaseTextEditorModel implements IEncodin
134134
this.contentChangeEventScheduler.schedule();
135135
}
136136

137+
backup(): Promise<void> {
138+
const snapshot = this.createSnapshot();
139+
if (!snapshot) {
140+
return Promise.resolve(); // should not happen
141+
}
142+
143+
return this.backupFileService.backupResource(this.resource, snapshot, this.versionId);
144+
}
145+
137146
load(): Promise<UntitledEditorModel & IResolvedTextEditorModel> {
138147

139148
// Check for backups first
@@ -143,15 +152,15 @@ export class UntitledEditorModel extends BaseTextEditorModel implements IEncodin
143152
}
144153

145154
return Promise.resolve(undefined);
146-
}).then(backupTextBufferFactory => {
147-
const hasBackup = !!backupTextBufferFactory;
155+
}).then(backup => {
156+
const hasBackup = !!backup;
148157

149158
// untitled associated to file path are dirty right away as well as untitled with content
150159
this.setDirty(this._hasAssociatedFilePath || hasBackup);
151160

152161
let untitledContents: ITextBufferFactory;
153-
if (backupTextBufferFactory) {
154-
untitledContents = backupTextBufferFactory;
162+
if (backup) {
163+
untitledContents = backup.value;
155164
} else {
156165
untitledContents = createTextBufferFactory(this.initialValue || '');
157166
}

src/vs/workbench/contrib/backup/common/backupModelTracker.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,15 @@ export class BackupModelTracker extends Disposable implements IWorkbenchContribu
6666
if (!this.configuredAutoSaveAfterDelay) {
6767
const model = this.textFileService.models.get(event.resource);
6868
if (model) {
69-
const snapshot = model.createSnapshot();
70-
if (snapshot) {
71-
this.backupFileService.backupResource(model.getResource(), snapshot, model.getVersionId());
72-
}
69+
model.backup();
7370
}
7471
}
7572
}
7673
}
7774

7875
private onUntitledModelChanged(resource: Uri): void {
7976
if (this.untitledEditorService.isDirty(resource)) {
80-
this.untitledEditorService.loadOrCreate({ resource }).then(model => {
81-
const snapshot = model.createSnapshot();
82-
if (snapshot) {
83-
this.backupFileService.backupResource(resource, snapshot, model.getVersionId());
84-
}
85-
});
77+
this.untitledEditorService.loadOrCreate({ resource }).then(model => model.backup());
8678
} else {
8779
this.discardBackup(resource);
8880
}

src/vs/workbench/services/backup/common/backup.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import { ITextBufferFactory, ITextSnapshot } from 'vs/editor/common/model';
99

1010
export const IBackupFileService = createDecorator<IBackupFileService>('backupFileService');
1111

12+
export interface IResolvedBackup<T extends object> {
13+
value: ITextBufferFactory;
14+
meta?: T;
15+
}
16+
1217
/**
1318
* A service that handles any I/O and state associated with the backup system.
1419
*/
@@ -42,8 +47,10 @@ export interface IBackupFileService {
4247
* @param resource The resource to back up.
4348
* @param content The content of the resource as snapshot.
4449
* @param versionId The version id of the resource to backup.
50+
* @param meta The (optional) meta data of the resource to backup. This information
51+
* can be restored later when loading the backup again.
4552
*/
46-
backupResource(resource: URI, content: ITextSnapshot, versionId?: number): Promise<void>;
53+
backupResource<T extends object>(resource: URI, content: ITextSnapshot, versionId?: number, meta?: T): Promise<void>;
4754

4855
/**
4956
* Gets a list of file backups for the current workspace.
@@ -55,10 +62,10 @@ export interface IBackupFileService {
5562
/**
5663
* Resolves the backup for the given resource.
5764
*
58-
* @param value The contents from a backup resource as stream.
59-
* @return The backup file's backed up content as text buffer factory.
65+
* @param resource The resource to get the backup for.
66+
* @return The backup file's backed up content and metadata if available.
6067
*/
61-
resolveBackupContent(backup: URI): Promise<ITextBufferFactory>;
68+
resolveBackupContent<T extends object>(resource: URI): Promise<IResolvedBackup<T>>;
6269

6370
/**
6471
* Discards the backup associated with a resource if it exists..

0 commit comments

Comments
 (0)