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

debt - avoid canHandleResource when restoring backups on startup

上级 2558a72d
......@@ -74,6 +74,7 @@ export abstract class BaseTextEditorModel extends EditorModel implements ITextEd
protected createTextEditorModel(value: ITextBufferFactory, resource?: URI, modeId?: string): TPromise<EditorModel> {
const firstLineText = this.getFirstLineText(value);
const mode = this.getOrCreateMode(this.modeService, modeId, firstLineText);
return TPromise.as(this.doCreateTextEditorModel(value, mode, resource));
}
......
......@@ -7,15 +7,12 @@
import { URI } from 'vs/base/common/uri';
import { TPromise } from 'vs/base/common/winjs.base';
import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { IBackupFileService } from 'vs/workbench/services/backup/common/backup';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IResourceInput } from 'vs/platform/editor/common/editor';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { Schemas } from 'vs/base/common/network';
import { ILifecycleService, LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
import { IFileService } from 'vs/platform/files/common/files';
import { IUntitledResourceInput } from 'vs/workbench/common/editor';
export class BackupRestorer implements IWorkbenchContribution {
......@@ -23,12 +20,9 @@ export class BackupRestorer implements IWorkbenchContribution {
private static readonly UNTITLED_REGEX = /Untitled-\d+/;
constructor(
@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
@IEditorService private editorService: IEditorService,
@IBackupFileService private backupFileService: IBackupFileService,
@ITextFileService private textFileService: ITextFileService,
@ILifecycleService private lifecycleService: ILifecycleService,
@IFileService private fileService: IFileService
@ILifecycleService private lifecycleService: ILifecycleService
) {
this.restoreBackups();
}
......@@ -60,12 +54,9 @@ export class BackupRestorer implements IWorkbenchContribution {
const unresolved: URI[] = [];
backups.forEach(backup => {
if (this.editorService.isOpen({ resource: backup })) {
if (this.fileService.canHandleResource(backup)) {
restorePromises.push(this.textFileService.models.loadOrCreate(backup).then(null, () => unresolved.push(backup)));
} else if (backup.scheme === Schemas.untitled) {
restorePromises.push(this.untitledEditorService.loadOrCreate({ resource: backup }).then(null, () => unresolved.push(backup)));
}
const openedEditor = this.editorService.getOpened({ resource: backup });
if (openedEditor) {
restorePromises.push(openedEditor.resolve().then(null, () => unresolved.push(backup)));
} else {
unresolved.push(backup);
}
......
......@@ -376,6 +376,25 @@ export class EditorService extends Disposable implements EditorServiceImpl {
//#region isOpen()
isOpen(editor: IEditorInput | IResourceInput | IUntitledResourceInput, group?: IEditorGroup | GroupIdentifier): boolean {
return !!this.doGetOpened(editor);
}
//#endregion
//#region getOpend()
getOpened(editor: IResourceInput | IUntitledResourceInput, group?: IEditorGroup | GroupIdentifier): IEditorInput {
return this.doGetOpened(editor);
}
private doGetOpened(editor: IEditorInput | IResourceInput | IUntitledResourceInput, group?: IEditorGroup | GroupIdentifier): IEditorInput {
if (!(editor instanceof EditorInput)) {
const resourceInput = editor as IResourceInput | IUntitledResourceInput;
if (!resourceInput.resource) {
return void 0; // we need a resource at least
}
}
let groups: IEditorGroup[] = [];
if (typeof group === 'number') {
groups.push(this.editorGroupService.getGroup(group));
......@@ -385,22 +404,32 @@ export class EditorService extends Disposable implements EditorServiceImpl {
groups = [...this.editorGroupService.groups];
}
return groups.some(group => {
// For each editor group
for (let i = 0; i < groups.length; i++) {
const group = groups[i];
// Typed editor
if (editor instanceof EditorInput) {
return group.isOpened(editor);
if (group.isOpened(editor)) {
return editor;
}
}
const resourceInput = editor as IResourceInput | IUntitledResourceInput;
if (!resourceInput.resource) {
return false;
}
// Resource editor
else {
for (let j = 0; j < group.editors.length; j++) {
const editorInGroup = group.editors[j];
const resource = toResource(editorInGroup, { supportSideBySide: true });
return group.editors.some(editorInGroup => {
const resource = toResource(editorInGroup, { supportSideBySide: true });
const resourceInput = editor as IResourceInput | IUntitledResourceInput;
if (resource.toString() === resourceInput.resource.toString()) {
return editorInGroup;
}
}
}
}
return resource && resource.toString() === resourceInput.resource.toString();
});
});
return void 0;
}
//#endregion
......
......@@ -151,6 +151,15 @@ export interface IEditorService {
*/
isOpen(editor: IEditorInput | IResourceInput | IUntitledResourceInput, group?: IEditorGroup | GroupIdentifier): boolean;
/**
* Get the actual opened editor input in any or a specific editor group based on the resource.
*
* Note: An editor can be opened but not actively visible.
*
* @param group optional to specify a group to check for the editor
*/
getOpened(editor: IResourceInput | IUntitledResourceInput, group?: IEditorGroup | GroupIdentifier): IEditorInput;
/**
* Allows to override the opening of editors by installing a handler that will
* be called each time an editor is about to open allowing to override the
......
......@@ -104,6 +104,7 @@ suite('Editor service', () => {
assert.ok(!service.activeTextEditorWidget);
assert.equal(service.visibleTextEditorWidgets.length, 0);
assert.equal(service.isOpen(input), true);
assert.equal(service.getOpened({ resource: input.getResource() }), input);
assert.equal(service.isOpen(input, part.activeGroup), true);
assert.equal(activeEditorChangeEventCounter, 1);
assert.equal(visibleEditorChangeEventCounter, 1);
......
......@@ -725,6 +725,10 @@ export class TestEditorService implements EditorServiceImpl {
return false;
}
getOpened(editor: IEditorInput | IResourceInput | IUntitledResourceInput): IEditorInput {
return void 0;
}
replaceEditors(editors: any, group: any) {
return TPromise.as(void 0);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册