1
0
forked from aniani/vim

patch 8.2.4758: when using an LSP channel want to get the message ID

Problem:    When using an LSP channel want to get the message ID.
Solution:   Have ch_sendexpr() return the ID. (Yegappan Lakshmanan,
            closes #10202)
This commit is contained in:
Yegappan Lakshmanan
2022-04-16 10:41:27 +01:00
committed by Bram Moolenaar
parent b9e99e58bb
commit 3b470ae88f
5 changed files with 146 additions and 35 deletions

View File

@@ -4520,6 +4520,7 @@ ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
ch_part_T part_read;
jobopt_T opt;
int timeout;
int callback_present = FALSE;
// return an empty string by default
rettv->v_type = VAR_STRING;
@@ -4546,7 +4547,9 @@ ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
{
dict_T *d;
dictitem_T *di;
int callback_present = FALSE;
// return an empty dict by default
rettv_dict_alloc(rettv);
if (argvars[1].v_type != VAR_DICT)
{
@@ -4629,6 +4632,14 @@ ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
}
}
free_job_options(&opt);
if (ch_mode == MODE_LSP && !eval && callback_present)
{
// if ch_sendexpr() is used to send a LSP message and a callback
// function is specified, then return the generated identifier for the
// message. The user can use this to cancel the request (if needed).
if (rettv->vval.v_dict != NULL)
dict_add_number(rettv->vval.v_dict, "id", id);
}
}
/*

View File

@@ -1660,7 +1660,7 @@ static funcentry_T global_functions[] =
{"ch_readraw", 1, 2, FEARG_1, arg2_chan_or_job_dict,
ret_string, JOB_FUNC(f_ch_readraw)},
{"ch_sendexpr", 2, 3, FEARG_1, arg23_chanexpr,
ret_void, JOB_FUNC(f_ch_sendexpr)},
ret_any, JOB_FUNC(f_ch_sendexpr)},
{"ch_sendraw", 2, 3, FEARG_1, arg23_chanraw,
ret_void, JOB_FUNC(f_ch_sendraw)},
{"ch_setoptions", 2, 2, FEARG_1, arg2_chan_or_job_dict,

View File

@@ -2494,9 +2494,10 @@ func LspTests(port)
" Wrong payload notification test
let g:lspNotif = []
call ch_sendexpr(ch, #{method: 'wrong-payload', params: {}})
let r = ch_sendexpr(ch, #{method: 'wrong-payload', params: {}})
call assert_equal({}, r)
" Send a ping to wait for all the notification messages to arrive
call ch_evalexpr(ch, #{method: 'ping'})
call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
call assert_equal([#{jsonrpc: '2.0', result: 'wrong-payload'}], g:lspNotif)
" Test for receiving a response with incorrect 'id' and additional
@@ -2516,14 +2517,14 @@ func LspTests(port)
let g:lspNotif = []
call ch_sendexpr(ch, #{method: 'simple-notif', params: [#{a: 10, b: []}]})
" Send a ping to wait for all the notification messages to arrive
call ch_evalexpr(ch, #{method: 'ping'})
call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
call assert_equal([#{jsonrpc: '2.0', result: 'simple-notif'}], g:lspNotif)
" multiple notifications test
let g:lspNotif = []
call ch_sendexpr(ch, #{method: 'multi-notif', params: [#{a: {}, b: {}}]})
" Send a ping to wait for all the notification messages to arrive
call ch_evalexpr(ch, #{method: 'ping'})
call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
call assert_equal([#{jsonrpc: '2.0', result: 'multi-notif1'},
\ #{jsonrpc: '2.0', result: 'multi-notif2'}], g:lspNotif)
@@ -2531,7 +2532,7 @@ func LspTests(port)
let g:lspNotif = []
call ch_sendexpr(ch, #{method: 'msg-with-id', id: 93, params: #{s: 'str'}})
" Send a ping to wait for all the notification messages to arrive
call ch_evalexpr(ch, #{method: 'ping'})
call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
call assert_equal([#{jsonrpc: '2.0', id: 93, result: 'msg-with-id'}],
\ g:lspNotif)
@@ -2541,16 +2542,17 @@ func LspTests(port)
" Test for using a one time callback function to process a response
let g:lspOtMsgs = []
call ch_sendexpr(ch, #{method: 'msg-specifc-cb', params: {}},
let r = ch_sendexpr(ch, #{method: 'msg-specifc-cb', params: {}},
\ #{callback: 'LspOtCb'})
call ch_evalexpr(ch, #{method: 'ping'})
call assert_equal(9, r.id)
call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
call assert_equal([#{id: 9, jsonrpc: '2.0', result: 'msg-specifc-cb'}],
\ g:lspOtMsgs)
" Test for generating a request message from the other end (server)
let g:lspNotif = []
call ch_sendexpr(ch, #{method: 'server-req', params: #{}})
call ch_evalexpr(ch, #{method: 'ping'})
call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
call assert_equal([{'id': 201, 'jsonrpc': '2.0',
\ 'result': {'method': 'checkhealth', 'params': {'a': 20}}}],
\ g:lspNotif)
@@ -2559,7 +2561,7 @@ func LspTests(port)
let g:lspNotif = []
call ch_sendexpr(ch, #{method: 'echo', params: #{s: 'msg-without-id'}})
" Send a ping to wait for all the notification messages to arrive
call ch_evalexpr(ch, #{method: 'ping'})
call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
call assert_equal([#{jsonrpc: '2.0', result:
\ #{method: 'echo', jsonrpc: '2.0', params: #{s: 'msg-without-id'}}}],
\ g:lspNotif)
@@ -2568,7 +2570,7 @@ func LspTests(port)
let g:lspNotif = []
call ch_sendexpr(ch, #{method: 'echo', id: 110, params: #{s: 'msg-with-id'}})
" Send a ping to wait for all the notification messages to arrive
call ch_evalexpr(ch, #{method: 'ping'})
call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
call assert_equal([#{jsonrpc: '2.0', result:
\ #{method: 'echo', jsonrpc: '2.0', id: 110,
\ params: #{s: 'msg-with-id'}}}], g:lspNotif)
@@ -2581,46 +2583,41 @@ func LspTests(port)
" Test for processing a HTTP header without the Content-Length field
let resp = ch_evalexpr(ch, #{method: 'hdr-without-len', params: {}},
\ #{timeout: 200})
call assert_equal('', resp)
call assert_equal({}, resp)
" send a ping to make sure communication still works
let resp = ch_evalexpr(ch, #{method: 'ping'})
call assert_equal({'id': 16, 'jsonrpc': '2.0', 'result': 'alive'}, resp)
call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
" Test for processing a HTTP header with wrong length
let resp = ch_evalexpr(ch, #{method: 'hdr-with-wrong-len', params: {}},
\ #{timeout: 200})
call assert_equal('', resp)
call assert_equal({}, resp)
" send a ping to make sure communication still works
let resp = ch_evalexpr(ch, #{method: 'ping'})
call assert_equal({'id': 18, 'jsonrpc': '2.0', 'result': 'alive'}, resp)
call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
" Test for processing a HTTP header with negative length
let resp = ch_evalexpr(ch, #{method: 'hdr-with-negative-len', params: {}},
\ #{timeout: 200})
call assert_equal('', resp)
call assert_equal({}, resp)
" send a ping to make sure communication still works
let resp = ch_evalexpr(ch, #{method: 'ping'})
call assert_equal({'id': 20, 'jsonrpc': '2.0', 'result': 'alive'}, resp)
call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
" Test for an empty header
let resp = ch_evalexpr(ch, #{method: 'empty-header', params: {}},
\ #{timeout: 200})
call assert_equal('', resp)
call assert_equal({}, resp)
" send a ping to make sure communication still works
let resp = ch_evalexpr(ch, #{method: 'ping'})
call assert_equal({'id': 22, 'jsonrpc': '2.0', 'result': 'alive'}, resp)
call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
" Test for an empty payload
let resp = ch_evalexpr(ch, #{method: 'empty-payload', params: {}},
\ #{timeout: 200})
call assert_equal('', resp)
call assert_equal({}, resp)
" send a ping to make sure communication still works
let resp = ch_evalexpr(ch, #{method: 'ping'})
call assert_equal({'id': 24, 'jsonrpc': '2.0', 'result': 'alive'}, resp)
call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
" Test for invoking an unsupported method
let resp = ch_evalexpr(ch, #{method: 'xyz', params: {}}, #{timeout: 200})
call assert_equal('', resp)
call assert_equal({}, resp)
" Test for sending a message without a callback function. Notification
" message should be dropped but RPC response should not be dropped.
@@ -2628,14 +2625,14 @@ func LspTests(port)
let g:lspNotif = []
call ch_sendexpr(ch, #{method: 'echo', params: #{s: 'no-callback'}})
" Send a ping to wait for all the notification messages to arrive
call ch_evalexpr(ch, #{method: 'ping'})
call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
call assert_equal([], g:lspNotif)
" Restore the callback function
call ch_setoptions(ch, #{callback: 'LspCb'})
let g:lspNotif = []
call ch_sendexpr(ch, #{method: 'echo', params: #{s: 'no-callback'}})
" Send a ping to wait for all the notification messages to arrive
call ch_evalexpr(ch, #{method: 'ping'})
call assert_equal('alive', ch_evalexpr(ch, #{method: 'ping'}).result)
call assert_equal([#{jsonrpc: '2.0', result:
\ #{method: 'echo', jsonrpc: '2.0', params: #{s: 'no-callback'}}}],
\ g:lspNotif)

View File

@@ -746,6 +746,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
4758,
/**/
4757,
/**/