completion_request.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2013 Strahinja Val Markovic <val@markovic.io>
  4. #
  5. # This file is part of YouCompleteMe.
  6. #
  7. # YouCompleteMe is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # YouCompleteMe is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  19. from ycm import base
  20. from ycm import vimsupport
  21. from ycm.client.base_request import BaseRequest, BuildRequestData
  22. class CompletionRequest( BaseRequest ):
  23. def __init__( self ):
  24. super( CompletionRequest, self ).__init__()
  25. self._completion_start_column = base.CompletionStartColumn()
  26. self._request_data = BuildRequestData( self._completion_start_column )
  27. # TODO: Do we need this anymore?
  28. # def ShouldComplete( self ):
  29. # return ( self._do_filetype_completion or
  30. # self._ycm_state.ShouldUseGeneralCompleter( self._request_data ) )
  31. def CompletionStartColumn( self ):
  32. return self._completion_start_column
  33. def Start( self, query ):
  34. self._request_data[ 'query' ] = query
  35. self._response = self.PostDataToHandler( self._request_data,
  36. 'get_completions' )
  37. def Results( self ):
  38. if not self._response:
  39. return []
  40. try:
  41. return [ _ConvertCompletionDataToVimData( x ) for x in self._response ]
  42. except Exception as e:
  43. vimsupport.PostVimMessage( str( e ) )
  44. return []
  45. def _ConvertCompletionDataToVimData( completion_data ):
  46. # see :h complete-items for a description of the dictionary fields
  47. vim_data = {
  48. 'word' : completion_data[ 'insertion_text' ],
  49. 'dup' : 1,
  50. }
  51. if 'menu_text' in completion_data:
  52. vim_data[ 'abbr' ] = completion_data[ 'menu_text' ]
  53. if 'extra_menu_info' in completion_data:
  54. vim_data[ 'menu' ] = completion_data[ 'extra_menu_info' ]
  55. if 'kind' in completion_data:
  56. vim_data[ 'kind' ] = completion_data[ 'kind' ]
  57. if 'detailed_info' in completion_data:
  58. vim_data[ 'info' ] = completion_data[ 'detailed_info' ]
  59. return vim_data