Skip to content

Commit f415979

Browse files
committed
Ensure previously active file is opened when restoring
1 parent 3b7e8e6 commit f415979

2 files changed

Lines changed: 60 additions & 4 deletions

File tree

src/vs/workbench/parts/files/common/workingFilesModel.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {IUntitledEditorService} from 'vs/workbench/services/untitled/common/unti
1818
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
1919
import {IPartService} from 'vs/workbench/services/part/common/partService';
2020
import {IWorkspaceContextService} from 'vs/workbench/services/workspace/common/contextService';
21-
import {asFileEditorInput} from 'vs/workbench/common/editor';
21+
import {asFileEditorInput, getUntitledOrFileResource} from 'vs/workbench/common/editor';
2222
import {IStorageService, StorageScope} from 'vs/platform/storage/common/storage';
2323
import {IEventService} from 'vs/platform/event/common/event';
2424

@@ -279,7 +279,7 @@ export class WorkingFilesModel implements IWorkingFilesModel {
279279
let index = this.indexOf(resource);
280280
if (index >= 0) {
281281
if (resource.scheme === 'file') {
282-
this.recentlyClosedEntries.push([this.mapEntryToResource[resource.toString()]]);
282+
this.recordRecentlyClosedEntries([this.mapEntryToResource[resource.toString()]]);
283283
}
284284

285285
// Remove entry
@@ -331,7 +331,7 @@ export class WorkingFilesModel implements IWorkingFilesModel {
331331
}
332332

333333
public clear(): void {
334-
this.recentlyClosedEntries.push(this.entries);
334+
this.recordRecentlyClosedEntries(this.entries);
335335
let deleted = this.entries;
336336
this.entries = [];
337337
this.mapEntryToResource = Object.create(null);
@@ -346,6 +346,27 @@ export class WorkingFilesModel implements IWorkingFilesModel {
346346
return this.mapEntryToResource[resource.toString()];
347347
}
348348

349+
private recordRecentlyClosedEntries(resources: WorkingFileEntry[]): void {
350+
if (resources.length === 0) {
351+
return;
352+
}
353+
354+
// Make the active entry the first entry
355+
let recentlyClosedEntry: WorkingFileEntry[] = [];
356+
let input = this.editorService.getActiveEditorInput();
357+
let resource: uri = getUntitledOrFileResource(input);
358+
let activeEntry: WorkingFileEntry;
359+
if (resource) {
360+
activeEntry = this.findEntry(resource);
361+
if (activeEntry) {
362+
recentlyClosedEntry.push(activeEntry);
363+
}
364+
}
365+
this.recentlyClosedEntries.push(recentlyClosedEntry.concat(resources.filter(e => {
366+
return !activeEntry || e.resource.path !== activeEntry.resource.path;
367+
})));
368+
}
369+
349370
private indexOf(resource: uri): number {
350371
let entry = this.findEntry(resource);
351372
if (entry) {

src/vs/workbench/parts/files/test/browser/workingFilesModel.test.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,29 @@
77

88
import * as assert from 'assert';
99
import URI from 'vs/base/common/uri';
10+
import paths = require('vs/base/common/paths');
1011
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
1112
import {createInstantiationService} from 'vs/platform/instantiation/common/instantiationService';
1213
import {TestFileService, TestLifecycleService, TestPartService, TestEditorService, TestConfigurationService, TestUntitledEditorService, TestStorageService, TestTelemetryService, TestContextService, TestMessageService, TestEventService} from 'vs/workbench/test/browser/servicesTestUtils';
1314
import {WorkingFileEntry, WorkingFilesModel} from 'vs/workbench/parts/files/common/workingFilesModel';
1415
import {TextFileService} from 'vs/workbench/parts/files/browser/textFileServices';
1516
import {createMockModelService, createMockModeService} from 'vs/editor/test/common/servicesTestUtils';
17+
import {EditorInput} from 'vs/workbench/common/editor';
18+
import {FileEditorInput} from 'vs/workbench/parts/files/browser/editors/fileEditorInput';
19+
20+
function toResource(path) {
21+
return URI.file(paths.join('C:\\', path));
22+
}
1623

1724
let baseInstantiationService: IInstantiationService;
25+
let editorService: TestEditorService;
1826
let eventService: TestEventService;
1927
let textFileService: TextFileService;
2028

2129
suite('Files - WorkingFilesModel', () => {
2230

2331
setup(() => {
32+
editorService = new TestEditorService();
2433
eventService = new TestEventService();
2534

2635
baseInstantiationService = createInstantiationService({
@@ -31,7 +40,7 @@ suite('Files - WorkingFilesModel', () => {
3140
telemetryService: new TestTelemetryService(),
3241
storageService: new TestStorageService(),
3342
untitledEditorService: new TestUntitledEditorService(),
34-
editorService: new TestEditorService(),
43+
editorService: editorService,
3544
partService: new TestPartService(),
3645
modeService: createMockModeService(),
3746
modelService: createMockModelService(),
@@ -103,4 +112,30 @@ suite('Files - WorkingFilesModel', () => {
103112

104113
assert.equal(model.popLastClosedEntry(), null);
105114
});
115+
116+
test("Reopening multiple files will open the editor in the previously opened file", function() {
117+
let model = baseInstantiationService.createInstance(WorkingFilesModel);
118+
119+
// Open /foo then /bar, set /foo as active input
120+
let fooEntry = model.addEntry(URI.create('file', null, '/foo'));
121+
editorService.getActiveEditorInput = () => {
122+
return baseInstantiationService.createInstance(FileEditorInput, fooEntry.resource, 'text/javascript', void 0);
123+
};
124+
model.addEntry(URI.create('file', null, '/bar'));
125+
model.clear();
126+
127+
let lastClosedEntry: WorkingFileEntry[] = model.popLastClosedEntry();
128+
assert.equal(lastClosedEntry[0].resource.path, '/foo');
129+
130+
// Open /bar then /foo, set /foo as active input
131+
model.addEntry(URI.create('file', null, '/bar'));
132+
fooEntry = model.addEntry(URI.create('file', null, '/foo'));
133+
editorService.getActiveEditorInput = () => {
134+
return baseInstantiationService.createInstance(FileEditorInput, fooEntry.resource, 'text/javascript', void 0);
135+
};
136+
model.clear();
137+
138+
lastClosedEntry = model.popLastClosedEntry();
139+
assert.equal(lastClosedEntry[0].resource.path, '/foo');
140+
});
106141
});

0 commit comments

Comments
 (0)