1
0

completion_request.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright (C) 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 ycmd.utils import ToUtf8IfNeeded
  18. from ycm.client.base_request import ( BaseRequest, JsonFromFuture,
  19. HandleServerException,
  20. MakeServerException )
  21. TIMEOUT_SECONDS = 0.5
  22. class CompletionRequest( BaseRequest ):
  23. def __init__( self, request_data ):
  24. super( CompletionRequest, self ).__init__()
  25. self.request_data = request_data
  26. def Start( self ):
  27. self._response_future = self.PostDataToHandlerAsync( self.request_data,
  28. 'completions',
  29. TIMEOUT_SECONDS )
  30. def Done( self ):
  31. return self._response_future.done()
  32. def RawResponse( self ):
  33. if not self._response_future:
  34. return []
  35. try:
  36. response = JsonFromFuture( self._response_future )
  37. errors = response['errors'] if 'errors' in response else []
  38. for e in errors:
  39. HandleServerException( MakeServerException( e ) )
  40. return JsonFromFuture( self._response_future )[ 'completions' ]
  41. except Exception as e:
  42. HandleServerException( e )
  43. return []
  44. def Response( self ):
  45. return _ConvertCompletionDatasToVimDatas( self.RawResponse() )
  46. def ConvertCompletionDataToVimData( completion_data ):
  47. # see :h complete-items for a description of the dictionary fields
  48. vim_data = {
  49. 'word' : '',
  50. 'dup' : 1,
  51. 'empty' : 1,
  52. }
  53. if ( 'extra_data' in completion_data and
  54. 'doc_string' in completion_data[ 'extra_data' ] ):
  55. doc_string = ToUtf8IfNeeded(
  56. completion_data[ 'extra_data' ][ 'doc_string' ] )
  57. else:
  58. doc_string = ""
  59. if 'insertion_text' in completion_data:
  60. vim_data[ 'word' ] = ToUtf8IfNeeded( completion_data[ 'insertion_text' ] )
  61. if 'menu_text' in completion_data:
  62. vim_data[ 'abbr' ] = ToUtf8IfNeeded( completion_data[ 'menu_text' ] )
  63. if 'extra_menu_info' in completion_data:
  64. vim_data[ 'menu' ] = ToUtf8IfNeeded( completion_data[ 'extra_menu_info' ] )
  65. if 'kind' in completion_data:
  66. vim_data[ 'kind' ] = ToUtf8IfNeeded(
  67. completion_data[ 'kind' ] )[ 0 ].lower()
  68. if 'detailed_info' in completion_data:
  69. vim_data[ 'info' ] = ToUtf8IfNeeded( completion_data[ 'detailed_info' ] )
  70. if doc_string:
  71. vim_data[ 'info' ] += '\n' + doc_string
  72. elif doc_string:
  73. vim_data[ 'info' ] = doc_string
  74. return vim_data
  75. def _ConvertCompletionDatasToVimDatas( response_data ):
  76. return [ ConvertCompletionDataToVimData( x )
  77. for x in response_data ]