event_notification.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 ycm.client.base_request import BaseRequest, BuildRequestData
  18. class EventNotification( BaseRequest ):
  19. def __init__( self, event_name, buffer_number = None, extra_data = None ):
  20. super( EventNotification, self ).__init__()
  21. self._event_name = event_name
  22. self._buffer_number = buffer_number
  23. self._extra_data = extra_data
  24. self._response_future = None
  25. self._cached_response = None
  26. def Start( self ):
  27. request_data = BuildRequestData( self._buffer_number )
  28. if self._extra_data:
  29. request_data.update( self._extra_data )
  30. request_data[ 'event_name' ] = self._event_name
  31. self._response_future = self.PostDataToHandlerAsync( request_data,
  32. 'event_notification' )
  33. def Done( self ):
  34. return bool( self._response_future ) and self._response_future.done()
  35. def Response( self ):
  36. if self._cached_response:
  37. return self._cached_response
  38. if not self._response_future or self._event_name != 'FileReadyToParse':
  39. return []
  40. self._cached_response = self.HandleFuture( self._response_future,
  41. truncate_message = True )
  42. return self._cached_response if self._cached_response else []
  43. def SendEventNotificationAsync( event_name,
  44. buffer_number = None,
  45. extra_data = None ):
  46. event = EventNotification( event_name, buffer_number, extra_data )
  47. event.Start()