From 6458a3e392f34251eb49e10a34d376baca627c17 Mon Sep 17 00:00:00 2001 From: punkfairie Date: Thu, 28 Nov 2024 17:26:07 -0800 Subject: [PATCH] WIP: Snacks.nvim --- nix/neovim-overlay.nix | 2 ++ nvim/init.lua | 4 +++ nvim/lua/lib/lazyvim/format.lua | 53 +++++++++++++++++++++++++++++++++ nvim/lua/snacks-nvim.lua | 45 ++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 nvim/lua/lib/lazyvim/format.lua create mode 100644 nvim/lua/snacks-nvim.lua diff --git a/nix/neovim-overlay.nix b/nix/neovim-overlay.nix index b1eb4bc..687d503 100644 --- a/nix/neovim-overlay.nix +++ b/nix/neovim-overlay.nix @@ -28,6 +28,8 @@ with final.pkgs.lib; let all-plugins = with pkgs.vimPlugins; [ # bleeding-edge plugins from flake inputs # (mkNvimPlugin inputs.wf-nvim "wf.nvim") # (example) keymap hints + lz-n + snacks-nvim ]; extraPackages = with pkgs; [ diff --git a/nvim/init.lua b/nvim/init.lua index ecbb6a4..8399c54 100644 --- a/nvim/init.lua +++ b/nvim/init.lua @@ -6,3 +6,7 @@ vim.g.sqlite_clib_path = require('luv').os_getenv('LIBSQLITE') require('options') require('keymaps') + +require('snacks-nvim') + +-- require('lz.n').load('plugins') diff --git a/nvim/lua/lib/lazyvim/format.lua b/nvim/lua/lib/lazyvim/format.lua new file mode 100644 index 0000000..cc447db --- /dev/null +++ b/nvim/lua/lib/lazyvim/format.lua @@ -0,0 +1,53 @@ +-- https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/util/format.lua + +---@class lib.lazyvim.format +local M = {} + +---@param buf? number The buffer to enable for +function M.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 Whether to enable or disable +---@param buf? boolean Whether to enable for current buffer only +function M.enable(enable, 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 Whether to toggle for current buffer only +function M.snacks_toggle(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 M.enabled() + end, + set = function(state) + M.enable(state, buf) + end, + }) +end + +return M diff --git a/nvim/lua/snacks-nvim.lua b/nvim/lua/snacks-nvim.lua new file mode 100644 index 0000000..75e71ab --- /dev/null +++ b/nvim/lua/snacks-nvim.lua @@ -0,0 +1,45 @@ +-- Snacks needs to be loaded very early, so it gets its own special file. +local set = vim.keymap.set +local format = require('lib.lazyvim.format') + +require('snacks').setup({ + bigfile = { enabled = true }, + dashboard = { enabled = true }, +}) + +-- Buffers -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +set({ 'n' }, 'bd', function() + Snacks.bufdelete() +end, { desc = 'Delete buffer' }) + +set({ 'n' }, 'bo', function() + Snacks.bufdelete.other() +end, { desc = 'Delete other buffers' }) + +-- Toggles -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + +local toggle = Snacks.toggle + +format.snacks_toggle():map('uf') +format.snacks_toggle(true):map('uF') -- current buffer only + +toggle.option('spell', { name = 'spelling' }):map('us') +toggle.option('wrap', { name = 'wrap' }):map('uw') + +toggle.option('relativenumber', { name = 'relative number' }):map('uL') +toggle.line_number():map('ul') + +toggle.diagnostics():map('ud') + +toggle + .option( + 'conceallevel', + { off = 0, on = vim.o.conceallevel > 0 and vim.o.conceallevel or 2 } + ) + :map('uc') + +-- LazyGit -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + +set({ 'n' }, 'gg', function() + Snacks.lazygit() +end, { desc = 'Lazygit' })