add commands to enable/disable format on save

This commit is contained in:
jc 2023-12-28 14:11:44 -05:00
commit 512ab888d1
No known key found for this signature in database
2 changed files with 53 additions and 41 deletions

View file

@ -3,20 +3,22 @@ local M = {}
---@class ConfigOptions
local defaultOpts = {
---@type string | fun()
colorscheme = "rose-pine",
---@type string | fun()
colorscheme = "rose-pine",
}
vim.g.disable_autoformat = false
---@param name "options" | "keymaps" | "clipboard"
function M.load(name)
local mod = "juancwu.config." .. name
local error_handler = function(err)
local msg = "Failed loading " .. mod .. "\n\n" .. err
print(msg)
end
xpcall(function()
require(mod)
end, error_handler)
local mod = "juancwu.config." .. name
local error_handler = function(err)
local msg = "Failed loading " .. mod .. "\n\n" .. err
print(msg)
end
xpcall(function()
require(mod)
end, error_handler)
end
---@type ConfigOptions
@ -24,40 +26,48 @@ local options
---@param opts? ConfigOptions
function M.setup(opts)
options = vim.tbl_deep_extend("force", defaultOpts, opts or {}) or {}
options = vim.tbl_deep_extend("force", defaultOpts, opts or {}) or {}
M.load("options")
M.load("keymaps")
M.load("clipboard")
M.load("options")
M.load("keymaps")
M.load("clipboard")
require("lazy").setup("juancwu.plugins")
require("lazy").setup("juancwu.plugins")
-- try to load colorscheme
xpcall(function()
if type(M.colorscheme) == "function" then
M.colorscheme()
else
vim.cmd.colorscheme(M.colorscheme)
end
end, function(err)
if type(M.colorscheme) == "string" then
local msg = "Failed to load colorscheme " .. M.colorscheme .. "\n\n" .. err
print(msg)
else
print("Failed to load colorscheme\n\n" .. err)
end
vim.cmd.colorscheme("rose-pine")
end)
-- try to load colorscheme
xpcall(function()
if type(M.colorscheme) == "function" then
M.colorscheme()
else
vim.cmd.colorscheme(M.colorscheme)
end
end, function(err)
if type(M.colorscheme) == "string" then
local msg = "Failed to load colorscheme " .. M.colorscheme .. "\n\n" .. err
print(msg)
else
print("Failed to load colorscheme\n\n" .. err)
end
vim.cmd.colorscheme("rose-pine")
end)
-- create command to disable autoformat
vim.api.nvim_create_user_command("FormatDisable", function(args)
vim.g.disable_autoformat = true
end, { desc = "Disable Autoformat" })
vim.api.nvim_create_user_command("FormatEnable", function(args)
vim.g.disable_autoformat = false
end, { desc = "Enable Autoformat" })
end
setmetatable(M, {
__index = function(_, k)
if options == nil then
return vim.deepcopy(defaultOpts)[k]
end
__index = function(_, k)
if options == nil then
return vim.deepcopy(defaultOpts)[k]
end
return options[k]
end
return options[k]
end,
})
return M

View file

@ -37,10 +37,12 @@ return {
javascriptreact = { "prettierd" },
typescriptreact = { "prettierd" },
},
format_on_save = {
timeout_ms = 500,
lsp_fallback = true,
},
format_on_save = function(bufnr)
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
return { timeout_ms = 500, lsp_fallback = true }
end,
},
},
},