Skip to content

Commit

Permalink
Update proposed API for env collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartik Raj committed Aug 15, 2023
1 parent ab8d3b2 commit 73239ac
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@

import * as path from 'path';
import { inject, injectable } from 'inversify';
import { ProgressOptions, ProgressLocation, MarkdownString, WorkspaceFolder } from 'vscode';
import {
ProgressOptions,
ProgressLocation,
MarkdownString,
WorkspaceFolder,
GlobalEnvironmentVariableCollection,
EnvironmentVariableScope,
} from 'vscode';
import { pathExists } from 'fs-extra';
import { IExtensionActivationService } from '../../activation/types';
import { IApplicationShell, IApplicationEnvironment, IWorkspaceService } from '../../common/application/types';
Expand Down Expand Up @@ -61,7 +68,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
public async activate(resource: Resource): Promise<void> {
if (!inTerminalEnvVarExperiment(this.experimentService)) {
const workspaceFolder = this.getWorkspaceFolder(resource);
this.context.getEnvironmentVariableCollection({ workspaceFolder }).clear();
this.getEnvironmentVariableCollection({ workspaceFolder }).clear();
await this.handleMicroVenv(resource);
if (!this.registeredOnce) {
this.interpreterService.onDidChangeInterpreter(
Expand Down Expand Up @@ -105,7 +112,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
public async _applyCollection(resource: Resource, shell = this.applicationEnvironment.shell): Promise<void> {
const workspaceFolder = this.getWorkspaceFolder(resource);
const settings = this.configurationService.getSettings(resource);
const envVarCollection = this.context.getEnvironmentVariableCollection({ workspaceFolder });
const envVarCollection = this.getEnvironmentVariableCollection({ workspaceFolder });
// Clear any previously set env vars from collection
envVarCollection.clear();
if (!settings.terminal.activateEnvironment) {
Expand Down Expand Up @@ -173,7 +180,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
if (interpreter?.envType === EnvironmentType.Venv) {
const activatePath = path.join(path.dirname(interpreter.path), 'activate');
if (!(await pathExists(activatePath))) {
const envVarCollection = this.context.getEnvironmentVariableCollection({ workspaceFolder });
const envVarCollection = this.getEnvironmentVariableCollection({ workspaceFolder });
const pathVarName = getSearchPathEnvVarNames()[0];
envVarCollection.replace(
'PATH',
Expand All @@ -183,7 +190,12 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
return;
}
}
this.context.getEnvironmentVariableCollection({ workspaceFolder }).clear();
this.getEnvironmentVariableCollection({ workspaceFolder }).clear();
}

private getEnvironmentVariableCollection(scope?: EnvironmentVariableScope) {
const envVarCollection = this.context.environmentVariableCollection as GlobalEnvironmentVariableCollection;
return scope?.workspaceFolder ? envVarCollection.getScoped(scope) : envVarCollection;
}

private getWorkspaceFolder(resource: Resource): WorkspaceFolder | undefined {
Expand Down
50 changes: 27 additions & 23 deletions types/vscode.proposed.envCollectionWorkspace.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,34 @@
*--------------------------------------------------------------------------------------------*/

declare module 'vscode' {
// https://github.com/microsoft/vscode/issues/171173

// https://github.com/microsoft/vscode/issues/182069
// export interface ExtensionContext {
// /**
// * Gets the extension's global environment variable collection for this workspace, enabling changes to be
// * applied to terminal environment variables.
// */
// readonly environmentVariableCollection: GlobalEnvironmentVariableCollection;
// }

export interface ExtensionContext {
/**
* Gets the extension's environment variable collection for this workspace, enabling changes
* to be applied to terminal environment variables.
*
* @deprecated Use {@link getEnvironmentVariableCollection} instead.
*/
readonly environmentVariableCollection: EnvironmentVariableCollection;
/**
* Gets the extension's environment variable collection for this workspace, enabling changes
* to be applied to terminal environment variables.
*
* @param scope The scope to which the environment variable collection applies to.
*/
getEnvironmentVariableCollection(scope?: EnvironmentVariableScope): EnvironmentVariableCollection;
}
export interface GlobalEnvironmentVariableCollection extends EnvironmentVariableCollection {
/**
* Gets scope-specific environment variable collection for the extension. This enables alterations to
* terminal environment variables solely within the designated scope, and is applied in addition to (and
* after) the global collection.
*
* Each object obtained through this method is isolated and does not impact objects for other scopes,
* including the global collection.
*
* @param scope The scope to which the environment variable collection applies to.
*/
getScoped(scope: EnvironmentVariableScope): EnvironmentVariableCollection;
}

export type EnvironmentVariableScope = {
/**
* Any specific workspace folder to get collection for. If unspecified, collection applicable to all workspace folders is returned.
*/
workspaceFolder?: WorkspaceFolder;
};
export type EnvironmentVariableScope = {
/**
* Any specific workspace folder to get collection for. If unspecified, collection applicable to all workspace folders is returned.
*/
workspaceFolder?: WorkspaceFolder;
};
}

0 comments on commit 73239ac

Please sign in to comment.