pojokcodeid.nvim-lazy/lua/user/format_onsave.lua

72 lines
2.1 KiB
Lua
Raw Normal View History

2023-02-19 16:41:10 +07:00
local run = 0
2024-06-14 08:21:36 +07:00
local frmt = pcode.format_on_save or 0
2024-05-21 14:20:50 +07:00
if frmt == 1 then
run = 1
2024-05-21 14:20:50 +07:00
else
run = 0
2023-02-19 16:41:10 +07:00
end
2023-02-19 17:26:07 +07:00
2024-05-21 14:20:50 +07:00
local buf_clients = vim.lsp.get_clients()
2024-02-23 19:49:22 +07:00
if next(buf_clients) == nil then
run = 0
2024-02-23 19:49:22 +07:00
end
2023-02-19 16:41:10 +07:00
if run == 1 then
2024-06-24 12:05:25 +07:00
-- function FORMAT_FILTER(client)
-- local filetype = vim.bo.filetype
-- local n = require("null-ls")
-- local s = require("null-ls.sources")
-- local method = n.methods.FORMATTING
-- local available_formatters = s.get_available(filetype, method)
--
-- if #available_formatters > 0 then
-- return client.name == "null-ls"
-- elseif client.supports_method("textDocument/formatting") then
-- return true
-- else
-- return false
-- end
-- end
--
-- vim.cmd([[
-- augroup _lsp
-- autocmd!
-- " autocmd BufWritePre * lua vim.lsp.buf.format{timeout_ms =200, filter=format_filter}
-- autocmd BufWritePre * lua vim.lsp.buf.format{timeout_ms=pcode.format_timeout_ms or 5000 ,filter=FORMAT_FILTER}
-- augroup end
-- ]])
---filter passed to vim.lsp.buf.format
---always selects null-ls if it's available and caches the value per buffer
---@param client table client attached to a buffer
---@return boolean if client matches
function FORMAT_FILTER(client)
local filetype = vim.bo.filetype
2024-06-14 08:40:35 +07:00
local n = require("null-ls")
local s = require("null-ls.sources")
local method = n.methods.FORMATTING
local available_formatters = s.get_available(filetype, method)
2023-01-15 00:17:41 +07:00
if #available_formatters > 0 then
return client.name == "null-ls"
2024-06-14 08:40:35 +07:00
elseif client.supports_method("textDocument/formatting") then
return true
else
return false
end
end
2024-06-24 12:05:25 +07:00
vim.api.nvim_create_autocmd("BufWritePre", {
group = "lsp_format_on_save",
pattern = "*",
callback = function()
vim.lsp.buf.format({ timeout_ms = pcode.format_timeout_ms or 5000, filter = FORMAT_FILTER })
end,
})
else
vim.schedule(function()
pcall(function()
vim.api.nvim_clear_autocmds({ group = "lsp_format_on_save" })
end)
end)
2023-02-19 16:41:10 +07:00
end