From: Yegappan Lakshmanan Date: Sun, 6 Feb 2022 18:25:15 +0000 (-0800) Subject: When running tests, make the LSP calls synchronous X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=f5eb543de1d9967a396281524991978fb58446ec;p=vim-lsp.git When running tests, make the LSP calls synchronous --- diff --git a/autoload/lspserver.vim b/autoload/lspserver.vim index 3da5239..9a71724 100644 --- a/autoload/lspserver.vim +++ b/autoload/lspserver.vim @@ -431,6 +431,10 @@ def s:getCompletion(lspserver: dict, triggerKind_arg: number): void req.params.context = {triggerKind: triggerKind_arg} lspserver.sendMessage(req) + if exists('g:LSPTest') && g:LSPTest + # When running LSP tests, make this a synchronous call + lspserver.waitForReponse(req) + endif enddef # Request: "textDocument/definition" @@ -453,7 +457,7 @@ def s:gotoDefinition(lspserver: dict, peek: bool) req.params->extend(s:getLspTextDocPosition()) lspserver.sendMessage(req) - s:waitForReponse(lspserver, req) + lspserver.waitForReponse(req) enddef # Request: "textDocument/declaration" @@ -478,7 +482,7 @@ def s:gotoDeclaration(lspserver: dict, peek: bool): void lspserver.sendMessage(req) - s:waitForReponse(lspserver, req) + lspserver.waitForReponse(req) enddef # Request: "textDocument/typeDefinition" @@ -503,7 +507,7 @@ def s:gotoTypeDef(lspserver: dict, peek: bool): void lspserver.sendMessage(req) - s:waitForReponse(lspserver, req) + lspserver.waitForReponse(req) enddef # Request: "textDocument/implementation" @@ -528,7 +532,7 @@ def s:gotoImplementation(lspserver: dict, peek: bool): void lspserver.sendMessage(req) - s:waitForReponse(lspserver, req) + lspserver.waitForReponse(req) enddef # get symbol signature help. @@ -547,6 +551,11 @@ def s:showSignature(lspserver: dict): void req.params->extend(s:getLspTextDocPosition()) lspserver.sendMessage(req) + + if exists('g:LSPTest') && g:LSPTest + # When running LSP tests, make this a synchronous call + lspserver.waitForReponse(req) + endif enddef def s:didSaveFile(lspserver: dict, bnr: number): void @@ -580,6 +589,10 @@ def s:hover(lspserver: dict): void # interface TextDocumentPositionParams req.params->extend(s:getLspTextDocPosition()) lspserver.sendMessage(req) + if exists('g:LSPTest') && g:LSPTest + # When running LSP tests, make this a synchronous call + lspserver.waitForReponse(req) + endif enddef # Request: "textDocument/references" @@ -600,6 +613,10 @@ def s:showReferences(lspserver: dict, peek: bool): void lspserver.peekSymbol = peek lspserver.sendMessage(req) + if exists('g:LSPTest') && g:LSPTest + # When running LSP tests, make this a synchronous call + lspserver.waitForReponse(req) + endif enddef # Request: "textDocument/documentHighlight" @@ -617,6 +634,10 @@ def s:docHighlight(lspserver: dict): void # interface TextDocumentPositionParams req.params->extend(s:getLspTextDocPosition()) lspserver.sendMessage(req) + if exists('g:LSPTest') && g:LSPTest + # When running LSP tests, make this a synchronous call + lspserver.waitForReponse(req) + endif enddef # Request: "textDocument/documentSymbol" @@ -634,6 +655,10 @@ def s:getDocSymbols(lspserver: dict, fname: string): void # interface TextDocumentIdentifier req.params->extend({textDocument: {uri: util.LspFileToUri(fname)}}) lspserver.sendMessage(req) + if exists('g:LSPTest') && g:LSPTest + # When running LSP tests, make this a synchronous call + lspserver.waitForReponse(req) + endif enddef # Request: "textDocument/formatting" @@ -684,6 +709,10 @@ def s:textDocFormat(lspserver: dict, fname: string, rangeFormat: bool, endif lspserver.sendMessage(req) + if exists('g:LSPTest') && g:LSPTest + # When running LSP tests, make this a synchronous call + lspserver.waitForReponse(req) + endif enddef # Request: "textDocument/prepareCallHierarchy" @@ -720,6 +749,10 @@ def s:incomingCalls(lspserver: dict, hierItem: dict) # interface CallHierarchyItem req.params->extend({item: hierItem}) lspserver.sendMessage(req) + if exists('g:LSPTest') && g:LSPTest + # When running LSP tests, make this a synchronous call + lspserver.waitForReponse(req) + endif enddef # Request: "callHierarchy/outgoingCalls" @@ -738,6 +771,10 @@ def s:outgoingCalls(lspserver: dict, hierItem: dict) # interface CallHierarchyItem req.params->extend({item: hierItem}) lspserver.sendMessage(req) + if exists('g:LSPTest') && g:LSPTest + # When running LSP tests, make this a synchronous call + lspserver.waitForReponse(req) + endif enddef # Request: "textDocument/rename" @@ -756,6 +793,10 @@ def s:renameSymbol(lspserver: dict, newName: string) req.params = s:getLspTextDocPosition() req.params.newName = newName lspserver.sendMessage(req) + if exists('g:LSPTest') && g:LSPTest + # When running LSP tests, make this a synchronous call + lspserver.waitForReponse(req) + endif enddef # Request: "textDocument/codeAction" @@ -786,6 +827,10 @@ def s:codeAction(lspserver: dict, fname_arg: string) req.params->extend({context: {diagnostics: d}}) lspserver.sendMessage(req) + if exists('g:LSPTest') && g:LSPTest + # When running LSP tests, make this a synchronous call + lspserver.waitForReponse(req) + endif enddef # List project-wide symbols matching query string @@ -879,7 +924,7 @@ def s:selectionRange(lspserver: dict, fname: string) req.params->extend({textDocument: {uri: util.LspFileToUri(fname)}, positions: [s:getLspPosition()]}) lspserver.sendMessage(req) - s:waitForReponse(lspserver, req) + lspserver.waitForReponse(req) enddef # Expand the previous selection or start a new one @@ -922,6 +967,10 @@ def s:foldRange(lspserver: dict, fname: string) # interface TextDocumentIdentifier req.params->extend({textDocument: {uri: util.LspFileToUri(fname)}}) lspserver.sendMessage(req) + if exists('g:LSPTest') && g:LSPTest + # When running LSP tests, make this a synchronous call + lspserver.waitForReponse(req) + endif enddef # Request the LSP server to execute a command @@ -931,6 +980,10 @@ def s:executeCommand(lspserver: dict, cmd: dict) var req = lspserver.createRequest('workspace/executeCommand') req.params->extend(cmd) lspserver.sendMessage(req) + if exists('g:LSPTest') && g:LSPTest + # When running LSP tests, make this a synchronous call + lspserver.waitForReponse(req) + endif enddef # Display the LSP server capabilities (received during the initialization @@ -977,6 +1030,7 @@ export def NewLspServer(path: string, args: list): dict createNotification: function('s:createNotification', [lspserver]), sendResponse: function('s:sendResponse', [lspserver]), sendMessage: function('s:sendMessage', [lspserver]), + waitForReponse: function('s:waitForReponse', [lspserver]), processReply: function(handlers.ProcessReply, [lspserver]), processNotif: function(handlers.ProcessNotif, [lspserver]), processRequest: function(handlers.ProcessRequest, [lspserver]), diff --git a/test/unit_tests.vim b/test/unit_tests.vim index 5d4dc9f..03b7095 100644 --- a/test/unit_tests.vim +++ b/test/unit_tests.vim @@ -88,17 +88,16 @@ endfunc # Test for formatting a file using LspFormat def Test_LspFormat() :silent! edit Xtest.c + sleep 200m setline(1, [' int i;', ' int j;']) :redraw! :LspFormat - :sleep 1 assert_equal(['int i;', 'int j;'], getline(1, '$')) deletebufline('', 1, '$') setline(1, ['int f1(int i)', '{', 'int j = 10; return j;', '}']) :redraw! :LspFormat - :sleep 500m assert_equal(['int f1(int i) {', ' int j = 10;', ' return j;', '}'], getline(1, '$')) @@ -106,35 +105,30 @@ def Test_LspFormat() setline(1, ['', 'int i;']) :redraw! :LspFormat - :sleep 500m assert_equal(['', 'int i;'], getline(1, '$')) deletebufline('', 1, '$') setline(1, [' int i;']) :redraw! :LspFormat - :sleep 500m assert_equal(['int i;'], getline(1, '$')) deletebufline('', 1, '$') setline(1, [' int i; ']) :redraw! :LspFormat - :sleep 500m assert_equal(['int i;'], getline(1, '$')) deletebufline('', 1, '$') setline(1, ['int i;', '', '', '']) :redraw! :LspFormat - :sleep 500m assert_equal(['int i;'], getline(1, '$')) deletebufline('', 1, '$') setline(1, ['int f1(){int x;int y;x=1;y=2;return x+y;}']) :redraw! :LspFormat - :sleep 500m var expected: list =<< trim END int f1() { int x; @@ -150,7 +144,6 @@ def Test_LspFormat() setline(1, ['', '', '', '']) :redraw! :LspFormat - :sleep 500m assert_equal([''], getline(1, '$')) deletebufline('', 1, '$') @@ -164,7 +157,6 @@ def Test_LspFormat() setline(1, lines) :redraw! :4LspFormat - :sleep 500m expected =<< trim END int f1() { int i, j; @@ -192,6 +184,7 @@ enddef # file using LSP def Test_LspShowReferences() :silent! edit Xtest.c + sleep 200m var lines: list =<< trim END int count; void redFunc() @@ -212,8 +205,7 @@ def Test_LspShowReferences() cursor(5, 2) var bnr: number = bufnr() :LspShowReferences - :sleep 1 - WaitForAssert(() => assert_equal('quickfix', getwinvar(winnr('$'), '&buftype'))) + assert_equal('quickfix', getwinvar(winnr('$'), '&buftype')) var qfl: list> = getloclist(0) assert_equal(bnr, qfl[0].bufnr) assert_equal(3, qfl->len()) @@ -223,8 +215,7 @@ def Test_LspShowReferences() :only cursor(1, 5) :LspShowReferences - :sleep 500m - WaitForAssert(() => assert_equal(1, getloclist(0)->len())) + assert_equal(1, getloclist(0)->len()) qfl = getloclist(0) assert_equal([1, 5], [qfl[0].lnum, qfl[0].col]) bw! @@ -243,6 +234,7 @@ enddef # Test for LSP diagnostics def Test_LspDiag() :silent! edit Xtest.c + sleep 200m var lines: list =<< trim END void blueFunc() { @@ -296,7 +288,7 @@ enddef # Test for :LspCodeAction def Test_LspCodeAction() silent! edit Xtest.c - sleep 500m + sleep 200m var lines: list =<< trim END void testFunc() { @@ -305,33 +297,29 @@ def Test_LspCodeAction() } END setline(1, lines) - sleep 500m + sleep 1 cursor(4, 1) redraw! g:LSPTest_CodeActionChoice = 1 :LspCodeAction - sleep 500m - WaitForAssert(() => assert_equal("\tcount = 20;", getline(4))) + assert_equal("\tcount = 20;", getline(4)) setline(4, "\tcount = 20:") cursor(4, 1) sleep 500m g:LSPTest_CodeActionChoice = 0 :LspCodeAction - sleep 500m - WaitForAssert(() => assert_equal("\tcount = 20:", getline(4))) + assert_equal("\tcount = 20:", getline(4)) g:LSPTest_CodeActionChoice = 2 cursor(4, 1) :LspCodeAction - sleep 500m - WaitForAssert(() => assert_equal("\tcount = 20:", getline(4))) + assert_equal("\tcount = 20:", getline(4)) g:LSPTest_CodeActionChoice = 1 cursor(4, 1) :LspCodeAction - sleep 500m - WaitForAssert(() => assert_equal("\tcount = 20;", getline(4))) + assert_equal("\tcount = 20;", getline(4)) bw! # empty file @@ -348,7 +336,7 @@ enddef # Test for :LspRename def Test_LspRename() silent! edit Xtest.c - sleep 1 + sleep 200m var lines: list =<< trim END void F1(int count) { @@ -368,7 +356,6 @@ def Test_LspRename() search('count') redraw! feedkeys(":LspRename\er\", "xt") - sleep 1 redraw! var expected: list =<< trim END void F1(int counter) @@ -383,7 +370,7 @@ def Test_LspRename() count = 5; } END - WaitForAssert(() => assert_equal(expected, getline(1, '$'))) + assert_equal(expected, getline(1, '$')) bw! # empty file @@ -400,7 +387,7 @@ enddef # Test for :LspSelectionExpand and :LspSelectionShrink def Test_LspSelection() silent! edit Xtest.c - sleep 500m + sleep 200m var lines: list =<< trim END void F1(int count) { @@ -500,6 +487,7 @@ enddef # Test for :LspGotoDefinition, :LspGotoDeclaration and :LspGotoImpl def Test_LspGotoDefinition() silent! edit Xtest.cpp + sleep 200m var lines: list =<< trim END #include using namespace std; @@ -550,17 +538,14 @@ def Test_LspGotoDefinition() :messages clear cursor(14, 5) :LspGotoDeclaration - sleep 1 var m = execute('messages')->split("\n") assert_equal('Error: declaration is not found', m[1]) :messages clear :LspGotoDefinition - sleep 1 m = execute('messages')->split("\n") assert_equal('Error: definition is not found', m[1]) :messages clear :LspGotoImpl - sleep 1 m = execute('messages')->split("\n") assert_equal('Error: implementation is not found', m[1]) endif @@ -586,6 +571,7 @@ enddef # Test for :LspHighlight def Test_LspHighlight() silent! edit Xtest.c + sleep 200m var lines: list =<< trim END void f1(int arg) { @@ -597,7 +583,6 @@ def Test_LspHighlight() :sleep 1 cursor(1, 13) :LspHighlight - :sleep 1 var expected: dict expected = {id: 0, col: 13, end: 1, type: 'LspTextRef', length: 3, start: 1} if has('patch-8.2.3233') @@ -615,7 +600,6 @@ def Test_LspHighlight() endif assert_equal([expected], prop_list(4)) :LspHighlightClear - :sleep 1 assert_equal([], prop_list(1)) assert_equal([], prop_list(3)) assert_equal([], prop_list(4)) @@ -625,6 +609,7 @@ enddef # Test for :LspHover def Test_LspHover() silent! edit Xtest.c + sleep 200m var lines: list =<< trim END int f1(int a) { @@ -640,14 +625,12 @@ def Test_LspHover() :sleep 1 cursor(8, 4) :LspHover - :sleep 1 var p: list = popup_list() assert_equal(1, p->len()) assert_equal(['function f1', '', '→ int', 'Parameters:', '- int a', '', 'int f1(int a)'], getbufline(winbufnr(p[0]), 1, '$')) popup_close(p[0]) cursor(7, 1) :LspHover - :sleep 1 assert_equal([], popup_list()) :%bw! enddef @@ -655,6 +638,7 @@ enddef # Test for :LspShowSignature def Test_LspShowSignature() silent! edit Xtest.c + sleep 200m var lines: list =<< trim END int MyFunc(int a, int b) { @@ -670,7 +654,6 @@ def Test_LspShowSignature() :sleep 1 cursor(8, 10) :LspShowSignature - :sleep 1 var p: list = popup_list() var bnr: number = winbufnr(p[0]) assert_equal(1, p->len()) @@ -686,7 +669,6 @@ def Test_LspShowSignature() setline(line('.'), ' MyFunc(10, ') cursor(8, 13) :LspShowSignature - :sleep 1 p = popup_list() bnr = winbufnr(p[0]) assert_equal(1, p->len())