Skip to content

Commit e0805b8

Browse files
yegappanbrammool
authored andcommitted
patch 8.2.4761: documentation for using LSP messages is incomplete
Problem: Documentation for using LSP messages is incomplete. Solution: Update the documentation. (Yegappan Lakshmanan, closes #10206)
1 parent 9029a6e commit e0805b8

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

runtime/doc/channel.txt

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,8 @@ ch_evalexpr({handle}, {expr} [, {options}]) *ch_evalexpr()*
530530

531531
ch_evalexpr() waits for a response and returns the decoded
532532
expression. When there is an error or timeout it returns an
533-
empty string.
533+
empty |String| or, when using the "lsp" channel mode, returns an
534+
empty |Dict|.
534535

535536
Note that while waiting for the response, Vim handles other
536537
messages. You need to make sure this doesn't cause trouble.
@@ -702,7 +703,8 @@ ch_sendexpr({handle}, {expr} [, {options}]) *ch_sendexpr()*
702703
returns an empty String. If the "callback" item is present in
703704
{options}, then the returned Dict contains the ID of the
704705
request message. The ID can be used to send a cancellation
705-
request to the LSP server (if needed).
706+
request to the LSP server (if needed). Returns an empty Dict
707+
on error.
706708

707709
If a response message is not expected for {expr}, then don't
708710
specify the "callback" item in {options}.
@@ -1405,11 +1407,19 @@ payload encoded in JSON-RPC format. This is described in:
14051407

14061408
https://www.jsonrpc.org/specification
14071409

1408-
For messages received on a channel with mode set to "lsp", Vim will process
1409-
the HTTP header and decode the payload into a Vim |Dict| type and call the
1410-
channel callback or the specified callback function. When sending messages on
1411-
a channel using |ch_evalexpr()| or |ch_sendexpr()|, Vim will add the HTTP
1412-
header and encode the Vim expression into JSON-RPC.
1410+
To encode and send a LSP request/notification message in a Vim |Dict| into a
1411+
LSP JSON-RPC message and to receive and decode a LSP JSON-RPC
1412+
response/notification message into a Vim |Dict|, connect to the LSP server
1413+
with the |channel-mode| set to "lsp".
1414+
1415+
For messages received on a channel with |channel-mode| set to "lsp", Vim will
1416+
process the HTTP header and decode the JSON-RPC payload into a Vim |Dict| type
1417+
and call the |channel-callback| function or the specified
1418+
|channel-onetime-callback| function. When sending messages on a channel using
1419+
the |ch_evalexpr()| or |ch_sendexpr()| functions, Vim will add the HTTP header
1420+
and encode the Vim expression into JSON. Refer to |json_encode()| and
1421+
|json_decode()| for more information about how Vim encodes and decodes the
1422+
builtin types into JSON.
14131423

14141424
To open a channel using the 'lsp' mode, set the 'mode' item in the |ch_open()|
14151425
{options} argument to 'lsp'. Example: >
@@ -1419,54 +1429,64 @@ To open a channel using the 'lsp' mode, set the 'mode' item in the |ch_open()|
14191429
To open a channel using the 'lsp' mode with a job, set the 'in_mode' and
14201430
'out_mode' items in the |job_start()| {options} argument to 'lsp'. Example: >
14211431
1422-
let job = job_start(...., #{in_mode: 'lsp', out_mode: 'lsp'})
1432+
let cmd = ['clangd', '--background-index', '--clang-tidy']
1433+
let opts = {}
1434+
let opts.in_mode = 'lsp'
1435+
let opts.out_mode = 'lsp'
1436+
let opts.out_cb = function('LspOutCallback')
1437+
let opts.err_cb = function('LspErrCallback')
1438+
let opts.exit_cb = function('LspExitCallback')
1439+
let job = job_start(cmd, opts)
14231440
14241441
To synchronously send a JSON-RPC request to the server, use the
14251442
|ch_evalexpr()| function. This function will wait and return the decoded
14261443
response message from the server. You can use either the |channel-timeout| or
14271444
the 'timeout' field in the {options} argument to control the response wait
1428-
time. If the request times out, then an empty string is returned. Example: >
1445+
time. If the request times out, then an empty |Dict| is returned. Example: >
14291446
14301447
let req = {}
14311448
let req.method = 'textDocument/definition'
14321449
let req.params = {}
14331450
let req.params.textDocument = #{uri: 'a.c'}
14341451
let req.params.position = #{line: 10, character: 3}
1435-
let resp = ch_evalexpr(ch, req, #{timeout: 100})
1452+
let defs = ch_evalexpr(ch, req, #{timeout: 100})
1453+
if defs->empty()
1454+
... <handle failure>
1455+
endif
14361456
14371457
Note that in the request message the 'id' field should not be specified. If it
14381458
is specified, then Vim will overwrite the value with an internally generated
14391459
identifier. Vim currently supports only a number type for the 'id' field.
14401460
The callback function will be invoked for both a successful and a failed RPC
1441-
request. If the "id" field is present in the request message, then Vim will
1442-
overwrite it with an internally generated number. This function returns a
1443-
Dict with the identifier used for the message. This can be used to send
1444-
cancellation request to the LSP server (if needed).
1461+
request.
14451462

14461463
To send a JSON-RPC request to the server and asynchronously process the
1447-
response, use the |ch_sendexpr()| function and supply a callback function.
1448-
1449-
Example: >
1464+
response, use the |ch_sendexpr()| function and supply a callback function. If
1465+
the "id" field is present in the request message, then Vim will overwrite it
1466+
with an internally generated number. This function returns a Dict with the
1467+
identifier used for the message. This can be used to send cancellation
1468+
request to the LSP server (if needed). Example: >
14501469
14511470
let req = {}
14521471
let req.method = 'textDocument/hover'
14531472
let req.id = 200
14541473
let req.params = {}
14551474
let req.params.textDocument = #{uri: 'a.c'}
14561475
let req.params.position = #{line: 10, character: 3}
1457-
let resp = ch_sendexpr(ch, req, #{callback: 'MyFn'})
1476+
let resp = ch_sendexpr(ch, req, #{callback: 'HoverFunc'})
14581477
1459-
To cancel an outstanding LSP request sent to the server using the
1478+
To cancel an outstanding asynchronous LSP request sent to the server using the
14601479
|ch_sendexpr()| function, send a cancelation message to the server using the
1461-
|ch_sendexpr()| function with the ID returned by |ch_sendexpr()|. Example: >
1480+
|ch_sendexpr()| function with the ID returned by the |ch_sendexpr()| function
1481+
for the request. Example: >
14621482
14631483
" send a completion request
14641484
let req = {}
14651485
let req.method = 'textDocument/completion'
14661486
let req.params = {}
14671487
let req.params.textDocument = #{uri: 'a.c'}
14681488
let req.params.position = #{line: 10, character: 3}
1469-
let reqstatus = ch_sendexpr(ch, req, #{callback: 'MyComplete'})
1489+
let reqstatus = ch_sendexpr(ch, req, #{callback: 'LspComplete'})
14701490
" send a cancellation notification
14711491
let notif = {}
14721492
let notif.method = '$/cancelRequest'

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,8 @@ static char *(features[]) =
746746

747747
static int included_patches[] =
748748
{ /* Add new patch number below this line */
749+
/**/
750+
4761,
749751
/**/
750752
4760,
751753
/**/

0 commit comments

Comments
 (0)