test fastwrap not finish

This commit is contained in:
windwp 2021-06-19 13:34:00 +07:00
parent 4b218c4e38
commit 86050e29dc
2 changed files with 119 additions and 1 deletions

View File

@ -18,6 +18,8 @@ local default = {
enable_moveright = true,
enable_afterquote = true,
enable_check_bracket_line = true,
enable_fastwrap_key = "<M-e>",
fastwrap_previous_char = string.gsub([[ [%'%"%)%>%]%)%}] ]],"%s+", ""),
ts_config = {
lua = {'string', 'source'},
javascript = {'string', 'template_string'}
@ -186,6 +188,14 @@ M.on_attach = function(bufnr)
augroup end ]],
bufnr, bufnr, bufnr), false)
end
if M.config.enable_fastwrap_key then
api.nvim_buf_set_keymap(bufnr,"i",
M.config.enable_fastwrap_key,
"<cmd>lua require('nvim-autopairs').autopairs_fastwrap()<cr>",
{noremap = true})
end
api.nvim_buf_set_keymap(bufnr,
'i',
"<bs>",
@ -450,7 +460,61 @@ M.autopairs_afterquote = function(line, key_char)
return utils.esc(key_char)
end
M.autopairs_closequote_expr=function ()
M.autopairs_fastwrap = function(line)
line = line or utils.text_get_current_line(0)
local _, col = utils.get_cursor()
local prev_char, next_char = utils.text_cusor_line(line, col, 1, 1, false)
local end_pair = ''
if
utils.is_bracket(prev_char)
and not utils.is_in_quote(line, col, next_char)
then
for _, rule in pairs(M.state.rules) do
if rule.start_pair == prev_char then
end_pair = rule.end_pair
end
end
if end_pair == '' then return end
local is_prev_slash = false
local end_pair_pos = 0
local target_pos = 0
for i = col + 3, #line, 1 do
local char = line:sub(i, i )
if not is_prev_slash and char == end_pair then
end_pair_pos = i
end
if string.match(char, M.config.fastwrap_previous_char) then
if target_pos == 0 then
target_pos = i + 1
elseif end_pair_pos > 0 and i > end_pair_pos then
target_pos = i + 1
else
target_pos = 0
end
end
-- if char == ' ' then
-- if target_pos == 0 then
-- target_pos = i -1
-- elseif end_pair_pos > 0 and i > end_pair_pos then
-- target_pos = i -1
-- break
-- end
-- end
is_prev_slash = char == '\\'
end
if target_pos > 0 then
if end_pair_pos > 0 then
line = line:sub(1, end_pair_pos -1) .. line:sub(end_pair_pos + 1,#line)
end
line = line:sub(1, target_pos -1) .. end_pair .. line:sub(target_pos,#line)
else
line = line .. end_pair
end
vim.fn.setline('.', line)
end
end
M.autopairs_closequote_expr = function ()
vim.fn.setline('.', M.state.expr_quote)
end

54
tests/fastwrap_spec.lua Normal file
View File

@ -0,0 +1,54 @@
local npairs = require('nvim-autopairs')
_G.npairs = npairs
npairs.setup({
enable_afterquote = true,
})
local data = {
{
name = 'move end wise after quote ',
filepath = './tests/endwise/init.lua',
filetype = 'lua',
linenr = 5,
key = [[<M-e>]],
before = [[const abc=(|"test",data ]],
after = [[const abc=(|"test"),data ]],
},
{
name = 'move end wise after quote ',
filepath = './tests/endwise/init.lua',
filetype = 'lua',
linenr = 5,
key = [[<M-e>]],
before = [[const abc=(|"test"),data ]],
after = [[const abc=(|"test",data) ]],
},
{
name = 'move end wise after quote ',
filepath = './tests/endwise/init.lua',
filetype = 'lua',
linenr = 5,
key = [[<M-e>]],
before = [[const abc=(|"test",data),dadas ]],
after = [[const abc=(|"test",data,dadas) ]],
},
{
name = 'move end wise after quote ',
filepath = './tests/endwise/init.lua',
filetype = 'lua',
linenr = 5,
key = [[<M-e>]],
before = [[Plug {(|'dsfdsa',) on = 'aaa'} ]],
after = [[Plug {('dsfdsa', on = 'aaa')} ]],
},
}
local run_data = _G.Test_filter(data)
local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils')
_G.TU = ts_utils
describe('[endwise tag]', function()
_G.Test_withfile(run_data, {})
end)