From a7b38ffececcd978d5a8ead695d5e27f961c4c88 Mon Sep 17 00:00:00 2001 From: Jon Calhoun Date: Mon, 5 Mar 2018 23:22:31 -0500 Subject: [PATCH] Suggestion to expand func types to func snippets (#1553) Given a type that defines a function, added a code completion suggestion to expand the type into a closure. Eg: type Demo func(int) string Will expand into the the suggest with the name "Demo" and upon triggering it the following snippet will be used: Demo(func(${1:arg1} int) { $2 }) --- src/goSuggest.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/goSuggest.ts b/src/goSuggest.ts index 0ee0453d1..0370db2b2 100644 --- a/src/goSuggest.ts +++ b/src/goSuggest.ts @@ -184,6 +184,23 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider { } item.insertText = new vscode.SnippetString(suggest.name + '(' + paramSnippets.join(', ') + ')'); } + if (conf.get('useCodeSnippetsOnFunctionSuggest') && suggest.class === 'type' && suggest.type.startsWith('func(')) { + let params = parameters(suggest.type.substring(4)); + let paramSnippets = []; + for (let i = 0; i < params.length; i++) { + let param = params[i].trim(); + if (param) { + param = param.replace('${', '\\${').replace('}', '\\}'); + if (!param.includes(' ')) { + // If we don't have an argument name, we need to create one + param = 'arg' + (i+1) + ' ' + param + } + let arg = param.substr(0, param.indexOf(' ')); + paramSnippets.push('${' + (i + 1) + ':' + arg + '}' + param.substr(param.indexOf(' '), param.length)); + } + } + item.insertText = new vscode.SnippetString(suggest.name + '(func(' + paramSnippets.join(', ') + ') {\n $' + (params.length + 1) + '\n})'); + } if (wordAtPosition && wordAtPosition.start.character === 0 && suggest.class === 'type' && !goBuiltinTypes.has(suggest.name)) {