From 6bc6ba5b5ad11b7985135a341c375d20f3479ad3 Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Mon, 21 Nov 2016 18:03:54 +0100 Subject: [PATCH] add variable-builder methods to snippet string, #4956 --- src/vs/vscode.d.ts | 23 +++++++++++++++++ src/vs/workbench/api/node/extHostTypes.ts | 25 +++++++++++++++++++ .../test/node/api/extHostTypes.test.ts | 16 ++++++++++++ 3 files changed, 64 insertions(+) diff --git a/src/vs/vscode.d.ts b/src/vs/vscode.d.ts index ae7b4852481..1b7073e8ea9 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 8a61703dac0..1007507b24c 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 3119db332cc..b2253497d7d 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}'); + }); }); -- GitLab