]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Use sync RPC call for formatting a file
authorYegappan Lakshmanan <yegappan@yahoo.com>
Sun, 9 Oct 2022 00:53:41 +0000 (17:53 -0700)
committerYegappan Lakshmanan <yegappan@yahoo.com>
Sun, 9 Oct 2022 00:53:41 +0000 (17:53 -0700)
autoload/lsp/handlers.vim
autoload/lsp/lspserver.vim

index f30e14ce04731d8a67f44f76c90d333dfb603759..015efe474b9d751b8e9fb30dd4e731a588a55016 100644 (file)
@@ -407,30 +407,6 @@ def ProcessDocSymbolReply(lspserver: dict<any>, req: dict<any>, reply: dict<any>
   outline.UpdateOutlineWindow(fname, symbolTypeTable, symbolLineTable)
 enddef
 
-# process the 'textDocument/formatting' reply from the LSP server
-# Result: TextEdit[] | null
-def ProcessFormatReply(lspserver: dict<any>, req: dict<any>, reply: dict<any>)
-  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<number> = 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<any>, req: dict<any>, reply: dict<any>):
       'textDocument/references': ProcessReferencesReply,
       'textDocument/documentHighlight': ProcessDocHighlightReply,
       'textDocument/documentSymbol': ProcessDocSymbolReply,
-      'textDocument/formatting': ProcessFormatReply,
-      'textDocument/rangeFormatting': ProcessFormatReply,
       'textDocument/rename': ProcessRenameReply,
       'textDocument/codeAction': ProcessCodeActionReply,
       'textDocument/foldingRange': ProcessFoldingRangeReply,
index 3aeba0f73faa7fda4034d46450eb9828e7cc1f85..79bd68fe856f8c536d65704144692b6fd685fde0 100644 (file)
@@ -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<any>, chan: channel, msg: any): void
@@ -703,30 +704,45 @@ def TextDocFormat(lspserver: dict<any>, 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<any> = {
     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<dict<number>> = {
        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<number> = getcurpos()
+  textedit.ApplyTextEdits(bnr, reply.result)
+  save_cursor->setpos('.')
 enddef
 
 # Request: "textDocument/prepareCallHierarchy"