From b8d00f600dc00bbf2e472c13eb62291a163d49c4 Mon Sep 17 00:00:00 2001 From: Minijackson Date: Wed, 2 Apr 2025 21:15:49 +0200 Subject: nixvim: set some keymaps with snacks --- common/nixvim.nix | 554 ++++++++++++++++++++++++++---------------------------- 1 file changed, 268 insertions(+), 286 deletions(-) diff --git a/common/nixvim.nix b/common/nixvim.nix index 9809736..659896d 100644 --- a/common/nixvim.nix +++ b/common/nixvim.nix @@ -67,270 +67,226 @@ inputs: diagnostics.virtual_lines = true; - keymaps = - let - toggle = key: option: { - mode = "n"; - key = "yo${key}"; - action = "setlocal ${option}! ${option}?"; - options.desc = "Toggle ${option}"; + keymaps = [ + { + key = "-"; + action.__raw = "require('oil').open"; + options.desc = "Open parent directory"; + } + + # Git + + { + key = "gg"; + action = "Neogit"; + options.desc = "Open Neogit"; + } + + { + key = "gd"; + action = "DiffviewOpen"; + options.desc = "Open Diffview"; + } + + # GitSigns + + # TODO: noremap? buffer local? silent? + { + key = "gs"; + action.__raw = "require('gitsigns').stage_hunk"; + options.desc = "Stage hunk"; + } + + { + key = "gr"; + action.__raw = "require('gitsigns').reset_hunk"; + options.desc = "Reset hunk"; + } + + # TODO: visual stage/reset + + { + key = "gS"; + action.__raw = "require('gitsigns').stage_buffer"; + options.desc = "Stage buffer"; + } + + { + key = "gR"; + action.__raw = "require('gitsigns').reset_buffer"; + options.desc = "Stage buffer"; + } + + { + key = "gu"; + action.__raw = "require('gitsigns').undo_stage_hunk"; + options.desc = "Undo stage hunk"; + } + + { + key = "gp"; + action.__raw = "require('gitsigns').preview_hunk_inline"; + options.desc = "Preview hunk"; + } + + { + key = "gb"; + action.__raw = "function() require('gitsigns').blame_line { full = true } end"; + options.desc = "Blame line"; + } + + { + key = "]g"; + action.__raw = '' + function() + if vim.wo.diff then return ']c' end + vim.schedule(function() package.loaded.gitsigns.next_hunk() end) + return '' + end + ''; + options = { + expr = true; + desc = "Next hunk"; }; - in - [ - { - key = "-"; - action.__raw = "require('oil').open"; - options.desc = "Open parent directory"; - } - - # Git - - { - key = "gg"; - action = "Neogit"; - options.desc = "Open Neogit"; - } - - { - key = "gd"; - action = "DiffviewOpen"; - options.desc = "Open Diffview"; - } - - # GitSigns - - # TODO: noremap? buffer local? silent? - { - key = "gs"; - action.__raw = "require('gitsigns').stage_hunk"; - options.desc = "Stage hunk"; - } - - { - key = "gr"; - action.__raw = "require('gitsigns').reset_hunk"; - options.desc = "Reset hunk"; - } - - # TODO: visual stage/reset - - { - key = "gS"; - action.__raw = "require('gitsigns').stage_buffer"; - options.desc = "Stage buffer"; - } - - { - key = "gR"; - action.__raw = "require('gitsigns').reset_buffer"; - options.desc = "Stage buffer"; - } - - { - key = "gu"; - action.__raw = "require('gitsigns').undo_stage_hunk"; - options.desc = "Undo stage hunk"; - } - - { - key = "gp"; - action.__raw = "require('gitsigns').preview_hunk_inline"; - options.desc = "Preview hunk"; - } - - { - key = "gb"; - action.__raw = "function() require('gitsigns').blame_line { full = true } end"; - options.desc = "Blame line"; - } - - { - key = "]g"; - action.__raw = '' - function() - if vim.wo.diff then return ']c' end - vim.schedule(function() package.loaded.gitsigns.next_hunk() end) - return '' - end - ''; - options = { - expr = true; - desc = "Next hunk"; - }; - } - - { - key = "[g"; - action.__raw = '' - function() - if vim.wo.diff then return '[c' end - vim.schedule(function() package.loaded.gitsigns.prev_hunk() end) - return '' - end - ''; - options = { - expr = true; - desc = "Next hunk"; - }; - } - - # Leap - - { - key = ""; - mode = [ - "n" - "x" - "o" - ]; - action = "(leap-forward)"; - options.desc = "Leap forward to"; - } - - { - key = ""; - mode = [ - "n" - "x" - "o" - ]; - action = "(leap-backward)"; - options.desc = "Leap backward to"; - } - - { - key = "g"; - mode = [ - "n" - "x" - "o" - ]; - action = "(leap-from-window)"; - options.desc = "Leap from window"; - } - - { - key = "gnn"; - mode = [ - "n" - "x" - "o" - ]; - action.__raw = '' - function() - require('leap.treesitter').select() - end - ''; - options.desc = "Start incremental selection"; - } - - { - key = "gnN"; - mode = [ - "n" - "x" - "o" - ]; - action = "Vlua require('leap.treesitter').select()"; - options.desc = "Start linewise incremental selection"; - } - - # Toggle options - (toggle "c" "cursorline") - (toggle "C" "cursorcolumn") - (toggle "h" "hlsearch") - (toggle "i" "ignorecase") - (toggle "l" "list") - (toggle "n" "number") - (toggle "r" "relativenumber") - (toggle "s" "spell") - (toggle "w" "wrap") - - { - mode = "n"; - key = "yod"; - action.__raw = '' - function() - if vim.wo.diff then - vim.cmd.diffoff() - print('diffoff') - else - vim.cmd.diffthis() - print('diffthis') - end - end - ''; - options.desc = "Toggle diff"; - } - - # Trouble - - { - key = "xx"; - mode = "n"; - action = "Trouble diagnostics toggle"; - options.desc = "Diagnostics (Trouble)"; - } - - { - key = "xX"; - mode = "n"; - action = "Trouble diagnostics toggle filter.buf=0"; - options.desc = "Buffer Diagnostics (Trouble)"; - } - - { - key = "cs"; - mode = "n"; - action = "Trouble symbols toggle focus=false"; - options.desc = "Symbols (Trouble)"; - } - - { - key = "cl"; - mode = "n"; - action = "Trouble lsp toggle focus=false win.position=right"; - options.desc = "LSP Definitions / references / ... (Trouble)"; - } - - { - key = "xL"; - mode = "n"; - action = "Trouble loclist toggle"; - options.desc = "Location List (Trouble)"; - } - - { - key = "xQ"; - mode = "n"; - action = "Trouble qflist toggle"; - options.desc = "Quickfix List (Trouble)"; - } - - { - key = "sd"; - action = '' - function() - local new_config = not vim.diagnostic.config().virtual_lines - vim.diagnostic.config({ virtual_lines = new_config }) - end - ''; - mode = "n"; - lua = true; - options.desc = "LSP toggle inline diagnostics"; - } - { - key = "sT"; - action = '' - function() - vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled()) + } + + { + key = "[g"; + action.__raw = '' + function() + if vim.wo.diff then return '[c' end + vim.schedule(function() package.loaded.gitsigns.prev_hunk() end) + return '' + end + ''; + options = { + expr = true; + desc = "Next hunk"; + }; + } + + # Leap + + { + key = ""; + mode = [ + "n" + "x" + "o" + ]; + action = "(leap-forward)"; + options.desc = "Leap forward to"; + } + + { + key = ""; + mode = [ + "n" + "x" + "o" + ]; + action = "(leap-backward)"; + options.desc = "Leap backward to"; + } + + { + key = "g"; + mode = [ + "n" + "x" + "o" + ]; + action = "(leap-from-window)"; + options.desc = "Leap from window"; + } + + { + key = "gnn"; + mode = [ + "n" + "x" + "o" + ]; + action.__raw = '' + function() + require('leap.treesitter').select() + end + ''; + options.desc = "Start incremental selection"; + } + + { + key = "gnN"; + mode = [ + "n" + "x" + "o" + ]; + action = "Vlua require('leap.treesitter').select()"; + options.desc = "Start linewise incremental selection"; + } + + { + mode = "n"; + key = "yod"; + action.__raw = '' + function() + if vim.wo.diff then + vim.cmd.diffoff() + print('diffoff') + else + vim.cmd.diffthis() + print('diffthis') end - ''; - mode = "n"; - lua = true; - options.desc = "LSP toggle inline text"; - } - ]; + end + ''; + options.desc = "Toggle diff"; + } + + # Trouble + + { + key = "xx"; + mode = "n"; + action = "Trouble diagnostics toggle"; + options.desc = "Diagnostics (Trouble)"; + } + + { + key = "xX"; + mode = "n"; + action = "Trouble diagnostics toggle filter.buf=0"; + options.desc = "Buffer Diagnostics (Trouble)"; + } + + { + key = "cs"; + mode = "n"; + action = "Trouble symbols toggle focus=false"; + options.desc = "Symbols (Trouble)"; + } + + { + key = "cl"; + mode = "n"; + action = "Trouble lsp toggle focus=false win.position=right"; + options.desc = "LSP Definitions / references / ... (Trouble)"; + } + + { + key = "xL"; + mode = "n"; + action = "Trouble loclist toggle"; + options.desc = "Location List (Trouble)"; + } + + { + key = "xQ"; + mode = "n"; + action = "Trouble qflist toggle"; + options.desc = "Quickfix List (Trouble)"; + } + ]; # TODO: extraPlugins = with pkgs.unstable.vimPlugins; [ @@ -428,30 +384,6 @@ inputs: }; align = { }; bracketed = { }; - clue = { - triggers = [ - { - mode = "n"; - keys = "yo"; - } - { - mode = "n"; - keys = "["; - } - { - mode = "n"; - keys = "]"; - } - { - mode = "n"; - keys = ""; - } - { - mode = "n"; - keys = "gr"; - } - ]; - }; trailspace = { }; }; }; @@ -676,9 +608,59 @@ inputs: }; web-devicons.enable = true; + + which-key = { + enable = true; + settings.spec = [ + { + __unkeyed = "f"; + group = "Find"; + } + { + __unkeyed = "g"; + group = "Git"; + } + { + __unkeyed = "s"; + group = "LSP"; + } + { + __unkeyed = "o"; + group = "Orgmode"; + } + { + __unkeyed = "n"; + group = "Notes"; + } + { + __unkeyed = "x"; + group = "Trouble"; + } + ]; + }; }; extraConfigLua = '' + -- Toggle options + for _, x in ipairs({ + {"c", "cursorline"}, + {"C", "cursorcolumn"}, + {"h", "hlsearch"}, + {"i", "ignorecase"}, + {"l", "list"}, + {"r", "relativenumber"}, + {"s", "spell"}, + {"w", "wrap"}, + }) do + Snacks.toggle.option(x[2]):map("yo" .. x[1]) + end + + Snacks.toggle.line_number():map("yon") + Snacks.toggle.indent():map("yoI") + Snacks.toggle.diagnostics():map("sd") + Snacks.toggle.inlay_hints():map("sT") + Snacks.toggle.words():map("sw") + vim.api.nvim_create_autocmd("TextYankPost", { desc = "Highlight yanked text", callback = function() -- cgit v1.2.3