Skip to content

Commit 43ad797

Browse files
committed
fix: ensure apply's validity when multiple formatting requests
1 parent dfe47ff commit 43ad797

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

lua/guard/lsp.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local M = {}
22
local api = vim.api
3+
local apply = vim.lsp.util.apply_text_edits
34

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

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

0 commit comments

Comments
 (0)