Skip to content

Commit

Permalink
💄
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Nov 29, 2016
1 parent 092718c commit 51108dd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
20 changes: 12 additions & 8 deletions src/vs/workbench/services/backup/node/backupFileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ export interface IBackupFilesModel {

add(resource: Uri, versionId?: number): void;
has(resource: Uri, versionId?: number): boolean;
get(scheme: string): Uri[];
remove(resource: Uri): void;
clear(): void;
getFilesByScheme(scheme: string): Uri[];
}

// TODO@daniel this should resolve the backups with their file names once we have the metadata in place
export class BackupFilesModel implements IBackupFilesModel {
private cache: { [resource: string]: number /* version ID */ } = Object.create(null);

Expand Down Expand Up @@ -67,7 +66,7 @@ export class BackupFilesModel implements IBackupFilesModel {
return true;
}

public getFilesByScheme(scheme: string): Uri[] {
public get(scheme: string): Uri[] {
return Object.keys(this.cache).filter(k => path.basename(path.dirname(k)) === scheme).map(k => Uri.parse(k));
}

Expand All @@ -84,6 +83,8 @@ export class BackupFileService implements IBackupFileService {

public _serviceBrand: any;

private static readonly META_MARKER = '\n';

protected backupHome: string;
protected workspacesJsonPath: string;

Expand Down Expand Up @@ -155,7 +156,7 @@ export class BackupFileService implements IBackupFileService {
}

// Add metadata to top of file
content = `${resource.toString()}\n${content}`;
content = `${resource.toString()}${BackupFileService.META_MARKER}${content}`;

return this.fileService.updateContent(backupResource, content, BACKUP_FILE_UPDATE_OPTIONS).then(() => model.add(backupResource, versionId));
});
Expand Down Expand Up @@ -184,23 +185,26 @@ export class BackupFileService implements IBackupFileService {

public getWorkspaceFileBackups(scheme: string): TPromise<Uri[]> {
return this.ready.then(model => {
let readPromises: TPromise<Uri>[] = [];
model.getFilesByScheme(scheme).forEach(textFile => {
const readPromises: TPromise<Uri>[] = [];

model.get(scheme).forEach(fileBackup => {
readPromises.push(new TPromise<Uri>((c, e) => {
readToMatchingString(textFile.fsPath, '\n', 2000, 10000, (error, result) => {
readToMatchingString(fileBackup.fsPath, BackupFileService.META_MARKER, 2000, 10000, (error, result) => {
if (result === null) {
e(error);
}

c(Uri.parse(result));
});
}));
});

return TPromise.join(readPromises);
});
}

public parseBackupContent(rawText: IRawTextContent): string {
return rawText.value.lines.slice(1).join('\n');
return rawText.value.lines.slice(1).join('\n'); // The first line of a backup text file is the file name
}

protected getBackupResource(resource: Uri): Uri {
Expand Down
10 changes: 5 additions & 5 deletions src/vs/workbench/services/backup/test/backupFileService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,11 @@ suite('BackupFileService', () => {
});
});

test('BackupFilesModel - getFilesByScheme', () => {
test('BackupFilesModel - get', () => {
const model = new BackupFilesModel();

assert.deepEqual(model.getFilesByScheme('file'), []);
assert.deepEqual(model.getFilesByScheme('untitled'), []);
assert.deepEqual(model.get('file'), []);
assert.deepEqual(model.get('untitled'), []);

const file1 = Uri.file('/root/file/foo.html');
const file2 = Uri.file('/root/file/bar.html');
Expand All @@ -304,7 +304,7 @@ suite('BackupFileService', () => {
model.add(file2);
model.add(untitled);

assert.deepEqual(model.getFilesByScheme('file').map(f => f.fsPath), [file1.fsPath, file2.fsPath]);
assert.deepEqual(model.getFilesByScheme('untitled').map(f => f.fsPath), [untitled.fsPath]);
assert.deepEqual(model.get('file').map(f => f.fsPath), [file1.fsPath, file2.fsPath]);
assert.deepEqual(model.get('untitled').map(f => f.fsPath), [untitled.fsPath]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil
// Try get restore content, if there is an issue fallback silently to the original file's content
if (backupResource) {
resolveBackupPromise = this.textFileService.resolveTextContent(backupResource, BACKUP_FILE_RESOLVE_OPTIONS).then(backup => {
// The first line of a backup text file is the file name
return this.backupFileService.parseBackupContent(backup);
}, error => content.value);
} else {
Expand Down

0 comments on commit 51108dd

Please sign in to comment.