completion_request.py 3.4 KB

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