Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: event API #181

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2d67975
chore: minor code simplification
xiaoshihou514 Dec 9, 2024
d2b03a1
feat!: event api
xiaoshihou514 Dec 9, 2024
7911e94
fix: test
xiaoshihou514 Dec 9, 2024
bd33a3b
feat: provide command interface
xiaoshihou514 Dec 10, 2024
7b7fb54
chore(doc): auto generate docs
github-actions[bot] Dec 10, 2024
771fa7b
feat: add error reporting for linters
xiaoshihou514 Dec 12, 2024
1193099
chore(doc): auto generate docs
github-actions[bot] Dec 12, 2024
596b3a3
ci: add linter test
xiaoshihou514 Dec 12, 2024
b82a8f7
Merge branch 'main' of github.com:xiaoshihou514/guard.nvim
xiaoshihou514 Dec 12, 2024
fbcbafd
ci: more tests
xiaoshihou514 Dec 12, 2024
8e12ec8
chore: update bug report issue template
xiaoshihou514 Dec 12, 2024
5510a1f
Update bug_report.yml
xiaoshihou514 Dec 12, 2024
964e5e3
chore(doc): auto generate docs
github-actions[bot] Dec 13, 2024
4ad9e63
wip: add custom event support for formatting
xiaoshihou514 Dec 15, 2024
c91acd5
chore(doc): auto generate docs
github-actions[bot] Dec 15, 2024
dcba169
ci: add tests for custom formatter events
xiaoshihou514 Dec 15, 2024
8678e4e
fix: enable/disable-fmt
xiaoshihou514 Dec 15, 2024
83350ac
fix(side quest): fix generic linters properly
xiaoshihou514 Dec 15, 2024
ec7a632
update
xiaoshihou514 Dec 15, 2024
3a36483
Merge branch 'main' into main
xiaoshihou514 Dec 15, 2024
5975c60
fix: type check
xiaoshihou514 Dec 15, 2024
e3dc4b8
scaffold more stuff
xiaoshihou514 Dec 18, 2024
7bb6967
chore(doc): auto generate docs
github-actions[bot] Dec 18, 2024
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
22 changes: 9 additions & 13 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,17 @@ body:
git clone https://github.com/nvimdev/guard-collection /tmp
echo "vim.opt.rtp:append('/tmp/guard.nvim')" >> /tmp/repro.lua
echo "vim.opt.rtp:append('/tmp/guard-collection')" >> /tmp/repro.lua
nvim --clean -u /tmp/repro.lua
validations:
required: true
- type: checkboxes
attributes:
label: Are you sure this is a min repro?
options:
- 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.
required: true
- label: I can confirm that my reproduction step only involves `vim.opt.rtp` and configuration themselves
required: true
- type: textarea
attributes:
label: "Expected behaviour"
Expand All @@ -53,16 +62,3 @@ body:
label: "Actual behaviour"
validations:
required: true
- type: textarea
attributes:
label: "The minimal config used to reproduce this issue."
description: |
Run with `nvim -u /tmp/repro.lua`
placeholder: |
vim.opt.rtp:append('/tmp/guard.nvim')
vim.opt.rtp:append('/tmp/guard-collection')
-- do anything else you need to do to reproduce the issue

render: "Lua"
validations:
required: true
1 change: 1 addition & 0 deletions CUSTOMIZE.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ A tool is specified as follows:

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

Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ vim.g.guard_config = {
```

- 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)
- `Guard disable` disables auto format for the current buffer, you can also `Guard disable 16` (the buffer number)
- Use `Guard enable` to re-enable auto format, usage is the same as `Guard disable`
- `enable-fmt`, `disable-fmt` turns auto formatting on and off for the current buffer.

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

Expand Down Expand Up @@ -92,6 +91,9 @@ Lint all your files with `codespell`
ft('*'):lint('codespell')
```

- Use `Guard Lint` to lint manually.
- `enable-lint` and `disable-lint` controls auto linting for the current buffer.

You can also easily create your own configuration that's not in `guard-collection`, see [CUSTOMIZE.md](./CUSTOMIZE.md).

For more niche use cases, [ADVANCED.md](./ADVANCED.md) demonstrates how to:
Expand Down
9 changes: 6 additions & 3 deletions 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: 2024 December 13
*guard.nvim.txt* For NVIM v0.8.0 Last change: 2024 December 18

==============================================================================
Table of Contents *guard.nvim-table-of-contents*
Expand Down Expand Up @@ -80,8 +80,7 @@ To register formatters and linters:
<

- 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)
- `Guard disable` disables auto format for the current buffer, you can also `Guard disable 16` (the buffer number)
- Use `Guard enable` to re-enable auto format, usage is the same as `Guard disable`
- `enable-fmt`, `disable-fmt` turns auto formatting on and off for the current buffer.

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

Expand Down Expand Up @@ -112,6 +111,9 @@ Lint all your files with `codespell`
ft('*'):lint('codespell')
<

- Use `Guard Lint` to lint manually.
- `enable-lint` and `disable-lint` controls auto linting for the current buffer.

