signature_help_request.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. def Start( self ):
  32. self._response_future = self.PostDataToHandlerAsync( self.request_data,
  33. 'signature_help' )
  34. def Done( self ):
  35. return bool( self._response_future ) and self._response_future.done()
  36. def Reset( self ):
  37. self._response_future = None
  38. def Response( self ):
  39. if not self._response_future:
  40. return {}
  41. response = self.HandleFuture( self._response_future,
  42. truncate_message = True )
  43. if not response:
  44. return {}
  45. # Vim may not be able to convert the 'errors' entry to its internal format
  46. # so we remove it from the response.
  47. errors = response.pop( 'errors', [] )
  48. for e in errors:
  49. exception = MakeServerException( e )
  50. _logger.error( exception )
  51. DisplayServerException( exception, truncate_message = True )
  52. return response.get( 'signature_help' ) or {}
  53. class SignatureHelpAvailableRequest( BaseRequest ):
  54. def __init__( self, filetype ):
  55. super( SignatureHelpAvailableRequest, self ).__init__()
  56. self._response_future = None
  57. self.Start( filetype )
  58. def Done( self ):
  59. return bool( self._response_future ) and self._response_future.done()
  60. def Response( self ):
  61. if not self._response_future:
  62. return None
  63. response = self.HandleFuture( self._response_future,
  64. truncate_message = True )
  65. if not response:
  66. return None
  67. return response[ 'available' ]
  68. def Start( self, filetype ):
  69. self._response_future = self.GetDataFromHandlerAsync(
  70. 'signature_help_available',
  71. payload = { 'subserver': filetype } )