Skip to content
This repository has been archived by the owner on Feb 1, 2019. It is now read-only.

Commit

Permalink
coalals.langserver: Add support for showMessageRequest
Browse files Browse the repository at this point in the history
Add support for window/showMessageRequest so that
coala-ls can interact with server when it requires.
  • Loading branch information
ksdme committed Jul 9, 2018
1 parent 8e75ead commit 99c0663
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
37 changes: 37 additions & 0 deletions coalals/langserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,40 @@ def send_diagnostics(self, path, diagnostics):
self._endpoint.notify(
'textDocument/publishDiagnostics',
params=params)

def send_show_message_req(self, message_type, message, actions):
"""
Send a showMessage request to the editor, this
can be useful for showing a dialog and getting
some user choice or action back.
(https://bit.ly/2J7TjX9)
:param message_type:
The type of message/severity of the dialog
in question. It can range from 1 - 4. Refer
to the LS docs for more detail.
:param message:
The message to display while providing the
choice.
:param actions:
A list/iterable of strings containing the
the various options to choose from.
:return:
Returns a Future that will be resolved with
error or result when the user chooses an
action or cancels it.
"""
if message_type > 4 or message_type < 1:
message_type = 4

params = {
'type': int(message_type),
'message': message,
'actions': list(map(lambda l: {'title': l, }, actions)),
}

req_future = self._endpoint.request(
'window/showMessageRequest',
params=params)

return req_future
31 changes: 31 additions & 0 deletions tests/tests_coalals/test_langserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,37 @@ def test_send_diagnostics(file_langserver, verify_publish_respone):
verify_publish_respone(file, langserver, 0)


@pytest.mark.parametrize('msg_type', [1, 5])
def test_send_show_message_req(file_langserver, verify_response, msg_type):
file, langserver = file_langserver

future = langserver.send_show_message_req(
msg_type,
'This is not a message.',
('Okay', 'Yo!'))

def _responder(file, request, passed, **kargs):
msg_id = request['id']

langserver._endpoint.consume({
'id': msg_id,
'result': {'title': 'Yo!', },
'jsonrpc': '2.0',
})

# now wait for the future to be resolved with
# the result of the selection
result = future.result()

assert result is not None
assert result['title'] == 'Yo!'

file.close()
passed[0] = True

verify_response(file, langserver, _responder)


def test_lanserver_shutdown(file_langserver):
file, langserver = file_langserver
langserver.m_shutdown()
Expand Down

0 comments on commit 99c0663

Please sign in to comment.