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

Truncate extra_menu_info when completeopt is in popup mode #3682

Merged
merged 13 commits into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2704,16 +2704,24 @@ let g:ycm_csharp_insert_namespace_expr = ''

When this option is set to `1`, YCM will add the `preview` string to Vim's
`completeopt` option (see `:h completeopt`). If your `completeopt` option
already has `preview` set, there will be no effect. You can see the current
state of your `completeopt` setting with `:set completeopt?` (yes, the question
mark is important).
already has `preview` set, there will be no effect. Alternatively, when set to
`popup` and your version of Vim supports popup windows (see `:help popup`), the
`popup` string will be used instead. You can see the current state of your
`completeopt` setting with `:set completeopt?` (yes, the question mark is
important).

When `preview` is present in `completeopt`, YCM will use the `preview` window at
the top of the file to store detailed information about the current completion
candidate (but only if the candidate came from the semantic engine). For
instance, it would show the full function prototype and all the function
overloads in the window if the current completion is a function name.

When `popup` is present in `completeopt`, YCM will instead use a `popup`
window to the side of the completion popup for storing detailed information
about the current completion candidate. In addition, YCM may truncate the
detailed completion information in order to give the popup sufficient room
to display that detailed information.

Default: `0`

```viml
Expand Down
4 changes: 3 additions & 1 deletion autoload/youcompleteme.vim
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,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
Expand Down
14 changes: 11 additions & 3 deletions doc/youcompleteme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2936,16 +2936,24 @@ The *g:ycm_add_preview_to_completeopt* option

When this option is set to '1', YCM will add the 'preview' string to Vim's
'completeopt' option (see ':h completeopt'). If your 'completeopt' option
already has 'preview' set, there will be no effect. You can see the current
state of your 'completeopt' setting with ':set completeopt?' (yes, the question
mark is important).
already has 'preview' set, there will be no effect. Alternatively, when set to
'popup' and your version of Vim supports popup windows (see ':help popup'), the
'popup' string will be used instead. You can see the current state of your
'completeopt' setting with ':set completeopt?' (yes, the question mark is
important).

When 'preview' is present in 'completeopt', YCM will use the 'preview' window
at the top of the file to store detailed information about the current
completion candidate (but only if the candidate came from the semantic engine).
For instance, it would show the full function prototype and all the function
overloads in the window if the current completion is a function name.

When 'popup' is present in 'completeopt', YCM will instead use a 'popup' window
to the side of the completion popup for storing detailed information about the
current completion candidate. In addition, YCM may truncate the detailed
completion information in order to give the popup sufficient room to display
that detailed information.

Default: '0'
>
let g:ycm_add_preview_to_completeopt = 0
Expand Down
19 changes: 17 additions & 2 deletions python/ycm/client/completion_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,26 @@ 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, 1/3 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:
if not preview_info.startswith( extra_menu_info ):
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,
Expand Down
105 changes: 104 additions & 1 deletion python/ycm/tests/client/completion_request_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import json
from hamcrest import assert_that, equal_to
from ycm.tests.conftest import UserOptions
from ycm.tests.test_utils import MockVimModule
vim_mock = MockVimModule()

Expand All @@ -32,7 +33,7 @@ def _Check( self, completion_data, expected_vim_data ):
completion_data )

try:
assert_that( expected_vim_data, equal_to( vim_data ) )
assert_that( vim_data, equal_to( expected_vim_data ) )
except Exception:
print( "Expected:\n'{}'\nwhen parsing:\n'{}'\nBut found:\n'{}'".format(
expected_vim_data,
Expand Down Expand Up @@ -213,3 +214,105 @@ def EmptyInsertionText_test( self ):
'empty' : 1,
'user_data': json.dumps( extra_data ),
} )


def TruncateForPopup_test( self, *args ):
with UserOptions( { '&columns': 60, '&completeopt': b'popup,menuone' } ):
extra_data = {
'doc_string': 'DOC STRING',
}
self._Check( {
'insertion_text': '',
'menu_text': 'MENU TEXT',
'extra_menu_info': 'ESPECIALLY LONG EXTRA MENU INFO LOREM IPSUM DOLOR',
'kind': 'K',
'detailed_info': 'DETAILED INFO',
'extra_data': extra_data,
}, {
'word' : '',
'abbr' : 'MENU TEXT',
'menu' : 'ESPECIALLY LONG E...',
'kind' : 'k',
'info' : 'ESPECIALLY LONG EXTRA MENU INFO LOREM IPSUM DOLOR\n\n' +
'DETAILED INFO\nDOC STRING',
'equal' : 1,
'dup' : 1,
'empty' : 1,
'user_data': json.dumps( extra_data ),
} )


def OnlyTruncateForPopupIfNecessary_test( self, *args ):
with UserOptions( { '&columns': 60, '&completeopt': b'popup,menuone' } ):
extra_data = {
'doc_string': 'DOC STRING',
}
self._Check( {
'insertion_text': '',
'menu_text': 'MENU TEXT',
'extra_menu_info': 'EXTRA MENU INFO',
'kind': 'K',
'detailed_info': 'DETAILED INFO',
'extra_data': extra_data,
}, {
'word' : '',
'abbr' : 'MENU TEXT',
'menu' : 'EXTRA MENU INFO',
'kind' : 'k',
'info' : 'DETAILED INFO\nDOC STRING',
'equal' : 1,
'dup' : 1,
'empty' : 1,
'user_data': json.dumps( extra_data ),
} )


def DontTruncateIfNotPopup_test( self, *args ):
with UserOptions( { '&columns': 60, '&completeopt': b'preview,menuone' } ):
extra_data = {
'doc_string': 'DOC STRING',
}
self._Check( {
'insertion_text': '',
'menu_text': 'MENU TEXT',
'extra_menu_info': 'ESPECIALLY LONG EXTRA MENU INFO LOREM IPSUM DOLOR',
'kind': 'K',
'detailed_info': 'DETAILED INFO',
'extra_data': extra_data,
}, {
'word' : '',
'abbr' : 'MENU TEXT',
'menu' : 'ESPECIALLY LONG EXTRA MENU INFO LOREM IPSUM DOLOR',
'kind' : 'k',
'info' : 'DETAILED INFO\nDOC STRING',
'equal' : 1,
'dup' : 1,
'empty' : 1,
'user_data': json.dumps( extra_data ),
} )


def TruncateForPopupWithoutDuplication_test( self, *args ):
with UserOptions( { '&columns': 60, '&completeopt': b'popup,menuone' } ):
extra_data = {
'doc_string': 'DOC STRING',
}
self._Check( {
'insertion_text': '',
'menu_text': 'MENU TEXT',
'extra_menu_info': 'ESPECIALLY LONG METHOD SIGNATURE LOREM IPSUM',
'kind': 'K',
'detailed_info': 'ESPECIALLY LONG METHOD SIGNATURE LOREM IPSUM',
'extra_data': extra_data,
}, {
'word' : '',
'abbr' : 'MENU TEXT',
'menu' : 'ESPECIALLY LONG M...',
'kind' : 'k',
'info' : 'ESPECIALLY LONG METHOD SIGNATURE LOREM IPSUM\n' +
'DOC STRING',
'equal' : 1,
'dup' : 1,
'empty' : 1,
'user_data': json.dumps( extra_data ),
} )
17 changes: 17 additions & 0 deletions python/ycm/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
OMNIFUNC_REGEX_FORMAT = (
'^{omnifunc_name}\\((?P<findstart>[01]),[\'"](?P<base>.*)[\'"]\\)$' )
FNAMEESCAPE_REGEX = re.compile( '^fnameescape\\(\'(?P<filepath>.+)\'\\)$' )
STRDISPLAYWIDTH_REGEX = re.compile(
'^strdisplaywidth\\( ?\'(?P<text>.+)\' ?\\)$' )
SIGN_LIST_REGEX = re.compile(
'^silent! sign place buffer=(?P<bufnr>\\d+)$' )
SIGN_PLACE_REGEX = re.compile(
Expand Down Expand Up @@ -70,6 +72,7 @@
VIM_SIGNS = []

VIM_OPTIONS = {
'&completeopt': b'',
'&previewheight': 12,
'&columns': 80,
'&ruler': 0,
Expand Down Expand Up @@ -289,6 +292,10 @@ def _MockVimEval( value ):
if value == REDIR[ 'variable' ]:
return REDIR[ 'output' ]

match = STRDISPLAYWIDTH_REGEX.search( value )
if match:
return len( match.group( 'text' ) )

raise VimError( 'Unexpected evaluation: {}'.format( value ) )


Expand Down Expand Up @@ -370,6 +377,14 @@ def _MockVimCommand( command ):
return DEFAULT


def _MockVimOptions( option ):
result = VIM_OPTIONS.get( '&' + option )
if result is not None:
return result

return None


class VimBuffer:
"""An object that looks like a vim.buffer object:
- |name| : full path of the buffer with symbolic links resolved;
Expand Down Expand Up @@ -632,6 +647,8 @@ def test( vim_command, vim_eval ):
VIM_MOCK.command = MagicMock( side_effect = _MockVimCommand )
VIM_MOCK.eval = MagicMock( side_effect = _MockVimEval )
VIM_MOCK.error = VimError
VIM_MOCK.options = MagicMock()
VIM_MOCK.options.__getitem__.side_effect = _MockVimOptions
sys.modules[ 'vim' ] = VIM_MOCK

return VIM_MOCK
Expand Down
12 changes: 12 additions & 0 deletions python/ycm/vimsupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -1308,3 +1308,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 ) ) )