Skip to content
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

Support expandMacro for rust-analyzer lsp #761

Merged
merged 1 commit into from
Nov 26, 2024

Conversation

simlay
Copy link
Contributor

@simlay simlay commented Jul 12, 2024

There are a bunch of LSP extensions rust-analyzer has. This project implements a few of them. One that it doesn't is rust-analyzer/expandMacro.

Full disclosure, I'm a novice at vimL. I copied the on_rust_analyzer_status function and modified it for the response handler. I won't feel bad about a strong constructive critique.

Below is an example of what my vim looks like when I expand the println! macro. I've got a powerbar plugin at the bottom. The split for the expanded macro yields source that frequently has more macros. Unfortunately, there's no file for that source so I'm not sure it's macro-expandable? I'd love to be able to expand each macro down the chain but my vimscript/vimL is weak.

image

setlocal bufhidden=hide
setlocal nobuflisted
setlocal buftype=nofile
setlocal ft=rust
Copy link
Contributor

@heavenshell heavenshell Jul 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, Thank you for great work!
I've try this and love it. But I have some feed backs.

I use g:lsp_diagnostics_virtual_text_enabled = true.
Execute :LspRustExpandMacro and move to preview window.
Shows a lot of errors and bit a annoying 😰

Kapture.2024-07-12.22.44.43.mp4

I'm not sure my suggestion is right or not, but set no filetype and enable syntax for preview looks good to me.

diff --git a/settings/rust-analyzer.vim b/settings/rust-analyzer.vim
index b71d6eb..dde8fd7 100644
--- a/settings/rust-analyzer.vim
+++ b/settings/rust-analyzer.vim
@@ -194,10 +194,13 @@ function! s:on_expand_macro(x) abort
     setlocal bufhidden=hide
     setlocal nobuflisted
     setlocal buftype=nofile
-    setlocal ft=rust
     setlocal noswapfile
     %d
     call setline(1, l:lines)
+
+    let l:win_id = win_getid()
+    call win_execute(l:win_id, $'setlocal syntax=rust')
+
     execute "normal \<c-w>p"
     call winrestview(l:view)
     let @#=l:alternate
Kapture.2024-07-12.22.42.22.mp4

@mattn Do you have any good solutions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice! I don't have the virtual diagnostics enabled but definitely see how it's rough. I think the syntax set to rust works. I've added those changes.

@simlay
Copy link
Contributor Author

simlay commented Jul 12, 2024

I was curios why the expansions always yield $crate but that crate is different depending on where the macro came from. $crate from println! is std, but with log::info!/trace::info!, it's log or trace. In any case, I don't think the client is suppose to swap those. rust-lang/rust-analyzer#7532 is the issue.

Poking around, some folks may want the macro to expand inline (rust-lang/rust-analyzer#11888 (comment)). That may be a feature for the future as it'd help with being able to manually recurse through the expansions. Given that the macro expansions have $crate in them, I think putting them in a separate/preview split is best for now.

@simlay simlay force-pushed the rust-analyzer-expand-macros branch from e9a31a8 to 131c63c Compare July 12, 2024 14:55
@simlay
Copy link
Contributor Author

simlay commented Jul 12, 2024

I was about to author a Rust Analyzer LSP Extensions tracking issue (really just a matrix of extensions) when I realized it should probably just go in the notes section of the README along with an example of a .vim-lsp-settings/settings.json. Thoughts?

@simlay
Copy link
Contributor Author

simlay commented Sep 8, 2024

@mattn Ping. Any requests on this PR? I find the feature pretty useful.

@mattn mattn merged commit 8ce8aba into mattn:master Nov 26, 2024
@mattn
Copy link
Owner

mattn commented Nov 26, 2024

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants