Skip to content

Commit

Permalink
fix: ensure apply's validity when multiple formatting requests
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoshihou514 committed Aug 9, 2024
1 parent dfe47ff commit 43ad797
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lua/guard/lsp.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local M = {}
local api = vim.api
local apply = vim.lsp.util.apply_text_edits

---@param buf number
---@param range table
Expand All @@ -14,7 +15,6 @@ function M.format(buf, range, acc)

-- use a temporary buffer to apply edits
local scratch = api.nvim_create_buf(false, true)
local apply = vim.lsp.util.apply_text_edits
local n_edits = #clients
api.nvim_buf_set_lines(scratch, 0, -1, false, vim.split(acc, '\r?\n'))
local line_offset = range and range.start[1] - 1 or 0
Expand All @@ -30,8 +30,9 @@ function M.format(buf, range, acc)
apply(text_edits, scratch, offset_encoding)
if n_edits == 0 then
vim.lsp.util.apply_text_edits = apply
coroutine.resume(co, table.concat(api.nvim_buf_get_lines(scratch, 0, -1, false), '\n'))
local lines = api.nvim_buf_get_lines(scratch, 0, -1, false)
api.nvim_command('silent! bwipe! ' .. scratch)
coroutine.resume(co, table.concat(lines, '\n'))
end
end

Expand Down

0 comments on commit 43ad797

Please sign in to comment.