Skip to content

Commit

Permalink
[vscode] support command variable substitution
Browse files Browse the repository at this point in the history
See https://code.visualstudio.com/docs/editor/variables-reference#_command-variables

Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
  • Loading branch information
akosyakov committed Aug 1, 2019
1 parent 87a89db commit 774c460
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
import { injectable, inject } from 'inversify';
import { VariableContribution, VariableRegistry } from './variable';
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
import { CommandService } from '@theia/core/lib/common/command';

@injectable()
export class EnvVariableContribution implements VariableContribution {
export class CommonVariableContribution implements VariableContribution {

@inject(EnvVariablesServer)
protected readonly env: EnvVariablesServer;

@inject(CommandService)
protected readonly commands: CommandService;

async registerVariables(variables: VariableRegistry): Promise<void> {
const execPath = await this.env.getExecPath();
variables.registerVariable({
Expand All @@ -32,11 +36,17 @@ export class EnvVariableContribution implements VariableContribution {
});
variables.registerVariable({
name: 'env',
resolve: async (_, argument) => {
const envVariable = argument && await this.env.getValue(argument);
resolve: async (_, envVariableName) => {
const envVariable = envVariableName && await this.env.getValue(envVariableName);
return envVariable && envVariable.value;
}
});
variables.registerVariable({
name: 'command',
resolve: async (_, command) =>
// tslint:disable-next-line:no-return-await
command && await this.commands.executeCommand(command)
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { inject, injectable } from 'inversify';
import { MessageService } from '@theia/core/lib/common/message-service';
import { QuickOpenService, QuickOpenModel, QuickOpenItem, QuickOpenMode, QuickInputService } from '@theia/core/lib/browser/quick-open';
import { VariableRegistry, Variable } from './variable';
import { VariableResolverService } from './variable-resolver-service';

@injectable()
export class VariableQuickOpenService implements QuickOpenModel {
Expand All @@ -30,6 +31,9 @@ export class VariableQuickOpenService implements QuickOpenModel {
@inject(QuickInputService)
protected readonly quickInputService: QuickInputService;

@inject(VariableResolverService)
protected readonly variableResolver: VariableResolverService;

constructor(
@inject(VariableRegistry) protected readonly variableRegistry: VariableRegistry,
@inject(QuickOpenService) protected readonly quickOpenService: QuickOpenService
Expand Down Expand Up @@ -64,8 +68,8 @@ export class VariableQuickOpenService implements QuickOpenModel {
const argument = await this.quickInputService.open({
placeHolder: 'Type a variable argument'
});
const value = await variable.resolve(undefined, argument);
if (value) {
const value = await this.variableResolver.resolve('${' + variable.name + ':' + argument + '}');
if (typeof value === 'string') {
this.messages.info(value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { VariableRegistry, VariableContribution } from './variable';
import { VariableQuickOpenService } from './variable-quick-open-service';
import { VariableResolverFrontendContribution } from './variable-resolver-frontend-contribution';
import { VariableResolverService } from './variable-resolver-service';
import { EnvVariableContribution } from './env-variable-contribution';
import { CommonVariableContribution } from './common-variable-contribution';

export default new ContainerModule(bind => {
bind(VariableRegistry).toSelf().inSingletonScope();
Expand All @@ -35,6 +35,6 @@ export default new ContainerModule(bind => {

bind(VariableQuickOpenService).toSelf().inSingletonScope();

bind(EnvVariableContribution).toSelf().inSingletonScope();
bind(VariableContribution).toService(EnvVariableContribution);
bind(CommonVariableContribution).toSelf().inSingletonScope();
bind(VariableContribution).toService(CommonVariableContribution);
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import { injectable, inject } from 'inversify';
import { VariableRegistry } from './variable';
import URI from '@theia/core/lib/common/uri';
import { JSONExt, ReadonlyJSONValue } from '@phosphor/coreutils/lib/json';

export interface VariableResolveOptions {
context?: URI;
Expand Down Expand Up @@ -140,7 +141,8 @@ export namespace VariableResolverService {
}
const variable = this.variableRegistry.getVariable(variableName);
const value = variable && await variable.resolve(this.options.context, argument);
this.resolved.set(name, value);
const stringValue = value !== undefined && value !== null && JSONExt.isPrimitive(value as ReadonlyJSONValue) ? String(value) : undefined;
this.resolved.set(name, stringValue);
} catch (e) {
console.error(`Failed to resolved '${name}' variable`, e);
this.resolved.set(name, undefined);
Expand Down
2 changes: 1 addition & 1 deletion packages/variable-resolver/src/browser/variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface Variable {
* `undefined` if variable cannot be resolved.
* Never reject.
*/
resolve(context?: URI, argment?: string): MaybePromise<string | undefined>;
resolve(context?: URI, argment?: string): MaybePromise<Object | undefined>;
}

export const VariableContribution = Symbol('VariableContribution');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
********************************************************************************/

import { injectable, inject, postConstruct } from 'inversify';
import { JSONExt, ReadonlyJSONValue } from '@phosphor/coreutils/lib/json';
import URI from '@theia/core/lib/common/uri';
import { Path } from '@theia/core/lib/common/path';
import { FileSystem } from '@theia/filesystem/lib/common';
Expand Down Expand Up @@ -73,8 +72,7 @@ export class WorkspaceVariableContribution implements VariableContribution {
return undefined;
}
const resourceUri = context || this.getResourceUri();
const value = this.preferences.get<ReadonlyJSONValue>(preferenceName, undefined, resourceUri && resourceUri.toString());
return value !== undefined && value !== null && JSONExt.isPrimitive(value) ? String(value) : undefined;
return this.preferences.get(preferenceName, undefined, resourceUri && resourceUri.toString());
}
});

Expand Down Expand Up @@ -124,8 +122,8 @@ export class WorkspaceVariableContribution implements VariableContribution {
const scoped = (variable: Variable): Variable => ({
name: variable.name,
description: variable.description,
resolve: (context, argument) => {
const workspaceRoot = argument && this.workspaceService.tryGetRoots().find(r => new URI(r.uri).path.name === argument);
resolve: (context, workspaceRootName) => {
const workspaceRoot = workspaceRootName && this.workspaceService.tryGetRoots().find(r => new URI(r.uri).path.name === workspaceRootName);
return variable.resolve(workspaceRoot ? new URI(workspaceRoot.uri) : context);
}
});
Expand Down

0 comments on commit 774c460

Please sign in to comment.