update rule api

This commit is contained in:
windwp 2021-04-22 13:53:27 +07:00
parent 6c1063cc65
commit 332a52198d
8 changed files with 91 additions and 34 deletions

View File

@ -169,9 +169,10 @@ npairs.add_rules({
--- check ./lua/nvim-autopairs/rules/basic.lua
```
[Rules API](./doc/rules.md)
### Treesitter
You can use treesitter to check
You can use treesitter to check pair
```lua
local npairs = require("nvim-autopairs")

32
doc/rules.md Normal file
View File

@ -0,0 +1,32 @@
##Rules
| function | usage |
|---------------------------|-----------------------------------------------------------|
| with_pair(cond) | add condition to check for pair event |
| with_move(cond) | add condition to check for move right event |
| with_cr(cond) | add condition to check for break line event |
| with_del(cond) | add condition to check for delete pair event |
| only_cr(cond) | disable move,del and pair event Only use break line event |
| use_regex(bool,"<key>") | input pair use regex and trigger by key |
| replace_endpair(function) | change a map pair with function |
| end_wise(cond) | use it on end_wise mode |
### Condition
```lua
local cond = require('nvim-autopairs.conds')
```
| function | Usage |
|------------------------|-----------------------------------|
| not_before_regex_check | check character before with regex |
| not_after_regex_check | check character after with regex |
| not_inside_quote | check is inside a quote |
### TreeSitter Condition
```lua
local ts_conds = require('nvim-autopairs.ts-conds')
```
| function | Usage |
|------------------------------|-------------------------------|
| is_ts_node({node_table}) | check current treesitter node |
| is_not_ts_node({node_table}) | check not in treesitter node |

View File

@ -116,31 +116,35 @@ M.on_attach = function(bufnr)
if utils.is_attached(bufnr) then return end
local enable_insert_auto = false
for _, rule in pairs(M.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})
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)
if rule.key_map~=nil then
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})
elseif rule.is_endwise == false then
enable_insert_auto = true
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})
elseif rule.is_endwise == false then
enable_insert_auto = true
end
end
end
end
if enable_insert_auto then
-- capture all key use it to trigger regex pairs
-- it can make an issue with paste from register
api.nvim_exec(string.format([[
augroup autopairs_insert_%d
autocmd!

View File

@ -66,6 +66,18 @@ cond.check_is_bracket_line=function ()
end
end
cond.not_inside_quote = function()
return function(opts)
log.debug('not_add_quote_inside_quote')
if
utils.is_in_quote(opts.text, opts.col, opts.char)
then
return false
end
end
end
cond.not_add_quote_inside_quote = function()
return function(opts)
log.debug('not_add_quote_inside_quote')

View File

@ -78,9 +78,18 @@ function Rule:with_pair(cond)
return self
end
function Rule:only_cr(cond)
self.key_map = nil
self.pair_cond = false
self.move_cond =false
self.del_cond = false
if cond then return self:with_cr(cond) end
return self
end
function Rule:end_wise()
self.is_endwise = true
return self
return self:only_cr()
end
local function can_do(conds, opt)

View File

@ -12,9 +12,7 @@ local function setup(opt)
Rule("<!--", "-->", 'html'):with_cr(cond.none()),
Rule("```", "```", { 'markdown', 'vimwiki' }),
Rule("```.*$", "```", { 'markdown', 'vimwiki' })
:with_move(cond.none())
:with_del(cond.none())
:with_pair(cond.none())
:only_cr()
:use_regex(true)
,
Rule('"""', '"""', 'python'),
@ -28,11 +26,9 @@ local function setup(opt)
:with_pair(cond.check_is_bracket_line()),
basic("{", "}")
:with_pair(cond.check_is_bracket_line()),
-- Rule(">", "<",
-- { 'html', 'typescript', 'typescriptreact', 'svelte', 'vue'})
-- :with_move(cond.none())
-- :with_pair(cond.none())
-- :with_del(cond.none()),
Rule(">", "<",
{ 'html', 'typescript', 'typescriptreact', 'svelte', 'vue'})
:only_cr()
}
return rules
end

View File

@ -8,12 +8,8 @@ return {
assert(type(params[4]) == 'string', 'treesitter name is string or "" ')
return Rule(...)
:with_pair(cond.none())
:with_move(cond.none())
:with_del(cond.none())
:with_cr(ts_conds.is_endwise_node(params[4]))
:use_regex(true)
:end_wise()
:end_wise(ts_conds.is_endwise_node(params[4]))
end
}

View File

@ -226,6 +226,13 @@ local data = {
before = [[<div>|</div>]],
after = [[</div>]]
},
{
name = "do not mapping on > html" ,
filetype = "html",
key = [[>]],
before = [[<div| ]],
after = [[<div>| ]]
},
{
name = "press multiple key" ,
filetype = "html",