Files
.dotfiles/.config/nvim/lua/plugin/autopairs.lua

33 lines
1.3 KiB
Lua

---@type LazySpec
return { {
'windwp/nvim-autopairs', -- automatically inserts closing brackets
event = { 'BufReadPre', 'BufNewFile' },
opts = {
enable_moveright = true, -- moves the cursor over existing pairs instead of inserting duplicates
check_ts = true, -- whether to check treesitter for specific nodes
ts_config = {}
},
config = function(_, opts)
local autopairs = require 'nvim-autopairs'
autopairs.setup(opts)
-- customise the rule for [] brackets so it doesn't close when typing ANSI escape codes
local rule = autopairs.get_rule '['
if rule then
rule:with_pair(function(opt)
local ei = math.max(1, opt.col - 1) -- get the end-index
local prev_oct = opt.line:sub(math.max(1, opt.col - 4), ei) -- get the range of an octal escape code
local prev_hex = opt.line:sub(math.max(1, opt.col - 4), ei) -- get the range of a hexadecimal escape code
local prev_cha = opt.line:sub(math.max(1, opt.col - 2), ei) -- get the range of a character escape code
-- check whether we can't match any of the patterns,
-- if so return true, otherwise return false (whether to insert a pair)
return (prev_oct ~= [[\033]] and prev_hex ~= [[\x1b]] and prev_cha ~= [[\e]])
end)
else
vim.notify('could not add a rule for not auto-closing [ when typing an ANSI escape code',
vim.log.levels.WARN)
end
end,
} }