microsoft / language-server-protocol

Defines a common protocol for language servers.
https://microsoft.github.io/language-server-protocol/
Creative Commons Attribution 4.0 International
10.91k stars 764 forks source link

support for generalized "references" queries #1911

Open adonovan opened 3 months ago

adonovan commented 3 months ago

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:

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?

HighCommander4 commented 6 days ago

A related enhancement proposal to the references request is https://github.com/microsoft/language-server-protocol/issues/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.