event_notification.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright (C) 2013 Google Inc.
  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 import vimsupport
  18. from ycmd.responses import UnknownExtraConf
  19. from ycm.client.base_request import ( BaseRequest, BuildRequestData,
  20. JsonFromFuture, HandleServerException )
  21. class EventNotification( BaseRequest ):
  22. def __init__( self, event_name, extra_data = None ):
  23. super( EventNotification, self ).__init__()
  24. self._event_name = event_name
  25. self._extra_data = extra_data
  26. self._cached_response = None
  27. def Start( self ):
  28. request_data = BuildRequestData()
  29. if self._extra_data:
  30. request_data.update( self._extra_data )
  31. request_data[ 'event_name' ] = self._event_name
  32. self._response_future = self.PostDataToHandlerAsync( request_data,
  33. 'event_notification' )
  34. def Done( self ):
  35. return self._response_future.done()
  36. def Response( self ):
  37. if self._cached_response:
  38. return self._cached_response
  39. if not self._response_future or self._event_name != 'FileReadyToParse':
  40. return []
  41. try:
  42. try:
  43. self._cached_response = JsonFromFuture( self._response_future )
  44. except UnknownExtraConf as e:
  45. if vimsupport.Confirm( str( e ) ):
  46. _LoadExtraConfFile( e.extra_conf_file )
  47. else:
  48. _IgnoreExtraConfFile( e.extra_conf_file )
  49. except Exception as e:
  50. HandleServerException( e )
  51. return self._cached_response if self._cached_response else []
  52. def SendEventNotificationAsync( event_name, extra_data = None ):
  53. event = EventNotification( event_name, extra_data )
  54. event.Start()
  55. def _LoadExtraConfFile( filepath ):
  56. BaseRequest.PostDataToHandler( { 'filepath': filepath },
  57. 'load_extra_conf_file' )
  58. def _IgnoreExtraConfFile( filepath ):
  59. BaseRequest.PostDataToHandler( { 'filepath': filepath },
  60. 'ignore_extra_conf_file' )