Skip to content

Commit 9facb6b

Browse files
fix: multiple lsp in formatting (#240)
* chore(doc): auto generate docs * fix: muiti client use case * fix: typo * fix: only 1 lsp is allowed --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 79209ee commit 9facb6b

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

lua/guard/lsp.lua

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,36 @@ function M.format(buf, range, acc)
1515

1616
-- use a temporary buffer to apply edits
1717
local scratch = api.nvim_create_buf(false, true)
18-
local n_edits = #clients
1918
api.nvim_buf_set_lines(scratch, 0, -1, false, vim.split(acc, '\r?\n'))
2019
local line_offset = range and range.start[1] - 1 or 0
2120

22-
for _, c in pairs(clients) do
23-
acc = async.await(1, function(callback)
24-
---@diagnostic disable-next-line: duplicate-set-field
25-
vim.lsp.util.apply_text_edits = function(text_edits, _, offset_encoding)
26-
-- the target buffer must be buf, we apply it to our scratch buffer
27-
n_edits = n_edits - 1
21+
local c = clients[1]
22+
return async.await(1, function(callback)
23+
---@diagnostic disable-next-line: duplicate-set-field
24+
vim.lsp.util.apply_text_edits = function(...)
25+
local text_edits, bufnr, offset_encoding = ...
26+
if bufnr == buf then
27+
-- we apply it to our scratch buffer
2828
vim.tbl_map(function(edit)
2929
edit.range.start.line = edit.range.start.line - line_offset
3030
edit.range['end'].line = edit.range['end'].line - line_offset
3131
end, text_edits)
3232
apply(text_edits, scratch, offset_encoding)
33-
if n_edits == 0 then
34-
vim.lsp.util.apply_text_edits = apply
35-
local lines = api.nvim_buf_get_lines(scratch, 0, -1, false)
36-
api.nvim_command('silent! bwipe! ' .. scratch)
37-
callback(table.concat(lines, '\n'))
38-
end
33+
vim.lsp.util.apply_text_edits = apply
34+
local lines = api.nvim_buf_get_lines(scratch, 0, -1, false)
35+
api.nvim_command('silent! bwipe! ' .. scratch)
36+
callback(table.concat(lines, '\n'))
37+
else
38+
apply(...)
3939
end
4040

41-
vim.lsp.buf.format({
42-
bufnr = buf,
43-
range = range,
44-
async = true,
45-
id = c.id,
46-
})
47-
end)
48-
end
49-
50-
return acc
41+
vim.lsp.buf.format({
42+
bufnr = buf,
43+
range = range,
44+
async = true,
45+
id = c.id,
46+
})
47+
end)
5148
end
5249

5350
return M

0 commit comments

Comments
 (0)