YouCompleteMe/python/ycm/client/messages_request.py
Ben Jackson 292de25c72 Add support for Java diagnostics and asynchronous messages
This implements an asynchronous message system using a long-poll request
to the server.

The server provides an endpoint /receive_messages which blocks until
either a timeout occurs or we receive a batch of asynchronous messages.
We send this request asynchronously and poll it 4 times a second to see
if we have received any messages.

The messages may either be simply for display (such as startup progress)
or diagnostics, which override the diagnostics returned by
OnFileReqdyToParse.

In the former case, we simply display the message, accepting that this
might be overwritten by any other message (indeed, requiring this), and
for the latter we fan out diagnostics to any open buffer for the file in
question.

Unfortunately, Vim has bugs related to timers when there is something
displayed (such as a "confirm" prompt or other), so we suspend
background timers when doing subcommands to avoid vim bugs. NOTE: This
requires a new version of Vim (detected by the presence of the
particular functions used).
2018-02-10 16:29:27 +00:00

98 lines
3.1 KiB
Python

# Copyright (C) 2017 YouCompleteMe contributors
#
# This file is part of YouCompleteMe.
#
# YouCompleteMe is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# YouCompleteMe is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
# Not installing aliases from python-future; it's unreliable and slow.
from builtins import * # noqa
from ycm.vimsupport import PostVimMessage
from ycm.client.base_request import ( BaseRequest, BuildRequestData,
JsonFromFuture, HandleServerException )
import logging
_logger = logging.getLogger( __name__ )
# Looooong poll
TIMEOUT_SECONDS = 60
class MessagesPoll( BaseRequest ):
def __init__( self ):
super( MessagesPoll, self ).__init__()
self._request_data = BuildRequestData()
self._response_future = None
def _SendRequest( self ):
self._response_future = self.PostDataToHandlerAsync(
self._request_data,
'receive_messages',
timeout = TIMEOUT_SECONDS )
return
def Poll( self, diagnostics_handler ):
"""This should be called regularly to check for new messages in this buffer.
Returns True if Poll should be called again in a while. Returns False when
the completer or server indicated that further polling should not be done
for the requested file."""
if self._response_future is None:
# First poll
self._SendRequest()
return True
if not self._response_future.done():
# Nothing yet...
return True
with HandleServerException( display = False ):
response = JsonFromFuture( self._response_future )
poll_again = _HandlePollResponse( response, diagnostics_handler )
if poll_again:
self._SendRequest()
return True
return False
def _HandlePollResponse( response, diagnostics_handler ):
if isinstance( response, list ):
for notification in response:
if 'message' in notification:
PostVimMessage( notification[ 'message' ],
warning = False,
truncate = True )
elif 'diagnostics' in notification:
diagnostics_handler.UpdateWithNewDiagnosticsForFile(
notification[ 'filepath' ],
notification[ 'diagnostics' ] )
elif response is False:
# Don't keep polling for this file
return False
# else any truthy response means "nothing to see here; poll again in a
# while"
# Start the next poll (only if the last poll didn't raise an exception)
return True