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

Go To Implementation #771

Closed
AlekSi opened this issue Feb 6, 2017 · 15 comments
Closed

Go To Implementation #771

AlekSi opened this issue Feb 6, 2017 · 15 comments

Comments

@AlekSi
Copy link
Contributor

AlekSi commented Feb 6, 2017

VS Code 1.9 introduced Go To Implementation command. Go plugin should support it.

@nochso
Copy link
Contributor

nochso commented Feb 6, 2017

To get someone else started because my TypeScript is weak: the guru tool can handle this. It's already being used by this extension to find references.

To search GOPATH for implementations of an interface (can be slow):
guru -scope ... -json implements io.go:#3799
..where the 3799th byte is the start of the name of an interface definition. It returns JSON with paths and positions to the found implementations.

It might be a good idea to allow the choice of scope between GOPATH (slow) or the currently opened project root including sub-folders.

@codmajik
Copy link
Contributor

codmajik commented Feb 7, 2017

Hi @ramya-rao-a, @AlekSi

I took a swing at this and got a working version here:
codmajik@3bf018b

as @nochso said, it is excruciatingly slow and vscode seems to be expecting a single result else it doesn't do anything.

There is one reason am not sending a push request and that is because registerImplementationProvider and ImplementationProvider are missing in the vscode-extension-vscode library (atleast what is currently deployed on npm). I manually overwrote my local copy with one from vscode repo directly for it to work.

This may be completely un-usable.

Cheers!!!

@ramya-rao-a
Copy link
Contributor

@codmajik That was quick!

You can get the latest vscode.d.ts by updating the "engines" field in the package.json file of the Go extension. Right now it is pointing to ^1.8.0 change that to ^1.9.0 and you will be able to get the the new APIs

Can you try using the -scope flag like @nochso suggested and pass the workspace root as scope?

@codmajik
Copy link
Contributor

codmajik commented Feb 8, 2017

@ramya-rao-a, guru expects -scope to be a go package, so setting it to the workspace root may not work.

I can try to be clever by figuring out the package, like so:

  • using the selected file ($HOME/mygopath/src/github.com/mycurrentproject/somespace/morepkg/my/face.go) look for a directory whose name looks like a domain eg: github.com
  • then go 1 level down still based on the filepath. eg: github.com/mycurrentproject/
  • try using that as the package: -scope github.com/mycurrentproject/...

because go enforces folder structure it may works most of the times.

warning: this may completely be needless

Q: should it be restricted to interfaces because guru go both ways. From concrete to interface and visversa

@nochso
Copy link
Contributor

nochso commented Feb 16, 2017

@codmajik go list might be what you're looking for:

The default invocation of go list returns the name of the import path that represents the directory you are currently in, or the package path you provide.

However that would mean running another external command. I'll try to have a look at the implementation of go list. Hopefully I can piece together what the official way to detect the import path is.

@ramya-rao-a
Copy link
Contributor

Won't ${workspaceRoot}/... work?

@codmajik
Copy link
Contributor

@ramya-rao-a it won't; ${workspaceRoot}/... would be /home/codmajik/gopath/src/github.com/codmajik/servicerouter/... what is expected is github.com/codmajik/servicerouter/...

I have been busy would get back to this in a few days.

@zignd
Copy link

zignd commented Apr 15, 2017

I think the current version of the extension already implements this feature. Am I right @ramya-rao-a?

@ramya-rao-a
Copy link
Contributor

@zignd I think you are referring to the "Go to Definition" feature

This one is about clicking on an interface and getting list of places where the interface has been implemented.

@zignd
Copy link

zignd commented Apr 15, 2017

@ramya-rao-a Oh, you're right. Sorry, my mistake. 😅

@jackie-qiu
Copy link

@codmajik ${workspaceRoot} strip the $GOROOT/src?

@codmajik
Copy link
Contributor

codmajik commented Jun 2, 2017

@jackie-qiu ${workspaceRoot} is the full path of the folder opened in explorer. It is not computed.

@ramya-rao-a
Copy link
Contributor

@codmajik So what you need is the import path. The same is needed for running go build and we computed the import path this way: https://github.com/Microsoft/vscode-go/blob/0.6.61/src/goCheck.ts#L168

@ironcladlou
Copy link
Contributor

I think go list might get us the import package needed for the -scope argument for the common case. I need to read up on ImplementationProvider to understand whether it's meant to cope with the multiple implements results (I'd expect something similar to the CodeLens view presented for Find References). I've really been wanting guru implements lately, and performance now seems good enough for me.

$ pwd
/Users/dan/Projects/kubernetes/src/k8s.io/kubernetes

$ go list -e -json
{
        "Dir": "/Users/dan/Projects/kubernetes/src/k8s.io/kubernetes",
        "ImportPath": "k8s.io/kubernetes",
        "Stale": true,
        "StaleReason": "errors loading package",
        "Root": "/Users/dan/Projects/kubernetes",
        "Incomplete": true,
        "Error": {
                "ImportStack": [
                        "k8s.io/kubernetes"
                ],
                "Pos": "",
                "Err": "no buildable Go source files in /Users/dan/Projects/kubernetes/src/k8s.io/kubernetes"
        }
}

@ramya-rao-a
Copy link
Contributor

This feature is now out in the latest update to the Go extension 0.6.64.

Thanks everyone!

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

No branches or pull requests

7 participants