From 47d704ad016814b22c2c7df4359f5d68f94d38d0 Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Tue, 27 Dec 2022 14:02:03 +0100 Subject: [PATCH] Started rewriting my neovim config --- dotfiles/neovim/ftplugin/purescript.lua | 19 ++- dotfiles/neovim/init.lua | 9 +- .../lua/my/helpers/update-nix-fetchgit.lua | 9 -- dotfiles/neovim/lua/my/init.lua | 5 - dotfiles/neovim/lua/my/keymaps.lua | 108 +++++++++++------- dotfiles/neovim/lua/my/options.lua | 6 +- dotfiles/neovim/lua/my/options/folding.lua | 7 -- dotfiles/neovim/lua/my/paq.lua | 1 + dotfiles/neovim/lua/my/plugins/arpeggio.lua | 28 ++++- dotfiles/neovim/lua/my/plugins/idris.lua | 37 +++--- dotfiles/neovim/lua/my/plugins/init.lua | 4 +- dotfiles/neovim/lua/my/plugins/iron.lua | 2 +- dotfiles/neovim/lua/my/plugins/lspconfig.lua | 2 +- dotfiles/neovim/lua/my/plugins/telescope.lua | 31 ++--- dotfiles/neovim/lua/my/plugins/ufo.lua | 6 +- dotfiles/neovim/lua/my/plugins/vimux.lua | 26 ++++- modules/applications/neovim.nix | 3 +- 17 files changed, 181 insertions(+), 122 deletions(-) delete mode 100644 dotfiles/neovim/lua/my/helpers/update-nix-fetchgit.lua delete mode 100644 dotfiles/neovim/lua/my/options/folding.lua diff --git a/dotfiles/neovim/ftplugin/purescript.lua b/dotfiles/neovim/ftplugin/purescript.lua index 0d0b6ce..642497c 100644 --- a/dotfiles/neovim/ftplugin/purescript.lua +++ b/dotfiles/neovim/ftplugin/purescript.lua @@ -1,13 +1,20 @@ -local arpeggio = require("my.plugins.arpeggio") local A = require("my.abbreviations") -- Use vt to test -arpeggio.chordSilent("n", "vt", ":VimuxRunCommand \"clear && spago test\"", - { settings = "b" }) +vim.keymap.set( + "n", + "vt", + ':VimuxRunCommand "clear && spago test"', + { desc = "[V]imtex run [t]ests", buffer = true } +) -- Use vb to build -arpeggio.chordSilent("n", "vb", ":VimuxRunCommand \"clear && spago build\"", - { settings = "b" }) +vim.keymap.set( + "n", + "vb", + ':VimuxRunCommand "clear && spago build"', + { desc = "[V]imtex [b]uild", buffer = true } +) vim.opt.expandtab = true -- Use spaces for the tab char @@ -17,7 +24,7 @@ local abbreviations = { { "tto", "->" }, { "iip", "=>" }, { "frl", "forall" }, - { "ott", "<-" } -- opposite of tto + { "ott", "<-" }, -- opposite of tto } A.manyLocalAbbr(abbreviations) diff --git a/dotfiles/neovim/init.lua b/dotfiles/neovim/init.lua index c9874d5..8e3c994 100644 --- a/dotfiles/neovim/init.lua +++ b/dotfiles/neovim/init.lua @@ -1,2 +1,9 @@ --- vim.opt.runtimepath:prepend(os.getenv("LAZY_NVIM_PATH")) +local lazy_path = os.getenv("LAZY_NVIM_PATH") + +if lazy_path == nil then + error("Lazy.nvim not installed!") +end + +vim.opt.runtimepath:prepend(lazy_path) + require("my.init").setup() diff --git a/dotfiles/neovim/lua/my/helpers/update-nix-fetchgit.lua b/dotfiles/neovim/lua/my/helpers/update-nix-fetchgit.lua deleted file mode 100644 index 7ce4505..0000000 --- a/dotfiles/neovim/lua/my/helpers/update-nix-fetchgit.lua +++ /dev/null @@ -1,9 +0,0 @@ -local M = {} - -function M.update() - require("my.helpers").saveCursor(function() - vim.cmd(":%!update-nix-fetchgit") - end) -end - -return M diff --git a/dotfiles/neovim/lua/my/init.lua b/dotfiles/neovim/lua/my/init.lua index 039c3d0..811cf66 100644 --- a/dotfiles/neovim/lua/my/init.lua +++ b/dotfiles/neovim/lua/my/init.lua @@ -2,13 +2,8 @@ local M = {} function M.setup() -- Import my other files - require("impatient") -- should make startups take less - require("my.paq").setup() - require("my.theme").setup() require("my.options").setup() require('my.keymaps').setup() - require('my.plugins').setup() - -- require("telescope.extensions.unicode").setupAbbreviations() end return M diff --git a/dotfiles/neovim/lua/my/keymaps.lua b/dotfiles/neovim/lua/my/keymaps.lua index 87c11ac..6484deb 100644 --- a/dotfiles/neovim/lua/my/keymaps.lua +++ b/dotfiles/neovim/lua/my/keymaps.lua @@ -1,21 +1,43 @@ local helpers = require("my.helpers") -local arpeggio = require("my.plugins.arpeggio") local M = {} -- {{{ Helpers --- Performs a basic move operation +---Performs a basic move operation +---Moves a keybind to a different set of keys. +---Useful for moving built in keybinds out of the way. +---@param from string +---@param to string +---@param opts table|nil function M.move(from, to, opts) vim.keymap.set("n", to, from, opts) vim.keymap.set("n", from, "") end +---Create a textobject defined by some delimiters +---@param from string +---@param to string +---@param name string +---@param perhapsOpts table|nil function M.delimitedTextobject(from, to, name, perhapsOpts) local opts = helpers.mergeTables(perhapsOpts or {}, { desc = name }) vim.keymap.set({ "v", "o" }, "i" .. from, "i" .. to, opts) vim.keymap.set({ "v", "o" }, "a" .. from, "a" .. to, opts) end + +---Helper to create a normal mode mapping and give it some description. +---@param from string +---@param to string|function +---@param desc string +---@param silent boolean|nil +function M.nmap(from, to, desc, silent) + if silent == nil then + silent = true + end + vim.keymap.set("n", from, to, { desc = desc }) +end + -- }}} function M.setup() @@ -23,47 +45,38 @@ function M.setup() M.move("q", "yq", { desc = "Record macro" }) M.move("Q", "yQ") -- }}} - -- {{{ Easier access to C^ - M.move("", "a", { desc = "Go to previous file" }) + -- {{{ Easier access to + M.move("", "a", { desc = "[A]lternate file" }) -- }}} -- {{{ Quit current buffer / all buffers vim.keymap.set({ "n", "v" }, "q", function() local buf = vim.api.nvim_win_get_buf(0) -- Only save if file is writable - if vim.bo[buf].modifiable and not vim.bo[buf].readonly then vim.cmd [[write]] end - - vim.cmd "q" - end, { desc = "Quit current buffer" }) - - vim.keymap.set("n", "Q", ":wqa", { desc = "Save all files and quit" }) - -- }}} - -- {{{ Replace word in file - vim.keymap.set("n", "rw", ":%s//", { desc = "Replace word in file" }) - -- }}} - -- {{{ Text objects - M.delimitedTextobject("q", '"', "quotes") - M.delimitedTextobject("a", "'", "'") - M.delimitedTextobject("r", "[", "square brackets") - -- }}} - -- {{{Diagnostic keymaps - do - local opts = function(desc) - return { noremap = true, silent = true, desc = desc } + if vim.bo[buf].modifiable and not vim.bo[buf].readonly then + vim.cmd([[write]]) end - vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, opts("Goto previous diagnostic")) - vim.keymap.set('n', ']d', vim.diagnostic.goto_next, opts("Goto next diagnostic")) - vim.keymap.set('n', 'J', vim.diagnostic.open_float, opts("Open current diagnostic")) - vim.keymap.set('n', 'J', vim.diagnostic.setloclist, opts("Set diagnostics loclist")) - end + vim.cmd("q") + end, { desc = "[q]uit current buffer" }) + + M.nmap("Q", ":wqa", "Save all files and [q]uit") -- }}} - -- {{{ Chords - if arpeggio ~= nil then - arpeggio.chordSilent("n", "ji", ":silent :write") -- Saving - arpeggio.chord("i", "jk", "") -- Remap Esc to jk - arpeggio.chord("nv", "cp", "\"+") -- Press cp to use the global clipboard - end + -- {{{ Replace word in file + M.nmap("rw", ":%s//", "[R]eplace [w]ord in file") + -- }}} + -- {{{ Text objects + M.delimitedTextobject("q", '"', "[q]uotes") + M.delimitedTextobject("a", "'", "[a]postrophes") + M.delimitedTextobject("r", "[", "squa[r]e brackets") + -- }}} + -- {{{Diagnostic keymaps + M.nmap("[d", vim.diagnostic.goto_prev, "Goto previous [d]iagnostic") + M.nmap("]d", vim.diagnostic.goto_next, "Goto next [d]iagnostic") + M.nmap("J", vim.diagnostic.open_float, "Open current diagnostic") + M.nmap("D", vim.diagnostic.setloclist, "[S]iagnostic loclist") + -- }}} + -- {{{ Chords (exit insert mode, save, clipboard) -- }}} -- {{{ Set up which-key structure local status, wk = pcall(require, "which-key") @@ -71,16 +84,31 @@ function M.setup() if status then wk.register({ [""] = { - f = { name = "Files" }, - g = { name = "Go to" }, - r = { name = "Rename / Replace / Reload" }, - l = { name = "Local" }, - v = "which_key_ignore" - } + f = { name = "[F]iles" }, + g = { name = "[G]o to" }, + r = { name = "[R]ename / [R]eplace / [R]eload" }, + l = { name = "[L]ocal" }, + w = { name = "[W]orkspace" }, + v = "which_key_ignore", + }, }) end -- }}} + + +vim.api.nvim_create_autocmd({ "FileType" }, { + pattern = { + "help", + "man", + "notify", + "lspinfo" + }, + callback = function(event) + vim.keymap.set("n", "q", "close", { buffer = event.buf, silent = true, desc = "[q]uit current buffer" }) + end, +}) + return M end diff --git a/dotfiles/neovim/lua/my/options.lua b/dotfiles/neovim/lua/my/options.lua index d01756e..e942f14 100644 --- a/dotfiles/neovim/lua/my/options.lua +++ b/dotfiles/neovim/lua/my/options.lua @@ -1,5 +1,3 @@ -local helpers = require("my.helpers") - local M = {} function M.setup() @@ -39,8 +37,8 @@ function M.setup() -- Set leader vim.g.mapleader = " " - -- Import other options - require("my.options.folding").setup() + -- Folding + vim.o.foldmethod = "marker" end return M diff --git a/dotfiles/neovim/lua/my/options/folding.lua b/dotfiles/neovim/lua/my/options/folding.lua deleted file mode 100644 index e3b3266..0000000 --- a/dotfiles/neovim/lua/my/options/folding.lua +++ /dev/null @@ -1,7 +0,0 @@ -local M = {} - -function M.setup() - vim.o.foldmethod = "marker" -end - -return M diff --git a/dotfiles/neovim/lua/my/paq.lua b/dotfiles/neovim/lua/my/paq.lua index 37e4258..5f39141 100644 --- a/dotfiles/neovim/lua/my/paq.lua +++ b/dotfiles/neovim/lua/my/paq.lua @@ -5,6 +5,7 @@ function M.setup() local themePackages = require("my.theme").deps local base = { "nvim-lua/plenary.nvim", -- async utility lib it seems? + --------------------------------- Unuported "folke/neoconf.nvim", -- per project neovim configuration "neovim/nvim-lspconfig", -- configures lsps for me "folke/neodev.nvim", -- lua support diff --git a/dotfiles/neovim/lua/my/plugins/arpeggio.lua b/dotfiles/neovim/lua/my/plugins/arpeggio.lua index 114675f..a270828 100644 --- a/dotfiles/neovim/lua/my/plugins/arpeggio.lua +++ b/dotfiles/neovim/lua/my/plugins/arpeggio.lua @@ -3,25 +3,43 @@ local arpeggio = vim.fn["arpeggio#map"] local M = {} -function M.chord(mode, lhs, rhs, opts) +---Create an arpeggio mapping +---@param mode string +---@param lhs string +---@param rhs string +---@param opts table|nil +local function chord(mode, lhs, rhs, opts) if string.len(mode) > 1 then for i = 1, #mode do local c = mode:sub(i, i) - M.chord(c, lhs, rhs, opts) + chord(c, lhs, rhs, opts) end else local options = helpers.mergeTables(opts or {}, { noremap = true }) local settings = options.settings or "" - if options.silent then settings = settings .. "s" end + if options.silent then + settings = settings .. "s" + end arpeggio(mode, settings, not options.noremap, lhs, rhs) end end -function M.chordSilent(mode, lhs, rhs, opts) +---Create a silent arpeggio mapping +---@param mode string +---@param lhs string +---@param rhs string +---@param opts table|nil +local function chordSilent(mode, lhs, rhs, opts) local options = helpers.mergeTables(opts or {}, { silent = true }) - M.chord(mode, lhs, rhs, options) + chord(mode, lhs, rhs, options) +end + +function M.setup() + chordSilent("n", "ji", ":silent :write") -- Saving + chord("i", "jk", "") -- Remap Esc to jk + chord("nv", "cp", '"+') -- Press cp to use the global clipboard end return M diff --git a/dotfiles/neovim/lua/my/plugins/idris.lua b/dotfiles/neovim/lua/my/plugins/idris.lua index 2102003..b060453 100644 --- a/dotfiles/neovim/lua/my/plugins/idris.lua +++ b/dotfiles/neovim/lua/my/plugins/idris.lua @@ -1,18 +1,7 @@ -local arpeggio = require("my.plugins.arpeggio") local lspconfig = require("my.plugins.lspconfig") local M = {} -local idrisChords = { - sc = "case_split", - mc = "make_case", - ml = "make_lemma", - es = "expr_search", - gd = "generate_def", - rh = "refine_hole", - ac = "add_clause" -} - function M.setup() local idris2 = require("idris2") @@ -21,14 +10,28 @@ function M.setup() on_attach = function(client, bufnr) lspconfig.on_attach(client, bufnr) - for key, value in pairs(idrisChords) do - arpeggio.chord("n", "i" .. key, - ":lua require('idris2.code_action')." .. value .. "()", - { settings = "b" }) + local function nmap(from, to, desc) + vim.keymap.set("n", "I" .. from, function() + require("idris2.code_action")[to]() + end, { desc = desc, bufnr = true }) end - end + + nmap("C", "make_case", "Make [c]plit") + nmap("L", "make_lemma", "Make [l]emma") + nmap("c", "add_clause", "Add [c]lause") + nmap("s", "expr_search", "Expression [s]earch") + nmap("d", "generate_def", "Generate [d]efinition") + nmap("s", "case_split", "Case [s]plit") + nmap("h", "refine_hole", "Refine [h]ole") + + local status, wk = pcall(require, "which-key") + + if status then + wk.register({ ["I"] = { name = "[I]dris", buffer = bufnr } }) + end + end, }, - client = { hover = { use_split = true } } + client = { hover = { use_split = true } }, }) end diff --git a/dotfiles/neovim/lua/my/plugins/init.lua b/dotfiles/neovim/lua/my/plugins/init.lua index 54c6797..30bb728 100644 --- a/dotfiles/neovim/lua/my/plugins/init.lua +++ b/dotfiles/neovim/lua/my/plugins/init.lua @@ -38,7 +38,6 @@ function M.setup() require("my.plugins.vimtex").setup() -- require("my.plugins.lean").setup() require("my.plugins.notify").setup() - require("my.plugins.iron").setup() end) if env.neovide.active() then @@ -59,7 +58,8 @@ function M.setup() require("my.plugins.hydra").setup() require("my.plugins.clipboard-image").setup() require("my.plugins.mind").setup() - require("my.plugins.ufo").setup() + -- require("my.plugins.ufo").setup() + require("my.plugins.arpeggio").setup() -- require("my.plugins.slam").setup() end diff --git a/dotfiles/neovim/lua/my/plugins/iron.lua b/dotfiles/neovim/lua/my/plugins/iron.lua index 0692811..5780e51 100644 --- a/dotfiles/neovim/lua/my/plugins/iron.lua +++ b/dotfiles/neovim/lua/my/plugins/iron.lua @@ -41,7 +41,7 @@ function M.setup() local status, wk = pcall(require, "which-key") - if status then wk.register({ ["i"] = { name = "Iron repl commands" } }) end + if status then wk.register({ ["i"] = { name = "[I]ron repl commands", s = {name = "[s]end"}, m = "[m]ark" } }) end end return M diff --git a/dotfiles/neovim/lua/my/plugins/lspconfig.lua b/dotfiles/neovim/lua/my/plugins/lspconfig.lua index f799ad0..f3cb777 100644 --- a/dotfiles/neovim/lua/my/plugins/lspconfig.lua +++ b/dotfiles/neovim/lua/my/plugins/lspconfig.lua @@ -16,7 +16,7 @@ function M.on_attach(client, bufnr) -- }}} -- {{{ Keymap helpers local opts = function(desc) - return { noremap = true, silent = true, desc = desc } + return { noremap = true, silent = true, desc = desc, buffer = true } end local nmap = function(from, to, desc) diff --git a/dotfiles/neovim/lua/my/plugins/telescope.lua b/dotfiles/neovim/lua/my/plugins/telescope.lua index b5755a3..025bc61 100644 --- a/dotfiles/neovim/lua/my/plugins/telescope.lua +++ b/dotfiles/neovim/lua/my/plugins/telescope.lua @@ -1,5 +1,3 @@ -local arpeggio = require("my.plugins.arpeggio") - local M = {} local function find_files_by_extension(extension) @@ -14,18 +12,23 @@ local defaultTheme = "ivy" local keybinds = { { "", "find_files", "Find files" }, - { "ft", find_files_by_extension("tex"), "Find tex files" }, - { "fl", find_files_by_extension("lua"), "Find lua files" }, - { "fp", find_files_by_extension("purs"), "Find purescript files" }, - { "d", "diagnostics", "Diagnostics" }, - { "", "live_grep", "Search in project" }, - { "t", "builtin", "Show builtin pickers" } + { "ft", find_files_by_extension("tex"), "[F]ind [t]ex files" }, + { "fl", find_files_by_extension("lua"), "[F]ind [l]ua files" }, + { + "fp", + find_files_by_extension("purs"), + "[F]ind [p]urescript files", + }, + { "d", "diagnostics", "[D]iagnostics" }, + { "", "live_grep", "[F]ind in project" }, + { "", "file_browser", "[F]ile browser" }, + { "t", "builtin", "[T]elescope pickers" }, } -local chords = { { "jp", "file_browser" } } - local function mkAction(action) - if not string.find(action, "theme=") then action = with_theme(action, defaultTheme) end + if not string.find(action, "theme=") then + action = with_theme(action, defaultTheme) + end return ":Telescope " .. action .. "" end @@ -34,8 +37,6 @@ local function setupKeybinds() for _, mapping in pairs(keybinds) do vim.keymap.set("n", mapping[1], mkAction(mapping[2]), { desc = mapping[3] }) end - - for _, mapping in pairs(chords) do arpeggio.chord("n", mapping[1], mkAction(mapping[2])) end end function M.setup() @@ -44,11 +45,11 @@ function M.setup() local settings = { defaults = { mappings = { i = { [""] = "which_key" } } }, pickers = { find_files = { hidden = true } }, - extensions = { file_browser = { path = "%:p:h" } } + extensions = { file_browser = { path = "%:p:h" } }, } require("telescope").setup(settings) - require("telescope").load_extension "file_browser" + require("telescope").load_extension("file_browser") end return M diff --git a/dotfiles/neovim/lua/my/plugins/ufo.lua b/dotfiles/neovim/lua/my/plugins/ufo.lua index d067bd5..4f41121 100644 --- a/dotfiles/neovim/lua/my/plugins/ufo.lua +++ b/dotfiles/neovim/lua/my/plugins/ufo.lua @@ -7,12 +7,12 @@ function M.setup() vim.o.foldenable = true -- Using ufo provider need remap `zR` and `zM`. - -- vim.keymap.set('n', 'zR', require('ufo').openAllFolds) - -- vim.keymap.set('n', 'zM', require('ufo').closeAllFolds) + vim.keymap.set('n', 'zR', require('ufo').openAllFolds) + vim.keymap.set('n', 'zM', require('ufo').closeAllFolds) -- Tell the server the capability of foldingRange, -- Neovim hasn't added foldingRange to default capabilities, users must add it manually - -- require('ufo').setup() + require('ufo').setup() end return M diff --git a/dotfiles/neovim/lua/my/plugins/vimux.lua b/dotfiles/neovim/lua/my/plugins/vimux.lua index f99eb1b..91ec761 100644 --- a/dotfiles/neovim/lua/my/plugins/vimux.lua +++ b/dotfiles/neovim/lua/my/plugins/vimux.lua @@ -1,11 +1,27 @@ -local arpeggio = require("my.plugins.arpeggio") - +local K = require("my.keymaps") local M = {} function M.setup() - arpeggio.chordSilent("n", "vp", ":VimuxPromptCommand") - arpeggio.chordSilent("n", "vc", ":VimuxRunCommand \"clear\"") - arpeggio.chordSilent("n", "vl", ":VimuxRunLastCommand") + --{{{ Register keybinds + K.nmap("vp", ":VimuxPromptCommand", "[V]imux: [p]rompt for command") + K.nmap("vc", ':VimuxRunCommand "clear"', "[V]imux: [c]lear pane") + K.nmap( + "vl", + ":VimuxRunLastCommand", + "[V]imux: rerun [l]ast command" + ) + --}}} + --{{{ Register which-key docs + local status, wk = pcall(require, "which-key") + + if status then + wk.register({ + ["v"] = { + name = "vimux", + }, + }) + end + --}}} end return M diff --git a/modules/applications/neovim.nix b/modules/applications/neovim.nix index 5b2e60c..fb7cf32 100644 --- a/modules/applications/neovim.nix +++ b/modules/applications/neovim.nix @@ -87,7 +87,8 @@ in home.file."${nixPlugins}/start/theming/lua/my/theme.lua".source = theme.neovim.theme; home.file."${nixPlugins}/start/teal/lua".source = teal; # teal (typed lua) home.file."${nixPlugins}/start/snippets".source = simlink "${paths.dotfiles}/vscode-snippets"; - home.file.".config/nvim".source = simlink "${paths.dotfiles}/neovim"; + # home.file.".config/nvim".source = simlink "${paths.dotfiles}/neovim"; + home.file.".config/nvim".source = ../../dotfiles/neovim; programs.neovim.enable = false;