diff --git a/src/vs/vscode.d.ts b/src/vs/vscode.d.ts index ae7b4852481407381626ec47263aa812a1e02190..1b7073e8ea9ec4cfb5714ceceaf2c6ceeb481748 100644 --- a/src/vs/vscode.d.ts +++ b/src/vs/vscode.d.ts @@ -2048,6 +2048,7 @@ declare module 'vscode' { * the [`value`](#SnippetString.value) of this snippet string. * * @param string A value to append 'as given'. The string will be escaped. + * @return This snippet string. */ appendText(string: string): SnippetString; @@ -2057,6 +2058,7 @@ declare module 'vscode' { * * @param number The number of this tabstop, defaults to an auto-incremet * value starting at 1. + * @return This snippet string. */ appendTabstop(number?: number): SnippetString; @@ -2068,8 +2070,29 @@ declare module 'vscode' { * with which a nested snippet can be created. * @param number The number of this tabstop, defaults to an auto-incremet * value starting at 1. + * @return This snippet string. */ appendPlaceholder(value: string | ((snippet: SnippetString) => any), number?: number): SnippetString; + + /** + * Builder-function that appends a variable (`$VAR`) to + * the [`value`](#SnippetString.value) of this snippet string. + * + * @param name The name of the variable - excluding the `$`. + * @return This snippet string. + */ + appendVariable(name: string): SnippetString; + + /** + * Builder-function that appends a variable and default value (`${VAR:fallback}`) to + * the [`value`](#SnippetString.value) of this snippet string. + * + * @param name The name of the variable (excluding the `$`) + * @param defaultValue The default value which is used when the variable name cannot + * be resolved - either a string or a function with which a nested snippet can be created. + * @return This snippet string. + */ + appendVariable(name: string, defaultValue: string | ((snippet: SnippetString) => any)): SnippetString; } /** diff --git a/src/vs/workbench/api/node/extHostTypes.ts b/src/vs/workbench/api/node/extHostTypes.ts index 8a61703dac0907efc0cbe69022ff2d0828111f63..1007507b24c2307c4bac52c793ac77f927c39611 100644 --- a/src/vs/workbench/api/node/extHostTypes.ts +++ b/src/vs/workbench/api/node/extHostTypes.ts @@ -561,6 +561,31 @@ export class SnippetString { this.value += value; this.value += '}'; + return this; + } + + appendVariable(name: string, defaultValue?: string | ((snippet: SnippetString) => any)): SnippetString { + + if (typeof defaultValue === 'function') { + const nested = new SnippetString(); + nested._tabstop = this._tabstop; + defaultValue(nested); + this._tabstop = nested._tabstop; + defaultValue = nested.value; + + } else if (typeof defaultValue === 'string') { + defaultValue = defaultValue.replace(/\$|}/g, '\\$&'); + } + + this.value += '${'; + this.value += name; + if (defaultValue) { + this.value += ':'; + this.value += defaultValue; + } + this.value += '}'; + + return this; } } diff --git a/src/vs/workbench/test/node/api/extHostTypes.test.ts b/src/vs/workbench/test/node/api/extHostTypes.test.ts index 3119db332cc7b63f7f0fda91d6d0b2c5f350355a..b2253497d7dca431727b21d3b8ce6baa687de345 100644 --- a/src/vs/workbench/test/node/api/extHostTypes.test.ts +++ b/src/vs/workbench/test/node/api/extHostTypes.test.ts @@ -452,5 +452,21 @@ suite('ExtHostTypes', function () { string.appendText('foo').appendPlaceholder(b => b.appendText('abc').appendPlaceholder('nested')).appendText('bar'); assert.equal(string.value, 'foo${1:abc${2:nested}}bar'); + string = new types.SnippetString(); + string.appendVariable('foo'); + assert.equal(string.value, '${foo}'); + + string = new types.SnippetString(); + string.appendText('foo').appendVariable('TM_SELECTED_TEXT').appendText('bar'); + assert.equal(string.value, 'foo${TM_SELECTED_TEXT}bar'); + + string = new types.SnippetString(); + string.appendVariable('BAR', b => b.appendPlaceholder('ops')); + assert.equal(string.value, '${BAR:${1:ops}}'); + + string = new types.SnippetString(); + string.appendVariable('BAR', b => { }); + assert.equal(string.value, '${BAR}'); + }); });