From 7053d468cc45b1afc18a12cc26dea0278fe9567c Mon Sep 17 00:00:00 2001 From: Martino Pilia Date: Fri, 31 May 2019 17:26:53 +0200 Subject: Add API for custom LSP requests Implement a function `ale#lsp_linter#SendRequest` that allows to send custom LSP requests to an enabled LSP linter. Resolves #2474 --- test/lsp/test_lsp_custom_request.vader | 120 +++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 test/lsp/test_lsp_custom_request.vader (limited to 'test/lsp') diff --git a/test/lsp/test_lsp_custom_request.vader b/test/lsp/test_lsp_custom_request.vader new file mode 100644 index 00000000..e5fecafc --- /dev/null +++ b/test/lsp/test_lsp_custom_request.vader @@ -0,0 +1,120 @@ +Before: + runtime autoload/ale/linter.vim + runtime autoload/ale/lsp.vim + runtime autoload/ale/lsp_linter.vim + + let g:address = 'ccls_address' + let g:callback_result = 0 + let g:conn_id = -1 + let g:executable = 'ccls' + let g:linter_name = 'ccls' + let g:magic_number = 42 + let g:message = -1 + let g:message_id = 1 + let g:method = '$ccls/call' + let g:parameters = {} + let g:project = '/project/root' + let g:return_value = -1 + + let g:linter_list = [{ + \ 'output_stream': 'stdout', + \ 'lint_file': 0, + \ 'language': 'cpp', + \ 'name': g:linter_name, + \ 'project_root': {b -> g:project}, + \ 'aliases': [], + \ 'language_callback': {b -> 'cpp'}, + \ 'read_buffer': 1, + \ 'command': '%e' + \ }] + + " Encode dictionary to jsonrpc + function! Encode(obj) abort + let l:body = json_encode(a:obj) + return 'Content-Length: ' . strlen(l:body) . "\r\n\r\n" . l:body + endfunction + + " Register the server with given executable or address + function! InitServer(executable_or_address) abort + let g:conn_id = ale#lsp#Register(a:executable_or_address, g:project, {}) + call ale#lsp#HandleMessage(g:conn_id, Encode({'method': 'initialize'})) + endfunction + + " Dummy callback + function! Callback(response) abort + let g:callback_result = a:response.result.value + endfunction + + " Replace the GetAll function to mock an LSP linter + function! ale#linter#GetAll(filetype) abort + return g:linter_list + endfunction + + " Replace the Send function to mock an LSP linter + function! ale#lsp#Send(conn_id, message) abort + let g:message = a:message + return g:message_id + endfunction + + " Code for a test case + function! TestCase() abort + " Test sending a custom request + let g:return_value = ale#lsp_linter#SendRequest(bufnr('%'), g:linter_name, g:method, g:parameters, function('Callback')) + + AssertEqual + \ 0, + \ g:return_value + + AssertEqual + \ [0, g:method, g:parameters], + \ g:message + + " Mock an incoming response to the request + call ale#lsp#HandleMessage(g:conn_id, Encode({'id': g:message_id, 'jsonrpc': '2.0', 'result': {'value': g:magic_number}})) + + AssertEqual + \ g:magic_number, + \ g:callback_result + endfunction + +After: + if g:conn_id isnot v:null + call ale#lsp#RemoveConnectionWithID(g:conn_id) + endif + + unlet! g:callback_result + unlet! g:conn_id + unlet! g:executable + unlet! g:linter_name + unlet! g:magic_number + unlet! g:message + unlet! g:message_id + unlet! g:method + unlet! g:parameters + unlet! g:project + unlet! g:return_value + + delfunction Encode + delfunction InitServer + delfunction Callback + delfunction TestCase + + runtime autoload/ale/linter.vim + runtime autoload/ale/lsp.vim + runtime autoload/ale/lsp_linter.vim + +Given cpp(Empty cpp file): +Execute(Test custom request to server identified by executable): + call InitServer(g:executable) + let g:linter_list[0].executable = {b -> g:executable} + let g:linter_list[0].lsp = 'stdio' + + call TestCase() + +Given cpp(Empty cpp file): +Execute(Test custom request to server identified by address): + call InitServer(g:address) + let g:linter_list[0].address = {b -> g:address} + let g:linter_list[0].lsp = 'socket' + + call TestCase() -- cgit v1.2.3 From 332168594001264e68601c03a6365626142be1fe Mon Sep 17 00:00:00 2001 From: Martino Pilia Date: Fri, 31 May 2019 21:56:38 +0200 Subject: Refactor LSP custom request handling --- test/lsp/test_lsp_custom_request.vader | 58 ++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 21 deletions(-) (limited to 'test/lsp') diff --git a/test/lsp/test_lsp_custom_request.vader b/test/lsp/test_lsp_custom_request.vader index e5fecafc..f50a4cf2 100644 --- a/test/lsp/test_lsp_custom_request.vader +++ b/test/lsp/test_lsp_custom_request.vader @@ -7,13 +7,15 @@ Before: let g:callback_result = 0 let g:conn_id = -1 let g:executable = 'ccls' + let g:executable_or_address = '' let g:linter_name = 'ccls' let g:magic_number = 42 - let g:message = -1 + let g:message_list = [] let g:message_id = 1 let g:method = '$ccls/call' let g:parameters = {} - let g:project = '/project/root' + let g:project_root = '/project/root' + let g:response = '' let g:return_value = -1 let g:linter_list = [{ @@ -21,7 +23,7 @@ Before: \ 'lint_file': 0, \ 'language': 'cpp', \ 'name': g:linter_name, - \ 'project_root': {b -> g:project}, + \ 'project_root': {b -> g:project_root}, \ 'aliases': [], \ 'language_callback': {b -> 'cpp'}, \ 'read_buffer': 1, @@ -34,10 +36,20 @@ Before: return 'Content-Length: ' . strlen(l:body) . "\r\n\r\n" . l:body endfunction - " Register the server with given executable or address - function! InitServer(executable_or_address) abort - let g:conn_id = ale#lsp#Register(a:executable_or_address, g:project, {}) + " Replace the StartLSP function to mock an LSP linter + function! ale#lsp_linter#StartLSP(buffer, linter, Callback) abort + let g:conn_id = ale#lsp#Register(g:executable_or_address, g:project_root, {}) + call ale#lsp#MarkDocumentAsOpen(g:conn_id, a:buffer) call ale#lsp#HandleMessage(g:conn_id, Encode({'method': 'initialize'})) + + let l:details = { + \ 'command': g:executable, + \ 'buffer': a:buffer, + \ 'connection_id': g:conn_id, + \ 'project_root': g:project_root, + \} + + call ale#lsp_linter#OnInit(a:linter, l:details, a:Callback) endfunction " Dummy callback @@ -52,25 +64,29 @@ Before: " Replace the Send function to mock an LSP linter function! ale#lsp#Send(conn_id, message) abort - let g:message = a:message + call add(g:message_list, a:message) return g:message_id endfunction " Code for a test case function! TestCase() abort " Test sending a custom request - let g:return_value = ale#lsp_linter#SendRequest(bufnr('%'), g:linter_name, g:method, g:parameters, function('Callback')) + let g:return_value = ale#lsp_linter#SendRequest( + \ bufnr('%'), + \ g:linter_name, + \ g:method, + \ g:parameters, + \ function('Callback')) - AssertEqual - \ 0, - \ g:return_value - - AssertEqual - \ [0, g:method, g:parameters], - \ g:message + Assert index(g:message_list, [0, g:method, g:parameters]) >= 0 " Mock an incoming response to the request - call ale#lsp#HandleMessage(g:conn_id, Encode({'id': g:message_id, 'jsonrpc': '2.0', 'result': {'value': g:magic_number}})) + let g:response = Encode({ + \ 'id': g:message_id, + \ 'jsonrpc': '2.0', + \ 'result': {'value': g:magic_number} + \ }) + call ale#lsp#HandleMessage(g:conn_id, g:response) AssertEqual \ g:magic_number, @@ -87,15 +103,15 @@ After: unlet! g:executable unlet! g:linter_name unlet! g:magic_number - unlet! g:message + unlet! g:message_list unlet! g:message_id unlet! g:method unlet! g:parameters - unlet! g:project + unlet! g:project_root + unlet! g:response unlet! g:return_value delfunction Encode - delfunction InitServer delfunction Callback delfunction TestCase @@ -105,7 +121,7 @@ After: Given cpp(Empty cpp file): Execute(Test custom request to server identified by executable): - call InitServer(g:executable) + let g:executable_or_address = g:executable let g:linter_list[0].executable = {b -> g:executable} let g:linter_list[0].lsp = 'stdio' @@ -113,7 +129,7 @@ Execute(Test custom request to server identified by executable): Given cpp(Empty cpp file): Execute(Test custom request to server identified by address): - call InitServer(g:address) + let g:executable_or_address = g:address let g:linter_list[0].address = {b -> g:address} let g:linter_list[0].lsp = 'socket' -- cgit v1.2.3 From 5542db1507edc054d654e748a2e7ccdd007e2f95 Mon Sep 17 00:00:00 2001 From: Martino Pilia Date: Sat, 1 Jun 2019 16:27:44 +0200 Subject: Support custom LSP notifications Allow to send custom notification mesages, that expect no response from the server. --- test/lsp/test_lsp_custom_request.vader | 39 +++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'test/lsp') diff --git a/test/lsp/test_lsp_custom_request.vader b/test/lsp/test_lsp_custom_request.vader index f50a4cf2..04f044af 100644 --- a/test/lsp/test_lsp_custom_request.vader +++ b/test/lsp/test_lsp_custom_request.vader @@ -4,12 +4,12 @@ Before: runtime autoload/ale/lsp_linter.vim let g:address = 'ccls_address' - let g:callback_result = 0 let g:conn_id = -1 let g:executable = 'ccls' let g:executable_or_address = '' let g:linter_name = 'ccls' let g:magic_number = 42 + let g:no_result = 0 let g:message_list = [] let g:message_id = 1 let g:method = '$ccls/call' @@ -30,6 +30,8 @@ Before: \ 'command': '%e' \ }] + let g:callback_result = g:no_result + " Encode dictionary to jsonrpc function! Encode(obj) abort let l:body = json_encode(a:obj) @@ -69,16 +71,15 @@ Before: endfunction " Code for a test case - function! TestCase() abort + function! TestCase(is_notification) abort " Test sending a custom request let g:return_value = ale#lsp_linter#SendRequest( \ bufnr('%'), \ g:linter_name, - \ g:method, - \ g:parameters, + \ [a:is_notification, g:method, g:parameters], \ function('Callback')) - Assert index(g:message_list, [0, g:method, g:parameters]) >= 0 + Assert index(g:message_list, [a:is_notification, g:method, g:parameters]) >= 0 " Mock an incoming response to the request let g:response = Encode({ @@ -89,7 +90,7 @@ Before: call ale#lsp#HandleMessage(g:conn_id, g:response) AssertEqual - \ g:magic_number, + \ a:is_notification ? g:no_result : g:magic_number, \ g:callback_result endfunction @@ -101,11 +102,13 @@ After: unlet! g:callback_result unlet! g:conn_id unlet! g:executable + unlet! g:is_notification unlet! g:linter_name unlet! g:magic_number unlet! g:message_list unlet! g:message_id unlet! g:method + unlet! g:no_result unlet! g:parameters unlet! g:project_root unlet! g:response @@ -124,13 +127,33 @@ Execute(Test custom request to server identified by executable): let g:executable_or_address = g:executable let g:linter_list[0].executable = {b -> g:executable} let g:linter_list[0].lsp = 'stdio' + let g:is_notification = 0 + + call TestCase(g:is_notification) + +Given cpp(Empty cpp file): +Execute(Test custom notification to server identified by executable): + let g:executable_or_address = g:executable + let g:linter_list[0].executable = {b -> g:executable} + let g:linter_list[0].lsp = 'stdio' + let g:is_notification = 1 - call TestCase() + call TestCase(g:is_notification) Given cpp(Empty cpp file): Execute(Test custom request to server identified by address): let g:executable_or_address = g:address let g:linter_list[0].address = {b -> g:address} let g:linter_list[0].lsp = 'socket' + let g:is_notification = 0 + + call TestCase(g:is_notification) + +Given cpp(Empty cpp file): +Execute(Test custom notification to server identified by address): + let g:executable_or_address = g:address + let g:linter_list[0].address = {b -> g:address} + let g:linter_list[0].lsp = 'socket' + let g:is_notification = 1 - call TestCase() + call TestCase(g:is_notification) -- cgit v1.2.3