Skip to content

Commit

Permalink
Merge pull request #119 from augustocdias/user_input_context
Browse files Browse the repository at this point in the history
Refactor UserInputContext
  • Loading branch information
MarcelRobitaille authored Nov 9, 2024
2 parents 8182914 + 991b17c commit ecc1244
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 45 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## [Unreleased] 2024-XX-XX

- Fix default value not recorded for useFirstResult / useSingleResult (#117)
- Add support for ${taskId:} input variables
- Do not reset user input context (#95)

## [1.12.4] 2024-10-03

- Work around VSCode bug with activeItems and selectedItems (#112)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ As of today, the extension supports variable substitution for:
* the remembered value (the default value when `rememberPrevious` is true), available as `${rememberedValue}`
* all config variables, pattern: `${config:variable}`
* all environment variables (`tasks.json` `options.env` with fallback to parent process), pattern: `${env:variable}`
* input variables which have been defined with `shellCommand.execute`, pattern: `${input:variable}` (limited supported see below for usage)
* input variables that have been defined with `shellCommand.execute`, pattern: `${input:variable}` (uses `input.id`) and `${taskId:task}` (uses `input.args.taskId`) (limited supported see below for usage)
* Support for ${command:...} pattern, for example to extract CMake's build directory using `${command:cmake.buildDirectory}`.
* multi-folder workspace support: `${workspaceFolder}` (the folder whose `.vscode/tasks.json` defined the given task), `${workspaceFolder[1]}` (a specific folder by index), and `${workspaceFolder:name}` (a specific folder by name)

Expand Down
4 changes: 0 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,4 @@ export function activate(this: any, context: vscode.ExtensionContext) {
};

context.subscriptions.push(vscode.commands.registerCommand(command, callback, this));

// Triggers a reset of the userInput context
context.subscriptions.push(vscode.tasks.onDidStartTask(() => userInputContext.reset()));
context.subscriptions.push(vscode.debug.onDidStartDebugSession(() => userInputContext.reset()));
}
40 changes: 23 additions & 17 deletions src/lib/CommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ export class CommandHandler {
: vscode.workspace.workspaceFolders?.[this.input.workspaceIndex].uri.fsPath;
}

async handle() {
// Get the result, either by showing a dropdown or taking the first / only
// option
async getResult() {
await this.resolveArgs();

const result = await this.runCommand();
Expand All @@ -153,27 +155,31 @@ export class CommandHandler {
(this.args.useSingleResult && nonEmptyInput.length === 1);

if (useFirstResult) {
if (this.input.id && this.userInputContext) {
this.userInputContext.recordInput(this.input.id, nonEmptyInput[0].value);
}
return nonEmptyInput[0].value;
const result = nonEmptyInput[0].value;
return [result];
} else {
const selectedItems = await this.quickPick(nonEmptyInput);
return selectedItems;
}
}

if (!selectedItems) {
this.userInputContext.reset();
return;
}

const result = selectedItems.join(this.args.multiselectSeparator!);
this.userInputContext.recordInput(this.input.id, result);
async handle() {
const selectedItems = await this.getResult();

if (this.args.rememberPrevious && this.args.taskId) {
this.setDefault(this.args.taskId, selectedItems);
}
if (selectedItems === undefined) {
return;
}

return result;
if (this.args.rememberPrevious && this.args.taskId) {
this.setDefault(this.args.taskId, selectedItems);
}

const result = selectedItems.join(this.args.multiselectSeparator!);

this.userInputContext.recordInputByInputId(this.input.id, result);
this.userInputContext.recordInputByTaskId(this.args.taskId, result);

return result;
}

protected async runCommand() {
Expand Down Expand Up @@ -292,7 +298,7 @@ export class CommandHandler {

picker.onDidHide(() => {
const didCancelQuickPickSession =
picker?.selectedItems?.length === 0 ?? true;
picker?.selectedItems?.length === 0;
const result = CommandHandler.transformSelection(picker);

if (didCancelQuickPickSession) {
Expand Down
26 changes: 16 additions & 10 deletions src/lib/UserInputContext.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
export class UserInputContext
{
protected recordedInputs: { [id: string] : string; } = {};
protected recordedInputsByInputId: { [id: string] : string | undefined; } = {};
protected recordedInputsByTaskId: { [id: string] : string | undefined; } = {};

reset(): void
{
this.recordedInputs = {};
recordInputByInputId(inputId: string | undefined, taskValue: string | undefined): void {
if (inputId !== undefined) {
this.recordedInputsByInputId[inputId] = taskValue;
}
}

recordInput(inputId: string, taskValue: string): void
{
this.recordedInputs[inputId] = taskValue;
recordInputByTaskId(taskId: string | undefined, taskValue: string | undefined): void {
if (taskId !== undefined) {
this.recordedInputsByTaskId[taskId] = taskValue;
}
}

lookupInputValue(inputId: string): string
{
return this.recordedInputs[inputId];
lookupInputValueByInputId(inputId: string): string | undefined {
return this.recordedInputsByInputId[inputId];
}

lookupInputValueByTaskId(taskId: string): string | undefined {
return this.recordedInputsByTaskId[taskId];
}
}
28 changes: 15 additions & 13 deletions src/lib/VariableResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ export class VariableResolver {
protected configVarRegex = /config:(.+)/m;
protected envVarRegex = /env:(.+)/m;
protected inputVarRegex = /input:(.+)/m;
protected taskIdVarRegex = /taskId:(.+)/m;
protected commandVarRegex = /command:(.+)/m;
protected rememberedValue?: string;
protected userInputContext?: UserInputContext;
protected userInputContext: UserInputContext;
protected input: Input;

constructor(input: Input, userInputContext?: UserInputContext,
constructor(input: Input, userInputContext: UserInputContext,
rememberedValue?: string) {
this.userInputContext = userInputContext;
this.rememberedValue = rememberedValue;
Expand All @@ -55,9 +56,17 @@ export class VariableResolver {
if (this.envVarRegex.test(value)) {
return this.bindEnvVariable(value);
}
if (this.userInputContext && this.inputVarRegex.test(value)) {
return this.bindInputVariable(value, this.userInputContext);

const inputVar = this.inputVarRegex.exec(value);
if (inputVar) {
return this.userInputContext.lookupInputValueByInputId(inputVar[1]) ?? '';
}

const taskIdVar = this.taskIdVarRegex.exec(value);
if (taskIdVar) {
return this.userInputContext.lookupInputValueByTaskId(taskIdVar[1]) ?? '';
}

if (this.commandVarRegex.test(value)) {
// We don't replace these yet, they have to be done asynchronously
promises.push(this.bindCommandVariable(value));
Expand Down Expand Up @@ -165,21 +174,14 @@ export class VariableResolver {

protected bindEnvVariable(value: string): string {
const result = this.envVarRegex.exec(value);

if (!result) {
return '';
}

const key = result[1];
const configuredEnv = this.input.env;

return configuredEnv[key] ?? process.env[key] ?? '';
}

protected bindInputVariable(value: string, userInputContext: UserInputContext): string {
const result = this.inputVarRegex.exec(value);
if (!result) {
return '';
}

return userInputContext.lookupInputValue(result[1]) || '';
}
}

0 comments on commit ecc1244

Please sign in to comment.