Merge pull request #84048 from okmttdhr/snippet-choice-builder-method

Add builder-method for snippet choice
This commit is contained in:
Johannes Rieken 2019-11-13 16:30:10 +01:00 committed by GitHub
commit 0a0f2bffe5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 0 deletions

11
src/vs/vscode.d.ts vendored
View file

@ -2967,6 +2967,17 @@ declare module 'vscode' {
*/
appendPlaceholder(value: string | ((snippet: SnippetString) => any), number?: number): SnippetString;
/**
* Builder-function that appends a choice (`${1|a,b,c}`) to
* the [`value`](#SnippetString.value) of this snippet string.
*
* @param values The values for choices - the array of strings
* @param number The number of this tabstop, defaults to an auto-increment
* value starting at 1.
* @return This snippet string.
*/
appendChoice(values: string[], number?: number): SnippetString;
/**
* Builder-function that appends a variable (`${VAR}`) to
* the [`value`](#SnippetString.value) of this snippet string.

View file

@ -740,6 +740,18 @@ export class SnippetString {
return this;
}
appendChoice(values: string[], number: number = this._tabstop++): SnippetString {
const value = SnippetString._escape(values.toString());
this.value += '${';
this.value += number;
this.value += '|';
this.value += value;
this.value += '|}';
return this;
}
appendVariable(name: string, defaultValue?: string | ((snippet: SnippetString) => any)): SnippetString {
if (typeof defaultValue === 'function') {

View file

@ -527,6 +527,25 @@ suite('ExtHostTypes', function () {
string.appendVariable('BAR', b => { });
assert.equal(string.value, '${BAR}');
string = new types.SnippetString();
string.appendChoice(['b', 'a', 'r']);
assert.equal(string.value, '${1|b,a,r|}');
string = new types.SnippetString();
string.appendChoice(['b', 'a', 'r'], 0);
assert.equal(string.value, '${0|b,a,r|}');
string = new types.SnippetString();
string.appendText('foo').appendChoice(['far', 'boo']).appendText('bar');
assert.equal(string.value, 'foo${1|far,boo|}bar');
string = new types.SnippetString();
string.appendText('foo').appendChoice(['far', '$boo']).appendText('bar');
assert.equal(string.value, 'foo${1|far,\\$boo|}bar');
string = new types.SnippetString();
string.appendText('foo').appendPlaceholder('farboo').appendChoice(['far', 'boo']).appendText('bar');
assert.equal(string.value, 'foo${1:farboo}${2|far,boo|}bar');
});
test('instanceof doesn\'t work for FileSystemError #49386', function () {