completion_request.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2013 Google Inc.
  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 ycmd.utils import ToUtf8IfNeeded
  20. from ycm.client.base_request import ( BaseRequest, JsonFromFuture,
  21. HandleServerException,
  22. MakeServerException )
  23. TIMEOUT_SECONDS = 0.5
  24. class CompletionRequest( BaseRequest ):
  25. def __init__( self, request_data ):
  26. super( CompletionRequest, self ).__init__()
  27. self.request_data = request_data
  28. def Start( self ):
  29. self._response_future = self.PostDataToHandlerAsync( self.request_data,
  30. 'completions',
  31. TIMEOUT_SECONDS )
  32. def Done( self ):
  33. return self._response_future.done()
  34. def RawResponse( self ):
  35. if not self._response_future:
  36. return []
  37. try:
  38. response = JsonFromFuture( self._response_future )
  39. errors = response['errors'] if 'errors' in response else []
  40. for e in errors:
  41. HandleServerException( MakeServerException( e ) )
  42. return JsonFromFuture( self._response_future )[ 'completions' ]
  43. except Exception as e:
  44. HandleServerException( e )
  45. return []
  46. def Response( self ):
  47. return _ConvertCompletionDatasToVimDatas( self.RawResponse() )
  48. def _ConvertCompletionDataToVimData( completion_data ):
  49. # see :h complete-items for a description of the dictionary fields
  50. vim_data = {
  51. 'word' : ToUtf8IfNeeded( completion_data[ 'insertion_text' ] ),
  52. 'dup' : 1,
  53. }
  54. if 'menu_text' in completion_data:
  55. vim_data[ 'abbr' ] = ToUtf8IfNeeded( completion_data[ 'menu_text' ] )
  56. if 'extra_menu_info' in completion_data:
  57. vim_data[ 'menu' ] = ToUtf8IfNeeded( completion_data[ 'extra_menu_info' ] )
  58. if 'kind' in completion_data:
  59. vim_data[ 'kind' ] = ToUtf8IfNeeded(
  60. completion_data[ 'kind' ] )[ 0 ].lower()
  61. if 'detailed_info' in completion_data:
  62. vim_data[ 'info' ] = ToUtf8IfNeeded( completion_data[ 'detailed_info' ] )
  63. return vim_data
  64. def _ConvertCompletionDatasToVimDatas( response_data ):
  65. return [ _ConvertCompletionDataToVimData( x )
  66. for x in response_data ]