Skip to content

Commit

Permalink
[vscode] Support EnvironmentVariableCollection description #12696
Browse files Browse the repository at this point in the history
* add description to SerializableExtensionEnvironmentVariableCollection
and use as DTO in $setEnvironmentVariableCollection
* add fromMarkdownOrString method to converter
* review comments

Contributed on behalf of STMicroelectronics

Signed-off-by: Johannes Faltermeier <[email protected]>
  • Loading branch information
jfaltermeier committed Aug 23, 2023
1 parent c27c87d commit b80c556
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 35 deletions.
5 changes: 2 additions & 3 deletions packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ import type {
TimelineChangeEvent,
TimelineProviderDescriptor
} from '@theia/timeline/lib/common/timeline-model';
import { SerializableEnvironmentVariableCollection } from '@theia/terminal/lib/common/base-terminal-protocol';
import { SerializableEnvironmentVariableCollection, SerializableExtensionEnvironmentVariableCollection } from '@theia/terminal/lib/common/base-terminal-protocol';
import { ThemeType } from '@theia/core/lib/common/theme';
import { Disposable } from '@theia/core/lib/common/disposable';
import { isString, isObject, PickOptions, QuickInputButtonHandle } from '@theia/core/lib/common';
Expand Down Expand Up @@ -404,8 +404,7 @@ export interface TerminalServiceMain {
*/
$disposeByTerminalId(id: number, waitOnExit?: boolean | string): void;

$setEnvironmentVariableCollection(extensionIdentifier: string, persistent: boolean, collection: SerializableEnvironmentVariableCollection | undefined,
description: string | MarkdownString | undefined): void;
$setEnvironmentVariableCollection(persistent: boolean, collection: SerializableExtensionEnvironmentVariableCollection): void;

/**
* Set the terminal widget name.
Expand Down
13 changes: 6 additions & 7 deletions packages/plugin-ext/src/main/browser/terminal-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

import { interfaces } from '@theia/core/shared/inversify';
import { ApplicationShell, WidgetOpenerOptions } from '@theia/core/lib/browser';
import { MarkdownString, TerminalEditorLocationOptions, TerminalOptions } from '@theia/plugin';
import { TerminalEditorLocationOptions, TerminalOptions } from '@theia/plugin';
import { TerminalLocation, TerminalWidget } from '@theia/terminal/lib/browser/base/terminal-widget';
import { TerminalService } from '@theia/terminal/lib/browser/base/terminal-service';
import { TerminalServiceMain, TerminalServiceExt, MAIN_RPC_CONTEXT } from '../../common/plugin-api-rpc';
import { RPCProtocol } from '../../common/rpc-protocol';
import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable';
import { SerializableEnvironmentVariableCollection } from '@theia/terminal/lib/common/base-terminal-protocol';
import { SerializableEnvironmentVariableCollection, SerializableExtensionEnvironmentVariableCollection } from '@theia/terminal/lib/common/base-terminal-protocol';
import { ShellTerminalServerProxy } from '@theia/terminal/lib/common/shell-terminal-protocol';
import { TerminalLink, TerminalLinkProvider } from '@theia/terminal/lib/browser/terminal-link-provider';
import { URI } from '@theia/core/lib/common/uri';
Expand Down Expand Up @@ -75,12 +75,11 @@ export class TerminalServiceMainImpl implements TerminalServiceMain, TerminalLin
return this.extProxy.$startProfile(id, CancellationToken.None);
}

$setEnvironmentVariableCollection(extensionIdentifier: string, persistent: boolean, collection: SerializableEnvironmentVariableCollection | undefined,
description: string | MarkdownString | undefined): void {
if (collection) {
this.shellTerminalServer.setCollection(extensionIdentifier, persistent, collection, description);
$setEnvironmentVariableCollection(persistent: boolean, collection: SerializableExtensionEnvironmentVariableCollection): void {
if (collection.collection) {
this.shellTerminalServer.setCollection(collection.extensionIdentifier, persistent, collection.collection, collection.description);
} else {
this.shellTerminalServer.deleteCollection(extensionIdentifier);
this.shellTerminalServer.deleteCollection(collection.extensionIdentifier);
}
}

Expand Down
18 changes: 5 additions & 13 deletions packages/plugin-ext/src/plugin/terminal-ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { Disposable, EnvironmentVariableMutatorType, TerminalExitReason, ThemeIc
import { SerializableEnvironmentVariableCollection } from '@theia/terminal/lib/common/base-terminal-protocol';
import { ProvidedTerminalLink } from '../common/plugin-api-rpc-model';
import { ThemeIcon as MonacoThemeIcon } from '@theia/monaco-editor-core/esm/vs/platform/theme/common/themeService';
import { MarkdownString as MarkdownStringDTO } from '@theia/core/lib/common/markdown-rendering';

export function getIconUris(iconPath: theia.TerminalOptions['iconPath']): { id: string } | undefined {
if (ThemeIcon.is(iconPath)) {
Expand Down Expand Up @@ -315,18 +314,11 @@ export class TerminalServiceExtImpl implements TerminalServiceExt {

private syncEnvironmentVariableCollection(extensionIdentifier: string, collection: EnvironmentVariableCollection): void {
const serialized = [...collection.map.entries()];
this.proxy.$setEnvironmentVariableCollection(extensionIdentifier, collection.persistent, serialized.length === 0 ? undefined : serialized,
this.descriptionToDTO(collection.description));
}

private descriptionToDTO(value: string | theia.MarkdownString | undefined): string | MarkdownStringDTO | undefined {
if (value === undefined) {
return undefined;
} else if (typeof value === 'string') {
return value;
} else {
return Converter.fromMarkdown(value);
}
this.proxy.$setEnvironmentVariableCollection(collection.persistent, {
extensionIdentifier,
collection: serialized.length === 0 ? undefined : serialized,
description: Converter.fromMarkdownOrString(collection.description)
});
}

private setEnvironmentVariableCollection(extensionIdentifier: string, collection: EnvironmentVariableCollection): void {
Expand Down
10 changes: 10 additions & 0 deletions packages/plugin-ext/src/plugin/type-converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ export function fromMarkdown(markup: theia.MarkdownString | theia.MarkedString):
}
}

export function fromMarkdownOrString(value: string | theia.MarkdownString | undefined): string | MarkdownStringDTO | undefined {
if (value === undefined) {
return undefined;
} else if (typeof value === 'string') {
return value;
} else {
return fromMarkdown(value);
}
}

export function toMarkdown(value: MarkdownStringDTO): PluginMarkdownStringImpl {
const implemented = new PluginMarkdownStringImpl(value.value, value.supportThemeIcons);
implemented.isTrusted = value.isTrusted;
Expand Down
4 changes: 2 additions & 2 deletions packages/terminal/src/browser/base/terminal-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export abstract class TerminalWidget extends BaseWidget {
*/
abstract processInfo: Promise<TerminalProcessInfo>;

/** The extensions contributing to the environment of this terminal */
abstract contributingExtensions: Promise<Map<string, string | MarkdownString | undefined>>;
/** The ids of extensions contributing to the environment of this terminal mapped to the provided description for their changes. */
abstract envVarCollectionDescriptionsByExtension: Promise<Map<string, string | MarkdownString | undefined>>;

/** Terminal kind that indicates whether a terminal is created by a user or by some extension for a user */
abstract readonly kind: 'user' | string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export class TerminalFrontendContribution implements FrontendApplicationContribu
this.storageService.getData<string>(ENVIRONMENT_VARIABLE_COLLECTIONS_KEY).then(data => {
if (data) {
const collectionsJson: SerializableExtensionEnvironmentVariableCollection[] = JSON.parse(data);
collectionsJson.forEach(c => this.shellTerminalServer.setCollection(c.extensionIdentifier, true, c.collection, undefined));
collectionsJson.forEach(c => this.shellTerminalServer.setCollection(c.extensionIdentifier, true, c.collection ? c.collection : [], c.description));
}
});
});
Expand Down
6 changes: 3 additions & 3 deletions packages/terminal/src/browser/terminal-info-toolbar-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class TerminalInfoToolbarItem implements ReactTabBarToolbarItem {
): Promise<void> {
const currentTarget = event.currentTarget;
if (currentTerminal instanceof TerminalWidget) {
const extensions = await currentTerminal.contributingExtensions;
const extensions = await currentTerminal.envVarCollectionDescriptionsByExtension;
const processId = await currentTerminal.processId;
const processInfo = await currentTerminal.processInfo;

Expand Down Expand Up @@ -107,7 +107,7 @@ export class TerminalInfoToolbarItem implements ReactTabBarToolbarItem {
}
}

protected async onMouseLeave(toDispose: DisposableCollection): Promise<void> {
toDispose.dispose();
protected async onMouseLeave(hoverResources: DisposableCollection): Promise<void> {
hoverResources.dispose();
}
}
4 changes: 2 additions & 2 deletions packages/terminal/src/browser/terminal-widget-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,11 @@ export class TerminalWidgetImpl extends TerminalWidget implements StatefulWidget
return this.shellTerminalServer.getProcessInfo(this.terminalId);
}

