From: Yegappan Lakshmanan Date: Sun, 9 Oct 2022 19:24:33 +0000 (-0700) Subject: Use sync RPC for symbol rename operation. Display an error message when a RPC reques... X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=7c376dd36b7fcad63b975b589fa6800e8674d6e0;p=vim-lsp.git Use sync RPC for symbol rename operation. Display an error message when a RPC request fails --- diff --git a/autoload/lsp/handlers.vim b/autoload/lsp/handlers.vim index 015efe4..27fe1de 100644 --- a/autoload/lsp/handlers.vim +++ b/autoload/lsp/handlers.vim @@ -407,19 +407,6 @@ def ProcessDocSymbolReply(lspserver: dict, req: dict, reply: dict outline.UpdateOutlineWindow(fname, symbolTypeTable, symbolLineTable) enddef -# Reply: 'textDocument/rename' -# Result: Range | { range: Range, placeholder: string } -# | { defaultBehavior: boolean } | null -def ProcessRenameReply(lspserver: dict, req: dict, reply: dict) - if reply.result->empty() - # nothing to rename - return - endif - - # result: WorkspaceEdit - textedit.ApplyWorkspaceEdit(reply.result) -enddef - # process the 'textDocument/codeAction' reply from the LSP server # Result: (Command | CodeAction)[] | null def ProcessCodeActionReply(lspserver: dict, req: dict, reply: dict) @@ -593,7 +580,6 @@ export def ProcessReply(lspserver: dict, req: dict, reply: dict): 'textDocument/references': ProcessReferencesReply, 'textDocument/documentHighlight': ProcessDocHighlightReply, 'textDocument/documentSymbol': ProcessDocSymbolReply, - 'textDocument/rename': ProcessRenameReply, 'textDocument/codeAction': ProcessCodeActionReply, 'textDocument/foldingRange': ProcessFoldingRangeReply, 'workspace/executeCommand': ProcessWorkspaceExecuteReply, diff --git a/autoload/lsp/lsp.vim b/autoload/lsp/lsp.vim index 662f32d..f6039aa 100644 --- a/autoload/lsp/lsp.vim +++ b/autoload/lsp/lsp.vim @@ -752,6 +752,9 @@ export def Rename() return endif + # clear the input prompt + echo "\r" + lspserver.renameSymbol(newName) enddef diff --git a/autoload/lsp/lspserver.vim b/autoload/lsp/lspserver.vim index 79bd68f..4ca8750 100644 --- a/autoload/lsp/lspserver.vim +++ b/autoload/lsp/lspserver.vim @@ -261,7 +261,8 @@ def SendMessage(lspserver: dict, content: dict): void ch->ch_sendexpr(content) enddef -# Send a RPC request message to LSP server +# Send a sync RPC request message to the LSP server and return the received +# reply. In case of an error, an empty Dict is returned. def RpcCall(lspserver: dict, method: string, params: any): dict var req = {} req.method = method @@ -273,7 +274,25 @@ def RpcCall(lspserver: dict, method: string, params: any): dict # LSP server has exited return {} endif - return ch->ch_evalexpr(req) + + var reply = ch->ch_evalexpr(req) + + if reply->has_key('result') + # successful reply + return reply + endif + + if reply->has_key('error') + # request failed + var emsg: string = reply.error.message + emsg ..= ', code = ' .. reply.error.code + if reply.error->has_key('data') + emsg ..= ', data = ' .. reply.error.data->string() + endif + util.ErrMsg($'Error(LSP): request {method} failed ({emsg})') + endif + + return {} enddef # Wait for a response message from the LSP server for the request "req" @@ -444,8 +463,8 @@ enddef # # Result: Location | Location[] | LocationLink[] | null def GotoSymbolLoc(lspserver: dict, msg: string, peekSymbol: bool) - var resp = lspserver.rpc(msg, GetLspTextDocPosition()) - if resp->empty() || resp.result->empty() + var reply = lspserver.rpc(msg, GetLspTextDocPosition()) + if reply->empty() || reply.result->empty() var emsg: string if msg ==# 'textDocument/declaration' emsg = 'Error: symbol declaration is not found' @@ -466,10 +485,10 @@ def GotoSymbolLoc(lspserver: dict, msg: string, peekSymbol: bool) endif var location: dict - if resp.result->type() == v:t_list - location = resp.result[0] + if reply.result->type() == v:t_list + location = reply.result[0] else - location = resp.result + location = reply.result endif symbol.GotoSymbol(lspserver, location, peekSymbol) @@ -541,14 +560,14 @@ enddef def SwitchSourceHeader(lspserver: dict) var param = {} param.uri = util.LspFileToUri(@%) - var resp = lspserver.rpc('textDocument/switchSourceHeader', param) - if resp->empty() || resp.result->empty() + var reply = lspserver.rpc('textDocument/switchSourceHeader', param) + if reply->empty() || reply.result->empty() return endif # process the 'textDocument/switchSourceHeader' reply from the LSP server # Result: URI | null - var fname = util.LspUriToFile(resp.result) + var fname = util.LspUriToFile(reply.result) if (&modified && !&hidden) || &buftype != '' # if the current buffer has unsaved changes and 'hidden' is not set, # or if the current buffer is a special buffer, then ask to save changes @@ -817,16 +836,22 @@ def RenameSymbol(lspserver: dict, newName: string) return endif - var req = lspserver.createRequest('textDocument/rename') # interface RenameParams # interface TextDocumentPositionParams - req.params = GetLspTextDocPosition() - req.params.newName = newName - lspserver.sendMessage(req) - if exists('g:LSPTest') && g:LSPTest - # When running LSP tests, make this a synchronous call - lspserver.waitForResponse(req) + var param: dict = {} + param = GetLspTextDocPosition() + param.newName = newName + + var reply = lspserver.rpc('textDocument/rename', param) + + # Result: WorkspaceEdit | null + if reply->empty() || reply.result->empty() + # nothing to rename + return endif + + # result: WorkspaceEdit + textedit.ApplyWorkspaceEdit(reply.result) enddef # Request: "textDocument/codeAction" @@ -954,13 +979,13 @@ def SelectionRange(lspserver: dict, fname: string) param.textDocument = {} param.textDocument.uri = util.LspFileToUri(fname) param.positions = [GetLspPosition()] - var resp = lspserver.rpc('textDocument/selectionRange', param) + var reply = lspserver.rpc('textDocument/selectionRange', param) - if resp->empty() || resp.result->empty() + if reply->empty() || reply.result->empty() return endif - selection.SelectionStart(lspserver, resp.result) + selection.SelectionStart(lspserver, reply.result) enddef # Expand the previous selection or start a new one