1
0

completion_request.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 ( 'extra_data' in completion_data and
  55. 'doc_string' in completion_data[ 'extra_data' ] ):
  56. doc_string = ToUtf8IfNeeded(
  57. completion_data[ 'extra_data' ][ 'doc_string' ] )
  58. else:
  59. doc_string = ""
  60. if 'menu_text' in completion_data:
  61. vim_data[ 'abbr' ] = ToUtf8IfNeeded( completion_data[ 'menu_text' ] )
  62. if 'extra_menu_info' in completion_data:
  63. vim_data[ 'menu' ] = ToUtf8IfNeeded( completion_data[ 'extra_menu_info' ] )
  64. if 'kind' in completion_data:
  65. vim_data[ 'kind' ] = ToUtf8IfNeeded(
  66. completion_data[ 'kind' ] )[ 0 ].lower()
  67. if 'detailed_info' in completion_data:
  68. vim_data[ 'info' ] = ToUtf8IfNeeded( completion_data[ 'detailed_info' ] )
  69. if doc_string:
  70. vim_data[ 'info' ] += '\n' + doc_string
  71. elif doc_string:
  72. vim_data[ 'info' ] = doc_string
  73. return vim_data
  74. def _ConvertCompletionDatasToVimDatas( response_data ):
  75. return [ ConvertCompletionDataToVimData( x )
  76. for x in response_data ]