|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import * as vscode from 'vscode'; |
| 5 | +import { IServiceContainer, IServiceManager } from '../ioc/types'; |
| 6 | +import { ILocator } from './base/locator'; |
| 7 | +import { ExtensionLocators, WorkspaceLocators } from './discovery/locators'; |
| 8 | +import { registerForIOC } from './legacyIOC'; |
| 9 | + |
| 10 | +export function activate(serviceManager: IServiceManager, serviceContainer: IServiceContainer) { |
| 11 | + registerForIOC(serviceManager, serviceContainer); |
| 12 | + |
| 13 | + const [locators, activateLocators] = initLocators(); |
| 14 | + activateLocators(); |
| 15 | + // We will pass the locators into the component API. |
| 16 | + // tslint:disable-next-line:no-unused-expression |
| 17 | + locators; |
| 18 | +} |
| 19 | + |
| 20 | +function initLocators(): [ExtensionLocators, () => void] { |
| 21 | + // We will add locators in similar order |
| 22 | + // to PythonInterpreterLocatorService.getLocators(). |
| 23 | + const nonWorkspaceLocators: ILocator[] = [ |
| 24 | + // Add an ILocator object here for each non-workspace locator. |
| 25 | + ]; |
| 26 | + |
| 27 | + const workspaceLocators = new WorkspaceLocators([ |
| 28 | + // Add an ILocator factory func here for each kind of workspace-rooted locator. |
| 29 | + ]); |
| 30 | + |
| 31 | + return [ |
| 32 | + new ExtensionLocators(nonWorkspaceLocators, workspaceLocators), |
| 33 | + // combined activation func: |
| 34 | + () => { |
| 35 | + // Any non-workspace locator activation goes here. |
| 36 | + workspaceLocators.activate(getWorkspaceFolders()); |
| 37 | + } |
| 38 | + ]; |
| 39 | +} |
| 40 | + |
| 41 | +function getWorkspaceFolders() { |
| 42 | + const rootAdded = new vscode.EventEmitter<vscode.Uri>(); |
| 43 | + const rootRemoved = new vscode.EventEmitter<vscode.Uri>(); |
| 44 | + vscode.workspace.onDidChangeWorkspaceFolders((event) => { |
| 45 | + for (const root of event.removed) { |
| 46 | + rootRemoved.fire(root.uri); |
| 47 | + } |
| 48 | + for (const root of event.added) { |
| 49 | + rootAdded.fire(root.uri); |
| 50 | + } |
| 51 | + }); |
| 52 | + const folders = vscode.workspace.workspaceFolders; |
| 53 | + return { |
| 54 | + roots: folders ? folders.map((f) => f.uri) : [], |
| 55 | + onAdded: rootAdded.event, |
| 56 | + onRemoved: rootRemoved.event |
| 57 | + }; |
| 58 | +} |
0 commit comments