diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index e89cdfe5fa..31321a3715 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -475,7 +475,9 @@ function! s:SetUpCompleteopt() " Also, having this option set breaks the plugin. set completeopt-=longest - if g:ycm_add_preview_to_completeopt + if g:ycm_add_preview_to_completeopt ==# 'popup' && exists( '*popup_open' ) + set completeopt+=popup + elseif g:ycm_add_preview_to_completeopt set completeopt+=preview endif endfunction diff --git a/python/ycm/client/completion_request.py b/python/ycm/client/completion_request.py index 50e53ec443..16293ccd7e 100644 --- a/python/ycm/client/completion_request.py +++ b/python/ycm/client/completion_request.py @@ -181,11 +181,25 @@ def _GetCompletionInfoField( completion_data ): def _ConvertCompletionDataToVimData( completion_data ): # See :h complete-items for a description of the dictionary fields. + extra_menu_info = completion_data.get( 'extra_menu_info', '' ) + preview_info = _GetCompletionInfoField( completion_data ) + + # When we are using a popup for the preview_info, it needs to fit on the + # screen alongside the extra_menu_info. Let's use some heuristics. If the + # length of the extra_menu_info is more than, say 50% of screen, truncate it + # and stick it in the preview_info. + if vimsupport.UsingPreviewPopup(): + max_width = max( int( vimsupport.DisplayWidth() / 3 ), 3 ) + extra_menu_info_width = vimsupport.DisplayWidthOfString( extra_menu_info ) + if extra_menu_info_width > max_width: + preview_info = extra_menu_info + '\n\n' + preview_info + extra_menu_info = extra_menu_info[ : ( max_width - 3 ) ] + '...' + return { 'word' : completion_data[ 'insertion_text' ], 'abbr' : completion_data.get( 'menu_text', '' ), - 'menu' : completion_data.get( 'extra_menu_info', '' ), - 'info' : _GetCompletionInfoField( completion_data ), + 'menu' : extra_menu_info, + 'info' : preview_info, 'kind' : ToUnicode( completion_data.get( 'kind', '' ) )[ :1 ].lower(), # Disable Vim filtering. 'equal' : 1, diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 93d5b676b3..292d4db712 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -1304,3 +1304,15 @@ def ScreenPositionForLineColumnInWindow( window, line, column ): WinIDForWindow( window ), line, column ) ) + + +def UsingPreviewPopup(): + return 'popup' in ToUnicode( vim.options[ 'completeopt' ] ).split( ',' ) + + +def DisplayWidth(): + return GetIntValue( '&columns' ) + + +def DisplayWidthOfString( s ): + return GetIntValue( "strdisplaywidth( '{}' )".format( EscapeForVim( s ) ) )