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

Commit

Permalink
Include return type in func type snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed May 15, 2018
1 parent f8826bb commit 8bb08f6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/goSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import vscode = require('vscode');
import { SignatureHelpProvider, SignatureHelp, SignatureInformation, ParameterInformation, TextDocument, Position, CancellationToken, WorkspaceConfiguration } from 'vscode';
import { definitionLocation } from './goDeclaration';
import { parameters } from './util';
import { getParametersAndReturnType } from './util';

export class GoSignatureHelpProvider implements SignatureHelpProvider {
private goConfig = null;
Expand Down Expand Up @@ -66,7 +66,7 @@ export class GoSignatureHelpProvider implements SignatureHelpProvider {
sig = declarationText.substring(res.name.length);
}

si.parameters = parameters(sig).map(paramText =>
si.parameters = getParametersAndReturnType(sig).params.map(paramText =>
new ParameterInformation(paramText)
);
result.signatures = [si];
Expand Down
10 changes: 5 additions & 5 deletions src/goSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import vscode = require('vscode');
import cp = require('child_process');
import { getBinPath, parameters, parseFilePrelude, isPositionInString, goKeywords, getToolsEnvVars, guessPackageNameFromFile, goBuiltinTypes, byteOffsetAt } from './util';
import { getBinPath, getParametersAndReturnType, parseFilePrelude, isPositionInString, goKeywords, getToolsEnvVars, guessPackageNameFromFile, goBuiltinTypes, byteOffsetAt } from './util';
import { promptForMissingTool } from './goInstallTools';
import { getTextEditForAddImport } from './goImport';
import { getImportablePackages } from './goPackages';
Expand Down Expand Up @@ -170,8 +170,8 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
);
}
if ((config['useCodeSnippetsOnFunctionSuggest'] || config['useCodeSnippetsOnFunctionSuggestWithoutType'])
&& (suggest.class === 'func' || suggest.class === 'var' && suggest.type.startsWith('func('))) {
let params = parameters(suggest.type.substring(4));
&& (suggest.class === 'func' || suggest.class === 'var' && suggest.type.startsWith('func('))) {
let { params, returnType } = getParametersAndReturnType(suggest.type.substring(4));
let paramSnippets = [];
for (let i = 0; i < params.length; i++) {
let param = params[i].trim();
Expand All @@ -189,7 +189,7 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
item.insertText = new vscode.SnippetString(suggest.name + '(' + paramSnippets.join(', ') + ')');
}
if (config['useCodeSnippetsOnFunctionSuggest'] && suggest.class === 'type' && suggest.type.startsWith('func(')) {
let params = parameters(suggest.type.substring(4));
let { params, returnType } = getParametersAndReturnType(suggest.type.substring(4));
let paramSnippets = [];
for (let i = 0; i < params.length; i++) {
let param = params[i].trim();
Expand All @@ -203,7 +203,7 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
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})');
item.insertText = new vscode.SnippetString(suggest.name + '(func(' + paramSnippets.join(', ') + ') {\n $' + (params.length + 1) + '\n})' + returnType);
}

if (wordAtPosition && wordAtPosition.start.character === 0 &&
Expand Down
15 changes: 9 additions & 6 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ export function parseFilePrelude(text: string): Prelude {
// ["foo", "bar string", "baz string"]
// Takes care of balancing parens so to not get confused by signatures like:
// (pattern string, handler func(ResponseWriter, *Request)) {
export function parameters(signature: string): string[] {
let ret: string[] = [];
export function getParametersAndReturnType(signature: string): { params: string[], returnType: string } {
let params: string[] = [];
let parenCount = 0;
let lastStart = 1;
for (let i = 1; i < signature.length; i++) {
Expand All @@ -135,20 +135,23 @@ export function parameters(signature: string): string[] {
parenCount--;
if (parenCount < 0) {
if (i > lastStart) {
ret.push(signature.substring(lastStart, i));
params.push(signature.substring(lastStart, i));
}
return ret;
return {
params,
returnType: i < signature.length - 1 ? signature.substr(i + 1) : ''
};
}
break;
case ',':
if (parenCount === 0) {
ret.push(signature.substring(lastStart, i));
params.push(signature.substring(lastStart, i));
lastStart = i + 2;
}
break;
}
}
return null;
return { params: [], returnType: '' };
}

export function canonicalizeGOPATHPrefix(filename: string): string {
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/completions/snippets.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import "fmt"
import "net/http"

type HandlerFunc func(http.ResponseWriter, *http.Request)
type HandlerFuncWithArgNames func(w http.ResponseWriter, r *http.Request)
type HandlerFunc func(string, string) (string, string)
type HandlerFuncWithArgNames func(w string, r string) int
type HandlerFuncNoReturnType func(string, string)

func main(){
fmt.Println("hello")
Expand Down
8 changes: 6 additions & 2 deletions test/go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -810,15 +810,19 @@ It returns the number of bytes written and any write error encountered.
let noFunctionAsTypeSnippet = provider.provideCompletionItemsInternal(editor.document, new vscode.Position(14, 0), null, Object.create(baseConfig, {'useCodeSnippetsOnFunctionSuggest': { value: false }})).then(items => {
let item1 = items.find(x => x.label === 'HandlerFunc');
let item2 = items.find(x => x.label === 'HandlerFuncWithArgNames');
let item3 = items.find(x => x.label === 'HandlerFuncNoReturnType');
assert.equal(!item1.insertText, true);
assert.equal(!item2.insertText, true);
assert.equal(!item3.insertText, true);
});

let withFunctionAsTypeSnippet = provider.provideCompletionItemsInternal(editor.document, new vscode.Position(14, 0), null, Object.create(baseConfig, {'useCodeSnippetsOnFunctionSuggest': { value: true }})).then(items => {
let item1 = items.find(x => x.label === 'HandlerFunc');
let item2 = items.find(x => x.label === 'HandlerFuncWithArgNames');
assert.equal((<vscode.SnippetString>item1.insertText).value, 'HandlerFunc(func(${1:arg1} http.ResponseWriter, ${2:arg2} *http.Request) {\n\t$3\n})');
assert.equal((<vscode.SnippetString>item2.insertText).value, 'HandlerFuncWithArgNames(func(${1:w} http.ResponseWriter, ${2:r} *http.Request) {\n\t$3\n})');
let item3 = items.find(x => x.label === 'HandlerFuncNoReturnType');
assert.equal((<vscode.SnippetString>item1.insertText).value, 'HandlerFunc(func(${1:arg1} string, ${2:arg2} string) {\n\t$3\n}) (string, string)');
assert.equal((<vscode.SnippetString>item2.insertText).value, 'HandlerFuncWithArgNames(func(${1:w} string, ${2:r} string) {\n\t$3\n}) int');
assert.equal((<vscode.SnippetString>item3.insertText).value, 'HandlerFuncNoReturnType(func(${1:arg1} string, ${2:arg2} string) {\n\t$3\n})');
});

return Promise.all([
Expand Down

0 comments on commit 8bb08f6

Please sign in to comment.