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 ---@class ConfigOptions
local defaultOpts = { local defaultOpts = {
---@type string | fun() ---@type string | fun()
colorscheme = "rose-pine", colorscheme = "rose-pine",
} }
vim.g.disable_autoformat = false
---@param name "options" | "keymaps" | "clipboard" ---@param name "options" | "keymaps" | "clipboard"
function M.load(name) function M.load(name)
local mod = "juancwu.config." .. name local mod = "juancwu.config." .. name
local error_handler = function(err) local error_handler = function(err)
local msg = "Failed loading " .. mod .. "\n\n" .. err local msg = "Failed loading " .. mod .. "\n\n" .. err
print(msg) print(msg)
end end
xpcall(function() xpcall(function()
require(mod) require(mod)
end, error_handler) end, error_handler)
end end
---@type ConfigOptions ---@type ConfigOptions
@ -24,40 +26,48 @@ local options
---@param opts? ConfigOptions ---@param opts? ConfigOptions
function M.setup(opts) 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("options")
M.load("keymaps") M.load("keymaps")
M.load("clipboard") M.load("clipboard")
require("lazy").setup("juancwu.plugins") require("lazy").setup("juancwu.plugins")
-- try to load colorscheme -- try to load colorscheme
xpcall(function() xpcall(function()
if type(M.colorscheme) == "function" then if type(M.colorscheme) == "function" then
M.colorscheme() M.colorscheme()
else else
vim.cmd.colorscheme(M.colorscheme) vim.cmd.colorscheme(M.colorscheme)
end end
end, function(err) end, function(err)
if type(M.colorscheme) == "string" then if type(M.colorscheme) == "string" then
local msg = "Failed to load colorscheme " .. M.colorscheme .. "\n\n" .. err local msg = "Failed to load colorscheme " .. M.colorscheme .. "\n\n" .. err
print(msg) print(msg)
else else
print("Failed to load colorscheme\n\n" .. err) print("Failed to load colorscheme\n\n" .. err)
end end
vim.cmd.colorscheme("rose-pine") vim.cmd.colorscheme("rose-pine")
end) 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 end
setmetatable(M, { setmetatable(M, {
__index = function(_, k) __index = function(_, k)
if options == nil then if options == nil then
return vim.deepcopy(defaultOpts)[k] return vim.deepcopy(defaultOpts)[k]
end end
return options[k] return options[k]
end end,
}) })
return M return M

View file

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