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

23 lines
669 B
Lua
Raw Normal View History

2023-01-15 00:17:41 +07:00
function format_filter(client)
2023-01-19 16:52:22 +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-01-19 16:52:22 +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-01-15 00:17:41 +07:00
end
2023-01-19 16:52:22 +07:00
vim.cmd([[
2023-01-15 00:17:41 +07:00
augroup _lsp
autocmd!
" autocmd BufWritePre * lua vim.lsp.buf.format{timeout_ms =200, filter=format_filter}
2023-01-19 16:52:22 +07:00
autocmd BufWritePre * lua vim.lsp.buf.format{timeout_ms=1000 ,filter=format_filter}
2023-01-15 00:17:41 +07:00
augroup end
2023-01-19 16:52:22 +07:00
]])