Skip to content

Commit e59c826

Browse files
authored
feat: Pass fname and cwd to parse function (#247)
* feat: Pass fname and cwd to parse function (#246) * feat: Update provider fn signature to receive fname and cwd closes #246 * docs: Add linter signatures and terraform example
1 parent 8e72406 commit e59c826

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

ADVANCED.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,41 @@ Et voilà!
180180

181181
![image](https://github.com/xiaoshihou514/guard.nvim/assets/108414369/f9137b5a-ae69-494f-9f5b-b6044ae63c86)
182182

183+
### Signatures for custom linter functions
184+
185+
When writing custom lint logic, `fn` and `parse` receive buffer context:
186+
187+
```lua
188+
lint.fn(prev_lines, fname, cwd)
189+
lint.parse(result, bufnr, fname, cwd)
190+
```
191+
192+
**Parameters:**
193+
194+
- `prev_lines` — buffer content (string)
195+
- `result` — linter output (string)
196+
- `bufnr` — target buffer number
197+
- `fname` — absolute file path
198+
- `cwd` — working directory
199+
200+
Use `fname` and `cwd` when linters output relative paths (e.g., `terraform validate` on a directory returns diagnostics for all files).
201+
202+
**Example** — filtering `terraform validate` by current file:
203+
204+
```lua
205+
parse = function(result, bufnr, fname, cwd)
206+
local current = fname:sub(#cwd + 2) -- +2 to include '/'
207+
local decoded = vim.json.decode(result)
208+
209+
for _, d in ipairs(decoded.diagnostics or {}) do
210+
-- terraform returns relative filenames; match against current
211+
if d.range and d.range.filename == current then
212+
-- add to diagnostics...
213+
end
214+
end
215+
end
216+
```
217+
183218
## Take advantage of autocmd events
184219

185220
Guard exposes a `GuardFmt` user event that you can use. It is called both before formatting starts and after it is completely done. To differentiate between pre-format and post-format calls, a `data` table is passed.

lua/guard/lint.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function M.do_lint_single(buf, config)
104104
end
105105

106106
if #data > 0 then
107-
results = lint.parse(data, buf)
107+
results = lint.parse(data, buf, fname, cwd)
108108
end
109109

110110
vim.schedule(function()
@@ -117,9 +117,9 @@ function M.do_lint_single(buf, config)
117117
end)
118118
end)
119119
else
120-
data = lint.fn(prev_lines)
120+
data = lint.fn(prev_lines, fname, cwd)
121121
if #data > 0 then
122-
results = lint.parse(data, buf)
122+
results = lint.parse(data, buf, fname, cwd)
123123
end
124124

125125
vim.schedule(function()

0 commit comments

Comments
 (0)