Skip to content

Commit

Permalink
Merge pull request #190532 from microsoft/kartik1/something
Browse files Browse the repository at this point in the history
Update proposed scoped environment collection API
  • Loading branch information
Kartik Raj authored Aug 22, 2023
2 parents bcea55a + 6a6dc14 commit f40dabc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { deepStrictEqual, doesNotThrow, equal, ok, strictEqual, throws } from 'assert';
import { commands, ConfigurationTarget, Disposable, env, EnvironmentVariableCollection, EnvironmentVariableMutator, EnvironmentVariableMutatorOptions, EnvironmentVariableMutatorType, EnvironmentVariableScope, EventEmitter, ExtensionContext, extensions, ExtensionTerminalOptions, Pseudoterminal, Terminal, TerminalDimensions, TerminalExitReason, TerminalOptions, TerminalState, UIKind, Uri, window, workspace } from 'vscode';
import { commands, ConfigurationTarget, Disposable, env, EnvironmentVariableMutator, EnvironmentVariableMutatorOptions, EnvironmentVariableMutatorType, EventEmitter, ExtensionContext, extensions, ExtensionTerminalOptions, GlobalEnvironmentVariableCollection, Pseudoterminal, Terminal, TerminalDimensions, TerminalExitReason, TerminalOptions, TerminalState, UIKind, Uri, window, workspace } from 'vscode';
import { assertNoRpc, poll } from '../utils';

// Disable terminal tests:
Expand Down Expand Up @@ -913,10 +913,10 @@ import { assertNoRpc, poll } from '../utils';

