]> git.rmz.io Git - dotfiles.git/blobdiff - nvim/lua/plugins/coding.lua
nvim: absorb yanky, but leave it disabled for now
[dotfiles.git] / nvim / lua / plugins / coding.lua
index 3dac23b32d3fc6f575f5c168bdd649d184ed5196..77d49557b73989443648b733a9a2ac86792eb5bb 100644 (file)
----@type LazyPluginSpec
+---@type LazySpec
 return {
   -- snippets
-  {
-    "L3MON4D3/LuaSnip",
-    -- disable luasnip bindings for <tab> and <s-tab>
+  { "l3mon4d3/luasnip",
+    build = "make install_jsregexp", -- optional
     dependencies = {
-      {
-        "honza/vim-snippets",
-        config = function()
-          require("luasnip.loaders.from_snipmate").lazy_load()
-          require("luasnip.loaders.from_lua").load({ paths = vim.fn.stdpath("config") .. "/lua/snippets" })
-        end,
-      },
+      { "honza/vim-snippets", },
     },
+    config = function()
+      local snippets = vim.fn.stdpath("config").."/snippets"
+      require("luasnip.loaders.from_snipmate").load({ paths = { snippets }})
+      require("luasnip.loaders.from_lua").load({ paths = { snippets }})
+    end,
     keys = function()
       return {}
     end,
     opts = {
+      history = true,
+      delete_check_events = "TextChanged",
       store_selection_keys = "<Tab>",
     },
   },
 
-  -- auto completion
-  {
-    "hrsh7th/nvim-cmp",
-    version = false, -- last release is way too old
-    event = "InsertEnter",
-    dependencies = {
-      "hrsh7th/cmp-nvim-lsp",
-      "hrsh7th/cmp-buffer",
-      "hrsh7th/cmp-path",
-      "saadparwaiz1/cmp_luasnip",
+  { "echasnovski/mini.pairs",
+    event = "VeryLazy",
+    opts = {
+      modes = { insert = true, command = true, terminal = false },
+      -- skip autopair when next character is one of these
+      skip_next = [=[[%w%%%'%[%"%.%`%$]]=],
+      -- skip autopair when the cursor is inside these treesitter nodes
+      skip_ts = { "string" },
+      -- skip autopair when next character is closing pair
+      -- and there are more closing pairs than opening pairs
+      skip_unbalanced = true,
+      -- better deal with markdown code blocks
+      markdown = true,
     },
-    ---@param opts cmp.ConfigSchema
-    opts = function(_, opts)
-      local has_words_before = function()
-        unpack = unpack or table.unpack
-        local line, col = unpack(vim.api.nvim_win_get_cursor(0))
-        return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
-      end
-
-      local cmp = require("cmp")
-      local luasnip = require("luasnip")
-
-      local upstream_format = opts.formatting.format
-      opts.formatting.format = function(entry, vim_item)
-        vim_item = upstream_format(entry, vim_item)
-        local menu = {
-          nvim_lsp = "[lsp]",
-          luasnip = "[snip]",
-          buffer = "[buf]",
-          path = "[path]",
-        }
-        vim_item.menu = menu[entry.source.name]
-        return vim_item
-      end
+    config = function(_, opts)
+      Snacks.toggle({
+        name = "Mini Pairs",
+        get = function()
+          return not vim.g.minipairs_disable
+        end,
+        set = function(state)
+          vim.g.minipairs_disable = not state
+        end,
+      }):map("<leader>up")
 
-      opts.completion = vim.tbl_extend("force", opts.completion, {
-        completeopt = "menu,menuone,noselect",
-      })
-      -- TODO: review if I want to keep any of LazyVim's mappings
-      opts.mapping = cmp.mapping.preset.insert({
-        -- lazyvims
-        ["<C-n>"] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }),
-        ["<C-p>"] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }),
-        ["<C-b>"] = cmp.mapping.scroll_docs(-4),
-        ["<C-f>"] = cmp.mapping.scroll_docs(4),
-        ["<C-e>"] = cmp.mapping.abort(),
-        ["<C-Space>"] = cmp.mapping.complete(),
-        -- mine
-        ["<Tab>"] = cmp.mapping(function(fallback)
-          if cmp.visible() then
-            cmp.select_next_item()
-          elseif luasnip.expand_or_jumpable() then
-            luasnip.expand_or_jump()
-          elseif has_words_before() then
-            cmp.complete()
-          else
-            fallback()
+      local pairs = require("mini.pairs")
+      pairs.setup(opts)
+      local open = pairs.open
+      --- Custom open command with extensions from LazyVim
+      ---@diagnostic disable-next-line: duplicate-set-field
+      pairs.open = function(pair, neigh_pattern)
+        if vim.fn.getcmdline() ~= "" then
+          return open(pair, neigh_pattern)
+        end
+        local o, c = pair:sub(1, 1), pair:sub(2, 2)
+        local line = vim.api.nvim_get_current_line()
+        local cursor = vim.api.nvim_win_get_cursor(0)
+        local next = line:sub(cursor[2] + 1, cursor[2] + 1)
+        local before = line:sub(1, cursor[2])
+        -- don't add fourth ` in markdown ```
+        if opts.markdown and o == "`" and vim.bo.filetype == "markdown" and before:match("^%s*``") then
+          return "`\n```" .. vim.api.nvim_replace_termcodes("<up>", true, true, true)
+        end
+        if opts.skip_next and next ~= "" and next:match(opts.skip_next) then
+          return o
+        end
+        if opts.skip_ts and #opts.skip_ts > 0 then
+          local ok, captures = pcall(vim.treesitter.get_captures_at_pos, 0, cursor[1] - 1, math.max(cursor[2] - 1, 0))
+          for _, capture in ipairs(ok and captures or {}) do
+            if vim.tbl_contains(opts.skip_ts, capture.capture) then
+              return o
+            end
           end
-        end, { "i", "s" }),
-        ["<S-Tab>"] = cmp.mapping(function(fallback)
-          if cmp.visible() then
-            cmp.select_prev_item()
-          elseif luasnip.jumpable(-1) then
-            luasnip.jump(-1)
-          else
-            fallback()
+        end
+        if opts.skip_unbalanced and next == c and c ~= o then
+          local _, count_open = line:gsub(vim.pesc(pair:sub(1, 1)), "")
+          local _, count_close = line:gsub(vim.pesc(pair:sub(2, 2)), "")
+          if count_close > count_open then
+            return o
           end
-        end, { "i", "s" }),
-      })
+        end
+        return open(pair, neigh_pattern)
+      end
     end,
   },
 
-  -- auto pairs
-  {
-    "echasnovski/mini.pairs",
-  },
-
-  -- surround
-  {
-    "echasnovski/mini.surround",
+  { "echasnovski/mini.surround",
     keys = function()
       -- HACK: use function to disable merging with LazyVim's keys definition
       return {
@@ -131,20 +118,117 @@ return {
       vim.keymap.set("x", "S", [[:<C-u>lua MiniSurround.add('visual')<CR>]], { silent = true })
     end,
   },
-  -- comments
-  {
-    "numToStr/Comment.nvim",
+
+  { "echasnovski/mini.ai",
+    -- TODO: port vim text objects over
+    event = "VeryLazy",
+    opts = function()
+      local ai = require("mini.ai")
+      return {
+        n_lines = 500,
+        custom_textobjects = {
+          o = ai.gen_spec.treesitter({ -- code block
+            a = { "@block.outer", "@conditional.outer", "@loop.outer" },
+            i = { "@block.inner", "@conditional.inner", "@loop.inner" },
+          }),
+          f = ai.gen_spec.treesitter({ a = "@function.outer", i = "@function.inner" }), -- function
+          c = ai.gen_spec.treesitter({ a = "@class.outer", i = "@class.inner" }), -- class
+          t = { "<([%p%w]-)%f[^<%w][^<>]->.-</%1>", "^<.->().*()</[^/]->$" }, -- tags
+          d = { "%f[%d]%d+" }, -- digits
+          e = { -- Word with case
+            { "%u[%l%d]+%f[^%l%d]", "%f[%S][%l%d]+%f[^%l%d]", "%f[%P][%l%d]+%f[^%l%d]", "^[%l%d]+%f[^%l%d]" },
+            "^().*()$",
+          },
+          u = ai.gen_spec.function_call(), -- u for "Usage"
+          U = ai.gen_spec.function_call({ name_pattern = "[%w_]" }), -- without dot in function name
+        },
+      }
+    end,
+  },
+  { "numToStr/Comment.nvim",
+    dependencies = {
+      { "JoosepAlviste/nvim-ts-context-commentstring",  -- nested language commenting (f.ex markdown code blocks)
+        opts = { enable_autocmd = false, },
+      }
+    },
+    opts = function(_, opts)
+      local tscci = require('ts_context_commentstring.integrations.comment_nvim')
+      vim.tbl_deep_extend('force', opts, {
+        toggler = {
+          line = "gcc",
+          block = "gbb",
+        },
+        mappings = {
+          basic = true,
+          extra = true,
+        },
+        pre_hook = tscci.create_pre_hook()
+      })
+      return opts
+    end,
+  },
+  { "danymat/neogen",  -- Generate annotations like doxygen
+    cmd = "Neogen",
+    keys = {
+      { "<leader>cn", function() require("neogen").generate() end, desc = "Generate Annotations (Neogen)", },
+    },
     opts = {
-      toggler = {
-        line = "gcc",
-        block = "gbb",
+      snippet_engine = "luasnip",
+    },
+  },
+  { "folke/lazydev.nvim",
+    ft = "lua",
+    cmd = "LazyDev",
+    opts = {
+      library = {
+        { path = "${3rd}/luv/library", words = { "vim%.uv" } },
+        { path = "snacks.nvim", words = { "Snacks" } },
+        { path = "lazy.nvim", words = { "LazySpec" } },
       },
-      mappings = {
-        basic = true,
-        extra = true,
+    },
+  },
+  { "gbprod/yanky.nvim", enabled = false, -- better yank/paste
+    -- TODO: integrate?
+    -- I'm not entirely convinced, it doesn't provide functionality like vim-peekaboo, 
+    -- maybe it can supplement it though?
+    -- Also, see issues on OSC52: https://github.com/gbprod/yanky.nvim/issues/213
+    recommended = true,
+    desc = "Better Yank/Paste",
+    event = "LazyFile",
+    opts = {
+      highlight = { timer = 150 },
+    },
+    keys = {
+      {
+        "<leader>p",
+        function() vim.cmd([[YankyRingHistory]]) end,
+        mode = { "n", "x" },
+        desc = "Open Yank History",
       },
+      -- stylua: ignore
+      { "y", "<Plug>(YankyYank)", mode = { "n", "x" }, desc = "Yank Text" },
+      { "p", "<Plug>(YankyPutAfter)", mode = { "n", "x" }, desc = "Put Text After Cursor" },
+      { "P", "<Plug>(YankyPutBefore)", mode = { "n", "x" }, desc = "Put Text Before Cursor" },
+      { "gp", "<Plug>(YankyGPutAfter)", mode = { "n", "x" }, desc = "Put Text After Selection" },
+      { "gP", "<Plug>(YankyGPutBefore)", mode = { "n", "x" }, desc = "Put Text Before Selection" },
+      { "[y", "<Plug>(YankyCycleForward)", desc = "Cycle Forward Through Yank History" },
+      { "]y", "<Plug>(YankyCycleBackward)", desc = "Cycle Backward Through Yank History" },
+      { "]p", "<Plug>(YankyPutIndentAfterLinewise)", desc = "Put Indented After Cursor (Linewise)" },
+      { "[p", "<Plug>(YankyPutIndentBeforeLinewise)", desc = "Put Indented Before Cursor (Linewise)" },
+      { "]P", "<Plug>(YankyPutIndentAfterLinewise)", desc = "Put Indented After Cursor (Linewise)" },
+      { "[P", "<Plug>(YankyPutIndentBeforeLinewise)", desc = "Put Indented Before Cursor (Linewise)" },
+      { ">p", "<Plug>(YankyPutIndentAfterShiftRight)", desc = "Put and Indent Right" },
+      { "<p", "<Plug>(YankyPutIndentAfterShiftLeft)", desc = "Put and Indent Left" },
+      { ">P", "<Plug>(YankyPutIndentBeforeShiftRight)", desc = "Put Before and Indent Right" },
+      { "<P", "<Plug>(YankyPutIndentBeforeShiftLeft)", desc = "Put Before and Indent Left" },
+      { "=p", "<Plug>(YankyPutAfterFilter)", desc = "Put After Applying a Filter" },
+      { "=P", "<Plug>(YankyPutBeforeFilter)", desc = "Put Before Applying a Filter" },
     },
   },
-  { "JoosepAlviste/nvim-ts-context-commentstring", enabled = false },
-  { "echasnovski/mini.comment", enabled = false },
+
+  { "andrewferrier/debugprint.nvim",
+    dependencies = { "nvim-treesitter" },
+    -- TODO: setup debugprint (or maybe not)
+    opts = { }
+  }
 }