From 86050e29dc9815ab82fda299ef27001a46b2d81a Mon Sep 17 00:00:00 2001 From: windwp Date: Sat, 19 Jun 2021 13:34:00 +0700 Subject: [PATCH] test fastwrap not finish --- lua/nvim-autopairs.lua | 66 ++++++++++++++++++++++++++++++++++++++++- tests/fastwrap_spec.lua | 54 +++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 tests/fastwrap_spec.lua diff --git a/lua/nvim-autopairs.lua b/lua/nvim-autopairs.lua index 8d87113..958990c 100644 --- a/lua/nvim-autopairs.lua +++ b/lua/nvim-autopairs.lua @@ -18,6 +18,8 @@ local default = { enable_moveright = true, enable_afterquote = true, enable_check_bracket_line = true, + enable_fastwrap_key = "", + 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, + "lua require('nvim-autopairs').autopairs_fastwrap()", + {noremap = true}) + end + api.nvim_buf_set_keymap(bufnr, 'i', "", @@ -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 diff --git a/tests/fastwrap_spec.lua b/tests/fastwrap_spec.lua new file mode 100644 index 0000000..a08e75d --- /dev/null +++ b/tests/fastwrap_spec.lua @@ -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 = [[]], + 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 = [[]], + 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 = [[]], + 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 = [[]], + 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)