completion_request.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. # Not installing aliases from python-future; it's unreliable and slow.
  22. from builtins import * # noqa
  23. from ycmd.utils import ToUnicode
  24. from ycm.client.base_request import ( BaseRequest, JsonFromFuture,
  25. HandleServerException,
  26. MakeServerException )
  27. class CompletionRequest( BaseRequest ):
  28. def __init__( self, request_data ):
  29. super( CompletionRequest, self ).__init__()
  30. self.request_data = request_data
  31. self._response_future = None
  32. self._response = { 'completions': [], 'completion_start_column': -1 }
  33. def Start( self ):
  34. self._response_future = self.PostDataToHandlerAsync( self.request_data,
  35. 'completions' )
  36. def Done( self ):
  37. return bool( self._response_future ) and self._response_future.done()
  38. def RawResponse( self ):
  39. if not self._response_future:
  40. return self._response
  41. with HandleServerException( truncate = True ):
  42. self._response = JsonFromFuture( self._response_future )
  43. # Vim may not be able to convert the 'errors' entry to its internal format
  44. # so we remove it from the response.
  45. errors = self._response.pop( 'errors', [] )
  46. for e in errors:
  47. with HandleServerException( truncate = True ):
  48. raise MakeServerException( e )
  49. return self._response
  50. def Response( self ):
  51. response = self.RawResponse()
  52. response[ 'completions' ] = _ConvertCompletionDatasToVimDatas(
  53. response[ 'completions' ] )
  54. return response
  55. def ConvertCompletionDataToVimData( completion_identifier, completion_data ):
  56. # see :h complete-items for a description of the dictionary fields
  57. vim_data = {
  58. 'word' : '',
  59. 'dup' : 1,
  60. 'empty' : 1,
  61. }
  62. if ( 'extra_data' in completion_data and
  63. 'doc_string' in completion_data[ 'extra_data' ] ):
  64. doc_string = completion_data[ 'extra_data' ][ 'doc_string' ]
  65. else:
  66. doc_string = ""
  67. if 'insertion_text' in completion_data:
  68. vim_data[ 'word' ] = completion_data[ 'insertion_text' ]
  69. if 'menu_text' in completion_data:
  70. vim_data[ 'abbr' ] = completion_data[ 'menu_text' ]
  71. if 'extra_menu_info' in completion_data:
  72. vim_data[ 'menu' ] = completion_data[ 'extra_menu_info' ]
  73. if 'kind' in completion_data:
  74. kind = ToUnicode( completion_data[ 'kind' ] )
  75. if kind:
  76. vim_data[ 'kind' ] = kind[ 0 ].lower()
  77. if 'detailed_info' in completion_data:
  78. vim_data[ 'info' ] = completion_data[ 'detailed_info' ]
  79. if doc_string:
  80. vim_data[ 'info' ] += '\n' + doc_string
  81. elif doc_string:
  82. vim_data[ 'info' ] = doc_string
  83. # We store the completion item index as a string in the completion user_data.
  84. # This allows us to identify the _exact_ item that was completed in the
  85. # CompleteDone handler, by inspecting this item from v:completed_item
  86. #
  87. # We convert to string because completion user data items must be strings.
  88. #
  89. # Note: Not all versions of Vim support this (added in 8.0.1483), but adding
  90. # the item to the dictionary is harmless in earlier Vims.
  91. vim_data[ 'user_data' ] = str( completion_identifier )
  92. return vim_data
  93. def _ConvertCompletionDatasToVimDatas( response_data ):
  94. return [ ConvertCompletionDataToVimData( i, x )
  95. for i, x in enumerate( response_data ) ]