Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
LUAROCKS_PATH_CMD = luarocks path --no-bin --lua-version 5.1
BUSTED = eval $$(luarocks path --no-bin --lua-version 5.1) && busted --lua nlua
TEST = luarocks test --local --lua-version=5.1
TEST_DIR = spec

.PHONY: test
test:
@echo "Running tests..."
@if [ -n "$(file)" ]; then \
$(BUSTED) $(file); \
$(TEST) $(file); \
else \
$(BUSTED) $(TEST_DIR); \
$(TEST); \
fi
2 changes: 1 addition & 1 deletion doc/guard.nvim.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*guard.nvim.txt* For NVIM v0.8.0 Last change: 2025 November 13
*guard.nvim.txt* For NVIM v0.8.0 Last change: 2025 December 15

==============================================================================
Table of Contents *guard.nvim-table-of-contents*
Expand Down
2 changes: 1 addition & 1 deletion lua/guard/_async.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function M.run(func, on_finish)
return {
--- @param timeout? integer
--- @return any ... return values of `func`
wait = function(_self, timeout)
wait = function(_, timeout)
vim.wait(timeout or max_timeout, function()
return res ~= nil
end)
Expand Down
2 changes: 1 addition & 1 deletion lua/guard/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ local function do_fmt(buf)
fail(err)
return
end
new_lines = output
new_lines = assert(output)
end

async.await(1, function(callback)
Expand Down
48 changes: 24 additions & 24 deletions lua/guard/lsp.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
local async = require('guard._async')
local M = {}
local api = vim.api
local apply = vim.lsp.util.apply_text_edits

---@param buf number
---@param range table
---@param range table?
---@param acc string
---@return string
function M.format(buf, range, acc)
local co = assert(coroutine.running())
local clients = vim.lsp.get_clients({ bufnr = buf, method = 'textDocument/formatting' })
if #clients == 0 then
return acc
Expand All @@ -19,30 +19,30 @@ 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

---@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)
coroutine.resume(co, table.concat(lines, '\n'))
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'))
end
end
end

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

return (coroutine.yield())
vim.lsp.buf.format({
bufnr = buf,
range = range,
async = true,
})
end)
end

return M
61 changes: 61 additions & 0 deletions spec/lsp_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
-- ---@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)
Loading