omni_completer.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. from ycm.client.base_request import BaseRequest, ServerError
  21. OMNIFUNC_RETURNED_BAD_VALUE = 'Omnifunc returned bad value to YCM!'
  22. OMNIFUNC_NOT_LIST = ( 'Omnifunc did not return a list or a dict with a "words" '
  23. ' list when expected.' )
  24. class OmniCompleter( Completer ):
  25. def __init__( self, user_options ):
  26. super( OmniCompleter, self ).__init__( user_options )
  27. self._omnifunc = None
  28. def SupportedFiletypes( self ):
  29. return []
  30. def ShouldUseCache( self ):
  31. return bool( self.user_options[ 'cache_omnifunc' ] )
  32. def ShouldUseNow( self, request_data ):
  33. if not self._omnifunc:
  34. return False
  35. if self.ShouldUseCache():
  36. return super( OmniCompleter, self ).ShouldUseNow( request_data )
  37. return self.ShouldUseNowInner( request_data )
  38. def ShouldUseNowInner( self, request_data ):
  39. if not self._omnifunc:
  40. return False
  41. return super( OmniCompleter, self ).ShouldUseNowInner( request_data )
  42. def ComputeCandidates( self, request_data ):
  43. if self.ShouldUseCache():
  44. return super( OmniCompleter, self ).ComputeCandidates(
  45. request_data )
  46. else:
  47. if self.ShouldUseNowInner( request_data ):
  48. return self.ComputeCandidatesInner( request_data )
  49. return []
  50. def ComputeCandidatesInner( self, request_data ):
  51. if not self._omnifunc:
  52. return []
  53. try:
  54. return_value = int( vim.eval( self._omnifunc + '(1,"")' ) )
  55. if return_value < 0:
  56. return []
  57. omnifunc_call = [ self._omnifunc,
  58. "(0,'",
  59. vimsupport.EscapeForVim( request_data[ 'query' ] ),
  60. "')" ]
  61. items = vim.eval( ''.join( omnifunc_call ) )
  62. if 'words' in items:
  63. items = items[ 'words' ]
  64. if not hasattr( items, '__iter__' ):
  65. raise TypeError( OMNIFUNC_NOT_LIST )
  66. return filter( bool, items )
  67. except ( TypeError, ValueError, vim.error ) as error:
  68. vimsupport.PostVimMessage(
  69. OMNIFUNC_RETURNED_BAD_VALUE + ' ' + str( error ) )
  70. return []
  71. def OnFileReadyToParse( self, request_data ):
  72. self._omnifunc = vim.eval( '&omnifunc' )
  73. def FilterAndSortCandidatesInner( self, candidates, sort_property, query ):
  74. request_data = {
  75. 'candidates': candidates,
  76. 'sort_property': sort_property,
  77. 'query': query
  78. }
  79. try:
  80. return BaseRequest.PostDataToHandler( request_data,
  81. 'filter_and_sort_candidates' )
  82. except ServerError as e:
  83. vimsupport.PostMultiLineNotice( e )
  84. return candidates