aboutsummaryrefslogtreecommitdiffstats
path: root/lua/conform
diff options
context:
space:
mode:
authorSteven Arcangeli <stevearc@stevearc.com>2023-08-27 21:16:33 -0700
committerSteven Arcangeli <stevearc@stevearc.com>2023-08-27 22:23:28 -0700
commit90e8a8d63c7d77d1872dca3da720abfa07271054 (patch)
treed233c967daa9eeb70b7f11165c646f2e1ada9fbe /lua/conform
parentb883fecaaca30be2fff62f0cf68fbe2bc94d113f (diff)
fix: keep window position stable when LSP formatting
Diffstat (limited to 'lua/conform')
-rw-r--r--lua/conform/init.lua15
-rw-r--r--lua/conform/util.lua11
2 files changed, 22 insertions, 4 deletions
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)
diff --git a/lua/conform/util.lua b/lua/conform/util.lua
index 8cbc013..353c447 100644
--- a/lua/conform/util.lua
+++ b/lua/conform/util.lua
@@ -46,9 +46,14 @@ M.save_win_positions = function(bufnr)
return function()
for winid, view in pairs(win_positions) do
- vim.api.nvim_win_call(winid, function()
- pcall(vim.fn.winrestview, view)
- end)
+ if
+ vim.api.nvim_win_is_valid(winid)
+ and vim.deep_equal(vim.api.nvim_win_get_cursor(winid), { 1, 0 })
+ then
+ vim.api.nvim_win_call(winid, function()
+ pcall(vim.fn.winrestview, view)
+ end)
+ end
end
end
end