Repository navigation
Description
The LSP's references method takes the client editor's selected range, and returns a set of locations--of all the identifiers that refer to the same symbol as the selected identifier. A large variety of useful queries can be expressed as minor variations on this theme. For example:
- show all implicit references to the selected identifier. For example, in Go,
Point2D{1, 2}is shorthand forPoint2D{X: 1, Y: 2}, and it might be useful to highlight it when finding references to X or Y. - show all free variables of the selected text. These are the variables that would need to become parameters in an "extract function" refactoring, but it's often useful to enumerate them even when just reading.
- show all statements that use the selected variable as an l-value. For example,
x = 1,x.a[i] = 2andp = &xall use x as an l-value, whereasy = xandprint(x)use it as an r-value. When trying to comprehend aliasing and mutation, this difference is crucial. - show all function declarations (including anonymous lambdas) that satisfy a particular function type (and vice versa).
- show all argument expressions that assign the selected parameter. For example, a query on y
func f(x, y int)might report the expression456in the callf(123, 456). - show all locations that construct a variable of the selected type (even as part of a larger aggregate).
- show all supertypes or subtypes of a selected type T. (The textDocument/implementation feature reports supertypes if T is concrete and subtypes if T is an interface, but gives no way to request superinterfaces of an interface or subtypes of a Java abstract class.)
- show all expressions that convert a value to the selected type. For example,
writer = filemight implicitly convert an*os.Fileto anio.Writer.
These examples use Go, but I'm sure you can think of other examples in your second-favorite language. ;-)
I'm not going to prescribe any particular implementation, but I think it would be very useful if the LSP allowed a server to respond to a CodeAction query with a command that tells the client: the result of this command should be displayed with a similar user interface to an ordinary 'references' query.
One subtlety: some of these queries produce slightly more information than a set of locations, especially when describing implicit operations with no obvious syntax; they need an annotation too. For example, the first query might annotate the location of Point2D{1, 2} with "implicit reference to Point.X", or x.f with "shorthand for x.A.B.C.f"; the second query might annotate each free variable with its type information; and the last one might annotate the assignment with "RHS has type *os.File". Thus the result type would need to be a list of (Location, string) pairs.
What do you think?
Activity
HighCommander4 commentedon Jul 2, 2024
A related enhancement proposal to the
referencesrequest is #396, with filtering read vs. write (rvalue vs. lvalue) references being a use case discussed there too.I like the approach you describe though, in that it avoids the need to build anything specific (like a notion of read vs. write references) into the protocol -- that customization can take the form of the server offering whatever variations of the request are relevant as different code actions.
ratzdi commentedon Nov 25, 2025
I would like to make a proposal in #2202 regarding the idea described by Alan Donovan (@adonovan), which I mostly understand and which seems sustainable.
Regarding the point with filtering for function type declarations in Alan's idea, I don't understand exactly what is meant. This might be very Go specific? I am not familiar with the this language.
adonovan commentedon Nov 25, 2025
The point of this feature is that each language server would be able to offer a whole menu of features via Code Actions whose effect is to search the workspace in some (perhaps highly language-specific) way and present the results using the usual References UI elements.
ratzdi commentedon Dec 7, 2025
Hi Alan Donovan (@adonovan), I like your idea for refining the reference search. However, I find using the Code-Action construct somewhat complicated compared to using the existing reference interface, as I demonstrated in my proposal. To allow for more flexibility, as you suggest, I would omit the fixed reference types in the proposal and let the server provide them to the client as a string array via Reference-Kind capabilities. What do you think? Perhaps you could also explain why you're considering using Code-Action?
adonovan commentedon Dec 7, 2025
The client should not need to know what reference-kinds of query are available.
Code Action is currently the only LSP mechanism that allows the server to arbitrarily extend the feature set of an IDE without the need for code changes in the client. The server can decide that any location in the source has an associated code action; and when the client executes the action, a command, the server decides what happens: arbitrary side effects, edits applied to client files, navigation to a specific location, etc. All clients already have some kind of user interface for displaying the available code actions for the current selection, and it is driven purely by data returned by the server.
So the simplest approach, it seems to me, is for the server to indicate in the Command that it produces a result similar in form to the existing References query.
ratzdi commentedon Dec 8, 2025
Hi Alan Donovan (@adonovan), thanks for the clarifying answer. I realize that this issue only slightly overlaps with mine. I'll post my request in a separate one and would appreciate your comments.
nemethf commentedon Jan 18, 2026
CodeActionTag is a new feature in the upcoming LSP 3.18 specification. I think a new tag named something like
returnLocationscould indicate what Alan Donovan (@adonovan) originally wanted. OrreturnLocationStringPairsfor his more complicated examples.kiet15101993-commits commentedon Jan 19, 2026
#2218 #
kiet15101993-commits commentedon Jan 19, 2026
/#1911 (comment)
timon-ul commentedon May 20, 2026
I ended up here with the interest (as others) of introducing a notion of read/write access to a variable. I personally liked the proposal in #396 more for this use case, but since you commented there and also used it as an example for this enhancement I feel like I should start the discussion here (also since Nathan Ridge (@HighCommander4) commented here with his support), before returning to the original issue.
Overall I think it is valuable to have these generic code actions that return in the same format as a reference lookup, but I think their main value comes from returning different elements than the base call. My reason for this is that if you just return the same element but with some additional annotations it leaves you with multiple ways to call the same base information just with a different flavour of details and to me this sounds like making the base functionality obsolete and more confusing for a user what to use. I think asking for read/write access information is a widely useful feature that in almost every case of a reference search yields value to its user, so hiding it behind a code action alternative, which returns the exact same elements, just with this annotation, feels like we are hiding valuable information. On top I would expect users who know of this functionality to always prefer this annotation over the bare one right now. To make it even more awkward this leaves the user to do a regular reference call on functions but then having to do a code action on variables, kinda annoying imo.
Just to reiterate, I think if I wanted a variation, like only write access and no read access, then having that as this code action seems valuable, but if it is just some annotation added to the regular information I think the other enhancement is where this should be added.
Anyway I am interested in hearing your take on this.