-
-
Notifications
You must be signed in to change notification settings - Fork 897
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
extract signature for rust-analyzer #1740
Conversation
@flodiebold can you help me out ? |
I don't really know what this does or why it's needed 🤔 |
The problem these lines of code want to solve is that hover in lsp-mode hardly show useful signature with rust-analyzer. Yes I know where this struct/function come from, please just show me the definition. The value in response from rust-analyzer is like:
or:
On its first line it often shows where this struct/function come from and the format is casual and different between versions of rust-analyzer. Yes In lsp-mode, This PR extract the first two markdown code block from the response value and prefer the second one. Yes his heuristic is wield and I hope anyone could come out a better solution. |
This seems very coupled to the exact layout of the hover response though, which could easily change. Maybe it would be possible to add a setting to RA that makes it only return the signature -- you're not using the full hover response in another place with this, right? |
FWIW if the server uses |
Well another problem: |
Can you elaborate? |
If you query the hover request when rust-analyzer is processing some new changes, it will return ridiculous result like:
And then a large range of source code is shadowed by saved bounds result. |
What's the status of this PR? It needs a rebase as there are some conflicts. I think it's a good idea to try to extract a Rust signature line for eldoc, but unfortunately we need to rely on some heuristics. Do you know if there's a better way to do this?
Yes, that's a bad thing. I think LSP should separate signature info from documentation so that they could be shown separately. |
The alternative approach is to get N symbols from the output by default and let users on their own to customize that with a function(which you already provided). |
I agree with the maintainer that there need be a better way. This PR is what I can do so far. |
@scturtle I copied your PR to my lsp-rust.el file, and restart Emacs, but nothing changed, is there anything I'm missing?
Test with rust-analyzer ca31b1d |
@jiacai2050 Try deleting |
@scturtle No luck. |
After set
Any ideas? |
@jiacai2050 Try replacing |
did something change? this stopped working for me :\ |
Would you mind if we upstreamed this in Doom? That looks like a very very useful snippet! |
I found a easy way to do this by adding this line in config.el:
It clears the cached bounds after every call to |
Now that this showed up on Reddit, and Rust-Analyzer is the official and preferred LS for Rust (since 2022), maybe it would be a good idea to revisit this PR? |
Just in case anyone need it, here is a revised version that fix issue with (with-eval-after-load 'lsp-rust
;; do not cache the shitty result from rust-analyzer
(advice-add #'lsp-eldoc-function :after (lambda (&rest _) (setq lsp--hover-saved-bounds nil)))
;; extract and show short signature for rust-analyzer
(cl-defmethod lsp-clients-extract-signature-on-hover (contents (_server-id (eql rust-analyzer)))
(let* ((value (if lsp-use-plists (plist-get contents :value) (gethash "value" contents)))
(groups (--partition-by (s-blank? it) (s-lines (s-trim value))))
(mod-group (cond ((s-equals? "```rust" (car (-fifth-item groups))) (-third-item groups))
((s-equals? "```rust" (car (-third-item groups))) (-first-item groups))
(t nil)))
(cmt (if (null mod-group) "" (concat " // " (cadr mod-group))))
(sig-group (cond ((s-equals? "```rust" (car (-fifth-item groups))) (-fifth-item groups))
((s-equals? "```rust" (car (-third-item groups))) (-third-item groups))
(t (-first-item groups))))
(sig (->> sig-group
(--drop-while (s-starts-with? "```" it))
(--take-while (not (s-equals? "```" it)))
(--map (s-replace-regexp "//.*" "" it))
(--map (s-trim it))
(s-join " "))))
(lsp--render-element (concat "```rust\n" sig cmt "\n```"))))
) |
This last function should be part of the mode? We can see how popular it is with the emojis and I have seen many user being confused by getting only the alignment from the type instead of the type in their mode-line when using Rust and lsp. |
This fixes a case where a type signature would be broken up over multiple lines, and we only display the first of these. Fixes: emacs-lsp#151 Related: emacs-lsp/lsp-mode#4362 Related: emacs-lsp/lsp-mode#1740
Add specific
lsp-clients-extract-signature-on-hover
function for rust-analyzer.