Skip to content

Commit c6315a3

Browse files
author
Benjamin Pasero
committed
debt - more services cleanup
1 parent f0983ab commit c6315a3

5 files changed

Lines changed: 134 additions & 46 deletions

File tree

src/vs/platform/lifecycle/common/lifecycle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export interface ILifecycleService {
133133
/**
134134
* A flag indicating in what phase of the lifecycle we currently are.
135135
*/
136-
readonly phase: LifecyclePhase;
136+
phase: LifecyclePhase;
137137

138138
/**
139139
* Fired before shutdown happens. Allows listeners to veto against the

src/vs/workbench/electron-browser/workbench.ts

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ import { WorkbenchContextKeysHandler } from 'vs/workbench/browser/contextkeys';
105105
import { ServicesAccessor } from 'vs/editor/browser/editorExtensions';
106106

107107
// import@node
108-
import { BackupFileService, InMemoryBackupFileService } from 'vs/workbench/services/backup/node/backupFileService';
109-
import { WorkspaceService } from 'vs/workbench/services/configuration/node/configurationService';
110108
import { getDelayedChannel } from 'vs/base/parts/ipc/node/ipc';
111109
import { connect as connectNet } from 'vs/base/parts/ipc/node/ipc.net';
112110
import { DialogChannel } from 'vs/platform/dialogs/node/dialogIpc';
@@ -190,13 +188,13 @@ export class Workbench extends Disposable implements IPartService {
190188
private editorService: EditorService;
191189
private editorGroupService: IEditorGroupsService;
192190
private contextViewService: ContextViewService;
193-
private backupFileService: IBackupFileService;
194191
private windowService: IWindowService;
195-
private lifecycleService: LifecycleService;
192+
private lifecycleService: ILifecycleService;
193+
196194
private instantiationService: IInstantiationService;
197195
private contextService: IWorkspaceContextService;
198196
private storageService: IStorageService;
199-
private configurationService: WorkspaceService;
197+
private configurationService: IConfigurationService;
200198
private environmentService: IEnvironmentService;
201199
private logService: ILogService;
202200
private windowsService: IWindowsService;
@@ -221,7 +219,7 @@ export class Workbench extends Disposable implements IPartService {
221219
@IInstantiationService instantiationService: IInstantiationService,
222220
@IWorkspaceContextService contextService: IWorkspaceContextService,
223221
@IStorageService storageService: IStorageService,
224-
@IConfigurationService configurationService: WorkspaceService,
222+
@IConfigurationService configurationService: IConfigurationService,
225223
@IEnvironmentService environmentService: IEnvironmentService,
226224
@ILogService logService: ILogService,
227225
@IWindowsService windowsService: IWindowsService
@@ -333,7 +331,7 @@ export class Workbench extends Disposable implements IPartService {
333331
this.registerLayoutListeners();
334332

335333
// Layout State
336-
this.initLayoutState();
334+
this.instantiationService.invokeFunction(accessor => this.initLayoutState(accessor));
337335

338336
// Render Workbench
339337
this.renderWorkbench();
@@ -525,14 +523,6 @@ export class Workbench extends Disposable implements IPartService {
525523
// History
526524
serviceCollection.set(IHistoryService, new SyncDescriptor(HistoryService));
527525

528-
// Backup File Service
529-
if (this.configuration.backupPath) {
530-
this.backupFileService = this.instantiationService.createInstance(BackupFileService, this.configuration.backupPath);
531-
} else {
532-
this.backupFileService = new InMemoryBackupFileService();
533-
}
534-
serviceCollection.set(IBackupFileService, this.backupFileService);
535-
536526
// Quick open service (quick open controller)
537527
this.quickOpen = this.instantiationService.createInstance(QuickOpenController);
538528
serviceCollection.set(IQuickOpenService, this.quickOpen);
@@ -561,12 +551,20 @@ export class Workbench extends Disposable implements IPartService {
561551
this.instantiationService.invokeFunction(accessor => {
562552
const fileService = accessor.get(IFileService);
563553
const instantiationService = accessor.get(IInstantiationService);
564-
const themeService = accessor.get(IWorkbenchThemeService) as WorkbenchThemeService;
554+
const configurationService = accessor.get(IConfigurationService) as any;
555+
const themeService = accessor.get(IWorkbenchThemeService) as any;
565556

566-
this.configurationService.acquireFileService(fileService);
567-
this.configurationService.acquireInstantiationService(instantiationService);
557+
if (typeof configurationService.acquireFileService === 'function') {
558+
configurationService.acquireFileService(fileService);
559+
}
560+
561+
if (typeof configurationService.acquireInstantiationService === 'function') {
562+
configurationService.acquireInstantiationService(instantiationService);
563+
}
568564

569-
themeService.acquireFileService(fileService);
565+
if (typeof themeService.acquireFileService === 'function') {
566+
themeService.acquireFileService(fileService);
567+
}
570568
});
571569
}
572570

@@ -785,7 +783,7 @@ export class Workbench extends Disposable implements IPartService {
785783
}
786784

787785
private whenStarted(accessor: ServicesAccessor, error?: Error): void {
788-
const lifecycleService = accessor.get(ILifecycleService) as LifecycleService;
786+
const lifecycleService = accessor.get(ILifecycleService);
789787

790788
this.restored = true;
791789

@@ -1020,31 +1018,36 @@ export class Workbench extends Disposable implements IPartService {
10201018
}
10211019
}
10221020

1023-
private initLayoutState(): void {
1021+
private initLayoutState(accessor: ServicesAccessor): void {
1022+
const configurationService = accessor.get(IConfigurationService);
1023+
const storageService = accessor.get(IStorageService);
1024+
const lifecycleService = accessor.get(ILifecycleService);
1025+
const contextService = accessor.get(IWorkspaceContextService);
1026+
const environmentService = accessor.get(IEnvironmentService);
10241027

10251028
// Fullscreen
10261029
this.state.fullscreen = isFullscreen();
10271030

10281031
// Menubar visibility
1029-
this.state.menuBar.visibility = this.configurationService.getValue<MenuBarVisibility>(Settings.MENUBAR_VISIBLE);
1032+
this.state.menuBar.visibility = configurationService.getValue<MenuBarVisibility>(Settings.MENUBAR_VISIBLE);
10301033

10311034
// Activity bar visibility
1032-
this.state.activityBar.hidden = !this.configurationService.getValue<string>(Settings.ACTIVITYBAR_VISIBLE);
1035+
this.state.activityBar.hidden = !configurationService.getValue<string>(Settings.ACTIVITYBAR_VISIBLE);
10331036

10341037
// Sidebar visibility
1035-
this.state.sideBar.hidden = this.storageService.getBoolean(Storage.SIDEBAR_HIDDEN, StorageScope.WORKSPACE, this.contextService.getWorkbenchState() === WorkbenchState.EMPTY);
1038+
this.state.sideBar.hidden = storageService.getBoolean(Storage.SIDEBAR_HIDDEN, StorageScope.WORKSPACE, contextService.getWorkbenchState() === WorkbenchState.EMPTY);
10361039

10371040
// Sidebar position
1038-
this.state.sideBar.position = (this.configurationService.getValue<string>(Settings.SIDEBAR_POSITION) === 'right') ? Position.RIGHT : Position.LEFT;
1041+
this.state.sideBar.position = (configurationService.getValue<string>(Settings.SIDEBAR_POSITION) === 'right') ? Position.RIGHT : Position.LEFT;
10391042

10401043
// Sidebar viewlet
10411044
if (!this.state.sideBar.hidden) {
10421045
const viewletRegistry = Registry.as<ViewletRegistry>(ViewletExtensions.Viewlets);
10431046

10441047
// Only restore last viewlet if window was reloaded or we are in development mode
10451048
let viewletToRestore: string;
1046-
if (!this.environmentService.isBuilt || this.lifecycleService.startupKind === StartupKind.ReloadedWindow) {
1047-
viewletToRestore = this.storageService.get(SidebarPart.activeViewletSettingsKey, StorageScope.WORKSPACE, viewletRegistry.getDefaultViewletId());
1049+
if (!environmentService.isBuilt || lifecycleService.startupKind === StartupKind.ReloadedWindow) {
1050+
viewletToRestore = storageService.get(SidebarPart.activeViewletSettingsKey, StorageScope.WORKSPACE, viewletRegistry.getDefaultViewletId());
10481051
} else {
10491052
viewletToRestore = viewletRegistry.getDefaultViewletId();
10501053
}
@@ -1057,13 +1060,13 @@ export class Workbench extends Disposable implements IPartService {
10571060
}
10581061

10591062
// Editor centered layout
1060-
this.state.editor.restoreCentered = this.storageService.getBoolean(Storage.CENTERED_LAYOUT_ENABLED, StorageScope.WORKSPACE, false);
1063+
this.state.editor.restoreCentered = storageService.getBoolean(Storage.CENTERED_LAYOUT_ENABLED, StorageScope.WORKSPACE, false);
10611064

10621065
// Editors to open
1063-
this.state.editor.editorsToOpen = this.resolveEditorsToOpen();
1066+
this.state.editor.editorsToOpen = this.resolveEditorsToOpen(accessor);
10641067

10651068
// Panel visibility
1066-
this.state.panel.hidden = this.storageService.getBoolean(Storage.PANEL_HIDDEN, StorageScope.WORKSPACE, true);
1069+
this.state.panel.hidden = storageService.getBoolean(Storage.PANEL_HIDDEN, StorageScope.WORKSPACE, true);
10671070

10681071
// Panel position
10691072
this.updatePanelPosition();
@@ -1072,7 +1075,7 @@ export class Workbench extends Disposable implements IPartService {
10721075
if (!this.state.panel.hidden) {
10731076
const panelRegistry = Registry.as<PanelRegistry>(PanelExtensions.Panels);
10741077

1075-
let panelToRestore = this.storageService.get(PanelPart.activePanelSettingsKey, StorageScope.WORKSPACE, panelRegistry.getDefaultPanelId());
1078+
let panelToRestore = storageService.get(PanelPart.activePanelSettingsKey, StorageScope.WORKSPACE, panelRegistry.getDefaultPanelId());
10761079
if (!panelRegistry.hasPanel(panelToRestore)) {
10771080
panelToRestore = panelRegistry.getDefaultPanelId(); // fallback to default if panel is unknown
10781081
}
@@ -1085,19 +1088,24 @@ export class Workbench extends Disposable implements IPartService {
10851088
}
10861089

10871090
// Statusbar visibility
1088-
this.state.statusBar.hidden = !this.configurationService.getValue<string>(Settings.STATUSBAR_VISIBLE);
1091+
this.state.statusBar.hidden = !configurationService.getValue<string>(Settings.STATUSBAR_VISIBLE);
10891092

10901093
// Zen mode enablement
1091-
this.state.zenMode.restore = this.storageService.getBoolean(Storage.ZEN_MODE_ENABLED, StorageScope.WORKSPACE, false) && this.configurationService.getValue(Settings.ZEN_MODE_RESTORE);
1094+
this.state.zenMode.restore = storageService.getBoolean(Storage.ZEN_MODE_ENABLED, StorageScope.WORKSPACE, false) && configurationService.getValue(Settings.ZEN_MODE_RESTORE);
10921095
}
10931096

1094-
private resolveEditorsToOpen(): Promise<IResourceEditor[]> | IResourceEditor[] {
1097+
private resolveEditorsToOpen(accessor: ServicesAccessor): Promise<IResourceEditor[]> | IResourceEditor[] {
1098+
const configuration = accessor.get(IWindowService).getConfiguration();
1099+
const configurationService = accessor.get(IConfigurationService);
1100+
const contextService = accessor.get(IWorkspaceContextService);
1101+
const editorGroupService = accessor.get(IEditorGroupsService);
1102+
const backupFileService = accessor.get(IBackupFileService);
10951103

10961104
// Files to open, diff or create
10971105
if (this.hasInitialFilesToOpen()) {
10981106

10991107
// Files to diff is exclusive
1100-
const filesToDiff = this.toInputs(this.configuration.filesToDiff, false);
1108+
const filesToDiff = this.toInputs(configuration.filesToDiff, false);
11011109
if (filesToDiff && filesToDiff.length === 2) {
11021110
return [<IResourceDiffInput>{
11031111
leftResource: filesToDiff[0].resource,
@@ -1107,21 +1115,21 @@ export class Workbench extends Disposable implements IPartService {
11071115
}];
11081116
}
11091117

1110-
const filesToCreate = this.toInputs(this.configuration.filesToCreate, true);
1111-
const filesToOpen = this.toInputs(this.configuration.filesToOpen, false);
1118+
const filesToCreate = this.toInputs(configuration.filesToCreate, true);
1119+
const filesToOpen = this.toInputs(configuration.filesToOpen, false);
11121120

11131121
// Otherwise: Open/Create files
11141122
return [...filesToOpen, ...filesToCreate];
11151123
}
11161124

11171125
// Empty workbench
1118-
else if (this.contextService.getWorkbenchState() === WorkbenchState.EMPTY && this.configurationService.inspect('workbench.startupEditor').value === 'newUntitledFile') {
1119-
const isEmpty = this.editorGroupService.count === 1 && this.editorGroupService.activeGroup.count === 0;
1126+
else if (contextService.getWorkbenchState() === WorkbenchState.EMPTY && configurationService.inspect('workbench.startupEditor').value === 'newUntitledFile') {
1127+
const isEmpty = editorGroupService.count === 1 && editorGroupService.activeGroup.count === 0;
11201128
if (!isEmpty) {
11211129
return []; // do not open any empty untitled file if we restored editors from previous session
11221130
}
11231131

1124-
return this.backupFileService.hasBackups().then(hasBackups => {
1132+
return backupFileService.hasBackups().then(hasBackups => {
11251133
if (hasBackups) {
11261134
return []; // do not open any empty untitled file if we have backups to restore
11271135
}

src/vs/workbench/services/backup/node/backupFileService.ts

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { ITextBufferFactory } from 'vs/editor/common/model';
1515
import { createTextBufferFactoryFromStream, createTextBufferFactoryFromSnapshot } from 'vs/editor/common/model/textModel';
1616
import { keys } from 'vs/base/common/map';
1717
import { Schemas } from 'vs/base/common/network';
18+
import { IWindowService } from 'vs/platform/windows/common/windows';
19+
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
1820

1921
export interface IBackupFilesModel {
2022
resolve(backupRoot: string): Promise<IBackupFilesModel>;
@@ -107,6 +109,63 @@ export class BackupFilesModel implements IBackupFilesModel {
107109

108110
export class BackupFileService implements IBackupFileService {
109111

112+
_serviceBrand: any;
113+
114+
private impl: IBackupFileService;
115+
116+
constructor(
117+
@IWindowService windowService: IWindowService,
118+
@IFileService fileService: IFileService
119+
) {
120+
const backupWorkspacePath = windowService.getConfiguration().backupPath;
121+
if (backupWorkspacePath) {
122+
this.impl = new BackupFileServiceImpl(backupWorkspacePath, fileService);
123+
} else {
124+
this.impl = new InMemoryBackupFileService();
125+
}
126+
}
127+
128+
initialize(backupWorkspacePath: string): void {
129+
if (this.impl instanceof BackupFileServiceImpl) {
130+
this.impl.initialize(backupWorkspacePath);
131+
}
132+
}
133+
134+
hasBackups(): Promise<boolean> {
135+
return this.impl.hasBackups();
136+
}
137+
138+
loadBackupResource(resource: Uri): Promise<Uri | undefined> {
139+
return this.impl.loadBackupResource(resource);
140+
}
141+
142+
backupResource(resource: Uri, content: ITextSnapshot, versionId?: number): Promise<void> {
143+
return this.impl.backupResource(resource, content, versionId);
144+
}
145+
146+
discardResourceBackup(resource: Uri): Promise<void> {
147+
return this.impl.discardResourceBackup(resource);
148+
}
149+
150+
discardAllWorkspaceBackups(): Promise<void> {
151+
return this.impl.discardAllWorkspaceBackups();
152+
}
153+
154+
getWorkspaceFileBackups(): Promise<Uri[]> {
155+
return this.impl.getWorkspaceFileBackups();
156+
}
157+
158+
resolveBackupContent(backup: Uri): Promise<ITextBufferFactory> {
159+
return this.impl.resolveBackupContent(backup);
160+
}
161+
162+
toBackupResource(resource: Uri): Uri {
163+
return this.impl.toBackupResource(resource);
164+
}
165+
}
166+
167+
class BackupFileServiceImpl implements IBackupFileService {
168+
110169
private static readonly META_MARKER = '\n';
111170

112171
_serviceBrand: any;
@@ -170,7 +229,7 @@ export class BackupFileService implements IBackupFileService {
170229
}
171230

172231
return this.ioOperationQueues.queueFor(backupResource).queue(() => {
173-
const preamble = `${resource.toString()}${BackupFileService.META_MARKER}`;
232+
const preamble = `${resource.toString()}${BackupFileServiceImpl.META_MARKER}`;
174233

175234
// Update content with value
176235
return this.fileService.updateContent(backupResource, new BackupSnapshot(content, preamble), BACKUP_FILE_UPDATE_OPTIONS).then(() => model.add(backupResource, versionId));
@@ -202,7 +261,7 @@ export class BackupFileService implements IBackupFileService {
202261

203262
model.get().forEach(fileBackup => {
204263
readPromises.push(
205-
readToMatchingString(fileBackup.fsPath, BackupFileService.META_MARKER, 2000, 10000).then(Uri.parse)
264+
readToMatchingString(fileBackup.fsPath, BackupFileServiceImpl.META_MARKER, 2000, 10000).then(Uri.parse)
206265
);
207266
});
208267

@@ -217,7 +276,7 @@ export class BackupFileService implements IBackupFileService {
217276
let metaFound = false;
218277
const metaPreambleFilter = (chunk: string) => {
219278
if (!metaFound && chunk) {
220-
const metaIndex = chunk.indexOf(BackupFileService.META_MARKER);
279+
const metaIndex = chunk.indexOf(BackupFileServiceImpl.META_MARKER);
221280
if (metaIndex === -1) {
222281
return ''; // meta not yet found, return empty string
223282
}
@@ -302,3 +361,5 @@ export function hashPath(resource: Uri): string {
302361
const str = resource.scheme === Schemas.file ? resource.fsPath : resource.toString();
303362
return crypto.createHash('md5').update(str).digest('hex');
304363
}
364+
365+
registerSingleton(IBackupFileService, BackupFileService);

src/vs/workbench/services/backup/test/electron-browser/backupFileService.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ import { URI as Uri } from 'vs/base/common/uri';
1313
import { BackupFileService, BackupFilesModel, hashPath } from 'vs/workbench/services/backup/node/backupFileService';
1414
import { FileService } from 'vs/workbench/services/files/node/fileService';
1515
import { TextModel, createTextBufferFactory } from 'vs/editor/common/model/textModel';
16-
import { TestContextService, TestTextResourceConfigurationService, TestLifecycleService, TestEnvironmentService, TestStorageService } from 'vs/workbench/test/workbenchTestServices';
16+
import { TestContextService, TestTextResourceConfigurationService, TestLifecycleService, TestEnvironmentService, TestStorageService, TestWindowService } from 'vs/workbench/test/workbenchTestServices';
1717
import { getRandomTestPath } from 'vs/base/test/node/testUtils';
1818
import { TestNotificationService } from 'vs/platform/notification/test/common/testNotificationService';
1919
import { Workspace, toWorkspaceFolders } from 'vs/platform/workspace/common/workspace';
2020
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';
2121
import { DefaultEndOfLine } from 'vs/editor/common/model';
2222
import { snapshotToString } from 'vs/platform/files/common/files';
2323
import { Schemas } from 'vs/base/common/network';
24+
import { IWindowConfiguration } from 'vs/platform/windows/common/windows';
2425

2526
const parentDir = getRandomTestPath(os.tmpdir(), 'vsctests', 'backupfileservice');
2627
const backupHome = path.join(parentDir, 'Backups');
@@ -35,11 +36,28 @@ const fooBackupPath = path.join(workspaceBackupPath, 'file', hashPath(fooFile));
3536
const barBackupPath = path.join(workspaceBackupPath, 'file', hashPath(barFile));
3637
const untitledBackupPath = path.join(workspaceBackupPath, 'untitled', hashPath(untitledFile));
3738

39+
class TestBackupWindowService extends TestWindowService {
40+
41+
private config: IWindowConfiguration;
42+
43+
constructor(workspaceBackupPath: string) {
44+
super();
45+
46+
this.config = Object.create(null);
47+
this.config.backupPath = workspaceBackupPath;
48+
}
49+
50+
getConfiguration(): IWindowConfiguration {
51+
return this.config;
52+
}
53+
}
54+
3855
class TestBackupFileService extends BackupFileService {
3956
constructor(workspace: Uri, backupHome: string, workspacesJsonPath: string) {
4057
const fileService = new FileService(new TestContextService(new Workspace(workspace.fsPath, toWorkspaceFolders([{ path: workspace.fsPath }]))), TestEnvironmentService, new TestTextResourceConfigurationService(), new TestConfigurationService(), new TestLifecycleService(), new TestStorageService(), new TestNotificationService(), { disableWatcher: true });
58+
const windowService = new TestBackupWindowService(workspaceBackupPath);
4159

42-
super(workspaceBackupPath, fileService);
60+
super(windowService, fileService);
4361
}
4462

4563
public toBackupResource(resource: Uri): Uri {

src/vs/workbench/workbench.main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import 'vs/workbench/services/configuration/node/jsonEditingService';
7373
import 'vs/workbench/services/textmodelResolver/common/textModelResolverService';
7474
import 'vs/workbench/services/textfile/common/textFileService';
7575
import 'vs/workbench/services/dialogs/electron-browser/dialogService';
76+
import 'vs/workbench/services/backup/node/backupFileService';
7677

7778
registerSingleton(IMenuService, MenuService, true);
7879
registerSingleton(IListService, ListService, true);

0 commit comments

Comments
 (0)