test('get and forEach should work (scope)', () => {
// TODO: Remove cast once `envCollectionWorkspace` API is finalized.
const collection = extensionContext.environmentVariableCollection as (EnvironmentVariableCollection & { getScopedEnvironmentVariableCollection(scope: EnvironmentVariableScope): EnvironmentVariableCollection });
const collection = extensionContext.environmentVariableCollection as GlobalEnvironmentVariableCollection;
disposables.push({ dispose: () => collection.clear() });
const scope = { workspaceFolder: { uri: Uri.file('workspace1'), name: 'workspace1', index: 0 } };
const scopedCollection = collection.getScopedEnvironmentVariableCollection(scope);
const scopedCollection = collection.getScoped(scope);
scopedCollection.replace('A', 'scoped~a2~');
scopedCollection.append('B', 'scoped~b2~');
scopedCollection.prepend('C', 'scoped~c2~');
Expand All @@ -928,7 +928,7 @@ import { assertNoRpc, poll } from '../utils';
applyAtProcessCreation: true,
applyAtShellIntegration: false
};
const expectedScopedCollection = collection.getScopedEnvironmentVariableCollection(scope);
const expectedScopedCollection = collection.getScoped(scope);
deepStrictEqual(expectedScopedCollection.get('A'), { value: 'scoped~a2~', type: EnvironmentVariableMutatorType.Replace, options: defaultOptions });
deepStrictEqual(expectedScopedCollection.get('B'), { value: 'scoped~b2~', type: EnvironmentVariableMutatorType.Append, options: defaultOptions });
deepStrictEqual(expectedScopedCollection.get('C'), { value: 'scoped~c2~', type: EnvironmentVariableMutatorType.Prepend, options: defaultOptions });
Expand Down
1 change: 0 additions & 1 deletion src/vs/workbench/api/common/extHostExtensionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,6 @@ export abstract class AbstractExtHostExtensionService extends Disposable impleme
return that.extensionRuntime;
},
get environmentVariableCollection() { return that._extHostTerminalService.getEnvironmentVariableCollection(extensionDescription); },
getEnvironmentVariableCollection(scope?: vscode.EnvironmentVariableScope) { return that._extHostTerminalService.getEnvironmentVariableCollection(extensionDescription, scope); },
get messagePassingProtocol() {
if (!messagePassingProtocol) {
if (!messagePort) {
Expand Down
17 changes: 11 additions & 6 deletions src/vs/workbench/api/common/extHostTerminalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ export interface IExtHostTerminalService extends ExtHostTerminalServiceShape, ID
registerLinkProvider(provider: vscode.TerminalLinkProvider): vscode.Disposable;
registerProfileProvider(extension: IExtensionDescription, id: string, provider: vscode.TerminalProfileProvider): vscode.Disposable;
registerTerminalQuickFixProvider(id: string, extensionId: string, provider: vscode.TerminalQuickFixProvider): vscode.Disposable;
getEnvironmentVariableCollection(extension: IExtensionDescription, scope?: vscode.EnvironmentVariableScope): vscode.EnvironmentVariableCollection;
getEnvironmentVariableCollection(extension: IExtensionDescription): IEnvironmentVariableCollection;
}

interface IEnvironmentVariableCollection extends vscode.EnvironmentVariableCollection {
getScoped(scope: vscode.EnvironmentVariableScope): vscode.EnvironmentVariableCollection;
}

export interface ITerminalInternalOptions {
isFeatureTerminal?: boolean;
useShellEnvironment?: boolean;
Expand Down Expand Up @@ -850,13 +855,13 @@ export abstract class BaseExtHostTerminalService extends Disposable implements I
return index;
}

public getEnvironmentVariableCollection(extension: IExtensionDescription, scope?: vscode.EnvironmentVariableScope): vscode.EnvironmentVariableCollection {
public getEnvironmentVariableCollection(extension: IExtensionDescription): IEnvironmentVariableCollection {
let collection = this._environmentVariableCollections.get(extension.identifier.value);
if (!collection) {
collection = new UnifiedEnvironmentVariableCollection(extension);
this._setEnvironmentVariableCollection(extension.identifier.value, collection);
}
return collection.getScopedEnvironmentVariableCollection(scope);
return collection.getScopedEnvironmentVariableCollection(undefined);
}

private _syncEnvironmentVariableCollection(extensionIdentifier: string, collection: UnifiedEnvironmentVariableCollection): void {
Expand Down Expand Up @@ -923,7 +928,7 @@ class UnifiedEnvironmentVariableCollection {
this.map = new Map(serialized);
}

getScopedEnvironmentVariableCollection(scope: vscode.EnvironmentVariableScope | undefined): vscode.EnvironmentVariableCollection {
getScopedEnvironmentVariableCollection(scope: vscode.EnvironmentVariableScope | undefined): IEnvironmentVariableCollection {
if (this._extension && scope) {
// TODO: This should be removed when the env var extension API(s) are stabilized
checkProposedApiEnabled(this._extension, 'envCollectionWorkspace');
Expand Down Expand Up @@ -1069,7 +1074,7 @@ class UnifiedEnvironmentVariableCollection {
}
}

class ScopedEnvironmentVariableCollection implements vscode.EnvironmentVariableCollection {
class ScopedEnvironmentVariableCollection implements IEnvironmentVariableCollection {
public get persistent(): boolean { return this.collection.persistent; }
public set persistent(value: boolean) {
this.collection.persistent = value;
Expand All @@ -1084,7 +1089,7 @@ class ScopedEnvironmentVariableCollection implements vscode.EnvironmentVariableC
) {
}

getScopedEnvironmentVariableCollection(scope: vscode.EnvironmentVariableScope | undefined) {
getScoped(scope: vscode.EnvironmentVariableScope | undefined) {
return this.collection.getScopedEnvironmentVariableCollection(scope);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,14 @@ function describeEnvironmentChanges(collection: IMergedEnvironmentVariableCollec
content += '\n';
const globalDescription = globalDescriptions.get(ext);
if (globalDescription) {
content += `\n${globalDescription}`;
content += `\n${globalDescription}\n`;
}
const workspaceDescription = workspaceDescriptions.get(ext);
if (workspaceDescription) {
// Only show '(workspace)' suffix if there is already a description for the extension.
const workspaceSuffix = globalDescription ? ` (${localize('ScopedEnvironmentContributionInfo', 'workspace')})` : '';
content += `\n${workspaceDescription}${workspaceSuffix}`;
content += `\n${workspaceDescription}${workspaceSuffix}\n`;
}
content += '\n';

for (const mutator of coll.map.values()) {
if (filterScope(mutator, scope) === false) {
Expand Down
33 changes: 21 additions & 12 deletions src/vscode-dts/vscode.proposed.envCollectionWorkspace.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,36 @@ declare module 'vscode' {

// https://github.com/microsoft/vscode/issues/171173

export interface ExtensionContext {
// 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 GlobalEnvironmentVariableCollection extends EnvironmentVariableCollection {
/**
* Gets the extension's environment variable collection for this workspace, enabling changes
* to be applied to terminal environment variables.
* 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.
*
* @deprecated Use {@link getEnvironmentVariableCollection} instead.
*/
readonly environmentVariableCollection: EnvironmentVariableCollection;
/**
* Gets the extension's environment variable collection for this scope, enabling changes
* to be applied to terminal environment variables.
* 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.
*
* If a scope parameter is omitted, collection applicable to all relevant scopes for that parameter is
* returned. For instance, if the 'workspaceFolder' parameter is not specified, the collection that applies
* across all workspace folders will be returned.
*/
getEnvironmentVariableCollection(scope?: EnvironmentVariableScope): EnvironmentVariableCollection;
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.
*/
* Any specific workspace folder to get collection for. If unspecified, collection applicable to all workspace folders is returned.
*/
workspaceFolder?: WorkspaceFolder;
};
}

0 comments on commit f40dabc

Please sign in to comment.