Skip to content

Commit

Permalink
add remove_rule func
Browse files Browse the repository at this point in the history
  • Loading branch information
windwp committed Apr 20, 2021
1 parent b4a4d53 commit bc18313
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ if you use another plugin you can temporary to disable autopair
-- do your task
require('nvim-autopairs').enable()
```

### Endwise

[endwise](./doc/endwise.md)
Expand Down
14 changes: 10 additions & 4 deletions doc/endwise.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#Endwise (experiment)

** warning** you endwise base on treesitter is not always correct.
treesitter group all error node with parent node so we can't find
a perfect solution for this.

I don't have time to add rule for all language.
so you can create your rule and make a PR. Thank
You can create your rule and make a PR.
Thank

read docs in readme.md about rules first.

and install TSPlayground

``` lua
Expand All @@ -28,14 +32,16 @@ npairs.add_rules({
})

```

-- run TSPlaygroudnToggle and get treesitter name

![treesitter](./images/endwise.png)


if that builtin endwise rule is not correct you can make your custom rule
conditon

take a look of that file and write your function

`lua/nvim-autopairs/ts-rule.lua`
`lua/nvim-autopairs/ts-conds.lua`
* `lua/nvim-autopairs/ts-rule.lua`
* `lua/nvim-autopairs/ts-conds.lua`
21 changes: 16 additions & 5 deletions lua/nvim-autopairs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ M.add_rule = function (rule)
table.insert(M.config.rules, rule)
end

M.remove_rule = function (pair)
local tbl={}
for _, r in pairs(M.config.rules) do
if r.start_pair ~= pair then
table.insert(tbl, r)
end
end
M.config.rules = tbl
end

M.add_rules = function (rules)
for _, rule in pairs(rules) do
table.insert(M.config.rules, rule)
Expand Down Expand Up @@ -69,24 +79,25 @@ M.on_attach = function(bufnr)
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
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
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
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
elseif rule.is_endwise == false then
enable_insert_auto = true
end
end
end

if enable_insert_auto then
api.nvim_exec(string.format([[
augroup autopairs_insert_%d
Expand Down Expand Up @@ -189,7 +200,7 @@ M.autopairs_map = function(bufnr, char)
and rule:can_pair(cond_opt)
then
local end_pair = rule:get_end_pair(cond_opt)
if add_char == 0 then char = ""end
if add_char == 0 then char = "" end
return utils.esc(char .. end_pair .. utils.repeat_key(utils.key.left,#end_pair))
end
end
Expand Down

0 comments on commit bc18313

Please sign in to comment.