-
-
Notifications
You must be signed in to change notification settings - Fork 26
more flexibility for autocmd control #41
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9b016ac
fix: LspAttach after GuardDisable errors out
xiaoshihou514 26a66cf
better GuardDisable, dumped a lot of code into utils.lua to make init…
xiaoshihou514 19fb9a1
readme
xiaoshihou514 2389663
mv attach_to_buf into format.lua
xiaoshihou514 d08230d
format: add fnlfmt
iujakchu 1b1af61
create cmds exactly once, debugging edge cases(WIP)
xiaoshihou514 969d6be
Merge pull request #42 from iujakchu/main
glepnir ccdd3ab
chore(doc): auto generate docs
github-actions[bot] ba15e5d
Merge branch 'nvimdev:main' into main
xiaoshihou514 0c404a2
new modules and done testing
xiaoshihou514 e28b1d0
remove redundant pcall
xiaoshihou514 cb2bcb8
change api
xiaoshihou514 713efd4
fix docs
xiaoshihou514 4d5a765
simplify
xiaoshihou514 d646c87
debloat
xiaoshihou514 4f7f169
since disable and enable are so short removed the api module
xiaoshihou514 fb375c9
Merge branch 'main' into main
glepnir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
new modules and done testing
- Loading branch information
commit 0c404a2949f6cad3bde894fe421f38005ff3a867
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
local api = vim.api | ||
local ft_handler = require('guard.filetype') | ||
local events = require('guard.events') | ||
local format = require('guard.format') | ||
|
||
local function disable(opts) | ||
local guard_aus = api.nvim_get_autocmds({ group = 'Guard' }) | ||
if #opts.fargs == 0 then | ||
for _, au in ipairs(guard_aus) do | ||
api.nvim_del_autocmd(au.id) | ||
end | ||
return | ||
end | ||
if #guard_aus == 0 then | ||
return | ||
end | ||
local arg = opts.args | ||
local _, bufnr = pcall(tonumber, arg) | ||
if bufnr then | ||
local bufau = api.nvim_get_autocmds({ group = 'Guard', event = 'BufWritePre', buffer = bufnr }) | ||
if #bufau ~= 0 then | ||
api.nvim_del_autocmd(bufau[1].id) | ||
end | ||
else | ||
xiaoshihou514 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
local listener = api.nvim_get_autocmds({ group = 'Guard', event = 'FileType', pattern = arg }) | ||
if #listener ~= 0 then | ||
api.nvim_del_autocmd(listener[1].id) | ||
end | ||
local bufaus = api.nvim_get_autocmds({ group = 'Guard', event = 'BufWritePre' }) | ||
for _, au in ipairs(bufaus) do | ||
if vim.bo[au.buffer].ft == arg then | ||
api.nvim_del_autocmd(au.id) | ||
end | ||
end | ||
end | ||
end | ||
|
||
local function enable(opts) | ||
if #opts.fargs == 0 and #api.nvim_get_autocmds({ group = 'Guard' }) == 0 then | ||
local pattern = vim.tbl_keys(ft_handler) | ||
events.watch_ft(pattern) | ||
for _, buf in ipairs(api.nvim_list_bufs()) do | ||
if vim.tbl_contains(pattern, vim.bo[buf].ft) then | ||
format.attach_to_buf(buf) | ||
end | ||
end | ||
return | ||
end | ||
local arg = opts.args | ||
local _, bufnr = pcall(tonumber, arg) | ||
if bufnr then | ||
local bufau = api.nvim_get_autocmds({ group = 'Guard', event = 'BufWritePre', buffer = bufnr }) | ||
if #bufau == 0 then | ||
format.attach_to_buf(bufnr) | ||
end | ||
else | ||
local listener = api.nvim_get_autocmds({ group = 'Guard', event = 'FileType', pattern = arg }) | ||
if #listener == 0 then | ||
events.watch_ft(arg) | ||
end | ||
for _, buf in ipairs(api.nvim_list_bufs()) do | ||
if vim.bo[buf].ft == arg then | ||
format.attach_to_buf(buf) | ||
end | ||
end | ||
end | ||
end | ||
|
||
return { | ||
disable = disable, | ||
enable = enable, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
local api = vim.api | ||
local group = api.nvim_create_augroup('Guard', { clear = true }) | ||
local ft_handler = require('guard.filetype') | ||
local format = require('guard.format') | ||
|
||
local function watch_ft(fts) | ||
api.nvim_create_autocmd('FileType', { | ||
group = group, | ||
pattern = fts, | ||
callback = function(args) | ||
format.attach_to_buf(args.buf) | ||
end, | ||
desc = 'guard', | ||
}) | ||
end | ||
|
||
local function create_lspattach_autocmd(fmt_on_save) | ||
api.nvim_create_autocmd('LspAttach', { | ||
group = group, | ||
callback = function(args) | ||
local client = vim.lsp.get_client_by_id(args.data.client_id) | ||
---@diagnostic disable-next-line: need-check-nil | ||
if not client.supports_method('textDocument/formatting') then | ||
return | ||
end | ||
local ft = vim.bo[args.buf].filetype | ||
if not(ft_handler[ft] and ft_handler[ft].format) then | ||
ft_handler(ft):fmt('lsp') | ||
end | ||
|
||
if | ||
fmt_on_save | ||
and #api.nvim_get_autocmds({ | ||
group = group, | ||
event = 'FileType', | ||
pattern = ft, | ||
}) | ||
== 0 | ||
then | ||
format.attach_to_buf(args.buf) | ||
end | ||
end, | ||
}) | ||
end | ||
|
||
return { | ||
watch_ft = watch_ft, | ||
create_lspattach_autocmd = create_lspattach_autocmd, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.