2013-02-10 19:55:05 -08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2014-01-13 11:08:43 -08:00
|
|
|
# Copyright (C) 2011, 2012, 2013 Google Inc.
|
2013-02-10 19:55:05 -08:00
|
|
|
#
|
|
|
|
# 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
|
2013-05-19 19:44:42 -07:00
|
|
|
from ycm import vimsupport
|
2014-05-13 13:09:19 -07:00
|
|
|
from ycmd.completers.completer import Completer
|
2013-02-10 19:55:05 -08:00
|
|
|
|
2013-03-22 10:22:50 -07:00
|
|
|
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.' )
|
|
|
|
|
2013-02-10 19:55:05 -08:00
|
|
|
class OmniCompleter( Completer ):
|
2013-09-02 14:45:53 -07:00
|
|
|
def __init__( self, user_options ):
|
|
|
|
super( OmniCompleter, self ).__init__( user_options )
|
2013-10-07 15:47:48 -07:00
|
|
|
self._omnifunc = None
|
2013-02-10 19:55:05 -08:00
|
|
|
|
|
|
|
|
|
|
|
def SupportedFiletypes( self ):
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
2013-04-08 21:26:34 +01:00
|
|
|
def ShouldUseCache( self ):
|
2013-09-02 14:45:53 -07:00
|
|
|
return bool( self.user_options[ 'cache_omnifunc' ] )
|
2013-04-08 21:26:34 +01:00
|
|
|
|
|
|
|
|
2014-05-27 17:38:34 -07:00
|
|
|
def ShouldUseNow( self, request_data ):
|
2013-10-14 13:29:28 -07:00
|
|
|
if not self._omnifunc:
|
|
|
|
return False
|
|
|
|
|
2013-04-08 21:26:34 +01:00
|
|
|
if self.ShouldUseCache():
|
2013-09-05 23:43:14 -07:00
|
|
|
return super( OmniCompleter, self ).ShouldUseNow( request_data )
|
|
|
|
return self.ShouldUseNowInner( request_data )
|
2013-04-08 21:26:34 +01:00
|
|
|
|
2013-06-09 19:00:49 -07:00
|
|
|
|
2013-09-05 23:43:14 -07:00
|
|
|
def ShouldUseNowInner( self, request_data ):
|
2013-10-07 15:47:48 -07:00
|
|
|
if not self._omnifunc:
|
2013-02-13 10:27:40 -08:00
|
|
|
return False
|
2013-09-05 23:43:14 -07:00
|
|
|
return super( OmniCompleter, self ).ShouldUseNowInner( request_data )
|
2013-02-13 10:27:40 -08:00
|
|
|
|
|
|
|
|
2013-10-07 15:47:48 -07:00
|
|
|
def ComputeCandidates( self, request_data ):
|
2013-04-08 21:26:34 +01:00
|
|
|
if self.ShouldUseCache():
|
2013-10-07 15:47:48 -07:00
|
|
|
return super( OmniCompleter, self ).ComputeCandidates(
|
2013-09-05 23:43:14 -07:00
|
|
|
request_data )
|
2013-04-08 21:26:34 +01:00
|
|
|
else:
|
2013-10-07 15:47:48 -07:00
|
|
|
if self.ShouldUseNowInner( request_data ):
|
|
|
|
return self.ComputeCandidatesInner( request_data )
|
|
|
|
return []
|
2013-04-08 21:26:34 +01:00
|
|
|
|
2013-06-09 19:00:49 -07:00
|
|
|
|
2013-10-07 15:47:48 -07:00
|
|
|
def ComputeCandidatesInner( self, request_data ):
|
|
|
|
if not self._omnifunc:
|
|
|
|
return []
|
2013-02-10 19:55:05 -08:00
|
|
|
|
2013-03-22 10:22:50 -07:00
|
|
|
try:
|
2013-10-07 15:47:48 -07:00
|
|
|
return_value = int( vim.eval( self._omnifunc + '(1,"")' ) )
|
2013-03-22 10:22:50 -07:00
|
|
|
if return_value < 0:
|
2013-10-07 15:47:48 -07:00
|
|
|
return []
|
2013-03-22 10:22:50 -07:00
|
|
|
|
2013-10-07 15:47:48 -07:00
|
|
|
omnifunc_call = [ self._omnifunc,
|
2013-03-22 10:22:50 -07:00
|
|
|
"(0,'",
|
2013-09-05 23:43:14 -07:00
|
|
|
vimsupport.EscapeForVim( request_data[ 'query' ] ),
|
2013-03-22 10:22:50 -07:00
|
|
|
"')" ]
|
|
|
|
|
|
|
|
items = vim.eval( ''.join( omnifunc_call ) )
|
2013-04-13 14:26:21 +02:00
|
|
|
|
|
|
|
if 'words' in items:
|
2013-10-07 15:47:48 -07:00
|
|
|
items = items[ 'words' ]
|
2013-03-22 10:22:50 -07:00
|
|
|
if not hasattr( items, '__iter__' ):
|
|
|
|
raise TypeError( OMNIFUNC_NOT_LIST )
|
|
|
|
|
2013-10-07 15:47:48 -07:00
|
|
|
return filter( bool, items )
|
2014-01-04 16:17:44 -08:00
|
|
|
except ( TypeError, ValueError, vim.error ) as error:
|
2013-03-22 10:22:50 -07:00
|
|
|
vimsupport.PostVimMessage(
|
|
|
|
OMNIFUNC_RETURNED_BAD_VALUE + ' ' + str( error ) )
|
2013-10-07 15:47:48 -07:00
|
|
|
return []
|
2013-02-10 19:55:05 -08:00
|
|
|
|
|
|
|
|
2013-09-05 23:43:14 -07:00
|
|
|
def OnFileReadyToParse( self, request_data ):
|
2013-10-07 15:47:48 -07:00
|
|
|
self._omnifunc = vim.eval( '&omnifunc' )
|