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

Feature Request: Expand func types to func snippets #1553

Closed
joncalhoun opened this issue Mar 2, 2018 · 2 comments · Fixed by #1560
Closed

Feature Request: Expand func types to func snippets #1553

joncalhoun opened this issue Mar 2, 2018 · 2 comments · Fixed by #1560

Comments

@joncalhoun
Copy link
Contributor

Given the code:

type HandlerFunc func(ResponseWriter, *Request)

I would like to see an expansion setting in vscode-go for expanding http.HandlerFunc into one of the following (I'm impartial on which is used):

http.HandlerFunc(func(arg1 http.ResponseWriter, arg2 *http.Request) {
	
})

or without the http.HandlerFunc prefix...

func(arg1 http.ResponseWriter, arg2 *http.Request) {
	
}

Ideally the cursors would be at each arg, and then inside the function like so:

func(${1:"arg1 http.ResponseWriter"}, ${2:"arg2 *http.Request"}) {
	$3
}

gocode returns enough information to do this, and it was done in Atom's Go package here: joefitzgerald/go-plus#719

I'm willing to help with this, but have no experience working on extensions for vscode so I'll have to find some time and figure the basics out. I suspect the actual PR will be fairly small once I learn how to get going.

Any suggestions on how to start developing the vscode-go extension are appreciated.

@ramya-rao-a
Copy link
Contributor

ramya-rao-a commented Mar 4, 2018

@joncalhoun Basic set up steps are described here: Building and Debugging the Go extension

It assumes you have node installed.

Once you are set up, add a breakpoint at https://github.com/Microsoft/vscode-go/blob/0.6.77/src/goSuggest.ts#L154

For your case above, you will see that the suggest variable would have

screen shot 2018-03-03 at 9 11 59 pm

So when suggest.class == 'type' && suggest.type.startsWith('func(') (or do a regex check), you can build your snippet.

Checkout https://github.com/Microsoft/vscode-go/blob/0.6.77/src/goSuggest.ts#L169-L186 to see how we build snippets for functions

Let's re-use the settings useCodeSnippetsOnFunctionSuggest and add these snippets only if that setting is enabled.

Good luck! Let me know if you have any issues. You can also ping me on the gophers slack

joncalhoun added a commit to joncalhoun/vscode-go that referenced this issue Mar 6, 2018
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
  })
joncalhoun added a commit to joncalhoun/vscode-go that referenced this issue Mar 6, 2018
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
  })
joncalhoun added a commit to joncalhoun/vscode-go that referenced this issue Mar 6, 2018
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
  })
ramya-rao-a pushed a commit that referenced this issue May 13, 2018
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
  })
@ramya-rao-a
Copy link
Contributor

This feature is now out in the latest update to the Go extension (0.6.80)

@vscodebot vscodebot bot locked and limited conversation to collaborators Jun 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants