diff --git a/doc/guard.nvim.txt b/doc/guard.nvim.txt index b4a0035..b55163d 100644 --- a/doc/guard.nvim.txt +++ b/doc/guard.nvim.txt @@ -1,4 +1,4 @@ -*guard.nvim.txt* For NVIM v0.8.0 Last change: 2025 December 15 +*guard.nvim.txt* For NVIM v0.8.0 Last change: 2025 December 16 ============================================================================== Table of Contents *guard.nvim-table-of-contents* diff --git a/lua/guard/lsp.lua b/lua/guard/lsp.lua index 8575cfc..22a2e53 100644 --- a/lua/guard/lsp.lua +++ b/lua/guard/lsp.lua @@ -19,30 +19,35 @@ function M.format(buf, range, acc) 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 - return 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 - 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')) + 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 + 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 end - end - vim.lsp.buf.format({ - bufnr = buf, - range = range, - async = true, - }) - end) + vim.lsp.buf.format({ + bufnr = buf, + range = range, + async = true, + id = c.id, + }) + end) + end + + return acc end return M diff --git a/spec/lsp_spec.lua b/spec/lsp_spec.lua index ba213d6..cf7894d 100644 --- a/spec/lsp_spec.lua +++ b/spec/lsp_spec.lua @@ -1,61 +1,68 @@ --- ---@diagnostic disable: undefined-field, undefined-global --- require('plugin.guard') --- local api = vim.api --- local same = assert.are.same --- local ft = require('guard.filetype') --- local gapi = require('guard.api') --- --- describe('format module', function() --- local bufnr --- local ill_c = { --- 'int main( void ) {return 0;}', --- } --- --- before_each(function() --- for k, _ in pairs(ft) do --- ft[k] = nil --- end --- vim.g.guard_config = { --- lsp_as_default_formatter = true, --- } --- --- vim.lsp.config('clangd', { --- cmd = { 'clangd' }, --- filetypes = { 'c' }, --- capabilities = { --- textDocument = { --- formatting = { --- dynamicRegistration = true, --- }, --- }, --- }, --- }) --- vim.lsp.enable('clangd') --- --- bufnr = api.nvim_create_buf(true, false) --- --- vim.bo[bufnr].filetype = 'c' --- api.nvim_set_current_buf(bufnr) --- vim.cmd('silent! write! /tmp/lsp_spec_test.c') --- end) --- --- after_each(function() --- vim.lsp.enable('clangd', false) --- end) --- --- local function getlines() --- return api.nvim_buf_get_lines(bufnr, 0, -1, false) --- end --- --- local function setlines(lines) --- api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) --- end --- --- it('lsp default formatter works', function() --- vim.wait(500) --- setlines(ill_c) --- gapi.fmt(bufnr) --- vim.wait(500) --- same({ 'int main(void) { return 0; }' }, getlines()) --- end) --- end) +---@diagnostic disable: undefined-field, undefined-global +require('plugin.guard') +local api = vim.api +local same = assert.are.same +local ft = require('guard.filetype') +local gapi = require('guard.api') + +describe('format module', function() + local bufnr + local ill_c = { + 'int main( void ) {return 0;}', + } + + before_each(function() + for k, _ in pairs(ft) do + ft[k] = nil + end + vim.g.guard_config = { + lsp_as_default_formatter = true, + } + + vim.lsp.config('clangd', { + cmd = { 'clangd' }, + filetypes = { 'c' }, + capabilities = { + textDocument = { + formatting = { + dynamicRegistration = true, + }, + }, + }, + }) + vim.lsp.enable('clangd') + + bufnr = api.nvim_create_buf(true, false) + + vim.bo[bufnr].filetype = 'c' + api.nvim_set_current_buf(bufnr) + vim.cmd('silent! write! /tmp/lsp_spec_test.c') + end) + + after_each(function() + vim.lsp.enable('clangd', false) + end) + + local function getlines() + return api.nvim_buf_get_lines(bufnr, 0, -1, false) + end + + local function setlines(lines) + api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) + end + + it('lsp default formatter works', function() + setlines(ill_c) + vim.api.nvim_create_autocmd('LspAttach', { + callback = function(args) + same(args.buf, bufnr) + same(args.file, '/tmp/lsp_spec_test.c') + same(args.match, '/tmp/lsp_spec_test.c') + gapi.fmt(bufnr) + vim.wait(500) + same({ 'int main(void) { return 0; }' }, getlines()) + end, + }) + vim.wait(1500) + end) +end)