2021-06-17 14:49:13 +00:00
|
|
|
-- https://github.com/wention/dotfiles/blob/master/.config/nvim/lua/config/lsp.lua
|
|
|
|
-- https://github.com/lukas-reineke/dotfiles/blob/master/vim/lua/lsp/handlers.lua
|
2022-01-16 04:36:28 +00:00
|
|
|
local mk_handler = require('navigator.util').mk_handler
|
2021-06-17 14:49:13 +00:00
|
|
|
return {
|
2021-09-05 22:34:26 +00:00
|
|
|
format_hdl = mk_handler(function(err, result, ctx, cfg) -- FIXME: bufnr is nil
|
2021-06-17 14:49:13 +00:00
|
|
|
if err ~= nil or result == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-01-16 04:36:28 +00:00
|
|
|
local util = require('navigator.util')
|
2021-10-13 09:27:46 +00:00
|
|
|
local log = util.log
|
2022-01-16 04:36:28 +00:00
|
|
|
|
2022-01-17 05:03:48 +00:00
|
|
|
local offset_encoding = util.encoding(vim.lsp.get_client_by_id(ctx.client_id))
|
2022-01-16 04:36:28 +00:00
|
|
|
|
2021-06-17 14:49:13 +00:00
|
|
|
-- If the buffer hasn't been modified before the formatting has finished,
|
|
|
|
-- update the buffer
|
2021-09-23 00:41:28 +00:00
|
|
|
-- if not vim.api.nvim_buf_get_option(ctx.bufnr, 'modified') then
|
2021-09-27 00:55:43 +00:00
|
|
|
vim.defer_fn(function()
|
2021-10-13 09:27:46 +00:00
|
|
|
log('fmt callback')
|
|
|
|
|
2022-01-16 04:36:28 +00:00
|
|
|
if ctx.bufnr == vim.api.nvim_get_current_buf() or not vim.api.nvim_buf_get_option(ctx.bufnr, 'modified') then
|
2021-09-27 00:55:43 +00:00
|
|
|
local view = vim.fn.winsaveview()
|
2022-01-16 04:36:28 +00:00
|
|
|
vim.lsp.util.apply_text_edits(result, ctx.bufnr, offset_encoding)
|
2021-09-27 00:55:43 +00:00
|
|
|
vim.fn.winrestview(view)
|
|
|
|
-- FIXME: commented out as a workaround
|
|
|
|
-- if bufnr == vim.api.nvim_get_current_buf() then
|
|
|
|
vim.api.nvim_command('noautocmd :update')
|
|
|
|
|
|
|
|
-- Trigger post-formatting autocommand which can be used to refresh gitgutter
|
|
|
|
vim.api.nvim_command('silent doautocmd <nomodeline> User FormatterPost')
|
|
|
|
-- end
|
|
|
|
end
|
2021-10-13 09:27:46 +00:00
|
|
|
end, 100)
|
2022-01-16 04:36:28 +00:00
|
|
|
end),
|
2021-06-17 14:49:13 +00:00
|
|
|
}
|