YouCompleteMe/python/ycm/omni_completer.py

124 lines
3.7 KiB
Python
Raw Normal View History

# Copyright (C) 2011, 2012, 2013 Google Inc.
#
# 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/>.
2016-02-27 19:12:24 -05:00
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from future import standard_library
standard_library.install_aliases()
from builtins import * # noqa
import vim
2013-05-19 22:44:42 -04:00
from ycm import vimsupport
from ycmd import utils
from ycmd.responses import ServerError
from ycmd.completers.completer import Completer
from ycm.client.base_request import BaseRequest, HandleServerException
OMNIFUNC_RETURNED_BAD_VALUE = 'Omnifunc returned bad value to YCM!'
OMNIFUNC_NOT_LIST = ( 'Omnifunc did not return a list or a dict with a "words" '
' list when expected.' )
2016-03-06 12:13:43 -05:00
class OmniCompleter( Completer ):
def __init__( self, user_options ):
super( OmniCompleter, self ).__init__( user_options )
2013-10-07 18:47:48 -04:00
self._omnifunc = None
def SupportedFiletypes( self ):
return []
def ShouldUseCache( self ):
return bool( self.user_options[ 'cache_omnifunc' ] )
def ShouldUseNow( self, request_data ):
if not self._omnifunc:
return False
if self.ShouldUseCache():
return super( OmniCompleter, self ).ShouldUseNow( request_data )
return self.ShouldUseNowInner( request_data )
def ShouldUseNowInner( self, request_data ):
2013-10-07 18:47:48 -04:00
if not self._omnifunc:
return False
return super( OmniCompleter, self ).ShouldUseNowInner( request_data )
2013-10-07 18:47:48 -04:00
def ComputeCandidates( self, request_data ):
if self.ShouldUseCache():
2013-10-07 18:47:48 -04:00
return super( OmniCompleter, self ).ComputeCandidates(
request_data )
else:
2013-10-07 18:47:48 -04:00
if self.ShouldUseNowInner( request_data ):
return self.ComputeCandidatesInner( request_data )
return []
2013-10-07 18:47:48 -04:00
def ComputeCandidatesInner( self, request_data ):
if not self._omnifunc:
return []
try:
2013-10-07 18:47:48 -04:00
return_value = int( vim.eval( self._omnifunc + '(1,"")' ) )
if return_value < 0:
2013-10-07 18:47:48 -04:00
return []
2013-10-07 18:47:48 -04:00
omnifunc_call = [ self._omnifunc,
"(0,'",
vimsupport.EscapeForVim( request_data[ 'query' ] ),
"')" ]
items = vim.eval( ''.join( omnifunc_call ) )
if hasattr( items, '__getitem__' ) and 'words' in items:
2013-10-07 18:47:48 -04:00
items = items[ 'words' ]
if not hasattr( items, '__iter__' ):
raise TypeError( OMNIFUNC_NOT_LIST )
return [ utils.ToUnicode( i ) for i in items if bool( i ) ]
except ( TypeError, ValueError, vim.error ) as error:
vimsupport.PostVimMessage(
OMNIFUNC_RETURNED_BAD_VALUE + ' ' + str( error ) )
2013-10-07 18:47:48 -04:00
return []
def OnFileReadyToParse( self, request_data ):
2013-10-07 18:47:48 -04:00
self._omnifunc = vim.eval( '&omnifunc' )
def FilterAndSortCandidatesInner( self, candidates, sort_property, query ):
request_data = {
'candidates': candidates,
'sort_property': sort_property,
'query': query
}
try:
return BaseRequest.PostDataToHandler( request_data,
'filter_and_sort_candidates' )
except ServerError as e:
HandleServerException( e )
return candidates