Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion 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 October 25
*guard.nvim.txt* For NVIM v0.8.0 Last change: 2024 October 26

==============================================================================
Table of Contents *guard.nvim-table-of-contents*
Expand Down
44 changes: 27 additions & 17 deletions lua/guard/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ function M.watch_ft(ft)
})
end

---@param config table
---@param ft string
---@param buf number
function M.maybe_default_to_lsp(config, ft, buf)
if config.formatter then
return
end
config:fmt('lsp')
if getopt('fmt_on_save') then
if
#api.nvim_get_autocmds({
group = M.group,
event = 'FileType',
pattern = ft,
}) == 0
then
M.watch_ft(ft)
end
M.try_attach_to_buf(buf)
end
end

function M.create_lspattach_autocmd()
au('LspAttach', {
group = M.group,
Expand All @@ -68,27 +90,15 @@ function M.create_lspattach_autocmd()
return
end
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
if
not client
or not client.supports_method('textDocument/formatting', { bufnr = args.data.buf })
then
return
end
local ft_handler = require('guard.filetype')
local ft = vim.bo[args.buf].filetype
if not (ft_handler[ft] and ft_handler[ft].formatter) then
ft_handler(ft):fmt('lsp')
end
if getopt('fmt_on_save') then
if
#api.nvim_get_autocmds({
group = M.group,
event = 'FileType',
pattern = ft,
}) == 0
then
M.watch_ft(ft)
end
M.try_attach_to_buf(args.buf)
end
M.maybe_default_to_lsp(ft_handler(ft), ft, args.buf)
end,
})
end
Expand Down
15 changes: 7 additions & 8 deletions lua/guard/format.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
local api = vim.api
local spawn = require('guard.spawn')
local util = require('guard.util')
local report_error = util.report_error
local filetype = require('guard.filetype')
local iter, filter = vim.iter, vim.tbl_filter
local api, iter, filter = vim.api, vim.iter, vim.tbl_filter

local function save_views(bufnr)
local views = {}
Expand Down Expand Up @@ -53,8 +51,9 @@ end
local function do_fmt(buf)
buf = buf or api.nvim_get_current_buf()
local ft_conf = filetype[vim.bo[buf].filetype]

if not ft_conf or not ft_conf.formatter then
report_error('missing config for filetype ' .. vim.bo[buf].filetype)
util.report_error('missing config for filetype ' .. vim.bo[buf].filetype)
return
end

Expand All @@ -81,7 +80,7 @@ local function do_fmt(buf)
-- check if all cmds executable again, since user can call format manually
local all_executable = not iter(fmt_configs):any(function(config)
if config.cmd and vim.fn.executable(config.cmd) ~= 1 then
report_error(config.cmd .. ' not executable')
util.report_error(config.cmd .. ' not executable')
return true
end
return false
Expand All @@ -101,8 +100,8 @@ local function do_fmt(buf)

-- error if one of the formatters is impure and the user requested range formatting
if range and #impure:totable() > 0 then
report_error('Cannot apply range formatting for filetype ' .. vim.bo[buf].filetype)
report_error(impure
util.report_error('Cannot apply range formatting for filetype ' .. vim.bo[buf].filetype)
util.report_error(impure
:map(function(config)
return config.cmd
end)
Expand Down Expand Up @@ -214,7 +213,7 @@ local function do_fmt(buf)
end

-- refresh buffer
if impure and #impure > 0 then
if impure and #impure:totable() > 0 then
vim.schedule(function()
api.nvim_buf_call(buf, function()
local views = save_views(buf)
Expand Down
14 changes: 14 additions & 0 deletions plugin/guard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ loaded = true

local api = vim.api
local events = require('guard.events')
local ft_handler = require('guard.filetype')

local cmds = {
fmt = function()
Expand Down Expand Up @@ -114,3 +115,16 @@ end, {
})

events.create_lspattach_autocmd()

for _, buf in ipairs(api.nvim_list_bufs()) do
if api.nvim_buf_is_loaded(buf) then
if
vim.iter(vim.lsp.get_clients({ bufnr = buf })):any(function(c)
return c.supports_method('textDocument/formatting')
end)
then
local ft = vim.bo[buf].ft
events.maybe_default_to_lsp(ft_handler(ft), ft, buf)
end
end
end