提交 51108dd8 编写于 作者: B Benjamin Pasero

💄

上级 092718c5
......@@ -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);
......@@ -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));
}
......@@ -84,6 +83,8 @@ export class BackupFileService implements IBackupFileService {
public _serviceBrand: any;
private static readonly META_MARKER = '\n';
protected backupHome: string;
protected workspacesJsonPath: string;
......@@ -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));
});
......@@ -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 {
......
......@@ -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');
......@@ -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]);
});
});
\ No newline at end of file
......@@ -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 {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册