]> git.rmz.io Git - dotfiles.git/blob - nvim/lua/plugins/coding.lua
eaa49cfb68e41b2ca832069939a7a290b37c34c0
[dotfiles.git] / nvim / lua / plugins / coding.lua
1 ---@type LazySpec
2 return {
3 -- snippets
4 {
5 "L3MON4D3/LuaSnip",
6 -- disable luasnip bindings for <tab> and <s-tab>
7 dependencies = {
8 {
9 "honza/vim-snippets",
10 config = function()
11 require("luasnip.loaders.from_snipmate").lazy_load()
12 require("luasnip.loaders.from_lua").load({ paths = vim.fn.stdpath("config") .. "/lua/snippets" })
13 end,
14 },
15 },
16 keys = function()
17 return {}
18 end,
19 opts = {
20 store_selection_keys = "<Tab>",
21 },
22 },
23
24 { "echasnovski/mini.pairs",
25 event = "VeryLazy",
26 opts = {
27 modes = { insert = true, command = true, terminal = false },
28 -- skip autopair when next character is one of these
29 skip_next = [=[[%w%%%'%[%"%.%`%$]]=],
30 -- skip autopair when the cursor is inside these treesitter nodes
31 skip_ts = { "string" },
32 -- skip autopair when next character is closing pair
33 -- and there are more closing pairs than opening pairs
34 skip_unbalanced = true,
35 -- better deal with markdown code blocks
36 markdown = true,
37 },
38 config = function(_, opts)
39 Snacks.toggle({
40 name = "Mini Pairs",
41 get = function()
42 return not vim.g.minipairs_disable
43 end,
44 set = function(state)
45 vim.g.minipairs_disable = not state
46 end,
47 }):map("<leader>up")
48
49 local pairs = require("mini.pairs")
50 pairs.setup(opts)
51 local open = pairs.open
52 --- Custom open command with extensions from LazyVim
53 ---@diagnostic disable-next-line: duplicate-set-field
54 pairs.open = function(pair, neigh_pattern)
55 if vim.fn.getcmdline() ~= "" then
56 return open(pair, neigh_pattern)
57 end
58 local o, c = pair:sub(1, 1), pair:sub(2, 2)
59 local line = vim.api.nvim_get_current_line()
60 local cursor = vim.api.nvim_win_get_cursor(0)
61 local next = line:sub(cursor[2] + 1, cursor[2] + 1)
62 local before = line:sub(1, cursor[2])
63 -- don't add fourth ` in markdown ```
64 if opts.markdown and o == "`" and vim.bo.filetype == "markdown" and before:match("^%s*``") then
65 return "`\n```" .. vim.api.nvim_replace_termcodes("<up>", true, true, true)
66 end
67 if opts.skip_next and next ~= "" and next:match(opts.skip_next) then
68 return o
69 end
70 if opts.skip_ts and #opts.skip_ts > 0 then
71 local ok, captures = pcall(vim.treesitter.get_captures_at_pos, 0, cursor[1] - 1, math.max(cursor[2] - 1, 0))
72 for _, capture in ipairs(ok and captures or {}) do
73 if vim.tbl_contains(opts.skip_ts, capture.capture) then
74 return o
75 end
76 end
77 end
78 if opts.skip_unbalanced and next == c and c ~= o then
79 local _, count_open = line:gsub(vim.pesc(pair:sub(1, 1)), "")
80 local _, count_close = line:gsub(vim.pesc(pair:sub(2, 2)), "")
81 if count_close > count_open then
82 return o
83 end
84 end
85 return open(pair, neigh_pattern)
86 end
87 end,
88 },
89
90 { "echasnovski/mini.surround",
91 keys = function()
92 -- HACK: use function to disable merging with LazyVim's keys definition
93 return {
94 { "ys", desc = "Add surrounding", "n" },
95 { "S", desc = "Add surrounding", "x" },
96 { "ds", desc = "Delete surrounding" },
97 { "cs", desc = "Change surrounding" },
98 { "yss", "ys_", remap = true },
99 }
100 end,
101 opts = {
102 mappings = {
103 -- TODO: this is tpope/surround like, but should consider using vim-sandwich mappings
104 -- see: :h MiniSurround-vim-surround-config
105 add = "ys",
106 delete = "ds",
107 find = "",
108 find_left = "",
109 highlight = "",
110 replace = "cs",
111 update_n_lines = "",
112 },
113 },
114 config = function(_, opts)
115 require("mini.surround").setup(opts)
116 -- remap visual
117 vim.keymap.del("x", "ys", { silent = true })
118 vim.keymap.set("x", "S", [[:<C-u>lua MiniSurround.add('visual')<CR>]], { silent = true })
119 end,
120 },
121
122 { "echasnovski/mini.ai",
123 -- TODO: port vim text objects over
124 event = "VeryLazy",
125 opts = function()
126 local ai = require("mini.ai")
127 return {
128 n_lines = 500,
129 custom_textobjects = {
130 o = ai.gen_spec.treesitter({ -- code block
131 a = { "@block.outer", "@conditional.outer", "@loop.outer" },
132 i = { "@block.inner", "@conditional.inner", "@loop.inner" },
133 }),
134 f = ai.gen_spec.treesitter({ a = "@function.outer", i = "@function.inner" }), -- function
135 c = ai.gen_spec.treesitter({ a = "@class.outer", i = "@class.inner" }), -- class
136 t = { "<([%p%w]-)%f[^<%w][^<>]->.-</%1>", "^<.->().*()</[^/]->$" }, -- tags
137 d = { "%f[%d]%d+" }, -- digits
138 e = { -- Word with case
139 { "%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]" },
140 "^().*()$",
141 },
142 u = ai.gen_spec.function_call(), -- u for "Usage"
143 U = ai.gen_spec.function_call({ name_pattern = "[%w_]" }), -- without dot in function name
144 },
145 }
146 end,
147 },
148 -- comments
149 { "numToStr/Comment.nvim",
150 -- used to be what I use, but nvim will include commenting based on mini.comment
151 enabled = false ,
152 opts = {
153 toggler = {
154 line = "gcc",
155 block = "gbb",
156 },
157 mappings = {
158 basic = true,
159 extra = true,
160 },
161 },
162 },
163 { "echasnovski/mini.comment", enabled = false },
164
165 {
166 "folke/lazydev.nvim",
167 ft = "lua",
168 cmd = "LazyDev",
169 opts = {
170 library = {
171 { path = "${3rd}/luv/library", words = { "vim%.uv" } },
172 { path = "LazyVim", words = { "LazyVim" } },
173 { path = "snacks.nvim", words = { "Snacks" } },
174 { path = "lazy.nvim", words = { "LazySpec" } },
175 },
176 },
177 },
178 }