feat(mini.pairs): allow to skip pairs in certain treesitter nodes

This commit is contained in:
Folke Lemaitre 2024-06-15 18:51:21 +02:00
parent f22a6022c4
commit d6bea06a82
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -109,4 +109,40 @@ function M.ai_whichkey()
}) })
end end
---@param opts {skip_next: string, skip_ts: string[], skip_unbalanced: boolean, markdown: boolean}
function M.pairs(opts)
local pairs = require("mini.pairs")
pairs.setup(opts)
local open = pairs.open
pairs.open = function(pair, neigh_pattern)
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])
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
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
return open(pair, neigh_pattern)
end
end
return M return M