mirror of
https://github.com/ilyakooo0/nvim-autopairs.git
synced 2024-09-11 07:15:44 +03:00
Feature: add set end pair length
It help to determine the length of end_pair string when it have a special key like <left> <right> and it can help to move cursor to a middle of end pair
This commit is contained in:
parent
50fec950de
commit
f561472efb
@ -295,7 +295,8 @@ M.autopairs_map = function(bufnr, char)
|
|||||||
and rule:can_pair(cond_opt)
|
and rule:can_pair(cond_opt)
|
||||||
then
|
then
|
||||||
local end_pair = rule:get_end_pair(cond_opt)
|
local end_pair = rule:get_end_pair(cond_opt)
|
||||||
local move_text = utils.repeat_key(utils.key.join_left,#end_pair)
|
local end_pair_length = rule:get_end_pair_length(end_pair)
|
||||||
|
local move_text = utils.repeat_key(utils.key.join_left, end_pair_length)
|
||||||
if add_char == 0 then
|
if add_char == 0 then
|
||||||
move_text =""
|
move_text =""
|
||||||
char = ""
|
char = ""
|
||||||
@ -352,7 +353,7 @@ M.autopairs_insert = function(bufnr, char)
|
|||||||
local end_pair = rule:get_end_pair(cond_opt)
|
local end_pair = rule:get_end_pair(cond_opt)
|
||||||
utils.set_vchar(char .. end_pair)
|
utils.set_vchar(char .. end_pair)
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
utils.feed(utils.key.left, #end_pair)
|
utils.feed(utils.key.left, rule:get_end_pair_length(end_pair))
|
||||||
end)
|
end)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
@ -32,7 +32,10 @@ function Rule.new(...)
|
|||||||
-- only use on end_wise
|
-- only use on end_wise
|
||||||
is_endwise = false,
|
is_endwise = false,
|
||||||
-- use regex to compare
|
-- use regex to compare
|
||||||
is_regex = false
|
is_regex = false,
|
||||||
|
-- some end_pair have key map like <left>.. then the length of string is
|
||||||
|
-- not correct
|
||||||
|
end_pair_length = nil,
|
||||||
},opt)
|
},opt)
|
||||||
return setmetatable(opt, {__index = Rule})
|
return setmetatable(opt, {__index = Rule})
|
||||||
end
|
end
|
||||||
@ -52,7 +55,17 @@ function Rule:get_end_pair(opts)
|
|||||||
if self.end_pair_func then
|
if self.end_pair_func then
|
||||||
return self.end_pair_func(opts)
|
return self.end_pair_func(opts)
|
||||||
end
|
end
|
||||||
return self.end_pair
|
return self.end_pair
|
||||||
|
end
|
||||||
|
|
||||||
|
function Rule:get_end_pair_length(opts)
|
||||||
|
if self.end_pair_length then
|
||||||
|
return self.end_pair_length
|
||||||
|
end
|
||||||
|
if type(opts) == 'string' then
|
||||||
|
return #opts
|
||||||
|
end
|
||||||
|
return self.get_end_pair(opts):length()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Rule:replace_endpair(value,check_pair)
|
function Rule:replace_endpair(value,check_pair)
|
||||||
@ -67,6 +80,11 @@ function Rule:replace_endpair(value,check_pair)
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Rule:set_end_pair_length(length)
|
||||||
|
self.end_pair_length = length
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
function Rule:with_move(cond)
|
function Rule:with_move(cond)
|
||||||
if self.move_cond == nil then self.move_cond = {}end
|
if self.move_cond == nil then self.move_cond = {}end
|
||||||
table.insert(self.move_cond, cond)
|
table.insert(self.move_cond, cond)
|
||||||
|
@ -390,14 +390,20 @@ local data = {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
setup_func = function()
|
setup_func = function()
|
||||||
npairs.add_rule(Rule("!", "!"):with_pair(cond.not_filetypes({"lua"})))
|
npairs.clear_rules()
|
||||||
|
npairs.add_rules({
|
||||||
|
Rule("%(.*%)%s*%=>", " { }", {"typescript", "typescriptreact","javascript"})
|
||||||
|
:use_regex(true)
|
||||||
|
:set_end_pair_length(2)
|
||||||
|
})
|
||||||
end,
|
end,
|
||||||
name="enable pairs in html",
|
name="mapping regex with custom end_pair_length",
|
||||||
filetype="html",
|
filetype="typescript",
|
||||||
key="!",
|
key=">",
|
||||||
before = [[x| ]],
|
before = [[(o)=| ]],
|
||||||
after = [[x!|! ]]
|
after = [[(o)=> { | } ]]
|
||||||
},
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
local run_data = {}
|
local run_data = {}
|
||||||
@ -436,6 +442,7 @@ local function Test(test_data)
|
|||||||
if value.key ~= '<cr>' then
|
if value.key ~= '<cr>' then
|
||||||
eq(after, result , "\n\n text error: " .. value.name .. "\n")
|
eq(after, result , "\n\n text error: " .. value.name .. "\n")
|
||||||
eq(p_after, pos[3] + 1, "\n\n pos error: " .. value.name .. "\n")
|
eq(p_after, pos[3] + 1, "\n\n pos error: " .. value.name .. "\n")
|
||||||
|
|
||||||
else
|
else
|
||||||
local line2 = vim.fn.getline(line + 2)
|
local line2 = vim.fn.getline(line + 2)
|
||||||
eq(line + 1, pos[2], '\n\n breakline error:' .. value.name .. "\n")
|
eq(line + 1, pos[2], '\n\n breakline error:' .. value.name .. "\n")
|
||||||
|
Loading…
Reference in New Issue
Block a user