From 8ce8abacac786ea4b55475ca732f316dd8ff0ee2 Mon Sep 17 00:00:00 2001 From: Sebastian Imlay Date: Thu, 11 Jul 2024 23:49:39 -0400 Subject: [PATCH] Add expand macro support for rust-analyzer lsp extension --- settings/rust-analyzer.vim | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/settings/rust-analyzer.vim b/settings/rust-analyzer.vim index 5e52e230..1f332c16 100644 --- a/settings/rust-analyzer.vim +++ b/settings/rust-analyzer.vim @@ -37,6 +37,9 @@ function! s:on_lsp_buffer_enabled() abort command! -buffer LspRustOpenDocs call open_docs() nnoremap (lsp-rust-open-docs) :call open_docs() + + command! -buffer LspRustExpandMacro call expand_macro() + nnoremap (lsp-rust-expand-macro) :call expand_macro() endfunction function! s:open_cargo_toml() abort @@ -159,6 +162,48 @@ function! s:on_find_matching_brace(x) abort endif endfunction +function! s:expand_macro() abort + echo 'Expanding Macros' + call lsp#callbag#pipe( + \ lsp#request('rust-analyzer', { + \ 'method': 'rust-analyzer/expandMacro', + \ 'params': { + \ 'textDocument': lsp#get_text_document_identifier(), + \ 'position': lsp#get_position(), + \ }, + \ }), + \ lsp#callbag#subscribe({ + \ 'next': {x->s:on_expand_macro(x)}, + \ 'error': {e->lsp_settings#utils#error(e)}, + \ }) + \ ) +endfunction + +function! s:on_expand_macro(x) abort + if lsp#client#is_error(a:x['response']) | echom 'Failed to expand macro' | endif + let l:contents = a:x['response']['result']['expansion'] + + let l:lines = lsp#utils#_split_by_eol(l:contents) + let l:view = winsaveview() + let l:alternate=@# + silent! pclose + sp LspRustExpandMacro + execute 'resize '.min([len(l:lines), &previewheight]) + set previewwindow + setlocal conceallevel=2 + setlocal bufhidden=hide + setlocal nobuflisted + setlocal buftype=nofile + 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 \p" + call winrestview(l:view) + let @#=l:alternate +endfunction + function! s:open_docs() abort echo 'Opening docs...' call lsp#callbag#pipe(