omni_completer.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. # Not installing aliases from python-future; it's unreliable and slow.
  22. from builtins import * # noqa
  23. import vim
  24. from ycm import vimsupport
  25. from ycmd import utils
  26. from ycmd.completers.completer import Completer
  27. from ycm.client.base_request import BaseRequest, HandleServerException
  28. OMNIFUNC_RETURNED_BAD_VALUE = 'Omnifunc returned bad value to YCM!'
  29. OMNIFUNC_NOT_LIST = ( 'Omnifunc did not return a list or a dict with a "words" '
  30. ' list when expected.' )
  31. class OmniCompleter( Completer ):
  32. def __init__( self, user_options ):
  33. super( OmniCompleter, self ).__init__( user_options )
  34. self._omnifunc = None
  35. def SupportedFiletypes( self ):
  36. return []
  37. def ShouldUseCache( self ):
  38. return bool( self.user_options[ 'cache_omnifunc' ] )
  39. def ShouldUseNow( self, request_data ):
  40. if not self._omnifunc:
  41. return False
  42. if self.ShouldUseCache():
  43. return super( OmniCompleter, self ).ShouldUseNow( request_data )
  44. return self.ShouldUseNowInner( request_data )
  45. def ShouldUseNowInner( self, request_data ):
  46. if not self._omnifunc:
  47. return False
  48. if request_data.get( 'force_semantic', False ):
  49. return True
  50. return super( OmniCompleter, self ).ShouldUseNowInner( request_data )
  51. def ComputeCandidates( self, request_data ):
  52. if self.ShouldUseCache():
  53. return super( OmniCompleter, self ).ComputeCandidates( request_data )
  54. else:
  55. if self.ShouldUseNowInner( request_data ):
  56. return self.ComputeCandidatesInner( request_data )
  57. return []
  58. def ComputeCandidatesInner( self, request_data ):
  59. if not self._omnifunc:
  60. return []
  61. try:
  62. return_value = int( vim.eval( self._omnifunc + '(1,"")' ) )
  63. if return_value < 0:
  64. # FIXME: Technically, if the return is -1 we should raise an error
  65. return []
  66. # Use the start column calculated by the omnifunc, rather than our own
  67. # interpretation. This is important for certain languages where our
  68. # identifier detection is either incorrect or not compatible with the
  69. # behaviour of the omnifunc. Note: do this before calling the omnifunc
  70. # because it affects the value returned by 'query'
  71. request_data[ 'start_column' ] = return_value + 1
  72. # Calling directly the omnifunc may move the cursor position. This is the
  73. # case with the default Vim omnifunc for C-family languages
  74. # (ccomplete#Complete) which calls searchdecl to find a declaration. This
  75. # function is supposed to move the cursor to the found declaration but it
  76. # doesn't when called through the omni completion mapping (CTRL-X CTRL-O).
  77. # So, we restore the cursor position after calling the omnifunc.
  78. line, column = vimsupport.CurrentLineAndColumn()
  79. omnifunc_call = [ self._omnifunc,
  80. "(0,'",
  81. vimsupport.EscapeForVim( request_data[ 'query' ] ),
  82. "')" ]
  83. items = vim.eval( ''.join( omnifunc_call ) )
  84. vimsupport.SetCurrentLineAndColumn( line, column )
  85. if isinstance( items, dict ) and 'words' in items:
  86. items = items[ 'words' ]
  87. if not hasattr( items, '__iter__' ):
  88. raise TypeError( OMNIFUNC_NOT_LIST )
  89. return list( filter( bool, items ) )
  90. except ( TypeError, ValueError, vim.error ) as error:
  91. vimsupport.PostVimMessage(
  92. OMNIFUNC_RETURNED_BAD_VALUE + ' ' + str( error ) )
  93. return []
  94. def OnFileReadyToParse( self, request_data ):
  95. self._omnifunc = utils.ToUnicode( vim.eval( '&omnifunc' ) )
  96. def FilterAndSortCandidatesInner( self, candidates, sort_property, query ):
  97. request_data = {
  98. 'candidates': candidates,
  99. 'sort_property': sort_property,
  100. 'query': query
  101. }
  102. with HandleServerException():
  103. return BaseRequest.PostDataToHandler( request_data,
  104. 'filter_and_sort_candidates' )
  105. return candidates