completion_request.py 3.2 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. import os
  24. TIMEOUT_SECONDS = 0.5
  25. class CompletionRequest( BaseRequest ):
  26. def __init__( self, request_data ):
  27. super( CompletionRequest, self ).__init__()
  28. self.request_data = request_data
  29. def Start( self ):
  30. self._response_future = self.PostDataToHandlerAsync( self.request_data,
  31. 'completions',
  32. TIMEOUT_SECONDS )
  33. def Done( self ):
  34. return self._response_future.done()
  35. def RawResponse( self ):
  36. if not self._response_future:
  37. return []
  38. try:
  39. response = JsonFromFuture( self._response_future )
  40. errors = response['errors'] if 'errors' in response else []
  41. for e in errors:
  42. HandleServerException( MakeServerException( e ) )
  43. return JsonFromFuture( self._response_future )[ 'completions' ]
  44. except Exception as e:
  45. HandleServerException( e )
  46. return []
  47. def Response( self ):
  48. return _ConvertCompletionDatasToVimDatas( self.RawResponse() )
  49. def ConvertCompletionDataToVimData( completion_data ):
  50. # see :h complete-items for a description of the dictionary fields
  51. vim_data = {
  52. 'word' : ToUtf8IfNeeded( completion_data[ 'insertion_text' ] ),
  53. 'dup' : 1,
  54. }
  55. if ( 'extra_data' in completion_data and
  56. 'doc_string' in completion_data[ 'extra_data' ] ):
  57. doc_string = ToUtf8IfNeeded(
  58. completion_data[ 'extra_data' ][ 'doc_string' ] )
  59. else:
  60. doc_string = ""
  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' ] += os.linesep + 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 ]