omni_completer.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # Copyright (C) 2011-2019 ycmd contributors
  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 import utils
  20. from ycmd.completers.completer import Completer
  21. from ycm.client.base_request import BaseRequest
  22. OMNIFUNC_RETURNED_BAD_VALUE = 'Omnifunc returned bad value to YCM!'
  23. OMNIFUNC_NOT_LIST = ( 'Omnifunc did not return a list or a dict with a "words" '
  24. ' list when expected.' )
  25. class OmniCompleter( Completer ):
  26. def __init__( self, user_options ):
  27. super( OmniCompleter, self ).__init__( user_options )
  28. self._omnifunc = None
  29. def SupportedFiletypes( self ):
  30. return []
  31. def ShouldUseCache( self ):
  32. return bool( self.user_options[ 'cache_omnifunc' ] )
  33. def ShouldUseNow( self, request_data ):
  34. self._omnifunc = utils.ToUnicode( vim.eval( '&omnifunc' ) )
  35. if not self._omnifunc:
  36. return False
  37. if self.ShouldUseCache():
  38. return super( OmniCompleter, self ).ShouldUseNow( request_data )
  39. return self.ShouldUseNowInner( request_data )
  40. def ShouldUseNowInner( self, request_data ):
  41. if request_data[ 'force_semantic' ]:
  42. return True
  43. disabled_filetypes = self.user_options[
  44. 'filetype_specific_completion_to_disable' ]
  45. if not vimsupport.CurrentFiletypesEnabled( disabled_filetypes ):
  46. return False
  47. return super( OmniCompleter, self ).ShouldUseNowInner( request_data )
  48. def ComputeCandidates( self, request_data ):
  49. if self.ShouldUseCache():
  50. return super( OmniCompleter, self ).ComputeCandidates( request_data )
  51. if self.ShouldUseNowInner( request_data ):
  52. return self.ComputeCandidatesInner( request_data )
  53. return []
  54. def ComputeCandidatesInner( self, request_data ):
  55. if not self._omnifunc:
  56. return []
  57. # Calling directly the omnifunc may move the cursor position. This is the
  58. # case with the default Vim omnifunc for C-family languages
  59. # (ccomplete#Complete) which calls searchdecl to find a declaration. This
  60. # function is supposed to move the cursor to the found declaration but it
  61. # doesn't when called through the omni completion mapping (CTRL-X CTRL-O).
  62. # So, we restore the cursor position after the omnifunc calls.
  63. line, column = vimsupport.CurrentLineAndColumn()
  64. try:
  65. start_column = vimsupport.GetIntValue( self._omnifunc + '(1,"")' )
  66. # Vim only stops completion if the value returned by the omnifunc is -3 or
  67. # -2. In other cases, if the value is negative or greater than the current
  68. # column, the start column is set to the current column; otherwise, the
  69. # value is used as the start column.
  70. if start_column in ( -3, -2 ):
  71. return []
  72. if start_column < 0 or start_column > column:
  73. start_column = column
  74. # Use the start column calculated by the omnifunc, rather than our own
  75. # interpretation. This is important for certain languages where our
  76. # identifier detection is either incorrect or not compatible with the
  77. # behaviour of the omnifunc. Note: do this before calling the omnifunc
  78. # because it affects the value returned by 'query'.
  79. request_data[ 'start_column' ] = start_column + 1
  80. # Vim internally moves the cursor to the start column before calling again
  81. # the omnifunc. Some omnifuncs like the one defined by the
  82. # LanguageClient-neovim plugin depend on this behavior to compute the list
  83. # of candidates.
  84. vimsupport.SetCurrentLineAndColumn( line, start_column )
  85. omnifunc_call = [ self._omnifunc,
  86. "(0,'",
  87. vimsupport.EscapeForVim( request_data[ 'query' ] ),
  88. "')" ]
  89. items = vim.eval( ''.join( omnifunc_call ) )
  90. if isinstance( items, dict ) and 'words' in items:
  91. items = items[ 'words' ]
  92. if not hasattr( items, '__iter__' ):
  93. raise TypeError( OMNIFUNC_NOT_LIST )
  94. # Vim allows each item of the list to be either a string or a dictionary
  95. # but ycmd only supports lists where items are all strings or all
  96. # dictionaries. Convert all strings into dictionaries.
  97. for index, item in enumerate( items ):
  98. # Set the 'equal' field to 1 to disable Vim filtering.
  99. if not isinstance( item, dict ):
  100. items[ index ] = {
  101. 'word': item,
  102. 'equal': 1
  103. }
  104. else:
  105. item[ 'equal' ] = 1
  106. return items
  107. except ( TypeError, ValueError, vim.error ) as error:
  108. vimsupport.PostVimMessage(
  109. OMNIFUNC_RETURNED_BAD_VALUE + ' ' + str( error ) )
  110. return []
  111. finally:
  112. vimsupport.SetCurrentLineAndColumn( line, column )
  113. def FilterAndSortCandidatesInner( self, candidates, sort_property, query ):
  114. request_data = {
  115. 'candidates': candidates,
  116. 'sort_property': sort_property,
  117. 'query': query
  118. }
  119. response = BaseRequest().PostDataToHandler( request_data,
  120. 'filter_and_sort_candidates' )
  121. return response if response is not None else []