event_notification.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (C) 2013-2018 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 __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 ycm.client.base_request import ( BaseRequest, BuildRequestData,
  24. JsonFromFuture, HandleServerException )
  25. class EventNotification( BaseRequest ):
  26. def __init__( self, event_name, buffer_number = None, extra_data = None ):
  27. super( EventNotification, self ).__init__()
  28. self._event_name = event_name
  29. self._buffer_number = buffer_number
  30. self._extra_data = extra_data
  31. self._response_future = None
  32. self._cached_response = None
  33. def Start( self ):
  34. request_data = BuildRequestData( self._buffer_number )
  35. if self._extra_data:
  36. request_data.update( self._extra_data )
  37. request_data[ 'event_name' ] = self._event_name
  38. self._response_future = self.PostDataToHandlerAsync( request_data,
  39. 'event_notification' )
  40. def Done( self ):
  41. return bool( self._response_future ) and self._response_future.done()
  42. def Response( self ):
  43. if self._cached_response:
  44. return self._cached_response
  45. if not self._response_future or self._event_name != 'FileReadyToParse':
  46. return []
  47. with HandleServerException( truncate = True ):
  48. self._cached_response = JsonFromFuture( self._response_future )
  49. return self._cached_response if self._cached_response else []
  50. def SendEventNotificationAsync( event_name,
  51. buffer_number = None,
  52. extra_data = None ):
  53. event = EventNotification( event_name, buffer_number, extra_data )
  54. event.Start()