New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
x/tools/cmd/gopls: consider *types.PkgName's type to be referenced package #31750
Comments
(I think you mean types.PkgName in the title and note.) My dissenting opinion: In a qualified identifier such as fmt.Print, the correct definition of the imported package name fmt is unambiguously the "fmt" import in that file. Admittedly, that might be obvious much of the time, because the identifier fmt matches the package name "fmt", but often the relationship is more obscure. Perhaps it's a renaming import, If you want to visit the fmt.Println function, it's easy to remember to invoke go-to-definition on the Println part of the name. |
Thanks - fixed.
To be clear, the existing go-to-definition behavior would not change. The "fmt" in "fmt.Println" will still take you to fmt's declaration, be it an import spec or some other object in your package. I'm proposing we add "go-to-type-definition" if "fmt" is a *types.PkgName. For regular objects, "go-to-type-definition" takes you to where the object's named type is declared. Currently there is no behavior implemented for "go-to-type-definition" of *types.PkgName. |
Completely agree; I think the import spec is the only sensible place to jump to for a qualified identifier.
I for one am a bit lost about what's being proposed here; can you post a code example, to make the discussion about the AST/types concrete? |
Ah; that sounds completely reasonable. |
Let me emphasize again that there are two different varieties of "jumps". LSP can jump to you an identifier's definition, or can jump you to an identifier's type definition. I am only proposing behavior for the latter. First a review of existing behavior not involving packages. "<>" indicates cursor position:
If cursor is on fooUse as indicated, jump-to-definition takes you to fooDecl, and jump-to-type takes you to typeDecl. Now with packages:
If cursor is on fmtUse as indicated, jump-to-definition takes you to fmtDecl. That is existing behavior and will not change. The question is where should jump-to-type take you? I am proposing it take you to the "fmt" package declaration in one of the files in go/src/fmt/. |
The conclusion from the golang-tools meeting is that we can support this behavior with |
For go-to-definition behavior, the definition of a
*types.PkgName
is the associated*ast.ImportSpec
. Currently the go-to-type-definition behavior is not defined for*types.PkgName
. I propose we consider the corresponding package itself to be the "type" of the*types.PkgName
. For example, if you havecontext.Background()
and run go-to-type-definition on "context", it would jump you topackage <>context
inside context.go. We need to pick a single file to jump to, and to start with we can choose the longest file. An alternate heuristic might be picking the file with the most public objects.@stamblerre pointed out that his behavior could be unintuitive/unexpected, and suggested I open an issue to widen the discussion.
Here is my (edited) reasoning from related discussion in the CL:
Edit: fixed *ast.PkgName references to be *types.PkgName
The text was updated successfully, but these errors were encountered: