Skip to content

Commit 047555e

Browse files
fix: lsp formatting (#236)
* chore(doc): auto generate docs * fix: lsp formatting * chore: rm debug msg * chore: add tests (broken) --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent f0080b7 commit 047555e

File tree

6 files changed

+91
-30
lines changed

6 files changed

+91
-30
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
LUAROCKS_PATH_CMD = luarocks path --no-bin --lua-version 5.1
2-
BUSTED = eval $$(luarocks path --no-bin --lua-version 5.1) && busted --lua nlua
2+
TEST = luarocks test --local --lua-version=5.1
33
TEST_DIR = spec
44

55
.PHONY: test
66
test:
77
@echo "Running tests..."
88
@if [ -n "$(file)" ]; then \
9-
$(BUSTED) $(file); \
9+
$(TEST) $(file); \
1010
else \
11-
$(BUSTED) $(TEST_DIR); \
11+
$(TEST); \
1212
fi

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 November 13
1+
*guard.nvim.txt* For NVIM v0.8.0 Last change: 2025 December 15
22

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

lua/guard/_async.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function M.run(func, on_finish)
4646
return {
4747
--- @param timeout? integer
4848
--- @return any ... return values of `func`
49-
wait = function(_self, timeout)
49+
wait = function(_, timeout)
5050
vim.wait(timeout or max_timeout, function()
5151
return res ~= nil
5252
end)

lua/guard/format.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ local function do_fmt(buf)
201201
fail(err)
202202
return
203203
end
204-
new_lines = output
204+
new_lines = assert(output)
205205
end
206206

207207
async.await(1, function(callback)

lua/guard/lsp.lua

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
local async = require('guard._async')
12
local M = {}
23
local api = vim.api
34
local apply = vim.lsp.util.apply_text_edits
45

56
---@param buf number
6-
---@param range table
7+
---@param range table?
78
---@param acc string
89
---@return string
910
function M.format(buf, range, acc)
10-
local co = assert(coroutine.running())
1111
local clients = vim.lsp.get_clients({ bufnr = buf, method = 'textDocument/formatting' })
1212
if #clients == 0 then
1313
return acc
@@ -19,30 +19,30 @@ 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-
---@diagnostic disable-next-line: duplicate-set-field
23-
vim.lsp.util.apply_text_edits = function(text_edits, _, offset_encoding)
24-
-- the target buffer must be buf, we apply it to our scratch buffer
25-
n_edits = n_edits - 1
26-
vim.tbl_map(function(edit)
27-
edit.range.start.line = edit.range.start.line - line_offset
28-
edit.range['end'].line = edit.range['end'].line - line_offset
29-
end, text_edits)
30-
apply(text_edits, scratch, offset_encoding)
31-
if n_edits == 0 then
32-
vim.lsp.util.apply_text_edits = apply
33-
local lines = api.nvim_buf_get_lines(scratch, 0, -1, false)
34-
api.nvim_command('silent! bwipe! ' .. scratch)
35-
coroutine.resume(co, table.concat(lines, '\n'))
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'))
37+
end
3638
end
37-
end
38-
39-
vim.lsp.buf.format({
40-
bufnr = buf,
41-
range = range,
42-
async = true,
43-
})
4439

45-
return (coroutine.yield())
40+
vim.lsp.buf.format({
41+
bufnr = buf,
42+
range = range,
43+
async = true,
44+
})
45+
end)
4646
end
4747

4848
return M

spec/lsp_spec.lua

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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)

0 commit comments

Comments
 (0)