From: Yegappan Lakshmanan Date: Sun, 9 Oct 2022 00:53:41 +0000 (-0700) Subject: Use sync RPC call for formatting a file X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=748cea25fc8474b7c6f4a990ed54f5761136d3aa;p=vim-lsp.git Use sync RPC call for formatting a file --- diff --git a/autoload/lsp/handlers.vim b/autoload/lsp/handlers.vim index f30e14c..015efe4 100644 --- a/autoload/lsp/handlers.vim +++ b/autoload/lsp/handlers.vim @@ -407,30 +407,6 @@ def ProcessDocSymbolReply(lspserver: dict, req: dict, reply: dict outline.UpdateOutlineWindow(fname, symbolTypeTable, symbolLineTable) enddef -# process the 'textDocument/formatting' reply from the LSP server -# Result: TextEdit[] | null -def ProcessFormatReply(lspserver: dict, req: dict, reply: dict) - if reply.result->empty() - # nothing to format - return - endif - - # result: TextEdit[] - - var fname: string = util.LspUriToFile(req.params.textDocument.uri) - var bnr: number = fname->bufnr() - if bnr == -1 - # file is already removed - return - endif - - # interface TextEdit - # Apply each of the text edit operations - var save_cursor: list = getcurpos() - textedit.ApplyTextEdits(bnr, reply.result) - save_cursor->setpos('.') -enddef - # Reply: 'textDocument/rename' # Result: Range | { range: Range, placeholder: string } # | { defaultBehavior: boolean } | null @@ -617,8 +593,6 @@ export def ProcessReply(lspserver: dict, req: dict, reply: dict): 'textDocument/references': ProcessReferencesReply, 'textDocument/documentHighlight': ProcessDocHighlightReply, 'textDocument/documentSymbol': ProcessDocSymbolReply, - 'textDocument/formatting': ProcessFormatReply, - 'textDocument/rangeFormatting': ProcessFormatReply, 'textDocument/rename': ProcessRenameReply, 'textDocument/codeAction': ProcessCodeActionReply, 'textDocument/foldingRange': ProcessFoldingRangeReply, diff --git a/autoload/lsp/lspserver.vim b/autoload/lsp/lspserver.vim index 3aeba0f..79bd68f 100644 --- a/autoload/lsp/lspserver.vim +++ b/autoload/lsp/lspserver.vim @@ -9,6 +9,7 @@ import './util.vim' import './diag.vim' import './selection.vim' import './symbol.vim' +import './textedit.vim' # LSP server standard output handler def Output_cb(lspserver: dict, chan: channel, msg: any): void @@ -703,30 +704,45 @@ def TextDocFormat(lspserver: dict, fname: string, rangeFormat: bool, else cmd = 'textDocument/formatting' endif - var req = lspserver.createRequest(cmd) # interface DocumentFormattingParams - # interface TextDocumentIdentifier - # interface FormattingOptions + # interface TextDocumentIdentifier + # interface FormattingOptions + var param = {} + param.textDocument = {uri: util.LspFileToUri(fname)} var fmtopts: dict = { tabSize: shiftwidth(), insertSpaces: &expandtab ? true : false, } - #req.params->extend({textDocument: {uri: util.LspFileToUri(fname)}, - # options: fmtopts}) - req.params->extend({textDocument: {uri: util.LspFileToUri(fname)}, options: fmtopts}) + param.options = fmtopts + if rangeFormat var r: dict> = { start: {line: start_lnum - 1, character: 0}, - end: {line: end_lnum, character: 0}} - req.params->extend({range: r}) + end: {line: end_lnum - 1, character: 99999}} + param.range = r endif - lspserver.sendMessage(req) - if exists('g:LSPTest') && g:LSPTest - # When running LSP tests, make this a synchronous call - lspserver.waitForResponse(req) + var reply = lspserver.rpc(cmd, param) + + # result: TextEdit[] | null + + if reply->empty() || reply.result->empty() + # nothing to format + return endif + + var bnr: number = fname->bufnr() + if bnr == -1 + # file is already removed + return + endif + + # interface TextEdit + # Apply each of the text edit operations + var save_cursor: list = getcurpos() + textedit.ApplyTextEdits(bnr, reply.result) + save_cursor->setpos('.') enddef # Request: "textDocument/prepareCallHierarchy"