godef icon indicating copy to clipboard operation
godef copied to clipboard

Support dot imports

Open applepi-icpc opened this issue 8 years ago • 4 comments

Solution for issue #19

applepi-icpc avatar Aug 27 '17 12:08 applepi-icpc

@rogpeppe any updates on it? I have tested it for a while now and I haven't found any problems.

lloiser avatar Oct 30 '17 09:10 lloiser

It doesn't look as if it will work properly to me. If issue #19 is to be fixed, I think it needs to be done at a lower level. Consider this example:

package foo
type Foo struct{
	A int
}

package bar
import . "foo"
type Bar struct {
	Foo
}

package main
import "bar"
func main() {
	var b bar.Bar
	b.A = 99
}

I haven't tried it, but I strongly suspect that if used the code in this PR and tried jumping to the definition of A on the b.A = 99 line, it wouldn't work.

To do it right, I think the fix needs to go into the github.com/rogpeppe/godef/go/parser package.

By the way, IMHO no-one should use dot imports. You never need them. That's the main reason why I haven't spent more time trying to fix the issue, although if someone does present a decent PR that properly fixes the issue (and has some decent tests, ideally), I'd be happy to accept it.

rogpeppe avatar Oct 30 '17 17:10 rogpeppe

@rogpeppe Yes, you're right. I mistakenly thought that only dot imports in current file need to be processed, and I neglected the case you have given.

Some projects that I'm working on used dot imports to help their refactoring. Some developers want to move some types and functions out of a package into a new one, without changing too much existing codes. So I think maybe dot import is useful in some situations.

Thank you. I will try to fix this problem.

applepi-icpc avatar Oct 31 '17 11:10 applepi-icpc

Some developers want to move some types and functions out of a package into a new one, without changing too much existing codes.

I'd suggest using gofmt -r to refactor code like that. For example:

gofmt -r 'MovedFunction -> externalPackage.MovedFunction' -w .

rogpeppe avatar Oct 31 '17 12:10 rogpeppe