Skip to content

Commit

Permalink
debt - avoid canHandleResource when restoring backups on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Sep 21, 2018
1 parent 2558a72 commit d0b3c1a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/vs/workbench/common/editor/textEditorModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
17 changes: 4 additions & 13 deletions src/vs/workbench/parts/backup/common/backupRestorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,22 @@

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 {

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();
}
Expand Down Expand Up @@ -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);
}
Expand Down
51 changes: 40 additions & 11 deletions src/vs/workbench/services/editor/browser/editorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/vs/workbench/services/editor/common/editorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/vs/workbench/test/workbenchTestServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit d0b3c1a

Please sign in to comment.