2023-02-19 16:41:10 +07:00
|
|
|
local run = 0
|
2023-03-05 17:34:12 +07:00
|
|
|
local data_exists, frmt = pcall(require, "core.config")
|
2023-02-19 16:41:10 +07:00
|
|
|
if not data_exists then
|
|
|
|
run = 1
|
|
|
|
end
|
2023-03-05 17:34:12 +07:00
|
|
|
if frmt.format_on_save == 1 then
|
2023-02-19 16:41:10 +07:00
|
|
|
run = 1
|
|
|
|
end
|
2023-02-19 17:26:07 +07:00
|
|
|
|
2024-02-23 19:49:22 +07:00
|
|
|
local buf_clients = vim.lsp.buf_get_clients()
|
|
|
|
if next(buf_clients) == nil then
|
|
|
|
run = 0
|
|
|
|
end
|
|
|
|
|
2023-02-19 16:41:10 +07:00
|
|
|
if run == 1 then
|
2024-02-23 19:49:22 +07:00
|
|
|
function FORMAT_FILTER(client)
|
2023-02-19 16:41:10 +07:00
|
|
|
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)
|
2023-01-15 00:17:41 +07:00
|
|
|
|
2023-02-19 16:41:10 +07:00
|
|
|
if #available_formatters > 0 then
|
|
|
|
return client.name == "null-ls"
|
|
|
|
elseif client.supports_method("textDocument/formatting") then
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
2023-02-19 16:10:17 +07:00
|
|
|
end
|
2024-02-23 19:49:22 +07:00
|
|
|
|
2023-02-19 16:41:10 +07:00
|
|
|
vim.cmd([[
|
2023-02-19 15:27:50 +07:00
|
|
|
augroup _lsp
|
|
|
|
autocmd!
|
|
|
|
" autocmd BufWritePre * lua vim.lsp.buf.format{timeout_ms =200, filter=format_filter}
|
2024-02-23 19:49:22 +07:00
|
|
|
autocmd BufWritePre * lua vim.lsp.buf.format{timeout_ms=2000 ,filter=FORMAT_FILTER}
|
2023-02-19 15:27:50 +07:00
|
|
|
augroup end
|
|
|
|
]])
|
2023-02-19 16:41:10 +07:00
|
|
|
end
|