From 453042d7e4dc80a2f8a69d3a36f63bda5d8b4976 Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Wed, 5 Jan 2022 17:25:48 -0800 Subject: [PATCH] Format Hover Pop-up Window (#1257) * Format Hover Pop-up Window 1) Applies Vim's gq command to each individual line of content returned by language server. 2) Fixes scrolling to last line of popup window * minor * Add help entry for popup formatting --- autoload/lsp/internal/document_hover/under_cursor.vim | 10 +++++++++- autoload/vital/_lsp/VS/Vim/Window.vim | 2 +- doc/vim-lsp.txt | 9 +++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/autoload/lsp/internal/document_hover/under_cursor.vim b/autoload/lsp/internal/document_hover/under_cursor.vim index 2b6ff10a..d3feb1c0 100644 --- a/autoload/lsp/internal/document_hover/under_cursor.vim +++ b/autoload/lsp/internal/document_hover/under_cursor.vim @@ -89,7 +89,7 @@ function! s:show_hover(ui, server_name, request, response) abort call s:show_floating_window(a:server_name, a:request, a:response) else call s:show_preview_window(a:server_name, a:request, a:response) - endif + endif endfunction function! s:show_preview_window(server_name, request, response) abort @@ -161,6 +161,14 @@ function! s:show_floating_window(server_name, request, response) abort \ 'border': v:true, \ }) call s:Window.do(l:doc_win.get_winid(), { -> s:Markdown.apply() }) + + " Format contents to fit window + call setbufvar(l:doc_win.get_bufnr(), '&textwidth', l:size.width) + call s:Window.do(l:doc_win.get_winid(), { -> s:format_window() }) +endfunction + +function! s:format_window() abort + global/^/normal! gqgq endfunction function! s:get_contents(contents) abort diff --git a/autoload/vital/_lsp/VS/Vim/Window.vim b/autoload/vital/_lsp/VS/Vim/Window.vim index c373af98..7b444454 100644 --- a/autoload/vital/_lsp/VS/Vim/Window.vim +++ b/autoload/vital/_lsp/VS/Vim/Window.vim @@ -106,7 +106,7 @@ function! s:scroll(winid, topline) abort function! l:ctx.callback(winid, topline) abort let l:wininfo = s:info(a:winid) let l:topline = a:topline - let l:topline = min([l:topline, line('$') - l:wininfo.height + 1]) + let l:topline = min([l:topline, line('$') - l:wininfo.height + 3]) let l:topline = max([l:topline, 1]) if l:topline == l:wininfo.topline diff --git a/doc/vim-lsp.txt b/doc/vim-lsp.txt index 0655594b..a2965213 100644 --- a/doc/vim-lsp.txt +++ b/doc/vim-lsp.txt @@ -155,6 +155,7 @@ CONTENTS *vim-lsp-contents* Snippets |vim-lsp-snippets| Folding |vim-lsp-folding| Semantic highlighting |vim-lsp-semantic| + Popup Formatting |vim-lsp-popup-format| License |vim-lsp-license| Maintainers |vim-lsp-maintainers| @@ -1998,6 +1999,14 @@ want function calls to still use the |Label| group, but use |Identifier| for \ } \ }) < +============================================================================== +Popup Formatting *vim-lsp-popup-format* + +Popup windows use the |gq| operator for formatting content to the window. + +For customization, see +|formatprg|. + ============================================================================== License *vim-lsp-license*