提交 d1d23510 编写于 作者: B Benjamin Pasero

introduce and use IFileService#existsFile()

上级 161bc8dd
......@@ -29,6 +29,11 @@ export interface IFileService {
*/
resolveFile(resource: URI, options?: IResolveFileOptions): winjs.TPromise<IFileStat>;
/**
*Finds out if a file identified by the resource exists.
*/
existsFile(resource: URI): winjs.TPromise<boolean>;
/**
* Resolve the contents of a file identified by the resource.
*
......
......@@ -1818,36 +1818,29 @@ export class ReopenClosedFileAction extends Action {
}
public run(): TPromise<any> {
let viewletPromise = TPromise.as(null);
if (!this.partService.isSideBarHidden()) {
viewletPromise = this.viewletService.openViewlet(Files.VIEWLET_ID, false);
}
let workingFilesModel: Files.IWorkingFilesModel = this.textFileService.getWorkingFilesModel();
let entry: Files.IWorkingFileEntry = workingFilesModel.popLastClosedEntry();
return viewletPromise.then(() => {
let workingFilesModel: Files.IWorkingFilesModel = this.textFileService.getWorkingFilesModel();
let entry: Files.IWorkingFileEntry = workingFilesModel.popLastClosedEntry();
if (entry === null) {
return TPromise.as(true);
}
if (entry === null) {
return TPromise.as(true);
}
// If the current resource is the recently closed resource, run action again
let activeResource = getUntitledOrFileResource(this.editorService.getActiveEditorInput());
if (activeResource && activeResource.path === entry.resource.path) {
return this.run();
}
// If the current resource is the recently closed resource, run action again
let activeResource = getUntitledOrFileResource(this.editorService.getActiveEditorInput());
if (activeResource && activeResource.path === entry.resource.path) {
return this.run();
return this.fileService.existsFile(entry.resource).then((exists) => {
if (!exists) {
return this.run(); // repeat in case the last closed file got deleted meanwhile
}
return this.fileService.resolveFile(entry.resource).then(() => {
workingFilesModel.addEntry(entry.resource);
return this.editorService.openEditor(entry);
}, (e: any) => {
// If the files no longer exists, run action again
if (e.code === 'ENOENT') {
return this.run();
}
// Make it a working file again
workingFilesModel.addEntry(entry.resource);
return TPromise.wrapError(e);
});
// Open in editor
return this.editorService.openEditor(entry);
});
}
}
......
......@@ -309,6 +309,7 @@ export class WorkingFilesModel implements IWorkingFilesModel {
if (this.recentlyClosedEntries.length > 0) {
return this.recentlyClosedEntries.pop();
}
return null;
}
......@@ -332,6 +333,7 @@ export class WorkingFilesModel implements IWorkingFilesModel {
public clear(): void {
this.recordRecentlyClosedEntries(this.entries);
let deleted = this.entries;
this.entries = [];
this.mapEntryToResource = Object.create(null);
......
......@@ -106,6 +106,10 @@ export class FileService implements IFileService {
return this.raw.resolveFile(resource, options);
}
public existsFile(resource: uri): TPromise<boolean> {
return this.raw.existsFile(resource);
}
public resolveContent(resource: uri, options?: IResolveContentOptions): TPromise<IContent> {
let contentId = resource.toString();
let timerEvent = timer.start(timer.Topic.WORKBENCH, strings.format('Load {0}', contentId));
......
......@@ -135,6 +135,10 @@ export class FileService implements files.IFileService {
return this.resolve(resource, options);
}
public existsFile(resource: uri): TPromise<boolean> {
return this.resolveFile(resource).then(() => true, () => false);
}
public resolveContent(resource: uri, options?: files.IResolveContentOptions): TPromise<files.IContent> {
let absolutePath = this.toAbsolutePath(resource);
......
......@@ -265,6 +265,18 @@ suite('FileService', () => {
});
});
test('existsFile', function (done: () => void) {
service.existsFile(uri.file(testDir)).then((exists) => {
assert.equal(exists, true);
service.existsFile(uri.file(testDir + 'something')).then((exists) => {
assert.equal(exists, false);
done();
});
});
});
test('updateContent', function (done: () => void) {
let resource = uri.file(path.join(testDir, 'small.txt'));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册