Skip to content

Commit 6bb7284

Browse files
committed
fix: dup au
1 parent 7d07590 commit 6bb7284

File tree

4 files changed

+45
-83
lines changed

4 files changed

+45
-83
lines changed

ADVANCED.md

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -51,46 +51,3 @@ vim.api.nvim_create_autocmd("User", {
5151
```
5252

5353
You can do the similar for your statusline plugin of choice as long as you "refresh" it on `GuardFmt`.
54-
55-
## Writing advanced formatting logic
56-
57-
Guard exposes an api for writing advanced formatting logic:
58-
59-
```lua
60-
require("guard.api").fmt_with({
61-
cmd -- string: tool command
62-
args -- table: command arugments
63-
fname -- boolean: insert filename to args tail
64-
stdin -- boolean: pass buffer contents into stdin
65-
timeout -- integer
66-
env -- table: environment variables passed to cmd (key value pair)
67-
68-
fn -- function: if fn is set other fields will not take effect
69-
}, {
70-
buf -- integer: buffer number
71-
range -- table: {integer, integer} for range formatting
72-
})
73-
-- returns:
74-
{
75-
result -- table: list of formatted lines
76-
stderr -- table: stderr output
77-
exit_code -- integer: exit code
78-
}
79-
```
80-
81-
Note that fmt_with does not apply the format for you, YOU would have to apply it:
82-
83-
```lua
84-
-- ...logic for checking exit code and stderr...
85-
require("guard.api").apply_fmt({
86-
bufnr, -- integer: buffer number
87-
prev_lines, -- table: previous text
88-
new_lines, -- table: format result
89-
srow, -- integer: start row
90-
erow, -- integer: end row
91-
})
92-
```
93-
94-
A bit of explanation: `prev_lines` if for using `vim.diff` to apply the minimal amount of changes to the buffer, if you choose to not pass this for some reason, you have to provide `erow` so that guard knows precisely what to replace.
95-
96-
Btw, you can get `prev_lines` using `require("guard.util").get_lines`

lua/guard/api.lua

Lines changed: 0 additions & 5 deletions
This file was deleted.

lua/guard/events.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ local function watch_ft(fts)
88
group = group,
99
pattern = fts,
1010
callback = function(args)
11-
format.attach_to_buf(args.buf)
11+
if
12+
#api.nvim_get_autocmds({
13+
group = group,
14+
event = 'BufWritePre',
15+
buffer = args.buf,
16+
}) == 0
17+
then
18+
format.attach_to_buf(args.buf)
19+
end
1220
end,
1321
desc = 'guard',
1422
})

lua/guard/format.lua

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
local api = vim.api
22
---@diagnostic disable-next-line: deprecated
33
local uv = vim.version().minor >= 10 and vim.uv or vim.loop
4+
local spawn = require('guard.spawn').try_spawn
45
local util = require('guard.util')
56
local get_prev_lines = util.get_prev_lines
67
local filetype = require('guard.filetype')
7-
local guard_api = require('guard.api')
88

99
local function ignored(buf, patterns)
1010
local fname = api.nvim_buf_get_name(buf)
@@ -123,7 +123,9 @@ local function do_fmt(buf)
123123
vim.notify('[Guard] missing config for filetype ' .. vim.bo[buf].filetype, vim.log.levels.ERROR)
124124
return
125125
end
126-
local range, srow, erow = nil, 0, -1
126+
local srow = 0
127+
local erow = -1
128+
local range
127129
local mode = api.nvim_get_mode().mode
128130
if mode == 'V' or mode == 'v' then
129131
range = util.range_from_selection(buf, mode)
@@ -142,10 +144,11 @@ local function do_fmt(buf)
142144
local prev_lines = table.concat(get_prev_lines(buf, srow, erow), '')
143145

144146
coroutine.resume(coroutine.create(function()
145-
local result
147+
local new_lines
146148
local changedtick = api.nvim_buf_get_changedtick(buf)
149+
local reload = nil
147150

148-
for _, config in ipairs(fmt_configs) do
151+
for i, config in ipairs(fmt_configs) do
149152
local allow = true
150153
if config.ignore_patterns and ignored(buf, config.ignore_patterns) then
151154
allow = false
@@ -156,35 +159,31 @@ local function do_fmt(buf)
156159
end
157160

158161
if allow then
159-
if config.fn and not config.override then
160-
override_lsp(buf)
161-
config.override = true
162+
if config.cmd then
163+
config.lines = new_lines and new_lines or prev_lines
164+
config.args = config.args or {}
165+
config.args[#config.args + 1] = config.fname and fname or nil
166+
config.cwd = cwd
167+
reload = (not reload and config.stdout == false) and true or false
168+
new_lines = spawn(config)
169+
--restore
170+
config.lines = nil
171+
config.cwd = nil
172+
if config.fname then
173+
config.args[#config.args] = nil
174+
end
175+
elseif config.fn then
176+
if not config.override then
177+
override_lsp(buf)
178+
config.override = true
179+
end
180+
config.fn(buf, range)
181+
coroutine.yield()
182+
if i ~= #fmt_configs then
183+
new_lines = table.concat(get_prev_lines(buf, srow, erow), '')
184+
end
162185
end
163-
config.args = config.args or {}
164-
config.args[#config.args + 1] = config.fname and fname or nil
165-
config.cwd = cwd
166-
local opts = {
167-
buf = buf,
168-
range = range,
169-
}
170-
-- apply previous format result for non-stdin formatters to read
171-
if result and (config.cmd and not config.stdin) or (config.fn and not config.reload) then
172-
update_buffer(buf, prev_lines, result, srow)
173-
prev_lines = result
174-
end
175-
local out = guard_api.fmt_with(config, opts)
176-
---@diagnostic disable-next-line: need-check-nil
177-
local stderr, exit_code = out[2], out[3]
178-
if not (stderr ~= '' or exit_code ~= 0) then
179-
---@diagnostic disable-next-line: need-check-nil
180-
result = out[1]
181-
end
182-
-- restore
183-
config.cwd = nil
184-
if config.fname then
185-
config.args[#config.args] = nil
186-
end
187-
changedtick = vim.bo[buf].changedtick
186+
changedtick = vim.b[buf].changedtick
188187
end
189188
end
190189

@@ -196,10 +195,13 @@ local function do_fmt(buf)
196195
})
197196
return
198197
end
199-
update_buffer(buf, prev_lines, result, srow)
198+
update_buffer(buf, prev_lines, new_lines, srow)
199+
if reload and api.nvim_get_current_buf() == buf then
200+
vim.cmd.edit()
201+
end
200202
util.doau('GuardFmt', {
201203
status = 'done',
202-
results = result,
204+
results = new_lines,
203205
})
204206
end)
205207
end))

0 commit comments

Comments
 (0)