From 1877209270d67da43b5a2aef0d9ef0bd4c3255fb Mon Sep 17 00:00:00 2001 From: Elias Daler Date: Fri, 17 Mar 2023 16:00:16 +0100 Subject: [PATCH 1/5] Add the ability to pass popup_params when creating a custom ycm_hover --- autoload/youcompleteme.vim | 27 ++++++++++++---------- doc/youcompleteme.txt | 20 ++++++++++++++++ test/hover.test.vim | 47 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 12 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index d350f2b50..3efc8bbfb 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -1650,18 +1650,21 @@ if exists( '*popup_atcursor' ) let wrap = 1 endif - let s:cursorhold_popup = popup_atcursor( - \ lines, - \ { - \ 'col': col, - \ 'wrap': wrap, - \ 'padding': [ 0, 1, 0, 1 ], - \ 'moved': 'word', - \ 'maxwidth': &columns, - \ 'close': 'click', - \ 'fixed': 0, - \ } - \ ) + let popup_params = { + \ 'col': col, + \ 'wrap': wrap, + \ 'padding': [ 0, 1, 0, 1 ], + \ 'moved': 'word', + \ 'maxwidth': &columns, + \ 'close': 'click', + \ 'fixed': 0, + \ } + + if has_key( b:ycm_hover, 'popup_params' ) + let popup_params = extend( copy( popup_params ), b:ycm_hover.popup_params ) + endif + + let s:cursorhold_popup = popup_atcursor( lines, popup_params ) call setbufvar( winbufnr( s:cursorhold_popup ), \ '&syntax', \ b:ycm_hover.syntax ) diff --git a/doc/youcompleteme.txt b/doc/youcompleteme.txt index 22a6f18e9..01e01db3b 100644 --- a/doc/youcompleteme.txt +++ b/doc/youcompleteme.txt @@ -3277,6 +3277,7 @@ This buffer-local variable can be set to a dictionary with the following keys: - 'command': The YCM completer subcommand which should be run on hover - 'syntax': The syntax to use (as in 'set syntax=') in the popup window for highlighting. +- 'popup_params': The params passed to a popup window which gets opened. For example, to use C/C++ syntax highlighting in the popup for C-family languages, add something like this to your vimrc: @@ -3289,6 +3290,25 @@ languages, add something like this to your vimrc: \ } augroup END < + +You can also modify the opened popup with 'popup_params' key. +For example, you can limit the popup's maximum width and add a border to it: +> + augroup MyYCMCustom + autocmd! + autocmd FileType c,cpp let b:ycm_hover = { + \ 'command': 'GetDoc', + \ 'syntax': &filetype + \ 'popup_params': { + \ 'maxwidth': 80, + \ 'border': [], + \ 'borderchars': ['─', '│', '─', '│', '┌', '┐', '┘', '└'], + \ }, + \ } + augroup END +< +See |popup_create-arguments| for the list of available popup window options. + Default: "'CursorHold'" ------------------------------------------------------------------------------- diff --git a/test/hover.test.vim b/test/hover.test.vim index 48e8fb6f8..97beb315d 100644 --- a/test/hover.test.vim +++ b/test/hover.test.vim @@ -387,6 +387,53 @@ function! TearDown_Test_Hover_Custom_Command() silent! au! MyYCMCustom endfunction +function! SetUp_Test_Hover_Custom_Popup() + augroup MyYCMCustom + autocmd! + autocmd FileType cpp let b:ycm_hover = { + \ 'command': 'GetDoc', + \ 'syntax': 'cpp', + \ 'popup_params': { + \ 'maxwidth': 10, + \ } + \ } + augroup END +endfunction + +function! Test_Hover_Custom_Popup() + call youcompleteme#test#setup#OpenFile( '/test/testdata/cpp/completion.cc', + \ {} ) + call assert_equal( 'cpp', &filetype ) + call assert_equal( { + \ 'command': 'GetDoc', + \ 'syntax': 'cpp', + \ 'popup_params': { 'maxwidth': 10 } + \ }, b:ycm_hover ) + + call setpos( '.', [ 0, 6, 8 ] ) + doautocmd CursorHold + call assert_equal( { + \ 'command': 'GetDoc', + \ 'syntax': 'cpp', + \ 'popup_params': { 'maxwidth': 10 } + \ }, b:ycm_hover ) + + call s:CheckPopupVisibleScreenPos( { 'row': 7, 'col': 9 }, + \ s:cpp_lifetime.GetDoc, + \ 'cpp' ) + " Check that popup's width is limited by maxwidth being passed + call s:CheckPopupNotVisibleScreenPos( { 'row': 7, 'col': 20 }, v:false ) + + normal \D + call s:CheckPopupNotVisibleScreenPos( { 'row': 7, 'col': 9 }, v:false ) + + call popup_clear() +endfunction + +function! TearDown_Test_Hover_Custom_Popup() + silent! au! MyYCMCustom +endfunction + function! Test_Long_Single_Line() call youcompleteme#test#setup#OpenFile( '/test/testdata/python/doc.py', {} ) call cursor( [ 37, 3 ] ) From a31e9aca42824dab052d142ee381d5ff75d0e379 Mon Sep 17 00:00:00 2001 From: Elias Daler Date: Fri, 17 Mar 2023 16:16:53 +0100 Subject: [PATCH 2/5] Formatting fixes, limit popup_params scope --- autoload/youcompleteme.vim | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 3efc8bbfb..9b3f30da7 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -1650,21 +1650,21 @@ if exists( '*popup_atcursor' ) let wrap = 1 endif - let popup_params = { - \ 'col': col, - \ 'wrap': wrap, - \ 'padding': [ 0, 1, 0, 1 ], - \ 'moved': 'word', - \ 'maxwidth': &columns, - \ 'close': 'click', - \ 'fixed': 0, - \ } + let s:popup_params = { + \ 'col': col, + \ 'wrap': wrap, + \ 'padding': [ 0, 1, 0, 1 ], + \ 'moved': 'word', + \ 'maxwidth': &columns, + \ 'close': 'click', + \ 'fixed': 0, + \ } if has_key( b:ycm_hover, 'popup_params' ) - let popup_params = extend( copy( popup_params ), b:ycm_hover.popup_params ) + let s:popup_params = extend( copy( s:popup_params ), b:ycm_hover.popup_params ) endif - let s:cursorhold_popup = popup_atcursor( lines, popup_params ) + let s:cursorhold_popup = popup_atcursor( lines, s:popup_params ) call setbufvar( winbufnr( s:cursorhold_popup ), \ '&syntax', \ b:ycm_hover.syntax ) From 1852bd7c077fdca5e700f0656ace16fe792e64e9 Mon Sep 17 00:00:00 2001 From: Elias Daler Date: Sat, 18 Mar 2023 01:52:09 +0100 Subject: [PATCH 3/5] Code review fixes --- README.md | 20 ++++++++++++++++++++ autoload/youcompleteme.vim | 9 +++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8d23f79a8..c8622d69e 100644 --- a/README.md +++ b/README.md @@ -3019,6 +3019,7 @@ buffer-local variable can be set to a dictionary with the following keys: * `command`: The YCM completer subcommand which should be run on hover * `syntax`: The syntax to use (as in `set syntax=`) in the popup window for highlighting. +* `popup_params`: The params passed to a popup window which gets opened. For example, to use C/C++ syntax highlighting in the popup for C-family languages, add something like this to your vimrc: @@ -3033,6 +3034,25 @@ augroup MyYCMCustom augroup END ``` +You can also modify the opened popup with `popup_params` key. +For example, you can limit the popup's maximum width and add a border to it: + +```viml +augroup MyYCMCustom + autocmd! + autocmd FileType c,cpp let b:ycm_hover = { + \ 'command': 'GetDoc', + \ 'syntax': &filetype + \ 'popup_params': { + \ 'maxwidth': 80, + \ 'border': [], + \ 'borderchars': ['─', '│', '─', '│', '┌', '┐', '┘', '└'], + \ }, + \ } +augroup END +``` +See `popup_create-arguments` for the list of available popup window options. + Default: `'CursorHold'` ### The `g:ycm_filter_diagnostics` option diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 9b3f30da7..ce10902e1 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -1650,9 +1650,9 @@ if exists( '*popup_atcursor' ) let wrap = 1 endif - let s:popup_params = { + let popup_params = { \ 'col': col, - \ 'wrap': wrap, + \ 'wrap': 1, \ 'padding': [ 0, 1, 0, 1 ], \ 'moved': 'word', \ 'maxwidth': &columns, @@ -1661,10 +1661,11 @@ if exists( '*popup_atcursor' ) \ } if has_key( b:ycm_hover, 'popup_params' ) - let s:popup_params = extend( copy( s:popup_params ), b:ycm_hover.popup_params ) + let popup_params = extend( copy( popup_params ), + \ b:ycm_hover.popup_params ) endif - let s:cursorhold_popup = popup_atcursor( lines, s:popup_params ) + let s:cursorhold_popup = popup_atcursor( lines, popup_params ) call setbufvar( winbufnr( s:cursorhold_popup ), \ '&syntax', \ b:ycm_hover.syntax ) From 6ba9842e115723a029d1e29b1a13f467bfe5b760 Mon Sep 17 00:00:00 2001 From: Elias Daler Date: Sat, 18 Mar 2023 01:56:02 +0100 Subject: [PATCH 4/5] Update docs --- README.md | 2 +- doc/youcompleteme.txt | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c8622d69e..a875af050 100644 --- a/README.md +++ b/README.md @@ -3051,7 +3051,7 @@ augroup MyYCMCustom \ } augroup END ``` -See `popup_create-arguments` for the list of available popup window options. +See `:help popup_create-arguments` for the list of available popup window options. Default: `'CursorHold'` diff --git a/doc/youcompleteme.txt b/doc/youcompleteme.txt index 01e01db3b..6e3c4b4cd 100644 --- a/doc/youcompleteme.txt +++ b/doc/youcompleteme.txt @@ -3290,9 +3290,10 @@ languages, add something like this to your vimrc: \ } augroup END < +You can also modify the opened popup with 'popup_params' key. For example, you +can limit the popup's maximum width and add a border to it: + -You can also modify the opened popup with 'popup_params' key. -For example, you can limit the popup's maximum width and add a border to it: > augroup MyYCMCustom autocmd! @@ -3307,7 +3308,8 @@ For example, you can limit the popup's maximum width and add a border to it: \ } augroup END < -See |popup_create-arguments| for the list of available popup window options. +See ':help popup_create-arguments' for the list of available popup window +options. Default: "'CursorHold'" From a4a869cd7a344c84bd48a77dbde24879e77977ad Mon Sep 17 00:00:00 2001 From: Elias Daler Date: Mon, 20 Mar 2023 11:56:16 +0100 Subject: [PATCH 5/5] Fix wrap --- autoload/youcompleteme.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index ce10902e1..cd2ebbb71 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -1652,7 +1652,7 @@ if exists( '*popup_atcursor' ) let popup_params = { \ 'col': col, - \ 'wrap': 1, + \ 'wrap': wrap, \ 'padding': [ 0, 1, 0, 1 ], \ 'moved': 'word', \ 'maxwidth': &columns,