mirror of
https://github.com/pojokcodeid/nvim-lazy.git
synced 2025-06-24 17:58:46 +02:00
enc: update format on save flow
This commit is contained in:
parent
2486c9e49e
commit
77ec605527
3 changed files with 114 additions and 137 deletions
|
@ -18,65 +18,7 @@ return {
|
|||
"nvimtools/none-ls-extras.nvim",
|
||||
},
|
||||
event = "InsertEnter",
|
||||
opts = function()
|
||||
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
|
||||
local sources = {}
|
||||
local data_ok, data_sources = pcall(require, "custom.null-ls")
|
||||
if data_ok then
|
||||
for _, cfg in pairs(data_sources.sources) do
|
||||
table.insert(sources, cfg)
|
||||
end
|
||||
end
|
||||
local on_save = pcode.format_on_save or 0
|
||||
if on_save == 1 then
|
||||
return {
|
||||
debug = false,
|
||||
sources = sources,
|
||||
on_attach = function(client, bufnr)
|
||||
vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
|
||||
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 and client.name == "null-ls")
|
||||
or client.supports_method("textDocument/formatting")
|
||||
then
|
||||
vim.api.nvim_create_autocmd("BufWritePre", {
|
||||
group = augroup,
|
||||
buffer = bufnr,
|
||||
callback = function()
|
||||
vim.lsp.buf.format({ bufnr = bufnr, timeout_ms = pcode.format_timeout_ms or 5000 })
|
||||
end,
|
||||
})
|
||||
else
|
||||
vim.api.nvim_create_autocmd("BufWritePre", {
|
||||
group = augroup,
|
||||
buffer = bufnr,
|
||||
callback = function()
|
||||
vim.lsp.buf.format({ bufnr = bufnr, timeout_ms = pcode.format_timeout_ms or 5000, filter = nil })
|
||||
end,
|
||||
})
|
||||
end
|
||||
end,
|
||||
}
|
||||
else
|
||||
vim.schedule(function()
|
||||
pcall(function()
|
||||
vim.api.nvim_clear_autocmds({ group = "LspFormatting" })
|
||||
end)
|
||||
end)
|
||||
|
||||
return {
|
||||
debug = false,
|
||||
sources = sources,
|
||||
}
|
||||
end
|
||||
end,
|
||||
config = function(_, opts)
|
||||
require("null-ls").setup(opts)
|
||||
end,
|
||||
opts = {},
|
||||
config = true,
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue