omni_completer.py 3.7 KB

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