mirror of
https://github.com/ilyakooo0/nvim-autopairs.git
synced 2024-09-11 07:15:44 +03:00
parent
8a5dbfcbcc
commit
1e75891fc6
12
README.md
12
README.md
@ -140,6 +140,7 @@ npairs.add_rules({
|
||||
})
|
||||
|
||||
|
||||
|
||||
-- press u1234 => u12341234
|
||||
npairs.add_rules({
|
||||
Rule("x%d%d%d%d*$", "number", "lua")
|
||||
@ -151,6 +152,17 @@ npairs.add_rules({
|
||||
})
|
||||
|
||||
|
||||
-- you can do anything with regex +special key
|
||||
-- example press tab will upper text
|
||||
-- press b1234s<tab> => B1234S1234S
|
||||
Rule("b%d%d%d%d%w$", "", "vim")
|
||||
:use_regex(true,"<tab>")
|
||||
:replace_endpair(function(opts)
|
||||
return
|
||||
opts.prev_char:sub(#opts.prev_char - 4,#opts.prev_char)
|
||||
.."<esc>viwU"
|
||||
end)
|
||||
|
||||
--- check ./lua/nvim-autopairs/rules/basic.lua
|
||||
|
||||
```
|
||||
|
@ -23,7 +23,7 @@ npairs.add_rules({
|
||||
-- then$ is a lua regex
|
||||
-- end is a match pair
|
||||
-- lua is a filetype
|
||||
-- if_statement is a treesitter name you can use nil to skip it
|
||||
-- if_statement is a treesitter name. "" will skip check with treesitter
|
||||
endwise('then$', 'end', 'lua', 'if_statement')
|
||||
})
|
||||
|
||||
|
@ -62,18 +62,43 @@ M.on_attach = function(bufnr)
|
||||
|
||||
state.rules = rules
|
||||
if utils.is_attached(bufnr) then return end
|
||||
local enable_insert_auto = false
|
||||
for _, rule in pairs(state.rules) do
|
||||
if rule.is_regex == false then
|
||||
if rule.key_map == "" then
|
||||
rule.key_map = rule.start_pair:sub((#rule.start_pair))
|
||||
end
|
||||
local key = string.format('"%s"', rule.key_map)
|
||||
if rule.key_map == '"' then key =[['"']] end
|
||||
local mapping = string.format("v:lua.MPairs.autopairs_map(%d,%s)", bufnr, key)
|
||||
api.nvim_buf_set_keymap(bufnr, "i", rule.key_map, mapping, {expr = true, noremap = true})
|
||||
|
||||
api.nvim_exec(string.format([[
|
||||
augroup autopairs_insert_%d
|
||||
autocmd!
|
||||
autocmd InsertCharPre <buffer=%d> call luaeval("require('nvim-autopairs').autopairs_insert(%d, _A)", v:char)
|
||||
augroup end ]],
|
||||
bufnr, bufnr, bufnr), false)
|
||||
if rule.key_map== "(" or rule.key_map == '[' or rule.key_map == "{" then
|
||||
key = rule.end_pair:sub(#rule.end_pair)
|
||||
mapping = string.format([[v:lua.MPairs.autopairs_map(%d, '%s')]], bufnr,key )
|
||||
vim.api.nvim_buf_set_keymap(bufnr, 'i',key, mapping, {expr = true, noremap = true})
|
||||
end
|
||||
else
|
||||
if rule.key_map ~="" then
|
||||
local mapping = string.format("v:lua.MPairs.autopairs_map(%d,'%s')", bufnr, rule.key_map)
|
||||
api.nvim_buf_set_keymap(bufnr, "i", rule.key_map, mapping, {expr = true, noremap = true})
|
||||
else
|
||||
enable_insert_auto = true
|
||||
end
|
||||
end
|
||||
end
|
||||
if enable_insert_auto then
|
||||
api.nvim_exec(string.format([[
|
||||
augroup autopairs_insert_%d
|
||||
autocmd!
|
||||
autocmd InsertCharPre <buffer=%d> call luaeval("require('nvim-autopairs').autopairs_insert(%d, _A)", v:char)
|
||||
augroup end ]],
|
||||
bufnr, bufnr, bufnr), false)
|
||||
end
|
||||
api.nvim_buf_set_keymap(bufnr,
|
||||
'i',
|
||||
"<bs>",
|
||||
string.format("v:lua.MPairs.autopairs_bs(%d)", bufnr), {expr = true, noremap = true})
|
||||
|
||||
api.nvim_buf_set_var(bufnr, "nvim-autopairs", 1)
|
||||
end
|
||||
|
||||
@ -83,6 +108,7 @@ M.autopairs_bs = function(bufnr)
|
||||
local _, col = utils.get_cursor()
|
||||
for _, rule in pairs(state.rules) do
|
||||
if rule.start_pair then
|
||||
|
||||
local prev_char, next_char = utils.text_cusor_line(
|
||||
line,
|
||||
col,
|
||||
@ -117,15 +143,67 @@ end
|
||||
|
||||
local skip_next = false
|
||||
|
||||
|
||||
M.autopairs_map = function(bufnr, char)
|
||||
if state.disabled then return end
|
||||
if skip_next then skip_next = false return end
|
||||
local line = utils.text_get_current_line(bufnr)
|
||||
local _, col = utils.get_cursor()
|
||||
local new_text = line:sub(1, col) .. char .. line:sub(col + 1,#line)
|
||||
local add_char = 1
|
||||
for _, rule in pairs(state.rules) do
|
||||
if rule.start_pair then
|
||||
if rule.is_regex and rule.key_map ~= "" then
|
||||
new_text = line:sub(1, col) .. line:sub(col + 1,#line)
|
||||
add_char = 0
|
||||
end
|
||||
log.debug("new_text:[" .. new_text .. "]")
|
||||
local prev_char, next_char = utils.text_cusor_line(
|
||||
new_text,
|
||||
col+ add_char,
|
||||
#rule.start_pair,
|
||||
#rule.end_pair, rule.is_regex
|
||||
)
|
||||
local cond_opt = {
|
||||
text = new_text,
|
||||
rule = rule,
|
||||
bufnr = bufnr,
|
||||
col = col,
|
||||
char = char,
|
||||
line = line,
|
||||
prev_char = prev_char,
|
||||
next_char = next_char,
|
||||
}
|
||||
-- log.debug("start_pair" .. rule.start_pair)
|
||||
-- log.debug('prev_char' .. prev_char)
|
||||
-- log.debug('next_char' .. next_char)
|
||||
if
|
||||
next_char == rule.end_pair
|
||||
and rule.is_regex==false
|
||||
and rule:can_move(cond_opt)
|
||||
then
|
||||
return utils.esc( utils.key.right)
|
||||
end
|
||||
if
|
||||
utils.is_equal(rule.start_pair, prev_char, rule.is_regex)
|
||||
and rule:can_pair(cond_opt)
|
||||
then
|
||||
local end_pair = rule:get_end_pair(cond_opt)
|
||||
if add_char == 0 then char = ""end
|
||||
return utils.esc(char .. end_pair .. utils.repeat_key(utils.key.left,#end_pair))
|
||||
end
|
||||
end
|
||||
end
|
||||
return char
|
||||
end
|
||||
M.autopairs_insert = function(bufnr, char)
|
||||
if state.disabled then return end
|
||||
if skip_next then skip_next = false return end
|
||||
local line = utils.text_get_current_line(bufnr)
|
||||
local _, col = utils.get_cursor()
|
||||
local new_text = line:sub(1, col) .. char .. line:sub(col + 1,#line)
|
||||
-- log.debug("new_text:[" .. new_text .. "]")
|
||||
for _, rule in pairs(state.rules) do
|
||||
if rule.start_pair then
|
||||
if rule.start_pair and rule.is_regex and rule.key_map == "" then
|
||||
local prev_char, next_char = utils.text_cusor_line(
|
||||
new_text,
|
||||
col + 1,
|
||||
@ -160,17 +238,16 @@ M.autopairs_insert = function(bufnr, char)
|
||||
utils.is_equal(rule.start_pair, prev_char, rule.is_regex)
|
||||
and rule:can_pair(cond_opt)
|
||||
then
|
||||
-- utils.set_vchar(char .. rule.end_pair)
|
||||
utils.set_vchar("")
|
||||
local end_pair = rule:get_end_pair(cond_opt)
|
||||
utils.set_vchar(char .. end_pair)
|
||||
vim.schedule(function()
|
||||
local end_pair = rule:get_end_pair(cond_opt)
|
||||
utils.insert_char(char .. end_pair)
|
||||
utils.feed(utils.key.left, #end_pair)
|
||||
end)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
return char
|
||||
end
|
||||
|
||||
M.autopairs_cr = function(bufnr)
|
||||
|
@ -17,6 +17,7 @@ function Rule.new(...)
|
||||
end
|
||||
end
|
||||
opt = vim.tbl_extend('force', {
|
||||
key_map = "",
|
||||
start_pair = nil,
|
||||
end_pair = nil,
|
||||
end_pair_func = false,
|
||||
@ -35,8 +36,9 @@ function Rule.new(...)
|
||||
return setmetatable(opt, {__index = Rule})
|
||||
end
|
||||
|
||||
function Rule:use_regex(value)
|
||||
function Rule:use_regex(value,key_map)
|
||||
self.is_regex = value
|
||||
self.key_map = key_map or ""
|
||||
return self
|
||||
end
|
||||
function Rule:get_end_pair(opts)
|
||||
|
@ -6,8 +6,7 @@ local parsers = require'nvim-treesitter.parsers'
|
||||
conds.is_ts_node = function(nodename)
|
||||
return function (opts)
|
||||
if not opts.check_ts then return true end
|
||||
if nodename == nil then return true end
|
||||
|
||||
if nodename == "" then return true end
|
||||
parsers.get_parser():parse()
|
||||
local target = ts_utils.get_node_at_cursor()
|
||||
if target ~= nil and target:type() == nodename then
|
||||
@ -22,7 +21,9 @@ conds.is_ts_node = function(nodename)
|
||||
local _,_, linenr_target = target:range()
|
||||
local _,_, linenr_parent = target:parent():range()
|
||||
log.debug(target:range())
|
||||
log.debug(ts_utils.get_node_text(target))
|
||||
log.debug(target:parent():range())
|
||||
log.debug(ts_utils.get_node_text(target:parent()))
|
||||
if linenr_parent - linenr_target == 1 then
|
||||
return true
|
||||
end
|
||||
|
@ -5,7 +5,7 @@ local ts_conds = require('nvim-autopairs.ts-conds')
|
||||
return {
|
||||
endwise = function (...)
|
||||
local params = {...}
|
||||
assert(type(params[4]) == 'string', 'type ')
|
||||
assert(type(params[4]) == 'string', 'treesitter name is string or"" ')
|
||||
|
||||
return Rule(...)
|
||||
:with_pair(cond.none())
|
||||
|
@ -62,6 +62,11 @@ M.is_attached = function(bufnr)
|
||||
return check == 1
|
||||
end
|
||||
|
||||
|
||||
M.set_attach = function(bufnr,value)
|
||||
api.nvim_buf_set_var(bufnr, "nvim-autopairs", value)
|
||||
end
|
||||
|
||||
M.is_in_table = function(tbl, val)
|
||||
if tbl == nil then return false end
|
||||
for _, value in pairs(tbl) do
|
||||
@ -99,7 +104,7 @@ M.text_get_current_line = function(bufnr)
|
||||
return M.text_get_line(bufnr, row -1)
|
||||
end
|
||||
|
||||
M.repeat_key=function(key,num)
|
||||
M.repeat_key = function(key, num)
|
||||
local text=''
|
||||
for _ = 1, num, 1 do
|
||||
text=text..key
|
||||
|
@ -30,7 +30,6 @@ ts.setup {
|
||||
|
||||
local data = {
|
||||
{
|
||||
only = true,
|
||||
name = "lua if add endwise" ,
|
||||
filepath = './tests/endwise/init.lua',
|
||||
filetype = "lua",
|
||||
@ -40,13 +39,13 @@ local data = {
|
||||
after = [[end ]]
|
||||
},
|
||||
{
|
||||
name = "lua if is have endwise" ,
|
||||
name = "add newline have endwise" ,
|
||||
filepath = './tests/endwise/init.lua',
|
||||
filetype = "lua",
|
||||
linenr = 5,
|
||||
key = [[<cr>]],
|
||||
before = [[if data== 'fdsafdsa' then|end ]],
|
||||
after = [[end ]]
|
||||
before = [[if data== 'fdsafdsa' then|end]],
|
||||
after = [[end]]
|
||||
},
|
||||
{
|
||||
name = "don't add endwise on match rule" ,
|
||||
|
@ -3,6 +3,7 @@ local npairs = require('nvim-autopairs')
|
||||
local Rule = require('nvim-autopairs.rule')
|
||||
local cond = require('nvim-autopairs.conds')
|
||||
local log = require('nvim-autopairs._log')
|
||||
local utils = require('nvim-autopairs.utils')
|
||||
_G.npairs = npairs;
|
||||
local eq=_G.eq
|
||||
|
||||
@ -11,8 +12,15 @@ npairs.add_rules({
|
||||
Rule("x%d%d%d%d*$", "number", "lua")
|
||||
:use_regex(true)
|
||||
:replace_endpair(function(opts)
|
||||
log.debug(opts.prev_char)
|
||||
return opts.prev_char:sub(#opts.prev_char - 3,#opts.prev_char)
|
||||
|
||||
end),
|
||||
Rule("b%d%d%d%d%w$", "", "vim")
|
||||
:use_regex(true,"<tab>")
|
||||
:replace_endpair(function(opts)
|
||||
return
|
||||
opts.prev_char:sub(#opts.prev_char - 4,#opts.prev_char)
|
||||
.."<esc>viwUi"
|
||||
end)
|
||||
})
|
||||
vim.api.nvim_set_keymap('i' , '<CR>','v:lua.npairs.check_break_line_char()', {expr = true , noremap = true})
|
||||
@ -232,11 +240,19 @@ local data = {
|
||||
after = [[u1234|number ]]
|
||||
},
|
||||
{
|
||||
name="text regex",
|
||||
name="text regex with custome end_pair",
|
||||
filetype = "lua",
|
||||
key="4",
|
||||
before = [[x123| ]],
|
||||
after = [[x1234|1234 ]]
|
||||
},
|
||||
{
|
||||
name="text regex with custome key",
|
||||
filetype = "vim",
|
||||
key="<tab>",
|
||||
before = [[b1234s| ]],
|
||||
after = [[B|1234S1234S ]]
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -264,6 +280,7 @@ local function Test(test_data)
|
||||
else
|
||||
vim.bo.filetype = "text"
|
||||
end
|
||||
utils.set_attach(vim.api.nvim_get_current_buf(),0)
|
||||
npairs.on_attach(vim.api.nvim_get_current_buf())
|
||||
vim.fn.setline(line , before)
|
||||
vim.fn.setpos('.' ,{0, line, p_before , 0})
|
||||
|
@ -7,11 +7,11 @@ utils.insert_char = function(text)
|
||||
end
|
||||
|
||||
utils.feed = function(text,num)
|
||||
if num > 0 then
|
||||
num = num + 1
|
||||
else
|
||||
num = 1
|
||||
end
|
||||
-- if num > 0 then
|
||||
-- num = num + 1
|
||||
-- else
|
||||
-- num = 1
|
||||
-- end
|
||||
|
||||
local result = ''
|
||||
for _ = 1, num, 1 do
|
||||
|
Loading…
Reference in New Issue
Block a user