changeset 313:a1ed17cc354b

Add a post-formatting handler to reload the buffer This fixes some issues with treesitter highlighting/refactor not working after formatting the buffer. source: https://github.com/lukas-reineke/dotfiles/blob/97b95c9c27cada6ff73080600f724c58f3d7ebfd/vim/lua/lsp.lua#L31
author Zeger Van de Vannet <zegervdv@me.com>
date Thu, 26 Nov 2020 09:07:17 +0100
parents f94a7c50a28d
children 205d82eb93b4
files dot_config/nvim/init.lua
diffstat 1 files changed, 13 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/dot_config/nvim/init.lua	Thu Nov 26 09:06:51 2020 +0100
+++ b/dot_config/nvim/init.lua	Thu Nov 26 09:07:17 2020 +0100
@@ -102,6 +102,19 @@
   mapper("i", "<c-k>", "<Plug>(completion_prev_source)", false)
 end
 
+vim.lsp.handlers["textDocument/formatting"] = function(err, _, result, _, bufnr)
+    if err ~= nil or result == nil then
+        return
+    end
+    if not vim.api.nvim_buf_get_option(bufnr, "modified") then
+        local view = vim.fn.winsaveview()
+        vim.lsp.util.apply_text_edits(result, bufnr)
+        vim.fn.winrestview(view)
+        -- Fix to reload Treesitter
+        vim.api.nvim_command("edit")
+    end
+end
+
 lsp.pyls.setup{
     cmd = {"pyls"},
     on_attach = on_attach;