From 7b9368ebc71c4e13504cfba38c6cb720cb561eed Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Tue, 30 Jan 2018 19:33:44 +0100 Subject: [PATCH] change to validateInput --- extensions/git/src/repository.ts | 2 +- src/vs/vscode.d.ts | 56 ------------------------- src/vs/vscode.proposed.d.ts | 46 ++++++++++++++++++++ src/vs/workbench/api/node/extHostSCM.ts | 41 ++++++++++++------ 4 files changed, 76 insertions(+), 69 deletions(-) diff --git a/extensions/git/src/repository.ts b/extensions/git/src/repository.ts index 874fa9568c2..893b2ce2939 100644 --- a/extensions/git/src/repository.ts +++ b/extensions/git/src/repository.ts @@ -523,7 +523,7 @@ export class Repository implements Disposable { this._sourceControl.inputBox.placeholder = localize('commitMessage', "Message (press {0} to commit)"); this._sourceControl.acceptInputCommand = { command: 'git.commitWithInput', title: localize('commit', "Commit"), arguments: [this._sourceControl] }; this._sourceControl.quickDiffProvider = this; - this._sourceControl.inputBox.validationProvider = this; + this._sourceControl.inputBox.validateInput = this.validateInput.bind(this); this.disposables.push(this._sourceControl); this._mergeGroup = this._sourceControl.createResourceGroup('merge', localize('merge changes', "Merge Changes")); diff --git a/src/vs/vscode.d.ts b/src/vs/vscode.d.ts index 7abf4f0ce7f..2662ecbd9d8 100644 --- a/src/vs/vscode.d.ts +++ b/src/vs/vscode.d.ts @@ -5986,56 +5986,6 @@ declare module 'vscode' { export function setLanguageConfiguration(language: string, configuration: LanguageConfiguration): Disposable; } - /** - * Represents the validation type of the Source Control input. - */ - export enum SourceControlInputBoxValidationType { - - /** - * Something not allowed by the rules of a language or other means. - */ - Error = 0, - - /** - * Something suspicious but allowed. - */ - Warning = 1, - - /** - * Something to inform about but not a problem. - */ - Information = 2 - } - - export interface SourceControlInputBoxValidation { - - /** - * The validation message to display. - */ - readonly message: string; - - /** - * The validation type. - */ - readonly type: SourceControlInputBoxValidationType; - } - - /** - * A validation provider which can validate Source Control input. - */ - export interface SourceControlInputBoxValidationProvider { - - /** - * A function that will be called to validate input and give a hint to the user. - * - * @param value The current value of the input box. - * @param cursorPosition The cusror position within the input box. - * @return A human readable string which is presented as diagnostic message. - * Return `undefined`, `null`, or the empty string when 'value' is valid. - */ - validateInput(value: string, cursorPosition: number): ProviderResult; - } - /** * Represents the input box in the Source Control viewlet. */ @@ -6050,12 +6000,6 @@ declare module 'vscode' { * A string to show as place holder in the input box to guide the user. */ placeholder: string; - - /** - * A validation provider for the input box. It's possible to change - * the validation provider simply by setting this property to a different value. - */ - validationProvider: SourceControlInputBoxValidationProvider; } interface QuickDiffProvider { diff --git a/src/vs/vscode.proposed.d.ts b/src/vs/vscode.proposed.d.ts index ef2c05f37c6..a52f2ce1bab 100644 --- a/src/vs/vscode.proposed.d.ts +++ b/src/vs/vscode.proposed.d.ts @@ -394,4 +394,50 @@ declare module 'vscode' { */ logger: Logger; } + + /** + * Represents the validation type of the Source Control input. + */ + export enum SourceControlInputBoxValidationType { + + /** + * Something not allowed by the rules of a language or other means. + */ + Error = 0, + + /** + * Something suspicious but allowed. + */ + Warning = 1, + + /** + * Something to inform about but not a problem. + */ + Information = 2 + } + + export interface SourceControlInputBoxValidation { + + /** + * The validation message to display. + */ + readonly message: string; + + /** + * The validation type. + */ + readonly type: SourceControlInputBoxValidationType; + } + + /** + * Represents the input box in the Source Control viewlet. + */ + export interface SourceControlInputBox { + + /** + * A validation function for the input box. It's possible to change + * the validation provider simply by setting this property to a different function. + */ + validateInput?(value: string, cursorPosition: number): ProviderResult; + } } diff --git a/src/vs/workbench/api/node/extHostSCM.ts b/src/vs/workbench/api/node/extHostSCM.ts index a83ec8f00fe..596aa1ead50 100644 --- a/src/vs/workbench/api/node/extHostSCM.ts +++ b/src/vs/workbench/api/node/extHostSCM.ts @@ -110,6 +110,10 @@ function compareResourceStates(a: vscode.SourceControlResourceState, b: vscode.S return result; } +export interface IValidateInput { + (value: string, cursorPosition: number): vscode.ProviderResult; +} + export class ExtHostSCMInputBox implements vscode.SourceControlInputBox { private _value: string = ''; @@ -140,23 +144,31 @@ export class ExtHostSCMInputBox implements vscode.SourceControlInputBox { this._placeholder = placeholder; } - private _validationProvider: vscode.SourceControlInputBoxValidationProvider; + private _validateInput: IValidateInput; - get validationProvider(): vscode.SourceControlInputBoxValidationProvider { - return this._validationProvider; + get validateInput(): IValidateInput { + if (!this._extension.enableProposedApi) { + throw new Error(`[${this._extension.id}]: Proposed API is only available when running out of dev or with the following command line switch: --enable-proposed-api ${this._extension.id}`); + } + + return this._validateInput; } - set validationProvider(provider: vscode.SourceControlInputBoxValidationProvider) { - if (!provider || typeof provider.validateInput !== 'function') { - console.warn('INVALID SCM input box validation provider'); + set validateInput(fn: IValidateInput) { + if (!this._extension.enableProposedApi) { + throw new Error(`[${this._extension.id}]: Proposed API is only available when running out of dev or with the following command line switch: --enable-proposed-api ${this._extension.id}`); + } + + if (fn && typeof fn !== 'function') { + console.warn('Invalid SCM input box validation function'); return; } - this._validationProvider = provider; - this._proxy.$setValidationProviderIsEnabled(this._sourceControlHandle, true); + this._validateInput = fn; + this._proxy.$setValidationProviderIsEnabled(this._sourceControlHandle, !!fn); } - constructor(private _proxy: MainThreadSCMShape, private _sourceControlHandle: number) { + constructor(private _extension: IExtensionDescription, private _proxy: MainThreadSCMShape, private _sourceControlHandle: number) { // noop } @@ -386,13 +398,14 @@ class ExtHostSourceControl implements vscode.SourceControl { private handle: number = ExtHostSourceControl._handlePool++; constructor( + _extension: IExtensionDescription, private _proxy: MainThreadSCMShape, private _commands: ExtHostCommands, private _id: string, private _label: string, private _rootUri?: vscode.Uri ) { - this._inputBox = new ExtHostSCMInputBox(this._proxy, this.handle); + this._inputBox = new ExtHostSCMInputBox(_extension, this._proxy, this.handle); this._proxy.$registerSourceControl(this.handle, _id, _label, _rootUri && _rootUri.toString()); } @@ -508,7 +521,7 @@ export class ExtHostSCM implements ExtHostSCMShape { this.logService.trace('ExtHostSCM#createSourceControl', extension.id, id, label, rootUri); const handle = ExtHostSCM._handlePool++; - const sourceControl = new ExtHostSourceControl(this._proxy, this._commands, id, label, rootUri); + const sourceControl = new ExtHostSourceControl(extension, this._proxy, this._commands, id, label, rootUri); this._sourceControls.set(handle, sourceControl); const sourceControls = this._sourceControlsByExtension.get(extension.id) || []; @@ -582,7 +595,11 @@ export class ExtHostSCM implements ExtHostSCMShape { return TPromise.as(undefined); } - const result = await sourceControl.inputBox.validationProvider.validateInput(value, cursorPosition); + if (!sourceControl.inputBox.validateInput) { + return TPromise.as(undefined); + } + + const result = await sourceControl.inputBox.validateInput(value, cursorPosition); if (!result) { return TPromise.as(undefined); -- GitLab