signature_help_request.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright (C) 2019 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. import logging
  18. from ycm.client.base_request import ( BaseRequest, DisplayServerException,
  19. MakeServerException )
  20. _logger = logging.getLogger( __name__ )
  21. class SigHelpAvailableByFileType( dict ):
  22. def __missing__( self, filetype ):
  23. request = SignatureHelpAvailableRequest( filetype )
  24. self[ filetype ] = request
  25. return request
  26. class SignatureHelpRequest( BaseRequest ):
  27. def __init__( self, request_data ):
  28. super( SignatureHelpRequest, self ).__init__()
  29. self.request_data = request_data
  30. self._response_future = None
  31. self._response = None
  32. def Start( self ):
  33. self._response_future = self.PostDataToHandlerAsync( self.request_data,
  34. 'signature_help' )
  35. def Done( self ):
  36. return bool( self._response_future ) and self._response_future.done()
  37. def Reset( self ):
  38. self._response_future = None
  39. def Response( self ):
  40. if self._response is None:
  41. self._response = self._Response()
  42. return self._response
  43. def _Response( self ):
  44. if not self._response_future:
  45. return {}
  46. response = self.HandleFuture( self._response_future,
  47. truncate_message = True )
  48. if not response:
  49. return {}
  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. return response.get( 'signature_help' ) or {}
  58. class SignatureHelpAvailableRequest( BaseRequest ):
  59. def __init__( self, filetype ):
  60. super( SignatureHelpAvailableRequest, self ).__init__()
  61. self._response_future = None
  62. self.Start( filetype )
  63. def Done( self ):
  64. return bool( self._response_future ) and self._response_future.done()
  65. def Response( self ):
  66. if not self._response_future:
  67. return None
  68. response = self.HandleFuture( self._response_future,
  69. truncate_message = True )
  70. if not response:
  71. return None
  72. return response[ 'available' ]
  73. def Start( self, filetype ):
  74. self._response_future = self.GetDataFromHandlerAsync(
  75. 'signature_help_available',
  76. payload = { 'subserver': filetype } )