2013-02-10 22:55:05 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2011, 2012, 2013 Strahinja Val Markovic <val@markovic.io>
|
|
|
|
#
|
|
|
|
# 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 22:44:42 -04:00
|
|
|
from ycm import vimsupport
|
|
|
|
from ycm.completers.completer import Completer
|
2013-02-10 22:55:05 -05:00
|
|
|
|
2013-03-22 13:22:50 -04: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 22:55:05 -05:00
|
|
|
class OmniCompleter( Completer ):
|
|
|
|
def __init__( self ):
|
|
|
|
super( OmniCompleter, self ).__init__()
|
|
|
|
self.omnifunc = None
|
|
|
|
self.stored_candidates = None
|
|
|
|
|
|
|
|
|
|
|
|
def SupportedFiletypes( self ):
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
2013-04-08 16:26:34 -04:00
|
|
|
def ShouldUseCache( self ):
|
|
|
|
return vimsupport.GetBoolValue( "g:ycm_cache_omnifunc" )
|
|
|
|
|
|
|
|
|
|
|
|
def ShouldUseNow( self, start_column ):
|
|
|
|
if self.ShouldUseCache():
|
|
|
|
return super( OmniCompleter, self ).ShouldUseNow( start_column )
|
|
|
|
return self.ShouldUseNowInner( start_column )
|
|
|
|
|
2013-02-13 13:27:40 -05:00
|
|
|
def ShouldUseNowInner( self, start_column ):
|
|
|
|
if not self.omnifunc:
|
|
|
|
return False
|
|
|
|
return super( OmniCompleter, self ).ShouldUseNowInner( start_column )
|
|
|
|
|
|
|
|
|
2013-04-08 16:26:34 -04:00
|
|
|
def CandidatesForQueryAsync( self, query, unused_start_column ):
|
|
|
|
if self.ShouldUseCache():
|
|
|
|
return super( OmniCompleter, self ).CandidatesForQueryAsync(
|
|
|
|
query, unused_start_column )
|
|
|
|
else:
|
|
|
|
return self.CandidatesForQueryAsyncInner( query, unused_start_column )
|
|
|
|
|
2013-04-09 22:25:53 -04:00
|
|
|
def CandidatesForQueryAsyncInner( self, query, unused_start_column ):
|
2013-02-10 22:55:05 -05:00
|
|
|
if not self.omnifunc:
|
|
|
|
self.stored_candidates = None
|
|
|
|
return
|
|
|
|
|
2013-03-22 13:22:50 -04:00
|
|
|
try:
|
|
|
|
return_value = int( vim.eval( self.omnifunc + '(1,"")' ) )
|
|
|
|
if return_value < 0:
|
|
|
|
self.stored_candidates = None
|
|
|
|
return
|
|
|
|
|
|
|
|
omnifunc_call = [ self.omnifunc,
|
|
|
|
"(0,'",
|
|
|
|
vimsupport.EscapeForVim( query ),
|
|
|
|
"')" ]
|
|
|
|
|
|
|
|
items = vim.eval( ''.join( omnifunc_call ) )
|
2013-04-13 08:26:21 -04:00
|
|
|
|
|
|
|
if 'words' in items:
|
|
|
|
items = items['words']
|
2013-03-22 13:22:50 -04:00
|
|
|
if not hasattr( items, '__iter__' ):
|
|
|
|
raise TypeError( OMNIFUNC_NOT_LIST )
|
|
|
|
|
|
|
|
self.stored_candidates = filter( bool, items )
|
|
|
|
except (TypeError, ValueError) as error:
|
|
|
|
vimsupport.PostVimMessage(
|
|
|
|
OMNIFUNC_RETURNED_BAD_VALUE + ' ' + str( error ) )
|
2013-02-10 22:55:05 -05:00
|
|
|
self.stored_candidates = None
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def AsyncCandidateRequestReadyInner( self ):
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def OnFileReadyToParse( self ):
|
|
|
|
self.omnifunc = vim.eval( '&omnifunc' )
|
|
|
|
|
|
|
|
|
2013-04-08 16:26:34 -04:00
|
|
|
def CandidatesFromStoredRequest( self ):
|
|
|
|
if self.ShouldUseCache():
|
|
|
|
return super( OmniCompleter, self ).CandidatesFromStoredRequest()
|
|
|
|
else:
|
|
|
|
return self.CandidatesFromStoredRequestInner()
|
|
|
|
|
2013-02-10 22:55:05 -05:00
|
|
|
def CandidatesFromStoredRequestInner( self ):
|
2013-03-22 13:22:50 -04:00
|
|
|
return self.stored_candidates if self.stored_candidates else []
|
2013-02-10 22:55:05 -05:00
|
|
|
|