aboutsummaryrefslogtreecommitdiff
path: root/internal/lsp/references.go
AgeCommit message (Collapse)Author
2022-01-13internal/lsp: honor the file kind provided by clients for overlaysRobert Findley
Honor the file kind provided by clients for overlays, by passing the FileHandle into View.FileKind and checking for overlays. For golang/vscode-go#1957 Change-Id: I4a767cb64dc5205f1d10c3126a2cbe67c21a34e4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/378314 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> gopls-CI: kokoro <noreply+kokoro@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Suzy Mueller <suzmue@golang.org>
2022-01-13internal/template: identify template files by the templateExtensions optionpjw
Make the language id (sent from the client) 'gotmpl' equivalent to 'tmpl' Wherever a view is known, use its options to determine which files are template files. Whenever the client sends an explicit languageID, use that. Partially fixes golang/vscode-go#1957 Change-Id: I04cd630d6c6c80e0a78c2fafb6ddc1166ce86829 Reviewed-on: https://go-review.googlesource.com/c/tools/+/376854 Trust: Peter Weinberger <pjw@google.com> Reviewed-by: Robert Findley <rfindley@google.com>
2021-05-05internal/lsp: support template filesPeter Weinbergr
Provide some support for template files, implementing most of https://docs.google.com/document/d/1clKAywucZVBXvL_v4mMhLQXso59lmQPMk1gtSpkV-Xw Template support is controlled by the option 'experimentalTemplateSupport' which defaults to false. Most of the code is in a new 'template' package. Implemented are semantic tokens, diagnostics, definitions, hover, and references, and there is a stub for completions. This code treats all the template files of a package together, so as to follow cross-references. Change-Id: I793606d8a0c9e96a0c015162d68f56b5d8599294 Reviewed-on: https://go-review.googlesource.com/c/tools/+/297871 Run-TryBot: Peter Weinberger <pjw@google.com> gopls-CI: kokoro <noreply+kokoro@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com> Trust: Peter Weinberger <pjw@google.com>
2020-08-03internal/lsp/cache: ref-count snapshotsHeschi Kreinick
To manually collect cache entries, we need to know when a snapshot is idle. Add a reference count in the form of a WaitGroup and keep track of its uses. The pattern is that any time a snapshot is returned, it comes with a release function that decrements the ref count. Almost all uses of a snapshot originate in a user-facing request, handled in beginFileRequest. There it's mostly an exercise in passing Snapshots around instead of Views. In the other places I took the path of least resistance. For file modifications I tried to minimize the amount of code that needed to deal with snapshots. For diagnostics I just acquired the snapshot at the diagnostics call. Change-Id: Id48a2df3acdd97f27d905e2c2be23072f28f196b Reviewed-on: https://go-review.googlesource.com/c/tools/+/241837 Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-11internal/lsp: read files eagerlyHeschi Kreinick
We use file identities pervasively throughout gopls. Prior to this change, the identity is the modification date of an unopened file, or the hash of an opened file. That means that opening a file changes its identity, which causes unnecessary churn in the cache. Unfortunately, there isn't an easy way to fix this. Changing the cache key to something else, such as the modification time, means that we won't unify cache entries if a change is made and then undone. The approach here is to read files eagerly in GetFile, so that we know their hashes immediately. That resolves the churn, but means that we do a ton of file IO at startup. Incidental changes: Remove the FileSystem interface; there was only one implementation and it added a fair amount of cruft. We have many other places that assume os.Stat and such work. Add direct accessors to FileHandle for URI, Kind, and Version. Most uses of (FileHandle).Identity were for stuff that we derive solely from the URI, and this helped me disentangle them. It is a *ton* of churn, though. I can revert it if you want. Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037 Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-03-27internal/lsp: refactor references/rename/implementationsRebecca Stambler
As part of investigating golang/go#38100, I noticed a few things that I wanted to clean up. Mostly, for renames, we were calling qualifiedObjAtProtocolPos twice, so I factored out a shared helper function. I also added an error return for builtins so that callers don't have to check. Change-Id: I28c75c801cbec1611736af931cfa72befd219201 Reviewed-on: https://go-review.googlesource.com/c/tools/+/225777 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rohan Challa <rohan@golang.org>
2020-02-14internal/lsp: check for file URIs on LSP requestsHeschi Kreinick
In general, we expect all URIs to be file:// scheme. Silently ignore requests that come in for other schemes. (In the command-line client we panic since we should never see anything else.) The calling convention for beginFileRequest is odd; see the function comment. Fixes golang/go#33699. Change-Id: Ie721e9a85478f3a12975f6528cfbd28cc7910be8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/219483 Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-02-14internal/span,lsp: disambiguate URIs, DocumentURIs, and pathsHeschi Kreinick
Create a real type for protocol.DocumentURIs. Remove span.NewURI in favor of path/URI-specific constructors. Remove span.Parse's ability to parse URI-based spans, which appears to be totally unused. As a consequence, we no longer mangle non-file URIs to start with file://, and crash all over the place when one is opened. Updates golang/go#33699. Change-Id: Ic7347c9768e38002b4ad9c84471329d0af7d2e05 Reviewed-on: https://go-review.googlesource.com/c/tools/+/219482 Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-01-16internal/lsp: refactor find-references and renameMuir Manders
The main goal is to push the package variant logic from internal/lsp into internal/lsp/source so all users of internal/lsp/source benefit. "references" and "rename" now have top-level source.References() and source.Rename() entry points (as opposed to hanging off source.Identifier()). I expanded objectsAtProtocolPos() to know about implicit objects (type switch and import spec), and to handle *ast.ImportSpec generically. This gets rid of special case handling of *types.PkgName in various places. The biggest practical benefit, though, is that "references" no longer needs to compute the objectpath for every types.Object comparison it does, instead using direct types.Object equality. This speeds up "references" and "rename" a lot. Two other notable improvements that fell out of not using source.Identifier()'s logic: - Finding references on an embedded field now shows references to the field, not the type being embedded. - Finding references on an imported object now works correctly (previously it searched the importing package's dependents rather than the imported package's dependents). Finally, I refactored findIdentifier() to use pathEnclosingObjNode() instead of astutil.PathEnclosingInterval. Now we only need a single call to get the path because pathEnclosingObjNode() has the "try pos || try pos-1" logic built in. Change-Id: I667be9bed6ad83912404b90257c5c1485b3a7025 Reviewed-on: https://go-review.googlesource.com/c/tools/+/211999 Run-TryBot: Muir Manders <muir@mnd.rs> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-01-13internal/lsp: remove view.FindPosInPackage and view.FindMapperInPackageRebecca Stambler
There is no reason for these functions to live on the view. They make more sense as unexported functions in internal/lsp/source. Initially, I had to propagate contexts through a lot of functions in internal/lsp/source, but instead I removed the unused contexts forom snapshot.GetFile. Change-Id: I8323419d0356feb2010091fe8d3ed35e511f801a Reviewed-on: https://go-review.googlesource.com/c/tools/+/214384 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-01-09internal/lsp: propagate errors from find referencesRebecca Stambler
We were ignoring errors in a few cases and returning incorrect errors in the case of the command-line interface (no identifier found when there were just no references). I think it is better for references to return an error than incomplete results. Change-Id: Id90bca58ebdd9f6a910853cb4ac5b6ab6bec57f8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/213817 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-01-07internal/lsp: parallelize initial workspace loadRebecca Stambler
The initial workspace load was happening when a view was created, in serial. It should really just be kicked off in a separate goroutine once we create a new view. Implementing this change required some other significant changes, particularly the additional work being done by the WorkspacePackageIDs method. Some other changes had to be made while debugging. In particular, the modification to the circular dependencies test was a consequence of golang/go#36265. Change-Id: I97586c9574f6c4106172d7983e4c6fad412e6aa1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/212102 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
2019-12-19internal/lsp: eliminate source.File type and move GetFile to snapshotRebecca Stambler
This change eliminates the extra step of calling GetFile on the view and getting the FileHandle from the snapshot. It also eliminiates the redundant source.File type. Follow up changes will clean up the file kind handling, since it still exists on the fileBase type. Change-Id: I635ab8632821b36e062be5151eaab425a5698f60 Reviewed-on: https://go-review.googlesource.com/c/tools/+/211778 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
2019-12-11internal/lsp: fix find-references to search from all package variantsMuir Manders
We previously searched the reverse dependencies of the "widest" package that contained out starting identifier, but if our package has tests then the widest package is the ".test" variant, and it has no reverse dependencies. Fix by searching through all of the packages that contain our starting identifier. For example: -- foo/foo.go -- package foo func Foo() {} -- foo/foo_test.go -- package foo func TestFoo(t *testing.T) {} -- bar/bar.go -- import "foo" func _() { foo.Foo() } We would start searching from the foo.test variant, but we wouldn't search package bar at all because bar does not import foo.test, it imports plain foo. Now we search from both foo and foo.test (you still need search foo.test to find references within foo_test.go). Fixes golang/go#35936. Change-Id: I5fd2f7bb130a421ed6fad92da11179995c99a2cf Reviewed-on: https://go-review.googlesource.com/c/tools/+/210537 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-12-11internal/lsp: add handling for go.mod files in internal/lsp functionsRohan Challa
When we are processing a go.mod file, we are calling go/packages.load when we should not be. It will always return 0 packages since it is not a .go file. This CL adds branching inside each internal/lsp protocol function and also adds a check in snapshot.PackageHandles for the file type and returns an error. This will prevent `go list` from running on go.mod files for now. Updates golang/go#31999 Change-Id: Ic6d0e9b7c81e1f404342b98e10b9c5387adde2ee Reviewed-on: https://go-review.googlesource.com/c/tools/+/210757 Reviewed-by: Rebecca Stambler <rstambler@golang.org> Run-TryBot: Rohan Challa <rohan@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-12-11internal/lsp: check all package variants in find-implementationsMuir Manders
We previously only searched for implementations of the object we found in the "widest" package variant. We instead need to search all variants because each variant is type checked separately, and implementations can be located in packages associated with different variants. For example, say you have: -- foo/foo.go -- package foo type Foo int type Fooer interface { Foo() Foo } -- foo/foo_test.go -- package foo func TestFoo(t *testing.T) {} -- bar/bar.go -- package bar import "foo" type impl struct {} func (impl) Foo() foo.Foo { return 0 } When you run find-implementations on the Fooer interface, we previously would start from the (widest) foo.test's Fooer named type. Unfortunately bar imports foo, not foo.test, so bar.impl does not implement foo.test.Fooer. The specific reason is that bar.impl.Foo returns foo.Foo, whereas foo.test.Fooer.Foo returns foo.test.Foo, which are distinct *types.Named objects. Starting our search instead from foo.Fooer resolves this issue. However, we also need to search from foo.test.Fooer so we match any implementations in foo_test.go. Change-Id: I0b0039c98925410751c8f643c8ebd185340e409f Reviewed-on: https://go-review.googlesource.com/c/tools/+/210459 Run-TryBot: Muir Manders <muir@mnd.rs> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-12-06internal/lsp: respect References.IncludeDeclaration settingRebecca Stambler
Previously, (*IdentifierInfo).References was returning the declaration of the identifier among the reference results. This change alters the behavior of this function to only ever return non-declaration references. Declarations can be accessed through the IdentifierInfo.Declaration field. Fixes golang/go#36007 Change-Id: I91d82b7e6d0d51a2468d3df67f666834d2905250 Reviewed-on: https://go-review.googlesource.com/c/tools/+/210238 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
2019-11-21internal/lsp: reorganize and refactor codeRebecca Stambler
This change cleans up internal/lsp/source/view.go to have a more logical ordering and deletes the view.CheckPackageHandle function. Now, the only way to get a CheckPackageHandle is through a snapshot (so all of the corresponding edits). Also, renamed fuzzy tests to fuzzymatch. Noticed this weird error when debugging - I had golang.org/x/tools/internal/lsp/fuzzy in my module cache and it conflicted with the test version. Change-Id: Ib87836796a8e76e6b6ed1306c2a93e9a5db91cce Reviewed-on: https://go-review.googlesource.com/c/tools/+/208099 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
2019-11-16internal/lsp: fix panic in bestViewRebecca Stambler
Rather than panicking when we have not created any views for the packages, we should show a reasonable error to the user. This change propagates the errors to the user. Updates golang/go#35599 Change-Id: I49789d8ce18e154f111bc3584488f468a129e30c Reviewed-on: https://go-review.googlesource.com/c/tools/+/207344 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Matloob <matloob@golang.org>
2019-10-01internal/lsp: rework snapshots and cache FileHandles per-snapshotRebecca Stambler
This change does not complete the work to handle snapshots correctly, but it does implement the behavior of re-building the snapshot on each file invalidation. It also moves to the approach of caching the FileHandles on the snapshot, rather than in the goFile object, which is now not necessary. Finally, this change shifts the logic of metadata invalidation into the content invalidation step, so there is less logic to decide if we should re-load a package or not. Change-Id: I18387c385fb070da4db1302bf97035ce6328b5c3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/197799 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-09-09internal/lsp: remove the GetToken and GetAST functionsRebecca Stambler
Change-Id: Iddbdde5f47a31da9baab6539cd2b5bd858e7f811 Reviewed-on: https://go-review.googlesource.com/c/tools/+/194057 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-09-05internal/lsp: fix declarations in referencesRebecca Stambler
Fixes golang/go#34087 Change-Id: I854c03cd124fe783f838dc53ee76cec5fffa563b Reviewed-on: https://go-review.googlesource.com/c/tools/+/193379 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-08-29internal/lsp: use protocol.Ranges for source.IdentifierRebecca Stambler
Change-Id: I42cb957e3c1676e2ec7e3f50dd5e3613f3dd9555 Reviewed-on: https://go-review.googlesource.com/c/tools/+/191880 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-08-16internal/lsp: separate out getMapper functionRebecca Stambler
This is a super minimal change that will simplify the diffs for when I actually delete the getMapper function. Change-Id: I16984b344c87b3645fd451668b6ea747c5be12ab Reviewed-on: https://go-review.googlesource.com/c/tools/+/190557 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-08-15internal/lsp: split the telemetry library outIan Cottrell
This is a straight move of some code with no changes. It splits the part of the telemetry code that will become a standalone library from the bit that belongs in the lsp. Change-Id: Icedb6bf1f3711da9251450531729984df6df7787 Reviewed-on: https://go-review.googlesource.com/c/tools/+/190403 Run-TryBot: Ian Cottrell <iancottrell@google.com> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-08-07internal/lsp: minor refactoring for source.IdentifierRebecca Stambler
Change-Id: Ia604f59d6229c2086fe63e73466de7489e1cda2d Reviewed-on: https://go-review.googlesource.com/c/tools/+/189321 Run-TryBot: Rebecca Stambler <rstambler@golang.org> Reviewed-by: Michael Matloob <matloob@golang.org>
2019-07-18internal/lsp: convert logging callsIan Cottrell
Change-Id: I09ee44d0121b7ced001b8195f9fa81b5225cb0c7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/186197 Run-TryBot: Ian Cottrell <iancottrell@google.com> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-07-15internal/lsp: convert all logging calls to the context versionIan Cottrell
Change-Id: I20e3acee4272f05a9f31a7bb4219fc2fe751e6b3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/185988 Run-TryBot: Ian Cottrell <iancottrell@google.com> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-07-03internal/lsp: include declaration for referencesSuzy Mueller
A client can specify "IncludeDeclaration" in its ReferenceParams. When they do so, we want to include the declaration, even if it was not in the scope we searched for references. Additionally, we also return the location of the declaration first in the result array when it is included in the results. Updates golang/go#32572 Change-Id: I12837cd98102ee8d531f0f4bac2fb7bded2564c0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/184723 Run-TryBot: Suzy Mueller <suzmue@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-07-01internal/lsp: find references in test packagesSuzy Mueller
Find references to identifiers in both a package and its test package. Change-Id: I9d9da4aa37c36c448336aed044df79cfd1c903f1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/183990 Run-TryBot: Suzy Mueller <suzmue@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-06-26internal/lsp: support a file belonging to multiple packagesRebecca Stambler
This change adds supports for a package belonging to multiple files. It requires additional packages.Loads for all of the packages to which a file belongs (for example, if a non-test file also belongs to a package's test variant). For now, we re-run go/packages.Load for each file we open, regardless of whether or not we already know about it. This solves the issue of packages randomly belonging to a test or not. Follow-up work needs to be done to support multiple packages in references, rename, and diagnostics. Fixes golang/go#32791 Fixes golang/go#30100 Change-Id: I0a5870a05825fc16cc46d405ef50c775094b0fbb Reviewed-on: https://go-review.googlesource.com/c/tools/+/183628 Run-TryBot: Rebecca Stambler <rstambler@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-06-20internal/lsp: check for conflicts on renameSuzy Mueller
Before renaming a variable, check the package to make sure that this renaming would not result in a conflict that could break the program. All of the implementation is taken from "refactor/rename" with the dependency on "go/loader" removed. Change-Id: Ib0782ec8f247a6df1750f2c8213f69186699ce1a Reviewed-on: https://go-review.googlesource.com/c/tools/+/183257 Run-TryBot: Suzy Mueller <suzmue@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-06-10internal/lsp: add find all referencesSuzy Mueller
This change implements the find all references feature by finding all of the uses and definitions of the identifier within the current package. Testing for references is done using "refs" in the testdata files and marking the references in the package. Change-Id: Ieb44b68608e940df5f65c3052eb9ec974f6fae6c Reviewed-on: https://go-review.googlesource.com/c/tools/+/181122 Run-TryBot: Suzy Mueller <suzmue@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>