Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 20 additions & 23 deletions lua/guard/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,36 @@ function M.format(buf, range, acc)

-- use a temporary buffer to apply edits
local scratch = api.nvim_create_buf(false, true)
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

for _, c in pairs(clients) do
acc = async.await(1, function(callback)
---@diagnostic disable-next-line: duplicate-set-field
vim.lsp.util.apply_text_edits = function(text_edits, _, offset_encoding)
-- the target buffer must be buf, we apply it to our scratch buffer
n_edits = n_edits - 1
local c = clients[1]
return async.await(1, function(callback)
---@diagnostic disable-next-line: duplicate-set-field
vim.lsp.util.apply_text_edits = function(...)
local text_edits, bufnr, offset_encoding = ...
if bufnr == buf then
-- we apply it to our scratch buffer
vim.tbl_map(function(edit)
edit.range.start.line = edit.range.start.line - line_offset
edit.range['end'].line = edit.range['end'].line - line_offset
end, text_edits)
apply(text_edits, scratch, offset_encoding)
if n_edits == 0 then
vim.lsp.util.apply_text_edits = apply
local lines = api.nvim_buf_get_lines(scratch, 0, -1, false)
api.nvim_command('silent! bwipe! ' .. scratch)
callback(table.concat(lines, '\n'))
end
vim.lsp.util.apply_text_edits = apply
local lines = api.nvim_buf_get_lines(scratch, 0, -1, false)
api.nvim_command('silent! bwipe! ' .. scratch)
callback(table.concat(lines, '\n'))
else
apply(...)
end

vim.lsp.buf.format({
bufnr = buf,
range = range,
async = true,
id = c.id,
})
end)
end

return acc
vim.lsp.buf.format({
bufnr = buf,
range = range,
async = true,
id = c.id,
})
end)
end

return M
Loading