|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +// eslint-disable-next-line max-classes-per-file |
| 5 | +import { Uri } from 'vscode'; |
| 6 | +import { iterEmpty } from '../../../common/utils/async'; |
| 7 | +import { getURIFilter } from '../../../common/utils/misc'; |
| 8 | +import { Disposables, IDisposable } from '../../../common/utils/resourceLifecycle'; |
| 9 | +import { PythonEnvInfo } from '../info'; |
| 10 | +import { ILocator, IPythonEnvsIterator, PythonLocatorQuery } from '../locator'; |
| 11 | +import { combineIterators, Locators } from '../locators'; |
| 12 | +import { LazyResourceBasedLocator } from './common/resourceBasedLocator'; |
| 13 | + |
| 14 | +/** |
| 15 | + * A wrapper around all locators used by the extension. |
| 16 | + */ |
| 17 | + |
| 18 | +export class ExtensionLocators<I = PythonEnvInfo> extends Locators<I> { |
| 19 | + constructor( |
| 20 | + // These are expected to be low-level locators (e.g. system). |
| 21 | + nonWorkspace: ILocator<I>[], |
| 22 | + // This is expected to be a locator wrapping any found in |
| 23 | + // the workspace (i.e. WorkspaceLocators). |
| 24 | + workspace: ILocator<I>, |
| 25 | + ) { |
| 26 | + super([...nonWorkspace, workspace]); |
| 27 | + } |
| 28 | +} |
| 29 | +type WorkspaceLocatorFactoryResult<I> = ILocator<I> & Partial<IDisposable>; |
| 30 | +type WorkspaceLocatorFactory<I = PythonEnvInfo> = (root: Uri) => WorkspaceLocatorFactoryResult<I>[]; |
| 31 | +type RootURI = string; |
| 32 | + |
| 33 | +export type WatchRootsArgs = { |
| 34 | + initRoot(root: Uri): void; |
| 35 | + addRoot(root: Uri): void; |
| 36 | + removeRoot(root: Uri): void; |
| 37 | +}; |
| 38 | +type WatchRootsFunc = (args: WatchRootsArgs) => IDisposable; |
| 39 | +// XXX Factor out RootedLocators and MultiRootedLocators. |
| 40 | +/** |
| 41 | + * The collection of all workspace-specific locators used by the extension. |
| 42 | + * |
| 43 | + * The factories are used to produce the locators for each workspace folder. |
| 44 | + */ |
| 45 | + |
| 46 | +export class WorkspaceLocators<I = PythonEnvInfo> extends LazyResourceBasedLocator<I> { |
| 47 | + private readonly locators: Record<RootURI, [ILocator<I>, IDisposable]> = {}; |
| 48 | + |
| 49 | + private readonly roots: Record<RootURI, Uri> = {}; |
| 50 | + |
| 51 | + constructor(private readonly watchRoots: WatchRootsFunc, private readonly factories: WorkspaceLocatorFactory<I>[]) { |
| 52 | + super(); |
| 53 | + } |
| 54 | + |
| 55 | + public async dispose(): Promise<void> { |
| 56 | + await super.dispose(); |
| 57 | + |
| 58 | + // Clear all the roots. |
| 59 | + const roots = Object.keys(this.roots).map((key) => this.roots[key]); |
| 60 | + roots.forEach((root) => this.removeRoot(root)); |
| 61 | + } |
| 62 | + |
| 63 | + protected doIterEnvs(query?: PythonLocatorQuery): IPythonEnvsIterator<I> { |
| 64 | + const iterators = Object.keys(this.locators).map((key) => { |
| 65 | + if (query?.searchLocations !== undefined) { |
| 66 | + const root = this.roots[key]; |
| 67 | + // Match any related search location. |
| 68 | + const filter = getURIFilter(root, { checkParent: true, checkChild: true, checkExact: true }); |
| 69 | + // Ignore any requests for global envs. |
| 70 | + if (!query.searchLocations.roots.some(filter)) { |
| 71 | + // This workspace folder did not match the query, so skip it! |
| 72 | + return iterEmpty<I>(); |
| 73 | + } |
| 74 | + } |
| 75 | + // The query matches or was not location-specific. |
| 76 | + const [locator] = this.locators[key]; |
| 77 | + return locator.iterEnvs(query); |
| 78 | + }); |
| 79 | + return combineIterators(iterators); |
| 80 | + } |
| 81 | + |
| 82 | + protected async initResources(): Promise<void> { |
| 83 | + const disposable = this.watchRoots({ |
| 84 | + initRoot: (root: Uri) => this.addRoot(root), |
| 85 | + addRoot: (root: Uri) => { |
| 86 | + // Drop the old one, if necessary. |
| 87 | + this.removeRoot(root); |
| 88 | + this.addRoot(root); |
| 89 | + this.emitter.fire({ searchLocation: root }); |
| 90 | + }, |
| 91 | + removeRoot: (root: Uri) => { |
| 92 | + this.removeRoot(root); |
| 93 | + this.emitter.fire({ searchLocation: root }); |
| 94 | + }, |
| 95 | + }); |
| 96 | + this.disposables.push(disposable); |
| 97 | + } |
| 98 | + |
| 99 | + private addRoot(root: Uri): void { |
| 100 | + // Create the root's locator, wrapping each factory-generated locator. |
| 101 | + const locators: ILocator<I>[] = []; |
| 102 | + const disposables = new Disposables(); |
| 103 | + this.factories.forEach((create) => { |
| 104 | + create(root).forEach((loc) => { |
| 105 | + locators.push(loc); |
| 106 | + if (loc.dispose !== undefined) { |
| 107 | + disposables.push(loc as IDisposable); |
| 108 | + } |
| 109 | + }); |
| 110 | + }); |
| 111 | + const locator = new Locators(locators); |
| 112 | + // Cache it. |
| 113 | + const key = root.toString(); |
| 114 | + this.locators[key] = [locator, disposables]; |
| 115 | + this.roots[key] = root; |
| 116 | + // Hook up the watchers. |
| 117 | + disposables.push( |
| 118 | + locator.onChanged((e) => { |
| 119 | + if (e.searchLocation === undefined) { |
| 120 | + e.searchLocation = root; |
| 121 | + } |
| 122 | + this.emitter.fire(e); |
| 123 | + }), |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + private removeRoot(root: Uri): void { |
| 128 | + const key = root.toString(); |
| 129 | + const found = this.locators[key]; |
| 130 | + if (found === undefined) { |
| 131 | + return; |
| 132 | + } |
| 133 | + const [, disposables] = found; |
| 134 | + delete this.locators[key]; |
| 135 | + delete this.roots[key]; |
| 136 | + disposables.dispose(); |
| 137 | + } |
| 138 | +} |
0 commit comments