Skip to content

Commit

Permalink
address feedback by having concrete ValidationMessage type
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerLeonhardt committed Apr 13, 2022
1 parent 27dd7b4 commit 527d631
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
1 change: 0 additions & 1 deletion src/vs/workbench/api/common/extHost.api.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,6 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
showInputBox(options?: vscode.InputBoxOptions, token?: vscode.CancellationToken) {
if (options?.validateInput2) {
checkProposedApiEnabled(extension, 'inputBoxSeverity');
options.validateInput = options.validateInput2 as any;
}
return extHostQuickOpen.showInput(options, token);
},
Expand Down
48 changes: 36 additions & 12 deletions src/vs/workbench/api/common/extHostQuickOpen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { asPromise } from 'vs/base/common/async';
import { CancellationToken } from 'vs/base/common/cancellation';
import { Emitter } from 'vs/base/common/event';
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
import { ExtHostCommands } from 'vs/workbench/api/common/extHostCommands';
import { IExtHostWorkspaceProvider } from 'vs/workbench/api/common/extHostWorkspace';
import type { InputBox, InputBoxOptions, InputBoxValidationSeverity, QuickInput, QuickInputButton, QuickPick, QuickPickItem, QuickPickItemButtonEvent, QuickPickOptions, WorkspaceFolder, WorkspaceFolderPickOptions } from 'vscode';
import { InputBox, InputBoxOptions, InputBoxValidationMessage, QuickInput, QuickInputButton, QuickPick, QuickPickItem, QuickPickItemButtonEvent, QuickPickOptions, WorkspaceFolder, WorkspaceFolderPickOptions } from 'vscode';
import { ExtHostQuickOpenShape, IMainContext, MainContext, TransferQuickInput, TransferQuickInputButton, TransferQuickPickItemOrSeparator } from './extHost.protocol';
import { URI } from 'vs/base/common/uri';
import { ThemeIcon, QuickInputButtons, QuickPickItemKind } from 'vs/workbench/api/common/extHostTypes';
import { ThemeIcon, QuickInputButtons, QuickPickItemKind, InputBoxValidationSeverity } from 'vs/workbench/api/common/extHostTypes';
import { isCancellationError } from 'vs/base/common/errors';
import { ExtensionIdentifier, IExtensionDescription } from 'vs/platform/extensions/common/extensions';
import { coalesce } from 'vs/base/common/arrays';
Expand Down Expand Up @@ -46,7 +45,7 @@ export function createExtHostQuickOpen(mainContext: IMainContext, workspace: IEx
private _commands: ExtHostCommands;

private _onDidSelectItem?: (handle: number) => void;
private _validateInput?: (input: string) => string | { content: string; severity: Severity } | undefined | null | Thenable<string | { content: string; severity: Severity } | undefined | null>;
private _validateInput?: (input: string) => string | InputBoxValidationMessage | undefined | null | Thenable<string | InputBoxValidationMessage | undefined | null>;

private _sessions = new Map<number, ExtHostQuickInput>();

Expand Down Expand Up @@ -148,7 +147,7 @@ export function createExtHostQuickOpen(mainContext: IMainContext, workspace: IEx
showInput(options?: InputBoxOptions, token: CancellationToken = CancellationToken.None): Promise<string | undefined> {

// global validate fn used in callback below
this._validateInput = options ? options.validateInput : undefined;
this._validateInput = options ? options.validateInput2 ?? options.validateInput : undefined;

return proxy.$input(options, typeof this._validateInput === 'function', token)
.then(undefined, err => {
Expand All @@ -160,11 +159,36 @@ export function createExtHostQuickOpen(mainContext: IMainContext, workspace: IEx
});
}

$validateInput(input: string): Promise<string | { content: string; severity: Severity } | null | undefined> {
if (this._validateInput) {
return asPromise(() => this._validateInput!(input));
async $validateInput(input: string): Promise<string | { content: string; severity: Severity } | null | undefined> {
if (!this._validateInput) {
return;
}

const result = await this._validateInput(input);
if (!result || typeof result === 'string') {
return result;
}
return Promise.resolve(undefined);

let severity: Severity;
switch (result.severity) {
case InputBoxValidationSeverity.Info:
severity = Severity.Info;
break;
case InputBoxValidationSeverity.Warning:
severity = Severity.Warning;
break;
case InputBoxValidationSeverity.Error:
severity = Severity.Error;
break;
default:
severity = result.message ? Severity.Error : Severity.Ignore;
break;
}

return {
content: result.message,
severity
};
}

// ---- workspace folder picker
Expand Down Expand Up @@ -675,7 +699,7 @@ export function createExtHostQuickOpen(mainContext: IMainContext, workspace: IEx
private _password = false;
private _prompt: string | undefined;
private _validationMessage: string | undefined;
private _validationMessage2: string | { content: string; severity: InputBoxValidationSeverity } | undefined;
private _validationMessage2: string | InputBoxValidationMessage | undefined;

constructor(private readonly extension: IExtensionDescription, onDispose: () => void) {
super(extension.identifier, onDispose);
Expand Down Expand Up @@ -713,15 +737,15 @@ export function createExtHostQuickOpen(mainContext: IMainContext, workspace: IEx
return this._validationMessage2;
}

set validationMessage2(validationMessage: string | { content: string; severity: InputBoxValidationSeverity } | undefined) {
set validationMessage2(validationMessage: string | InputBoxValidationMessage | undefined) {
checkProposedApiEnabled(this.extension, 'inputBoxSeverity');
this._validationMessage2 = validationMessage;
if (!validationMessage) {
this.update({ validationMessage: undefined, severity: Severity.Ignore });
} else if (typeof validationMessage === 'string') {
this.update({ validationMessage, severity: Severity.Error });
} else {
this.update({ validationMessage: validationMessage.content, severity: validationMessage.severity ?? Severity.Error });
this.update({ validationMessage: validationMessage.message, severity: validationMessage.severity ?? Severity.Error });
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/vscode-dts/vscode.proposed.inputBoxSeverity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@ declare module 'vscode' {
Error = 3
}

export interface InputBoxValidationMessage {
readonly message: string;
readonly severity: InputBoxValidationSeverity;
}

export interface InputBoxOptions {
/**
* The validation message to display. This will become the new {@link InputBoxOptions#validateInput} upon finalization.
*/
// TODO@API consider to extract InputBoxValidationMessage
validateInput2?(value: string): string | { content: string; severity: InputBoxValidationSeverity } | undefined | null |
Thenable<string | { content: string; severity: InputBoxValidationSeverity } | undefined | null>;
validateInput2?(value: string): string | InputBoxValidationMessage | undefined | null |
Thenable<string | InputBoxValidationMessage | undefined | null>;
}

export interface InputBox {
/**
* The validation message to display. This will become the new {@link InputBox#validationMessage} upon finalization.
*/
validationMessage2: string | { content: string; severity: InputBoxValidationSeverity } | undefined;
validationMessage2: string | InputBoxValidationMessage | undefined;
}
}

0 comments on commit 527d631

Please sign in to comment.