From 90e8a8d63c7d77d1872dca3da720abfa07271054 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Sun, 27 Aug 2023 21:16:33 -0700 Subject: fix: keep window position stable when LSP formatting --- lua/conform/init.lua | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'lua/conform/init.lua') diff --git a/lua/conform/init.lua b/lua/conform/init.lua index dcc8143..21c1d1f 100644 --- a/lua/conform/init.lua +++ b/lua/conform/init.lua @@ -74,6 +74,17 @@ M.setup = function(opts) end, }) end + + ---@diagnostic disable-next-line: duplicate-set-field + vim.lsp.handlers["textDocument/formatting"] = function(_, result, ctx, _) + if not result then + return + end + local client = vim.lsp.get_client_by_id(ctx.client_id) + local restore = require("conform.util").save_win_positions(ctx.bufnr) + vim.lsp.util.apply_text_edits(result, ctx.bufnr, client.offset_encoding) + restore() + end end ---Format a buffer @@ -130,7 +141,9 @@ M.format = function(opts) if supports_lsp_formatting then local restore = require("conform.util").save_win_positions(opts.bufnr) vim.lsp.buf.format(opts) - restore() + if not opts.async then + restore() + end end else vim.notify("No formatters found for buffer. See :checkhealth conform", vim.log.levels.WARN) -- cgit v1.2.3-70-g09d2