Skip to content

Commit 1ff1893

Browse files
committed
feat: add methodSnippets.replaceArguments (powerful way to customize methodSnippets)
1 parent ba91858 commit 1ff1893

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/configurationType.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,21 @@ export type Configuration = {
354354
* @default pick-first
355355
*/
356356
'methodSnippets.multipleSignatures': 'pick-first' | 'empty' /* DON'T SEE A NEED TO IMPLEMENT: | 'pick-longest' | 'pick-shortest' */
357+
/**
358+
* Use empty string for tabstop, `null` to remove argument
359+
* In replace string use `$` to insert tabstop and `\$` to insert `$`
360+
*
361+
* Examples:
362+
* ```json
363+
* "listener": "() => $",
364+
* "eventName": "'$'"
365+
* "thisArg": null
366+
* ```
367+
* @default {}
368+
*/
369+
'methodSnippets.replaceArguments': {
370+
[argumentName: string]: string | null
371+
}
357372
/**
358373
* Wether to disable our and builtin method snippets within jsx attributes
359374
* @default true

src/onCompletionAccepted.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default (tsApi: { onCompletionAccepted }) => {
1212
let justAcceptedReturnKeywordSuggestion = false
1313
let onCompletionAcceptedOverride: ((item: any) => void) | undefined
1414

15+
// eslint-disable-next-line complexity
1516
tsApi.onCompletionAccepted(async (item: vscode.CompletionItem & { document: vscode.TextDocument }) => {
1617
if (onCompletionAcceptedOverride) {
1718
onCompletionAcceptedOverride(item)
@@ -45,11 +46,27 @@ export default (tsApi: { onCompletionAccepted }) => {
4546
inFlightMethodSnippetOperation = controller
4647
const params: RequestResponseTypes['getFullMethodSnippet'] | undefined = await sendCommand('getFullMethodSnippet')
4748
if (!controller.signal.aborted && params) {
49+
const replaceArguments = getExtensionSetting('methodSnippets.replaceArguments')
50+
4851
const snippet = new vscode.SnippetString('')
4952
snippet.appendText('(')
50-
// todo maybe when have skipped, add a way to leave trailing , (previous behavior)
53+
// todo maybe when have optional (skipped), add a way to leave trailing , with tabstop (previous behavior)
5154
for (const [i, param] of params.entries()) {
52-
snippet.appendPlaceholder(param)
55+
const replacer = replaceArguments[param.replace(/\?$/, '')]
56+
if (replacer === null) continue
57+
if (replacer) {
58+
snippet.appendPlaceholder(inner => {
59+
// eslint-disable-next-line unicorn/no-array-for-each
60+
replacer.split(/(?<!\\)\$/g).forEach((text, i, arr) => {
61+
// inner.appendText(text.replace(/\\\$/g, '$'))
62+
inner.value += text
63+
if (i !== arr.length - 1) inner.appendTabstop()
64+
})
65+
})
66+
} else {
67+
snippet.appendPlaceholder(param)
68+
}
69+
5370
if (i !== params.length - 1) snippet.appendText(', ')
5471
}
5572

typescript/src/constructMethodSnippet.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export default (languageService: ts.LanguageService, sourceFile: ts.SourceFile,
6060
const allFiltered = paramsToInsert.length === 0 && parameters.length > paramsToInsert.length
6161
if (allFiltered) return ['']
6262

63+
// methodSnippets.replaceArguments is processed with last stage in onCompletionAccepted
6364
return paramsToInsert
6465
// return `(${paramsToInsert.map((param, i) => `\${${i + 1}:${param.replaceAll}}`).join(', ')})`
6566

0 commit comments

Comments
 (0)