You can also easily create your own configuration that’s not in
`guard-collection`, see CUSTOMIZE.md <./CUSTOMIZE.md>.

Expand Down Expand Up @@ -150,6 +152,7 @@ A tool is specified as follows:

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

Expand Down
8 changes: 8 additions & 0 deletions lua/guard/_meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
---@field fname boolean?
---@field stdin boolean?
---@field fn function?
---@field events EventOption[]
---@field ignore_patterns string|string[]?
---@field ignore_error boolean?
---@field find string|string[]?
Expand All @@ -18,6 +19,7 @@
---@field fname boolean?
---@field stdin boolean?
---@field fn function?
---@field events EventOption[]
---@field parse function
---@field ignore_patterns string|string[]?
---@field ignore_error boolean?
Expand All @@ -26,3 +28,9 @@
---@field timeout integer?

---@alias LintConfig LintConfigTable|fun(): LintConfigTable

---@alias AuOption vim.api.keyset.create_autocmd

---@class EventOption
---@field name string
---@field opt AuOption?
122 changes: 122 additions & 0 deletions lua/guard/api.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
-- These are considered public API and changing their signature would be a breaking change
local M = {}
local api = vim.api
local events = require('guard.events')

---Format bufnr or current buffer
---@param bufnr number?
function M.fmt(bufnr)
require('guard.format').do_fmt(bufnr)
end

---Lint bufnr or current buffer
---@param bufnr number?
function M.lint(bufnr)
require('guard.lint').do_lint(bufnr)
end

---Enable format for bufnr or current buffer
---@param bufnr number?
function M.enable_fmt(bufnr)
local buf = bufnr or api.nvim_get_current_buf()
local ft_handler = require('guard.filetype')
local ft = vim.bo[buf].ft
local head = vim.tbl_get(ft_handler, ft, 'formatter', 1)
if type(head) == 'table' and type(head.events) == 'table' then
events.attach_custom(ft, head.events)
else
events.try_attach_fmt_to_buf(buf)
end
end

---Disable format for bufnr or current buffer
---@param bufnr number?
function M.disable_fmt(bufnr)
local buf = bufnr or api.nvim_get_current_buf()
vim.iter(events.get_format_autocmds(buf)):each(function(x)
api.nvim_del_autocmd(x)
end)
events.user_fmt_autocmds[vim.bo[buf].ft] = {}
end

---Enable lint for bufnr or current buffer
---@param bufnr number?
function M.enable_lint(bufnr)
local buf = bufnr or api.nvim_get_current_buf()
local ft = require('guard.filetype')[vim.bo[buf].ft] or {}
if ft.linter and #ft.linter > 0 then
events.try_attach_lint_to_buf(buf, require('guard.util').linter_events(ft.linter[1]), ft)
end
end

---Disable format for bufnr or current buffer
---@param bufnr number?
function M.disable_lint(bufnr)
local aus = events.get_lint_autocmds(bufnr or api.nvim_get_current_buf())
vim.iter(aus):each(function(au)
api.nvim_del_autocmd(au.id)
end)
end

---Show guard info for current buffer
function M.info()
xiaoshihou514 marked this conversation as resolved.
Show resolved Hide resolved
local util = require('guard.util')
local buf = api.nvim_get_current_buf()
local ft = require('guard.filetype')[vim.bo[buf].ft] or {}
local formatters = ft.formatter or {}
local linters = ft.linter or {}
local fmtau = events.get_format_autocmds(buf)
local lintau = events.get_lint_autocmds(buf)

util.open_info_win()
local lines = {
'# Guard info (press Esc or q to close)',
'## Settings:',
('- `fmt_on_save`: %s'):format(util.getopt('fmt_on_save')),
('- `lsp_as_default_formatter`: %s'):format(util.getopt('lsp_as_default_formatter')),
('- `save_on_fmt`: %s'):format(util.getopt('save_on_fmt')),
'',
('## Current buffer has filetype %s:'):format(vim.bo[buf].ft),
('- %s formatter autocmds attached'):format(#fmtau),
('- %s linter autocmds attached'):format(#lintau),
'- formatters:',
'',
}

if #formatters > 0 then
vim.list_extend(lines, { '', '```lua' })
vim.list_extend(
lines,
vim
.iter(formatters)
:map(function(formatter)
return vim.split(vim.inspect(formatter), '\n', { trimempty = true })
end)
:flatten()
:totable()
)
vim.list_extend(lines, { '```', '' })
end

vim.list_extend(lines, { '- linters:' })

if #linters > 0 then
vim.list_extend(lines, { '', '```lua' })
vim.list_extend(
lines,
vim
.iter(linters)
:map(function(linter)
return vim.split(vim.inspect(linter), '\n', { trimempty = true })
end)
:flatten()
:totable()
)
vim.list_extend(lines, { '```' })
end

api.nvim_buf_set_lines(0, 0, -1, true, lines)
api.nvim_set_option_value('modifiable', false, { buf = 0 })
end

return M
Loading