messages_request.py 3.1 KB

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