marleyvim/nvim/lua/plugins/coding/mini-ai.lua
2024-12-15 11:18:47 -08:00

124 lines
3.4 KiB
Lua

return {
'mini.ai',
event = 'DeferredUIEnter',
before = function()
require('lz.n').trigger_load('which-key.nvim')
end,
after = function()
local ai = require('mini.ai')
ai.setup({
n_lines = 500,
custom_textobjects = {
-- code blOck
o = ai.gen_spec.treesitter({
a = { '@block.outer', '@conditional.outer', '@loop.outer' },
i = { '@block.inner', '@conditional.inner', '@loop.inner' },
}),
-- Class
c = ai.gen_spec.treesitter({
a = '@class.outer',
i = '@class.inner',
}),
-- Usage (fn call)
u = ai.gen_spec.function_call(),
-- Usage (to last .)
U = ai.gen_spec.function_call({ name_pattern = '[%w_]' }),
-- Digit
d = { '%f[%d]%d+' },
-- word within casE (snake_case, CamelCase, etc)
e = {
{
'%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]',
},
'^().*()$',
},
-- buffer
g = {
from = { line = 1, col = 1 },
to = {
line = vim.fn.line('$'),
col = math.max(vim.fn.getline('$'):len(), 1),
},
},
},
})
local i = require('icons')
local c = require('colors')
local mkA = MarleyVim.wkSpec(c.around)
local mkI = MarleyVim.wkSpec(c.inner)
---@param lhs string
---@param icon string
---@param opts table
local function mkKey(lhs, icon, opts)
if lhs:sub(1, 1) == 'a' then
return mkA(lhs, nil, icon, opts)
else
return mkI(lhs, nil, icon, opts)
end
end
local groups = {
around = { 'a', i.around },
around_last = { 'al', i.around },
around_next = { 'an', i.around },
inner = { 'i', i.inner },
inner_last = { 'il', i.inner },
inner_next = { 'in', i.inner },
}
local mappings = {
{ '<SPACE>', '', 'whitespace' },
{ '"', '', 'double quotes' },
{ "'", '', 'single quotes' },
{ '`', '', 'backticks' },
{ '(', '󰅲', '()' },
{ ')', '󰅲', '() with ws' },
{ '[', '󰅪', '[] block' },
{ ']', '󰅪', '[] block with ws' },
{ '{', '󰅩', '{} block' },
{ '}', '󰅩', '{} block with ws' },
{ '<', '󰅴', '<>' },
{ '>', '󰅴', '<> with ws' },
{ '_', '󱁐', 'underscores' },
{ '?', '󰘎', 'user prompt' },
{ 'a', i.lsp.variable, 'argument' },
{ 'b', '󰅲', ')]} block' },
{ 'c', i.lsp.class, 'class' },
{ 'd', i.lsp.number, 'numbers' },
{ 'e', i.lsp.variable, 'word within case' },
{ 'f', i.lsp['function'], 'function' },
{ 'g', i.lsp.file, 'entire buffer' },
{ 'o', i.lsp.control, 'block, conditional, loop' },
{ 'q', i.lsp.string, 'quotes/backticks' },
{ 't', '󰅴', 'tags' },
{ 'u', i.lsp.method, 'use/call' },
{ 'U', i.lsp.method, 'use/call without dot' },
}
local spec = { mode = { 'o', 'x' } }
for name, data in pairs(groups) do
name = name:gsub('^around_', ''):gsub('^insude_', '')
spec[#spec + 1] = mkKey(data[1], data[2], { group = name })
for _, item in ipairs(mappings) do
spec[#spec + 1] = mkKey(data[1] .. item[1], item[2], { desc = item[3] })
end
end
require('which-key').add({ spec })
end,
}