add variable-builder methods to snippet string, #4956

This commit is contained in:
Johannes Rieken
2016-11-21 18:03:54 +01:00
parent 417214fbde
commit 6bc6ba5b5a
3 changed files with 64 additions and 0 deletions

View File

@@ -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;
}
}