fix: pairs on after quote

This commit is contained in:
windwp 2021-05-24 12:04:37 +07:00
parent 150697f4a5
commit d616b72b05
3 changed files with 21 additions and 9 deletions

View File

@ -14,11 +14,11 @@ require('nvim-autopairs').setup()
## Default values ## Default values
``` lua ``` lua
local disable_filetype = { "TelescopePrompt" } local disable_filetype = { "TelescopePrompt" }
local ignored_next_char = string.gsub([[ [%w%%%'%[%"%.] ]],"%s+", "") local ignored_next_char = string.gsub([[ [%w%%%'%[%"%.] ]],"%s+", "")
local enable_moveright = true local enable_moveright = true
local check_ts = false, local enable_afterquote = true -- add bracket pairs after quote
local check_ts = false
``` ```
@ -251,7 +251,7 @@ Before Input After
require('nvim-autopairs').get_rule('"') -- get rule " then modify it require('nvim-autopairs').get_rule('"') -- get rule " then modify it
``` ```
### autotag html and tsx ### autotag html and tsx
[autotag](https://github.com/windwp/nvim-ts-autotag) [autotag](https://github.com/windwp/nvim-ts-autotag)

View File

@ -400,7 +400,7 @@ M.autopairs_cr = function(bufnr)
return utils.esc('<cr>') return utils.esc('<cr>')
end end
--- map to auto add pairs on close quote (|"aaaaa" => (|"aaaaaa") --- add bracket pairs after quote (|"aaaaa" => (|"aaaaaa")
M.autopairs_afterquote = function(line, key_char) M.autopairs_afterquote = function(line, key_char)
if M.config.enable_afterquote then if M.config.enable_afterquote then
line = line or utils.text_get_current_line(0) line = line or utils.text_get_current_line(0)
@ -417,7 +417,7 @@ M.autopairs_afterquote = function(line, key_char)
local new_text = line:sub(0, i) .. rule.end_pair .. line:sub(i + 1,#line) local new_text = line:sub(0, i) .. rule.end_pair .. line:sub(i + 1,#line)
M.state.expr_quote = new_text M.state.expr_quote = new_text
local append = "a"; local append = "a";
if col > 2 then append = "la" end if col > 0 then append = "la" end
return utils.esc('<esc><cmd>lua MPairs.autopairs_closequote_expr()<cr>' .. append) return utils.esc('<esc><cmd>lua MPairs.autopairs_closequote_expr()<cr>' .. append)
end end
end end

View File

@ -2,6 +2,9 @@ local npairs = require('nvim-autopairs')
_G.npairs = npairs _G.npairs = npairs
npairs.setup({
enable_afterquote = true,
})
local data = { local data = {
{ {
name = 'add bracket after quote ', name = 'add bracket after quote ',
@ -9,8 +12,8 @@ local data = {
filetype = 'lua', filetype = 'lua',
linenr = 5, linenr = 5,
key = [[(]], key = [[(]],
before = [[const abc=|"visudsa" ]], before = [[const abc=|"test" ]],
after = [[const abc=(|"visudsa") ]], after = [[const abc=(|"test") ]],
}, },
{ {
name = 'add bracket after quote ', name = 'add bracket after quote ',
@ -18,10 +21,19 @@ local data = {
filetype = 'lua', filetype = 'lua',
linenr = 5, linenr = 5,
key = [[(]], key = [[(]],
before = [[|"visudsa" ]], before = [[|"test" ]],
after = [[(|"visudsa") ]], after = [[(|"test") ]],
}, },
{
name = 'add bracket after quote ',
filepath = './tests/endwise/init.lua',
filetype = 'lua',
linenr = 5,
key = [[{]],
before = [[(|"test") ]],
after = [[({|"test"}) ]],
},
{ {
name = 'add bracket after quote ', name = 'add bracket after quote ',
filepath = './tests/endwise/init.lua', filepath = './tests/endwise/init.lua',