autoload/lsp/handlers.vim | 26 -------------------------- autoload/lsp/lspserver.vim | 40 ++++++++++++++++++++++++++++------------ diff --git a/autoload/lsp/handlers.vim b/autoload/lsp/handlers.vim index f30e14ce04731d8a67f44f76c90d333dfb603759..015efe474b9d751b8e9fb30dd4e731a588a55016 100644 --- a/autoload/lsp/handlers.vim +++ b/autoload/lsp/handlers.vim @@ -407,30 +407,6 @@ symbolLineTable->sort((a, b) => a.range.start.line - b.range.start.line) 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 @@ 'textDocument/hover': ProcessHoverReply, '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 3aeba0f73faa7fda4034d46450eb9828e7cc1f85..79bd68fe856f8c536d65704144692b6fd685fde0 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 @@ cmd = 'textDocument/rangeFormatting' 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 + + var reply = lspserver.rpc(cmd, param) + + # result: TextEdit[] | null + + if reply->empty() || reply.result->empty() + # nothing to format + return endif - lspserver.sendMessage(req) - if exists('g:LSPTest') && g:LSPTest - # When running LSP tests, make this a synchronous call - lspserver.waitForResponse(req) + 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"