add use_regex

This commit is contained in:
windwp 2021-04-19 08:21:08 +07:00 committed by windwp
parent 460cdfce42
commit dc73b76a0c
6 changed files with 151 additions and 100 deletions

136
README.md
View File

@ -1,7 +1,7 @@
## nvim-autopairs
A minimalist autopairs for Neovim written by Lua.
It can support multipple character
It support multipple character
Requires neovim 0.5+
@ -27,69 +27,7 @@ require('nvim-autopairs').setup({
disable_filetype = { "TelescopePrompt" , "vim" },
})
```
### Rule
It can support multipple character
``` lua
local Rule = require('nvim-autopairs.rule')
local npairs = require('nvim-autopairs')
npairs.add_rule({
Rule("$$","$$","tex")
})
-- you can use some builtin condition
local cond = require('nvim-autopairs.conds')
print(vim.inspect(cond))
npairs.add_rules({
Rule("$", "$",{"tex", "latex"})
-- don't add a pair if the next character is %
:with_pair(cond.not_after_regex_check("%%"))
-- don't add a pair if the previous character is xxx
:with_pair(cond.not_before_regex_check("xxx", 3))
-- don't move right when repeat character
:with_move(cond.none())
-- don't delete if the next character is xx
:with_del(cond.not_after_regex_check("xx"))
-- disable add newline when press <cr>
:with_cr(cond.none())
},
)
npairs.add_rules({
Rule("$$","$$","tex")
:with_pair(function(otps)
print(vim.inspect(otps))
if opts.line=="aa $$" then
-- don't add pair on that line
return false
end
end)
}
)
--- check ./lua/nvim-autopairs/rules/basic.lua
```
### Break line on html or inside pairs
By default nvim-autopairs don't mapping `<CR>` on insert mode
if you want to do that you need to mapping it by your self
``` text
Before Input After
------------------------------------
{|} <CR> {
|
}
------------------------------------
<div>|</div> <CR> <div>
|
</div>
```
#### Sample of mapping `<CR>`
@ -148,8 +86,74 @@ remap('i' , '<CR>','v:lua.MUtils.completion_confirm()', {expr = true , noremap =
```
### Rule
``` lua
local Rule = require('nvim-autopairs.rule')
local npairs = require('nvim-autopairs')
npairs.add_rule({
Rule("$$","$$","tex")
})
-- you can use some builtin condition
local cond = require('nvim-autopairs.conds')
print(vim.inspect(cond))
npairs.add_rules({
Rule("$", "$",{"tex", "latex"})
-- don't add a pair if the next character is %
:with_pair(cond.not_after_regex_check("%%"))
-- don't add a pair if the previous character is xxx
:with_pair(cond.not_before_regex_check("xxx", 3))
-- don't move right when repeat character
:with_move(cond.none())
-- don't delete if the next character is xx
:with_del(cond.not_after_regex_check("xx"))
-- disable add newline when press <cr>
:with_cr(cond.none())
},
)
npairs.add_rules({
Rule("$$","$$","tex")
:with_pair(function(otps)
print(vim.inspect(otps))
if opts.line=="aa $$" then
-- don't add pair on that line
return false
end
end)
}
)
-- you can use regex
-- press u1234 => u1234number
npairs.add_rules({
Rule("u%d%d%d%d*$", "number", "lua")
:use_regex(true)
})
-- press u1234 => u12341234
npairs.add_rules({
Rule("x%d%d%d%d*$", "number", "lua")
:use_regex(true)
:replace_endpair(function(opts)
-- print(vim.inspect(opts))
return opts.prev_char:sub(#opts.prev_char - 3,#opts.prev_char)
end)
})
--- check ./lua/nvim-autopairs/rules/basic.lua
```
### Don't add pairs if it already have a close pairs in same line
if **next character** is a close pairs and it doesn't have an open pairs in same line then it will not add a close pairs
@ -161,12 +165,6 @@ Before Input After
```
``` lua
-- default is true if you want to disable it set it to false
require('nvim-autopairs').setup({
check_line_pair = false
})
```
### Don't add pairs if the next char is alphanumeric

View File

@ -81,13 +81,17 @@ M.autopairs_bs = function(bufnr)
if state.disabled then return end
local line = utils.text_get_current_line(bufnr)
local _, col = utils.get_cursor()
local filetype = vim.bo.filetype
for _, rule in pairs(state.rules) do
if rule.start_pair and utils.check_filetype(rule.filetypes, filetype) then
local prev_char = utils.text_sub_char(line, col,-#rule.start_pair)
local next_char = utils.text_sub_char(line, col+1,#rule.start_pair)
if rule.start_pair then
local prev_char, next_char = utils.text_cusor_line(
line,
col,
#rule.start_pair,
#rule.end_pair,
rule.is_regex
)
if
rule.start_pair == prev_char
utils.is_equal(rule.start_pair, prev_char, rule.is_regex)
and rule.end_pair == next_char
and rule:can_del({
bufnr = bufnr,
@ -119,11 +123,15 @@ M.autopairs_insert = function(bufnr, char)
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 .. "]")
-- log.debug("new_text:[" .. new_text .. "]")
for _, rule in pairs(state.rules) do
if rule.start_pair then
local prev_char = utils.text_sub_char(new_text, col + 1,-#rule.start_pair)
local next_char = utils.text_sub_char(new_text, col + 2,#rule.end_pair)
local prev_char, next_char = utils.text_cusor_line(
new_text,
col + 1,
#rule.start_pair,
#rule.end_pair, rule.is_regex
)
local cond_opt = {
text = new_text,
rule = rule,
@ -134,14 +142,14 @@ M.autopairs_insert = function(bufnr, char)
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)
-- 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:can_move(cond_opt)
then
utils.reset_vchar()
utils.set_vchar("")
vim.schedule(function()
utils.feed(utils.key.right, -1)
end)
@ -149,11 +157,13 @@ M.autopairs_insert = function(bufnr, char)
end
if
prev_char == rule.start_pair
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("")
vim.schedule(function()
utils.insert_char(rule.end_pair)
utils.insert_char(char .. rule:get_end_pair(cond_opt))
utils.feed(utils.key.left, #rule.end_pair)
end)
return
@ -163,21 +173,18 @@ M.autopairs_insert = function(bufnr, char)
end
M.autopairs_cr = function(bufnr)
log.debug("on_cr")
if state.disabled then return end
bufnr = bufnr or api.nvim_get_current_buf()
local line = utils.text_get_current_line(bufnr)
local _, col = utils.get_cursor()
local filetype = vim.bo.filetype
for _, rule in pairs(state.rules) do
if rule.start_pair and utils.check_filetype(rule.filetypes, filetype) then
if rule.start_pair then
local prev_char, next_char = utils.text_cusor_line(
line,
col,
#rule.start_pair,
#rule.end_pair, rule.is_regex
)
log.debug(vim.inspect( prev_char))
if
rule.is_endwise
and utils.is_equal(rule.start_pair, prev_char, rule.is_regex)
@ -190,7 +197,7 @@ M.autopairs_cr = function(bufnr)
line = line
})
then
log.debug('correct_endwise')
log.debug('do endwise')
return utils.esc(
rule.end_pair
.. utils.repeat_key(utils.key.left, 3)
@ -209,10 +216,9 @@ M.autopairs_cr = function(bufnr)
line = line
})
then
log.debug('map _cr')
log.debug('do _cr')
return utils.esc("<cr><c-o>O")
end
log.debug("end_cr")
end
end
return utils.esc("<cr>")

View File

@ -19,6 +19,7 @@ function Rule.new(...)
opt = vim.tbl_extend('force', {
start_pair = nil,
end_pair = nil,
replace_endpair = nil,
filetypes = nil,
-- allow move when press close_pairs
move_cond = nil,
@ -38,6 +39,18 @@ function Rule:use_regex(value)
self.is_regex = value
return self
end
function Rule:get_end_pair(opts)
log.debug(self.replace_endpair)
if self.replace_endpair ~= nil then
return self.replace_endpair(opts)
end
return self.end_pair
end
function Rule:replace_endpair(value)
self.replace_endpair = value
return self
end
function Rule:with_move(cond)
if self.move_cond == nil then self.move_cond = {}end

View File

@ -1,7 +1,6 @@
local conds = {}
local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils')
local log = require('nvim-autopairs._log')
local ts_lib=require('nvim-autopairs.ts-utils')
local parsers = require'nvim-treesitter.parsers'
conds.is_ts_node = function(nodename)
@ -20,8 +19,8 @@ conds.is_ts_node = function(nodename)
-- if match then we need tocheck parent node
local _,_, linenr_target = target:range()
local _,_, linenr_parent = target:parent():range()
log.debug(linenr_target)
log.debug(linenr_parent)
log.debug(target:range())
log.debug(target:parent():range())
if linenr_parent - linenr_target == 1 then
return true
end

View File

@ -9,8 +9,9 @@ M.key = {
right = "<right>"
}
M.reset_vchar = function()
vim.cmd [[let v:char = ""]]
M.set_vchar = function(text)
text = text:gsub('"', '\\"')
vim.cmd(string.format([[let v:char = "%s"]],text))
end
@ -28,9 +29,10 @@ M.is_close_bracket = function (char)
end
M.is_equal = function (value,text, is_regex)
log.debug('value'..value)
log.debug(vim.inspect( is_regex))
log.debug("text" .. value)
log.debug("text" .. text)
if is_regex and string.match(text, value) then
log.debug('match')
return true
elseif text == value then
return true
@ -123,7 +125,7 @@ M.text_cusor_line = function(line, col, prev_count, next_count, is_regex)
prev_count = col
next_count = #line - col
end
local prev = M.text_sub_char(line, col, -prev_count)
local prev = M.text_sub_char(line, col, - prev_count)
local next = M.text_sub_char(line, col + 1, next_count)
return prev, next
end
@ -151,7 +153,7 @@ M.feed = function(text, num)
result = result .. text
end
log.debug("result" .. result)
api.nvim_feedkeys (api.nvim_replace_termcodes(
api.nvim_feedkeys(api.nvim_replace_termcodes(
result, true, false, true),
"n", true)
end

View File

@ -6,6 +6,17 @@ local log = require('nvim-autopairs._log')
_G.npairs = npairs;
local eq=_G.eq
npairs.add_rules({
Rule("u%d%d%d%d*$", "number", "lua"):use_regex(true),
Rule("x%d%d%d%d*$", "number", "lua")
:use_regex(true)
:replace_endpair(function(opts)
-- print(vim.inspect(opts))
-- return "dfsafsa"
return opts.prev_char:sub(#opts.prev_char - 3,#opts.prev_char)
end)
})
vim.api.nvim_set_keymap('i' , '<CR>','v:lua.npairs.check_break_line_char()', {expr = true , noremap = true})
function helpers.feed(text, feed_opts)
feed_opts = feed_opts or 'n'
@ -44,7 +55,7 @@ local data = {
after = [[aa"|" aa]]
},
{
-- only = true,
name = "add python quote" ,
filetype = "python",
key = [["]],
@ -200,6 +211,28 @@ local data = {
key = [[<cr>]],
before = [[<div>|</div>]],
after = [[</div>]]
},
{
name = "press multiple key" ,
filetype = "html",
key = [[((((]],
before = [[a| ]],
after = [[a((((|)))) ]]
},
{
name="text regex",
filetype = "lua",
key="4",
before = [[u123| ]],
after = [[u1234|number ]]
},
{
only = true,
name="text regex",
filetype = "lua",
key="4",
before = [[x123| ]],
after = [[x1234|vv ]]
}
}