提交 6bc6ba5b 编写于 作者: J Johannes Rieken

add variable-builder methods to snippet string, #4956

上级 417214fb
......@@ -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;
}
/**
......
......@@ -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;
}
}
......
......@@ -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}');
});
});
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册