omni_completer.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright (C) 2011, 2012, 2013 Google Inc.
  2. #
  3. # This file is part of YouCompleteMe.
  4. #
  5. # YouCompleteMe is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # YouCompleteMe is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. import vim
  18. from ycm import vimsupport
  19. from ycmd.completers.completer import Completer
  20. OMNIFUNC_RETURNED_BAD_VALUE = 'Omnifunc returned bad value to YCM!'
  21. OMNIFUNC_NOT_LIST = ( 'Omnifunc did not return a list or a dict with a "words" '
  22. ' list when expected.' )
  23. class OmniCompleter( Completer ):
  24. def __init__( self, user_options ):
  25. super( OmniCompleter, self ).__init__( user_options )
  26. self._omnifunc = None
  27. def SupportedFiletypes( self ):
  28. return []
  29. def ShouldUseCache( self ):
  30. return bool( self.user_options[ 'cache_omnifunc' ] )
  31. def ShouldUseNow( self, request_data ):
  32. if not self._omnifunc:
  33. return False
  34. if self.ShouldUseCache():
  35. return super( OmniCompleter, self ).ShouldUseNow( request_data )
  36. return self.ShouldUseNowInner( request_data )
  37. def ShouldUseNowInner( self, request_data ):
  38. if not self._omnifunc:
  39. return False
  40. return super( OmniCompleter, self ).ShouldUseNowInner( request_data )
  41. def ComputeCandidates( self, request_data ):
  42. if self.ShouldUseCache():
  43. return super( OmniCompleter, self ).ComputeCandidates(
  44. request_data )
  45. else:
  46. if self.ShouldUseNowInner( request_data ):
  47. return self.ComputeCandidatesInner( request_data )
  48. return []
  49. def ComputeCandidatesInner( self, request_data ):
  50. if not self._omnifunc:
  51. return []
  52. try:
  53. return_value = int( vim.eval( self._omnifunc + '(1,"")' ) )
  54. if return_value < 0:
  55. return []
  56. omnifunc_call = [ self._omnifunc,
  57. "(0,'",
  58. vimsupport.EscapeForVim( request_data[ 'query' ] ),
  59. "')" ]
  60. items = vim.eval( ''.join( omnifunc_call ) )
  61. if 'words' in items:
  62. items = items[ 'words' ]
  63. if not hasattr( items, '__iter__' ):
  64. raise TypeError( OMNIFUNC_NOT_LIST )
  65. return filter( bool, items )
  66. except ( TypeError, ValueError, vim.error ) as error:
  67. vimsupport.PostVimMessage(
  68. OMNIFUNC_RETURNED_BAD_VALUE + ' ' + str( error ) )
  69. return []
  70. def OnFileReadyToParse( self, request_data ):
  71. self._omnifunc = vim.eval( '&omnifunc' )