Skip to content

Commit 79209ee

Browse files
fix: muiti client use case (#238)
* chore(doc): auto generate docs * fix: muiti client use case * fix: typo --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent bde431c commit 79209ee

3 files changed

Lines changed: 96 additions & 84 deletions

File tree

doc/guard.nvim.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*guard.nvim.txt* For NVIM v0.8.0 Last change: 2025 December 15
1+
*guard.nvim.txt* For NVIM v0.8.0 Last change: 2025 December 16
22

33
==============================================================================
44
Table of Contents *guard.nvim-table-of-contents*

lua/guard/lsp.lua

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,35 @@ function M.format(buf, range, acc)
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
2121

22-
return async.await(1, function(callback)
23-
---@diagnostic disable-next-line: duplicate-set-field
24-
vim.lsp.util.apply_text_edits = function(text_edits, _, offset_encoding)
25-
-- the target buffer must be buf, we apply it to our scratch buffer
26-
n_edits = n_edits - 1
27-
vim.tbl_map(function(edit)
28-
edit.range.start.line = edit.range.start.line - line_offset
29-
edit.range['end'].line = edit.range['end'].line - line_offset
30-
end, text_edits)
31-
apply(text_edits, scratch, offset_encoding)
32-
if n_edits == 0 then
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'))
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
28+
vim.tbl_map(function(edit)
29+
edit.range.start.line = edit.range.start.line - line_offset
30+
edit.range['end'].line = edit.range['end'].line - line_offset
31+
end, text_edits)
32+
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
3739
end
38-
end
3940

40-
vim.lsp.buf.format({
41-
bufnr = buf,
42-
range = range,
43-
async = true,
44-
})
45-
end)
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
4651
end
4752

4853
return M

spec/lsp_spec.lua

Lines changed: 68 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,68 @@
1-
-- ---@diagnostic disable: undefined-field, undefined-global
2-
-- require('plugin.guard')
3-
-- local api = vim.api
4-
-- local same = assert.are.same
5-
-- local ft = require('guard.filetype')
6-
-- local gapi = require('guard.api')
7-
--
8-
-- describe('format module', function()
9-
-- local bufnr
10-
-- local ill_c = {
11-
-- 'int main( void ) {return 0;}',
12-
-- }
13-
--
14-
-- before_each(function()
15-
-- for k, _ in pairs(ft) do
16-
-- ft[k] = nil
17-
-- end
18-
-- vim.g.guard_config = {
19-
-- lsp_as_default_formatter = true,
20-
-- }
21-
--
22-
-- vim.lsp.config('clangd', {
23-
-- cmd = { 'clangd' },
24-
-- filetypes = { 'c' },
25-
-- capabilities = {
26-
-- textDocument = {
27-
-- formatting = {
28-
-- dynamicRegistration = true,
29-
-- },
30-
-- },
31-
-- },
32-
-- })
33-
-- vim.lsp.enable('clangd')
34-
--
35-
-- bufnr = api.nvim_create_buf(true, false)
36-
--
37-
-- vim.bo[bufnr].filetype = 'c'
38-
-- api.nvim_set_current_buf(bufnr)
39-
-- vim.cmd('silent! write! /tmp/lsp_spec_test.c')
40-
-- end)
41-
--
42-
-- after_each(function()
43-
-- vim.lsp.enable('clangd', false)
44-
-- end)
45-
--
46-
-- local function getlines()
47-
-- return api.nvim_buf_get_lines(bufnr, 0, -1, false)
48-
-- end
49-
--
50-
-- local function setlines(lines)
51-
-- api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
52-
-- end
53-
--
54-
-- it('lsp default formatter works', function()
55-
-- vim.wait(500)
56-
-- setlines(ill_c)
57-
-- gapi.fmt(bufnr)
58-
-- vim.wait(500)
59-
-- same({ 'int main(void) { return 0; }' }, getlines())
60-
-- end)
61-
-- end)
1+
---@diagnostic disable: undefined-field, undefined-global
2+
require('plugin.guard')
3+
local api = vim.api
4+
local same = assert.are.same
5+
local ft = require('guard.filetype')
6+
local gapi = require('guard.api')
7+
8+
describe('format module', function()
9+
local bufnr
10+
local ill_c = {
11+
'int main( void ) {return 0;}',
12+
}
13+
14+
before_each(function()
15+
for k, _ in pairs(ft) do
16+
ft[k] = nil
17+
end
18+
vim.g.guard_config = {
19+
lsp_as_default_formatter = true,
20+
}
21+
22+
vim.lsp.config('clangd', {
23+
cmd = { 'clangd' },
24+
filetypes = { 'c' },
25+
capabilities = {
26+
textDocument = {
27+
formatting = {
28+
dynamicRegistration = true,
29+
},
30+
},
31+
},
32+
})
33+
vim.lsp.enable('clangd')
34+
35+
bufnr = api.nvim_create_buf(true, false)
36+
37+
vim.bo[bufnr].filetype = 'c'
38+
api.nvim_set_current_buf(bufnr)
39+
vim.cmd('silent! write! /tmp/lsp_spec_test.c')
40+
end)
41+
42+
after_each(function()
43+
vim.lsp.enable('clangd', false)
44+
end)
45+
46+
local function getlines()
47+
return api.nvim_buf_get_lines(bufnr, 0, -1, false)
48+
end
49+
50+
local function setlines(lines)
51+
api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
52+
end
53+
54+
it('lsp default formatter works', function()
55+
setlines(ill_c)
56+
vim.api.nvim_create_autocmd('LspAttach', {
57+
callback = function(args)
58+
same(args.buf, bufnr)
59+
same(args.file, '/tmp/lsp_spec_test.c')
60+
same(args.match, '/tmp/lsp_spec_test.c')
61+
gapi.fmt(bufnr)
62+
vim.wait(500)
63+
same({ 'int main(void) { return 0; }' }, getlines())
64+
end,
65+
})
66+
vim.wait(1500)
67+
end)
68+
end)

0 commit comments

Comments
 (0)