resolve_completion_request.py 3.6 KB

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