Skip to content

Commit 25ba07f

Browse files
xiaoshihou514github-actions[bot]glepnir
authored
feat!: event API (#181)
* chore: minor code simplification * feat!: event api * fix: test * feat: provide command interface * chore(doc): auto generate docs * feat: add error reporting for linters * chore(doc): auto generate docs * ci: add linter test * ci: more tests * chore: update bug report issue template * Update bug_report.yml * chore(doc): auto generate docs * wip: add custom event support for formatting * chore(doc): auto generate docs * ci: add tests for custom formatter events * fix: enable/disable-fmt * fix(side quest): fix generic linters properly * update * fix: type check * scaffold more stuff * chore(doc): auto generate docs * feat: add custom linter event logic * chore(doc): auto generate docs * update * feat: auto_lint * chore(doc): auto generate docs * special treat * fix global state * feat: add lint interval option + naming changes * chore(doc): auto generate docs * Update lua/guard/filetype.lua Co-authored-by: glepnir <[email protected]> * Update lua/guard/filetype.lua Co-authored-by: glepnir <[email protected]> * Update lua/guard/events.lua Co-authored-by: glepnir <[email protected]> * Update lua/guard/events.lua Co-authored-by: glepnir <[email protected]> * Update lua/guard/events.lua Co-authored-by: glepnir <[email protected]> * feat: unify guard info with healthcheck * chore: tweak issue template --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: glepnir <[email protected]>
1 parent 0a6d015 commit 25ba07f

19 files changed

+920
-365
lines changed

.github/ISSUE_TEMPLATE/bug_report.yml

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
1-
# https://github.com/mrcjkb/haskell-tools.nvim/blob/master/.github/ISSUE_TEMPLATE/bug_report.yml
1+
# Based on https://github.com/mrcjkb/haskell-tools.nvim/blob/master/.github/ISSUE_TEMPLATE/bug_report.yml
22
name: Bug Report
33
description: Report a problem with guard.nvim
44
labels: [bug]
55
body:
6-
76
- type: input
87
attributes:
98
label: "Neovim version (nvim -v)"
109
placeholder: "v0.10.0"
1110
validations:
1211
required: true
12+
1313
- type: input
1414
attributes:
1515
label: "Operating system/version"
1616
placeholder: "Fedora Linux 40"
1717
validations:
1818
required: true
19+
1920
- type: textarea
2021
attributes:
21-
label: "Output of :checkhealth guard"
22-
render: "console"
23-
placeholder: |
24-
Perhaps the tools are not in your $PATH?
22+
label: "Expected behaviour"
23+
description: "Describe the behaviour you expect."
2524
validations:
2625
required: true
26+
2727
- type: textarea
2828
attributes:
29-
label: "Output of :Guard info"
30-
render: "markdown"
29+
label: "Actual behaviour"
30+
validations:
31+
required: true
32+
33+
- type: textarea
34+
attributes:
35+
label: "Output of :checkhealth guard"
36+
render: "console"
3137
placeholder: |
32-
Are the autocmds attached?
38+
Perhaps the tools are not in your $PATH?
3339
validations:
3440
required: true
41+
3542
- type: textarea
3643
attributes:
3744
label: "How to reproduce the issue"
@@ -40,29 +47,15 @@ body:
4047
git clone https://github.com/nvimdev/guard-collection /tmp
4148
echo "vim.opt.rtp:append('/tmp/guard.nvim')" >> /tmp/repro.lua
4249
echo "vim.opt.rtp:append('/tmp/guard-collection')" >> /tmp/repro.lua
50+
nvim --clean -u /tmp/repro.lua
4351
validations:
4452
required: true
45-
- type: textarea
46-
attributes:
47-
label: "Expected behaviour"
48-
description: "Describe the behaviour you expect."
49-
validations:
50-
required: true
51-
- type: textarea
52-
attributes:
53-
label: "Actual behaviour"
54-
validations:
55-
required: true
56-
- type: textarea
57-
attributes:
58-
label: "The minimal config used to reproduce this issue."
59-
description: |
60-
Run with `nvim -u /tmp/repro.lua`
61-
placeholder: |
62-
vim.opt.rtp:append('/tmp/guard.nvim')
63-
vim.opt.rtp:append('/tmp/guard-collection')
64-
-- do anything else you need to do to reproduce the issue
6553

66-
render: "Lua"
67-
validations:
68-
required: true
54+
- type: checkboxes
55+
attributes:
56+
label: Are you sure this is a min repro?
57+
options:
58+
- label: I understand that if my repro step is too complicated (e.g. here is my 1k+ line config and please help me), developers might not be able to help.
59+
required: true
60+
- label: I can confirm that my reproduction step only involves `vim.opt.rtp` and configuration themselves
61+
required: true

CUSTOMIZE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ A tool is specified as follows:
2424

2525
-- special
2626
parse -- function: linter only, parses linter output to neovim diagnostic
27+
events -- { name: string, opt: autocmd options }: override default events, for formatter autocmds only the first one is used (passed in from `:fmt`)
2728
}
2829
```
2930

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,23 @@ ft('lang'):fmt('format-tool-1')
4949
:lint('lint-tool-1')
5050
:extra(extra_args)
5151

52-
-- change this anywhere in your config, these are the defaults
52+
-- change this anywhere in your config (or not), these are the defaults
5353
vim.g.guard_config = {
5454
-- format on write to buffer
5555
fmt_on_save = true,
5656
-- use lsp if no formatter was defined for this filetype
5757
lsp_as_default_formatter = false,
5858
-- whether or not to save the buffer after formatting
5959
save_on_fmt = true,
60+
-- automatic linting
61+
auto_lint = true,
62+
-- how frequently can linters be called
63+
lint_interval = 500
6064
}
6165
```
6266

