Skip to content

Instantly share code, notes, and snippets.

@yardnsm
Last active April 20, 2022 09:47
Show Gist options
  • Save yardnsm/3818a56526d3d56343b00a03aa2b0269 to your computer and use it in GitHub Desktop.
Save yardnsm/3818a56526d3d56343b00a03aa2b0269 to your computer and use it in GitHub Desktop.
-- vim-closer replacement
-- https://github.com/rstacruz/vim-closer/blob/master/autoload/closer.vim
local get_closing_for_line = function (line)
local i = -1
local clo = ''
while true do
i, _= string.find(line, "[%(%)%{%}%[%]]", i + 1)
if i == nil then break end
local ch = string.sub(line, i, i)
local st = string.sub(clo, 1, 1)
if ch == '{' then
clo = '}' .. clo
elseif ch == '}' then
if st ~= '}' then return '' end
clo = string.sub(clo, 2)
elseif ch == '(' then
clo = ')' .. clo
elseif ch == ')' then
if st ~= ')' then return '' end
clo = string.sub(clo, 2)
elseif ch == '[' then
clo = ']' .. clo
elseif ch == ']' then
if st ~= ']' then return '' end
clo = string.sub(clo, 2)
end
end
return clo
end
autopairs.clear_rules()
autopairs.add_rule(
Rule("[%(%{%[]", "")
:use_regex(true)
:replace_endpair(function(opts)
return get_closing_for_line(opts.line)
end)
:end_wise()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment