Skip to content

Commit

Permalink
Allow the completer (omnifunc or server) to reset the start column
Browse files Browse the repository at this point in the history
  • Loading branch information
puremourning committed Jan 3, 2017
1 parent 3828cfd commit 6a535d7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
18 changes: 9 additions & 9 deletions autoload/youcompleteme.vim
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,8 @@ function! s:SetUpCompleteopt()
" don't need to insert the prefix; they just type the differentiating chars.
" Also, having this option set breaks the plugin.
set completeopt-=longest
set completeopt+=noselect
set completeopt+=noinsert

if g:ycm_add_preview_to_completeopt
set completeopt+=preview
Expand Down Expand Up @@ -705,13 +707,11 @@ function! s:InvokeCompletion()
endif

" <c-x><c-u> invokes the user's completion function (which we have set to
" youcompleteme#Complete), and <c-p> tells Vim to select the previous
" completion candidate. This is necessary because by default, Vim selects the
" first candidate when completion is invoked, and selecting a candidate
" automatically replaces the current text with it. Calling <c-p> forces Vim to
" deselect the first candidate and in turn preserve the user's current text
" until he explicitly chooses to replace it with a completion.
call feedkeys( "\<C-X>\<C-U>\<C-P>", 'n' )
" youcompleteme#Complete). Note that when we enabled YCM, we added 'noinsert'
" and 'noselect' to 'completeopt'. This prevents Vim from selecting or
" inserting the first match (and allows us to use complete_check() within the
" 'findstart' call).
call feedkeys( "\<C-X>\<C-U>", 'n' )
endfunction


Expand Down Expand Up @@ -744,7 +744,7 @@ function! youcompleteme#Complete( findstart, base )
return -2
endif
exec s:python_command "ycm_state.CreateCompletionRequest()"
return s:Pyeval( 'base.CompletionStartColumn()' )
return s:Pyeval( 'ycm_state.GetCompletionStartColumn()' )
else
return s:GetCompletions()
endif
Expand All @@ -759,7 +759,7 @@ function! youcompleteme#OmniComplete( findstart, base )
let s:omnifunc_mode = 1
exec s:python_command "ycm_state.CreateCompletionRequest(" .
\ "force_semantic = True )"
return s:Pyeval( 'base.CompletionStartColumn()' )
return s:Pyeval( 'ycm_state.GetCompletionStartColumn()' )
else
return s:GetCompletions()
endif
Expand Down
5 changes: 3 additions & 2 deletions python/ycm/client/completion_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ def RawResponse( self ):
with HandleServerException( truncate = True ):
raise MakeServerException( e )

return response[ 'completions' ]
return response
return []


def Response( self ):
return _ConvertCompletionDatasToVimDatas( self.RawResponse() )
return _ConvertCompletionDatasToVimDatas(
self.RawResponse()[ 'completions' ])


def ConvertCompletionDataToVimData( completion_data ):
Expand Down
7 changes: 6 additions & 1 deletion python/ycm/client/omni_completion_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class OmniCompletionRequest( CompletionRequest ):
def __init__( self, omni_completer, request_data ):
super( OmniCompletionRequest, self ).__init__( request_data )
self._omni_completer = omni_completer
self._results = None


def Start( self ):
Expand All @@ -41,7 +42,11 @@ def Done( self ):


def RawResponse( self ):
return _ConvertVimDatasToCompletionDatas( self._results )
return {
'completions': _ConvertVimDatasToCompletionDatas( self._results ) ,
'completion_start_column': self.request_data[ 'start_column' ],
'errors': []
}


def Response( self ):
Expand Down
6 changes: 6 additions & 0 deletions python/ycm/omni_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ def ComputeCandidatesInner( self, request_data ):

items = vim.eval( ''.join( omnifunc_call ) )

# Use the start column calculated by the omnifunc, rather than our own
# interpretation. This is important for certain languages where our
# identifier detection is either incorrect or not compatible with the
# behaviour of the omnifunc.
request_data.SetCompletionStartColumn( return_value + 1 )

if isinstance( items, dict ) and 'words' in items:
items = items[ 'words' ]

Expand Down
12 changes: 11 additions & 1 deletion python/ycm/youcompleteme.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ def CreateCompletionRequest( self, force_semantic = False ):
if self._omnicomp.ShouldUseNow( wrapped_request_data ):
self._latest_completion_request = OmniCompletionRequest(
self._omnicomp, wrapped_request_data )
self._latest_completion_request.Start()
return self._latest_completion_request

request_data[ 'working_dir' ] = utils.GetCurrentDirectory()
Expand All @@ -279,12 +280,21 @@ def CreateCompletionRequest( self, force_semantic = False ):
if force_semantic:
request_data[ 'force_semantic' ] = True
self._latest_completion_request = CompletionRequest( request_data )
self._latest_completion_request.Start()
return self._latest_completion_request


def GetCompletionStartColumn( self ):
request = self.GetCurrentCompletionRequest()
while not request.Done():
if vimsupport.GetBoolValue( 'complete_check()' ):
return base.CompletionStartColumn()

return request.RawResponse()[ 'completion_start_column' ] - 1


def GetCompletions( self ):
request = self.GetCurrentCompletionRequest()
request.Start()
while not request.Done():
try:
if vimsupport.GetBoolValue( 'complete_check()' ):
Expand Down

0 comments on commit 6a535d7

Please sign in to comment.