diff --git a/lua/core/init.lua b/lua/core/init.lua index 984f458..c261ef4 100644 --- a/lua/core/init.lua +++ b/lua/core/init.lua @@ -2,7 +2,7 @@ require("user.options") require("user.keymaps") require("user.autocommands") -require("user.format_onsave") +require("user.format") -- require("user.colorscheme") -- require("user.snip") -- require("user.bufferline") diff --git a/lua/custom/format_onsave.lua b/lua/custom/format_onsave.lua deleted file mode 100644 index 085c4ef..0000000 --- a/lua/custom/format_onsave.lua +++ /dev/null @@ -1,5 +0,0 @@ -local M = { - disable = 0, -- 0 ( format jalan) 1 (fromat off) -} - -return M diff --git a/lua/custom/format_onsave_20230219.lua b/lua/custom/format_onsave_20230219.lua new file mode 100644 index 0000000..87ce7dc --- /dev/null +++ b/lua/custom/format_onsave_20230219.lua @@ -0,0 +1,5 @@ +local M = { + disable = 1, -- 0 ( format jalan) 1 (fromat off) +} + +return M diff --git a/lua/user/format.lua b/lua/user/format.lua new file mode 100644 index 0000000..d88133b --- /dev/null +++ b/lua/user/format.lua @@ -0,0 +1,7 @@ +local data_exists, frmt = pcall(require, "custom.format_onsave") +if not data_exists then + require("user.format_onsave") +end +if frmt.disable == 0 then + require("user.format_onsave") +end diff --git a/lua/user/format_onsave.lua b/lua/user/format_onsave.lua index 9d46249..9d78330 100644 --- a/lua/user/format_onsave.lua +++ b/lua/user/format_onsave.lua @@ -1,27 +1,22 @@ -local data_exists, frmt = pcall(require, "custom.format_onsave") -if data_exists then - if frmt.disable == 0 then - 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) +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([[ + 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=1000 ,filter=format_filter} augroup end ]]) - end -end