marleyvim/modules/nixvim/plugins/util/snacks/default.nix

190 lines
5.2 KiB
Nix

{
pkgs,
lib,
helpers,
...
}: let
inherit (helpers) mkRaw;
inherit (lib.marleyvim) keys;
in {
extraPackages = [pkgs.lazygit];
plugins.snacks = {
enable = true;
settings = {
bigfile.enabled = true;
notifier.enabled = true;
quickfile.enabled = true;
statuscolumn.enabled = true;
words.enabled = true;
};
};
keymaps = [
(keys.mk ["n"] "<LEADER>un" (
mkRaw ''
function() Snacks.notifier.hide() end
''
) "Dismiss All Notifications")
(keys.mk ["n"] "<LEADER>bd" (
mkRaw ''
function() Snacks.bufdelete() end
''
) "Delete Buffer")
(keys.mk ["n"] "<LEADER>bo" (
mkRaw ''
function() Snacks.bufdelete.other() end
''
) "Delete Other Buffers")
# LazyGit - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(keys.mk ["n"] "<LEADER>gg" (
mkRaw ''
function() Snacks.lazygit() end
''
) "Lazygit")
(keys.mk ["n"] "<LEADER>gf" (
mkRaw ''
function() Snacks.lazygit.log_file() end
''
) "Lazygit Current File History")
(keys.mk ["n"] "<LEADER>gl" (
mkRaw ''
function() Snacks.lazygit.log() end
''
) "Lazygit Log")
# Git - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(keys.mk ["n"] "<LEADER>gb" (
mkRaw ''
function() Snacks.git.blame_line() end
''
) "Git Blame Line")
(keys.mk ["n" "x"] "<LEADER>gB" (
mkRaw ''
function() Snacks.gitbrowse() end
''
) "Git Browse (open)")
(keys.mk ["n" "x"] "<LEADER>gY" (
mkRaw ''
function()
Snacks.gitbrowse({ open = function(url) vim.fn.setreg("+", url) end })
end
''
) "Git Browse (copy)")
];
# Toggles - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
extraConfigLua =
# lua
''
---@param buf? number
function format_enabled(buf)
buf = (buf == nil or buf == 0) and vim.api.nvim_get_current_buf() or buf
local gaf = vim.g.autoformat
local baf = vim.b[buf].autoformat
-- If the buffer has a local value, use that
if baf ~= nil then
return baf
end
-- Otherwise use the global value if set, or true by default
return gaf == nil or gaf
end
---@param enable? boolean
---@param buf? boolean
function format_enable(state, buf)
if enable == nil then
enable = true
end
if buf then
vim.b.autoformat = enable
else
vim.g.autoformat = enable
vim.b.autoformat = nil
end
end
---@param buf? boolean
function toggle_format(buf)
return Snacks.toggle({
name = "Auto Format (" .. (buf and "Buffer" or "Global") .. ")",
get = function()
if not buf then
return vim.g.autoformat == nil or vim.g.autoformat
end
return format_enabled()
end,
set = function(state)
format_enable(state, buf)
end,
})
end
toggle_format():map("<LEADER>uf")
toggle_format(true):map("<LEADER>uF")
Snacks.toggle.option("spell", { name = "Spelling"}):map("<LEADER>us")
Snacks.toggle.option("wrap", {name = "Wrap"}):map("<LEADER>uw")
Snacks.toggle.option("relativenumber", { name = "Relative Number"}):map("<LEADER>uL")
Snacks.toggle.diagnostics():map("<LEADER>ud")
Snacks.toggle.line_number():map("<LEADER>ul")
Snacks.toggle.option("conceallevel", {off = 0, on = vim.o.conceallevel > 0 and vim.o.conceallevel or 2}):map("<LEADER>uc")
Snacks.toggle.treesitter():map("<LEADER>uT")
Snacks.toggle.option("background", { off = "light", on = "dark" , name = "Dark Background"}):map("<LEADER>ub")
if vim.lsp.inlay_hint then
Snacks.toggle.inlay_hints():map("<LEADER>uh")
end
function maximize_window()
---@type {k:string, v:any}[]?
local maximized = nil
return Snacks.toggle({
name = "Maximize",
get = function()
return maximized ~= nil
end,
set = function(state)
if state then
maximized = {}
local function set(k, v)
table.insert(maximized, 1, { k = v, v = vim.o[k] })
vim.o[k] = v
end
set("winwidth", 999)
set("winheight", 999)
set("winminwidth", 10)
set("winminheight", 4)
vim.cmd("wincmd =")
vim.api.nvim_create_autocmd("ExitPre", {
once = true,
group = vim.api.nvim_create_augroup("marleyvim_restore_max_exit_pre", { clear = true }),
desc = "Restore width/height when closing Neovim while maximized",
callback = function()
maximize_window.set(false)
end,
})
else
for _, opt in ipairs(maximized) do
vim.o[opt.k] = opt.v
end
maximized = nil
vim.cmd("wincmd =")
end
end,
})
end
maximize_window():map("<LEADER>wm")
'';
}