messages_request.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright (C) 2017 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. from ycm.vimsupport import PostVimMessage
  19. import logging
  20. _logger = logging.getLogger( __name__ )
  21. # Looooong poll
  22. TIMEOUT_SECONDS = 60
  23. class MessagesPoll( BaseRequest ):
  24. def __init__( self, buff ):
  25. super( MessagesPoll, self ).__init__()
  26. self._request_data = BuildRequestData( buff.number )
  27. self._response_future = None
  28. def _SendRequest( self ):
  29. self._response_future = self.PostDataToHandlerAsync(
  30. self._request_data,
  31. 'receive_messages',
  32. timeout = TIMEOUT_SECONDS )
  33. return
  34. def Poll( self, diagnostics_handler ):
  35. """This should be called regularly to check for new messages in this buffer.
  36. Returns True if Poll should be called again in a while. Returns False when
  37. the completer or server indicated that further polling should not be done
  38. for the requested file."""
  39. if self._response_future is None:
  40. # First poll
  41. self._SendRequest()
  42. return True
  43. if not self._response_future.done():
  44. # Nothing yet...
  45. return True
  46. response = self.HandleFuture( self._response_future,
  47. display_message = False )
  48. if response is None:
  49. # Server returned an exception.
  50. return False
  51. poll_again = _HandlePollResponse( response, diagnostics_handler )
  52. if poll_again:
  53. self._SendRequest()
  54. return True
  55. return False
  56. def _HandlePollResponse( response, diagnostics_handler ):
  57. if isinstance( response, list ):
  58. for notification in response:
  59. if 'message' in notification:
  60. PostVimMessage( notification[ 'message' ],
  61. warning = False,
  62. truncate = True )
  63. elif 'diagnostics' in notification:
  64. diagnostics_handler.UpdateWithNewDiagnosticsForFile(
  65. notification[ 'filepath' ],
  66. notification[ 'diagnostics' ] )
  67. elif response is False:
  68. # Don't keep polling for this file
  69. return False
  70. # else any truthy response means "nothing to see here; poll again in a
  71. # while"
  72. # Start the next poll (only if the last poll didn't raise an exception)
  73. return True