1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
# Copyright (C) 2011-2018 YouCompleteMe contributors
#
# This file is part of YouCompleteMe.
#
# YouCompleteMe is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# YouCompleteMe is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
import vim
import json
from ycm import vimsupport
from ycmd import utils
from ycm.vimsupport import memoize, GetIntValue
class SignatureHelpState:
ACTIVE = 'ACTIVE'
INACTIVE = 'INACTIVE'
ACTIVE_SUPPRESSED = 'ACTIVE_SUPPRESSED'
def __init__( self,
popup_win_id = None,
state = INACTIVE ):
self.popup_win_id = popup_win_id
self.state = state
self.anchor = None
def ToggleVisibility( self ):
if self.state == 'ACTIVE':
self.state = 'ACTIVE_SUPPRESSED'
vim.eval( f'popup_hide( { self.popup_win_id } )' )
elif self.state == 'ACTIVE_SUPPRESSED':
self.state = 'ACTIVE'
vim.eval( f'popup_show( { self.popup_win_id } )' )
def IsActive( self ):
if self.state in ( 'ACTIVE', 'ACTIVE_SUPPRESSED' ):
return 'ACTIVE'
return 'INACTIVE'
def _MakeSignatureHelpBuffer( signature_info ):
active_parameter = int( signature_info.get( 'activeParameter', 0 ) )
lines = []
signatures = ( signature_info.get( 'signatures' ) or [] )
for sig_index, signature in enumerate( signatures ):
props = []
sig_label = signature[ 'label' ]
parameters = ( signature.get( 'parameters' ) or [] )
for param_index, parameter in enumerate( parameters ):
param_label = parameter[ 'label' ]
begin = int( param_label[ 0 ] )
end = int( param_label[ 1 ] )
if param_index == active_parameter:
props.append( {
'col': begin + 1, # 1-based
'length': end - begin,
'type': 'YCM-signature-help-current-argument'
} )
lines.append( {
'text': sig_label,
'props': props
} )
return lines
@memoize()
def ShouldUseSignatureHelp():
return ( vimsupport.VimHasFunctions( 'screenpos', 'pum_getpos' ) and
vimsupport.VimSupportsPopupWindows() )
def UpdateSignatureHelp( state, signature_info ): # noqa
if not ShouldUseSignatureHelp():
return state
signatures = signature_info.get( 'signatures' ) or []
if not signatures:
if state.popup_win_id:
# TODO/FIXME: Should we use popup_hide() instead ?
vim.eval( f"popup_close( { state.popup_win_id } )" )
return SignatureHelpState( None, SignatureHelpState.INACTIVE )
if state.state == SignatureHelpState.INACTIVE:
state.anchor = vimsupport.CurrentLineAndColumn()
state.state = SignatureHelpState.ACTIVE
# Generate the buffer as a list of lines
buf_lines = _MakeSignatureHelpBuffer( signature_info )
screen_pos = vimsupport.ScreenPositionForLineColumnInWindow(
vim.current.window,
state.anchor[ 0 ] + 1, # anchor 0-based
state.anchor[ 1 ] + 1 ) # anchor 0-based
# Simulate 'flip' at the screen boundaries by using screenpos and hiding the
# signature help menu if it overlaps the completion popup (pum).
#
# FIXME: revert to cursor-relative positioning and the 'flip' option when that
# is implemented (if that is indeed better).
# By default display above the anchor
line = int( screen_pos[ 'row' ] ) - 1 # -1 to display above the cur line
pos = "botleft"
cursor_line = vimsupport.CurrentLineAndColumn()[ 0 ] + 1
if int( screen_pos[ 'row' ] ) <= len( buf_lines ):
# No room at the top, display below
line = int( screen_pos[ 'row' ] ) + 1
pos = "topleft"
# Don't allow the popup to overlap the cursor
if ( pos == 'topleft' and
line < cursor_line and
line + len( buf_lines ) >= cursor_line ):
line = 0
# Don't allow the popup to overlap the pum
if line > 0 and GetIntValue( 'pumvisible()' ):
pum_line = GetIntValue( 'pum_getpos().row' ) + 1
if pos == 'botleft' and pum_line <= line:
line = 0
elif ( pos == 'topleft' and
pum_line >= line and
pum_line < ( line + len( buf_lines ) ) ):
line = 0
if line <= 0:
# Nowhere to put it so hide it
if state.popup_win_id:
# TODO/FIXME: Should we use popup_hide() instead ?
vim.eval( f"popup_close( { state.popup_win_id } )" )
return SignatureHelpState( None, SignatureHelpState.INACTIVE )
if int( screen_pos[ 'curscol' ] ) <= 1:
col = 1
else:
# -1 for padding,
# -1 for the trigger character inserted (the anchor is set _after_ the
# character is inserted, so we remove it).
# FIXME: multi-byte characters would be wrong. Need to set anchor before
# inserting the char ?
col = int( screen_pos[ 'curscol' ] ) - 2
if col <= 0:
col = 1
options = {
"line": line,
"col": col,
"pos": pos,
"wrap": 0,
# NOTE: We *dont'* use "cursorline" here - that actually uses PMenuSel,
# which is just too invasive for us (it's more selected item than actual
# cursorline. So instead, we manually set 'cursorline' in the popup window
# and enable syntax based on the current file syntax)
"flip": 1,
"padding": [ 0, 1, 0, 1 ], # Pad 1 char in X axis to match completion menu
"hidden": int( state.state == SignatureHelpState.ACTIVE_SUPPRESSED )
}
if not state.popup_win_id:
state.popup_win_id = GetIntValue(
f'popup_create( { json.dumps( buf_lines ) }, '
f'{ json.dumps( options ) } )' )
else:
vim.eval( f'popup_settext( { state.popup_win_id }, '
f'{ json.dumps( buf_lines ) } )' )
# Should do nothing if already visible
vim.eval( f'popup_move( { state.popup_win_id }, { json.dumps( options ) } )' )
if state.state == SignatureHelpState.ACTIVE:
vim.eval( f'popup_show( { state.popup_win_id } )' )
syntax = utils.ToUnicode( vim.current.buffer.options[ 'syntax' ] )
active_signature = int( signature_info.get( 'activeSignature', 0 ) )
vim.eval( f"win_execute( { state.popup_win_id }, "
f"'set syntax={ syntax } cursorline | "
f"call cursor( [ { active_signature + 1 }, 1 ] )' )" )
return state
|