resolve_completion_request.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright (C) 2020 YouCompleteMe contributors
  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 ycm.client.base_request import ( BaseRequest,
  18. DisplayServerException,
  19. MakeServerException )
  20. from ycm.client.completion_request import ConvertCompletionDataToVimData
  21. import logging
  22. import json
  23. _logger = logging.getLogger( __name__ )
  24. class ResolveCompletionRequest( BaseRequest ):
  25. def __init__( self, request_data ):
  26. super().__init__()
  27. self.request_data = request_data
  28. def Start( self ):
  29. self._response_future = self.PostDataToHandlerAsync( self.request_data,
  30. 'resolve_completion' )
  31. def Done( self ):
  32. return bool( self._response_future ) and self._response_future.done()
  33. def OnCompleteDone( self ):
  34. # This is required to be compatible with the "CompletionRequest" API. We're
  35. # not really a CompletionRequest, but we are mutually exclusive with
  36. # completion requests, so we impleent this API.
  37. pass
  38. def Response( self ):
  39. response = self.HandleFuture( self._response_future,
  40. truncate_message = True,
  41. display_message = True )
  42. if not response or not response[ 'completion' ]:
  43. return { 'completion': [] }
  44. # Vim may not be able to convert the 'errors' entry to its internal format
  45. # so we remove it from the response.
  46. errors = response.pop( 'errors', [] )
  47. for e in errors:
  48. exception = MakeServerException( e )
  49. _logger.error( exception )
  50. DisplayServerException( exception, truncate_message = True )
  51. response[ 'completion' ] = ConvertCompletionDataToVimData(
  52. response[ 'completion' ] )
  53. return response
  54. def ResolveCompletionItem( completion_request, item ):
  55. if not completion_request.Done():
  56. return None
  57. try:
  58. completion_extra_data = json.loads( item[ 'user_data' ] )
  59. except KeyError:
  60. return None
  61. except ( TypeError, json.JSONDecodeError ):
  62. # Can happen with the omni completer
  63. return None
  64. request_data = completion_request.request_data
  65. try:
  66. # Note: We mutate the request_data inside the original completion request
  67. # and pass it into the new request object. this is just a big efficiency
  68. # saving. The request_data for a Done() request is almost certainly no
  69. # longer needed.
  70. request_data[ 'resolve' ] = completion_extra_data[ 'resolve' ]
  71. except KeyError:
  72. return None
  73. resolve_request = ResolveCompletionRequest( request_data )
  74. resolve_request.Start()
  75. return resolve_request