get contributingExtensions(): Promise<Map<string, string | MarkdownString | undefined>> {
get envVarCollectionDescriptionsByExtension(): Promise<Map<string, string | MarkdownString | undefined>> {
if (!IBaseTerminalServer.validateId(this.terminalId)) {
return Promise.reject(new Error('terminal is not started'));
}
return this.shellTerminalServer.getContributingExtensions(this.terminalId);
return this.shellTerminalServer.getEnvVarCollectionDescriptionsByExtension(this.terminalId);
}

get terminalId(): number {
Expand Down
5 changes: 3 additions & 2 deletions packages/terminal/src/common/base-terminal-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface IBaseTerminalServer extends RpcServer<IBaseTerminalClient> {
create(IBaseTerminalServerOptions: object): Promise<number>;
getProcessId(id: number): Promise<number>;
getProcessInfo(id: number): Promise<TerminalProcessInfo>;
getContributingExtensions(id: number): Promise<Map<string, string | MarkdownString | undefined>>;
getEnvVarCollectionDescriptionsByExtension(id: number): Promise<Map<string, string | MarkdownString | undefined>>;
getCwdURI(id: number): Promise<string>;
resize(id: number, cols: number, rows: number): Promise<void>;
attach(id: number): Promise<number>;
Expand Down Expand Up @@ -189,7 +189,8 @@ export interface MergedEnvironmentVariableCollection {

export interface SerializableExtensionEnvironmentVariableCollection {
extensionIdentifier: string,
collection: SerializableEnvironmentVariableCollection
collection: SerializableEnvironmentVariableCollection | undefined,
description: string | MarkdownString | undefined
}

export type SerializableEnvironmentVariableCollection = [string, EnvironmentVariableMutator][];
5 changes: 3 additions & 2 deletions packages/terminal/src/node/base-terminal-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export abstract class BaseTerminalServer implements IBaseTerminalServer {
};
}

async getContributingExtensions(id: number): Promise<Map<string, string | MarkdownString | undefined>> {
async getEnvVarCollectionDescriptionsByExtension(id: number): Promise<Map<string, string | MarkdownString | undefined>> {
const terminal = this.processManager.get(id);
if (!(terminal instanceof TerminalProcess)) {
throw new Error(`terminal "${id}" does not exist`);
Expand Down Expand Up @@ -211,7 +211,8 @@ export abstract class BaseTerminalServer implements IBaseTerminalServer {
if (collection.persistent) {
collectionsJson.push({
extensionIdentifier,
collection: [...this.collections.get(extensionIdentifier)!.map.entries()]
collection: [...this.collections.get(extensionIdentifier)!.map.entries()],
description: collection.description
});
}
});
Expand Down

0 comments on commit b80c556

Please sign in to comment.