I was taking a quick look at https://github.com/clojure-lsp/clojure-lsp/issues/2302 (Static analysis for a var should include the defined value). Basically the filer would like to display the text for a definition without actually using Jump to Definition. "textDocument/definition" already returns the URI and the range for a definition. The editor could call textDocument/definition, pull the range out of the text in the URI, and show it to the user in a popup. Alternatively, clojure-lsp could either 1) open the file and return the appropriate part, or 2) somehow store the text in the db during parsing. Option #2 would cause an issue with memory on large codebases and would involve some managing/syncing of it (perhaps already done by kondo?). #1 would kinda be OK, Iām not sure if this is better done at the client. Anyway, given textdocument/definition's existence, it seems like this issue might be something better done by the editor. Am I missing something?
I only now noticed that there's already a PR for this. Doh! (As the filer requested) it only does def and reuses hover. Adding defonce should be easy enough
1. correct, agree 2. yep, I do think the feature itself sounds a little bit too much IMO, usually find a definition and go back to where you are is pretty quickly and every editor has bindings for that, and in the end there is nothing in the protocol that support that "find the function/var body" so we would need custom things in each editor + server for this IIUC
OK, I'll let this one go for now
yes, agree, I feel like server provides everything it needs for this, or we consider present this in hover LSP feature which I'm not sure is enough for user
I was thinking about this some more. textDocument/definition actually gives the client the "definition". The filer wants the implementation, so (def my-value 10), they want 10.
I'm pretty sure I remember intellj (or some other Java IDE) doing this.
When we're talking about a def, or a defn, or other cases it's straightfoward - get the AST and move right or whatever. For destructured variables though, it's more complex.
In LSP there is text document/implementation as well, but it's used for implementations of a symbol, like a defrecord of a protocol etc. But yes, it's hard, and even defs could have complex logic to return a value or lots of fn calling
Yeah, I went down a rabbit hole looking at queries/find-implementations today before I realized that it wasn't for vars. š š
I think it might be possible to do just the simple case. For instance, db/version would be simple. And maybe for something complex, showing the first few lines would be good enough to jog the user's memory. So db/db* would be only the text of (atom initial-db).
There's a couple things I'm concerned about:
1. I'm worried about destructuring. What would we give back for my-value in [{:keys [my-value]} my-map]? Nothing? The whole destructuring? Create a synthetic (:my-value my-map)to show? Just show my-map? More worrisome, what if it is a nested destructuring? OTOH, correct me if I'm wrong, you can only destructure in local blocks (eg let, defn arg list, etc...) so even if we don't get something super useful or can't do it at all, it might not be horrible because it would be nearby where the user is reading.
2. I wonder how useful this is. I found it useful back in the day with Java code, but maybe there are other tools that will do a different/better job for Clojure? Or maybe just naming functions more appropriately would get the user mostly there?
I figure I might look a little deeper into the rabbit hole, but if people with more actual Clojure experience have opinions, I'd be all ears. š°