Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
Suggestion to expand func types to func snippets (#1553)
Browse files Browse the repository at this point in the history
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
  })
  • Loading branch information
joncalhoun committed Mar 6, 2018
1 parent 810791e commit a7b38ff
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/goSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit a7b38ff

Please sign in to comment.