6367
- Use `Guard fmt` to manually call format, when there is a visual selection only the selection is formatted. **NOTE**: Regional formatting just sends your selection to the formatter, if there's not enough context incoherent formatting might occur (e.g. indent being erased)
64-
- `Guard disable` disables auto format for the current buffer, you can also `Guard disable 16` (the buffer number)
65-
- Use `Guard enable` to re-enable auto format, usage is the same as `Guard disable`
68+
- `enable-fmt`, `disable-fmt` turns auto formatting on and off for the current buffer.
6669

6770
Format c files with clang-format and lint with clang-tidy:
6871

@@ -92,6 +95,9 @@ Lint all your files with `codespell`
9295
ft('*'):lint('codespell')
9396
```
9497

98+
- Use `Guard Lint` to lint manually.
99+
- `enable-lint` and `disable-lint` controls auto linting for the current buffer.
100+
95101
You can also easily create your own configuration that's not in `guard-collection`, see [CUSTOMIZE.md](./CUSTOMIZE.md).
96102

97103
For more niche use cases, [ADVANCED.md](./ADVANCED.md) demonstrates how to:

doc/guard.nvim.txt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*guard.nvim.txt* For NVIM v0.8.0 Last change: 2024 December 13
1+
*guard.nvim.txt* For NVIM v0.8.0 Last change: 2024 December 23
22

33
==============================================================================
44
Table of Contents *guard.nvim-table-of-contents*
@@ -68,20 +68,23 @@ To register formatters and linters:
6868
:lint('lint-tool-1')
6969
:extra(extra_args)
7070

71-
-- change this anywhere in your config, these are the defaults
71+
-- change this anywhere in your config (or not), these are the defaults
7272
vim.g.guard_config = {
7373
-- format on write to buffer
7474
fmt_on_save = true,
7575
-- use lsp if no formatter was defined for this filetype
7676
lsp_as_default_formatter = false,
7777
-- whether or not to save the buffer after formatting
7878
save_on_fmt = true,
79+
-- automatic linting
80+
auto_lint = true,
81+
-- how frequently can linters be called
82+
lint_interval = 500
7983
}
8084
<
8185

8286
- Use `Guard fmt` to manually call format, when there is a visual selection only the selection is formatted. **NOTE**: Regional formatting just sends your selection to the formatter, if there’s not enough context incoherent formatting might occur (e.g. indent being erased)
83-
- `Guard disable` disables auto format for the current buffer, you can also `Guard disable 16` (the buffer number)
84-
- Use `Guard enable` to re-enable auto format, usage is the same as `Guard disable`
87+
- `enable-fmt`, `disable-fmt` turns auto formatting on and off for the current buffer.
8588

8689
Format c files with clang-format and lint with clang-tidy:
8790

@@ -112,6 +115,9 @@ Lint all your files with `codespell`
112115
ft('*'):lint('codespell')
113116
<
114117

118+
- Use `Guard Lint` to lint manually.
119+
- `enable-lint` and `disable-lint` controls auto linting for the current buffer.
120+
115121
You can also easily create your own configuration that’s not in
116122
`guard-collection`, see CUSTOMIZE.md <./CUSTOMIZE.md>.
117123

@@ -150,6 +156,7 @@ A tool is specified as follows:
150156

151157
-- special
152158
parse -- function: linter only, parses linter output to neovim diagnostic
159+
events -- { name: string, opt: autocmd options }: override default events, for formatter autocmds only the first one is used (passed in from `:fmt`)
153160
}
154161
<
155162

lua/guard/_meta.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
---@field fname boolean?
55
---@field stdin boolean?
66
---@field fn function?
7+
---@field events EventOption[]
78
---@field ignore_patterns string|string[]?
89
---@field ignore_error boolean?
910
---@field find string|string[]?
@@ -18,6 +19,7 @@
1819
---@field fname boolean?
1920
---@field stdin boolean?
2021
---@field fn function?
22+
---@field events EventOption[]
2123
---@field parse function
2224
---@field ignore_patterns string|string[]?
2325
---@field ignore_error boolean?
@@ -26,3 +28,9 @@
2628
---@field timeout integer?
2729

2830
---@alias LintConfig LintConfigTable|fun(): LintConfigTable
31+
32+
---@alias AuOption vim.api.keyset.create_autocmd
33+
34+
---@class EventOption
35+
---@field name string
36+
---@field opt AuOption?

lua/guard/api.lua

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
-- These are considered public API and changing their signature would be a breaking change
2+
local M = {}
3+
local api = vim.api
4+
local events = require('guard.events')
5+
6+
---Format bufnr or current buffer
7+
---@param bufnr number?
8+
function M.fmt(bufnr)
9+
require('guard.format').do_fmt(bufnr)
10+
end
11+
12+
---Lint bufnr or current buffer
13+
---@param bufnr number?
14+
function M.lint(bufnr)
15+
require('guard.lint').do_lint(bufnr)
16+
end
17+
18+
---Enable format for bufnr or current buffer
19+
---@param bufnr number?
20+
function M.enable_fmt(bufnr)
21+
local buf = bufnr or api.nvim_get_current_buf()
22+
local ft_handler = require('guard.filetype')
23+
local ft = vim.bo[buf].ft
24+
local head = vim.tbl_get(ft_handler, ft, 'formatter', 1)
25+
if type(head) == 'table' and type(head.events) == 'table' then
26+
events.fmt_attach_custom(ft, head.events)
27+
else
28+
events.try_attach_fmt_to_buf(buf)
29+
end
30+
end
31+
32+
---Disable format for bufnr or current buffer
33+
---@param bufnr number?
34+
function M.disable_fmt(bufnr)
35+
local buf = bufnr or api.nvim_get_current_buf()
36+
vim.iter(events.get_format_autocmds(buf)):each(function(it)
37+
api.nvim_del_autocmd(it.id)
38+
end)
39+
events.user_fmt_autocmds[vim.bo[buf].ft] = {}
40+
end
41+
42+
---Enable lint for bufnr or current buffer
43+
---@param bufnr number?
44+
function M.enable_lint(bufnr)
45+
local buf = bufnr or api.nvim_get_current_buf()
46+
local ft = require('guard.filetype')[vim.bo[buf].ft] or {}
47+
if ft.linter and #ft.linter > 0 then
48+
events.try_attach_lint_to_buf(
49+
buf,
50+
require('guard.util').linter_events(ft.linter[1]),
51+
vim.bo[buf].ft
52+
)
53+
end
54+
end
55+
56+
---Disable format for bufnr or current buffer
57+
---@param bufnr number?
58+
function M.disable_lint(bufnr)
59+
local aus = events.get_lint_autocmds(bufnr or api.nvim_get_current_buf())
60+
vim.iter(aus):each(function(au)
61+
api.nvim_del_autocmd(au.id)
62+
end)
63+
end
64+
65+
---Show guard info for current buffer
66+
function M.info()
67+
vim.cmd('checkhealth guard')
68+
end
69+
70+
return M

0 commit comments

Comments
 (0)