mirror of
https://github.com/ilyakooo0/nvim-autopairs.git
synced 2024-09-11 07:15:44 +03:00
add pair afterquote (#59)
* add autopairs close end quote * add pairs after quote
This commit is contained in:
parent
71d8397621
commit
150697f4a5
@ -16,6 +16,7 @@ local default = {
|
||||
ignored_next_char = string.gsub([[ [%w%%%'%[%"%.] ]],"%s+", ""),
|
||||
check_ts = false,
|
||||
enable_moveright = true,
|
||||
enable_afterquote = true,
|
||||
ts_config = {
|
||||
lua = {'string', 'source'},
|
||||
javascript = {'string', 'template_string'}
|
||||
@ -47,7 +48,6 @@ M.setup = function(opt)
|
||||
else
|
||||
print("you need to install treesitter")
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
M.force_attach()
|
||||
@ -127,7 +127,7 @@ M.on_attach = function(bufnr)
|
||||
if is_disable() then return end
|
||||
bufnr = bufnr or api.nvim_get_current_buf()
|
||||
|
||||
local rules = {};
|
||||
local rules = {}
|
||||
for _, rule in pairs(M.config.rules) do
|
||||
if utils.check_filetype(rule.filetypes,vim.bo.filetype) then
|
||||
table.insert(rules, rule)
|
||||
@ -198,7 +198,6 @@ M.autopairs_bs = function(bufnr)
|
||||
local _, col = utils.get_cursor()
|
||||
for _, rule in pairs(M.state.rules) do
|
||||
if rule.start_pair then
|
||||
|
||||
local prev_char, next_char = utils.text_cusor_line(
|
||||
line,
|
||||
col,
|
||||
@ -231,7 +230,6 @@ M.autopairs_bs = function(bufnr)
|
||||
return utils.esc(utils.key.bs)
|
||||
end
|
||||
|
||||
|
||||
M.autopairs_map = function(bufnr, char)
|
||||
if is_disable() then return char end
|
||||
local line = utils.text_get_current_line(bufnr)
|
||||
@ -251,7 +249,7 @@ M.autopairs_map = function(bufnr, char)
|
||||
-- log.debug("new_text:[" .. new_text .. "]")
|
||||
local prev_char, next_char = utils.text_cusor_line(
|
||||
new_text,
|
||||
col+ add_char,
|
||||
col + add_char,
|
||||
#rule.start_pair,
|
||||
#rule.end_pair, rule.is_regex
|
||||
)
|
||||
@ -271,10 +269,10 @@ M.autopairs_map = function(bufnr, char)
|
||||
-- log.debug('next_char' .. next_char)
|
||||
if
|
||||
next_char == rule.end_pair
|
||||
and rule.is_regex==false
|
||||
and rule.is_regex == false
|
||||
and rule:can_move(cond_opt)
|
||||
then
|
||||
return utils.esc( utils.key.right)
|
||||
return utils.esc(utils.key.right)
|
||||
end
|
||||
if
|
||||
utils.is_equal(rule.start_pair, prev_char, rule.is_regex)
|
||||
@ -286,7 +284,7 @@ M.autopairs_map = function(bufnr, char)
|
||||
end
|
||||
end
|
||||
end
|
||||
return char
|
||||
return M.autopairs_afterquote(new_text, char)
|
||||
end
|
||||
|
||||
M.autopairs_insert = function(bufnr, char)
|
||||
@ -355,7 +353,8 @@ M.autopairs_cr = function(bufnr)
|
||||
line,
|
||||
col,
|
||||
#rule.start_pair,
|
||||
#rule.end_pair, rule.is_regex
|
||||
#rule.end_pair,
|
||||
rule.is_regex
|
||||
)
|
||||
-- log.debug('prev_char' .. rule.start_pair)
|
||||
-- log.debug('prev_char' .. prev_char)
|
||||
@ -390,15 +389,48 @@ M.autopairs_cr = function(bufnr)
|
||||
rule = rule,
|
||||
prev_char = prev_char,
|
||||
next_char = next_char,
|
||||
line = line
|
||||
line = line,
|
||||
})
|
||||
then
|
||||
log.debug('do_cr')
|
||||
return utils.esc("<cr><c-o>O")
|
||||
return utils.esc('<cr><c-o>O')
|
||||
end
|
||||
end
|
||||
end
|
||||
return utils.esc("<cr>")
|
||||
return utils.esc('<cr>')
|
||||
end
|
||||
|
||||
--- map to auto add pairs on close quote (|"aaaaa" => (|"aaaaaa")
|
||||
M.autopairs_afterquote = function(line, key_char)
|
||||
if M.config.enable_afterquote then
|
||||
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, 1, false)
|
||||
if utils.is_bracket(prev_char) and utils.is_quote(next_char) then
|
||||
local is_prev_slash = false
|
||||
for i = col + 2 + #next_char, #line, 1 do
|
||||
local char = line:sub(i, i + #next_char -1)
|
||||
local char_end = line:sub(i + 1, i + #next_char )
|
||||
if not is_prev_slash and char == next_char then
|
||||
for _, rule in pairs(M.state.rules) do
|
||||
if rule.start_pair == prev_char and char_end ~= rule.end_pair then
|
||||
local new_text = line:sub(0, i) .. rule.end_pair .. line:sub(i + 1,#line)
|
||||
M.state.expr_quote = new_text
|
||||
local append = "a";
|
||||
if col > 2 then append = "la" end
|
||||
return utils.esc('<esc><cmd>lua MPairs.autopairs_closequote_expr()<cr>' .. append)
|
||||
end
|
||||
end
|
||||
end
|
||||
is_prev_slash = char == '\\'
|
||||
end
|
||||
end
|
||||
end
|
||||
return utils.esc(key_char)
|
||||
end
|
||||
|
||||
M.autopairs_closequote_expr=function ()
|
||||
vim.fn.setline('.', M.state.expr_quote)
|
||||
end
|
||||
|
||||
M.check_break_line_char = function()
|
||||
|
@ -111,17 +111,12 @@ M.repeat_key = function(key, num)
|
||||
end
|
||||
return text
|
||||
end
|
||||
|
||||
-- function M.text_prev_next(line, col, prev_count, next_char)
|
||||
-- prev_count = prev_count or 1
|
||||
-- next_char = next_char or prev_count
|
||||
|
||||
-- if line then
|
||||
-- local prev = string.sub(line, math.max(col - prev_count + 1, 0), col)
|
||||
-- local next = string.sub(line, col + 1, math.min(col + next_char, #line + 1))
|
||||
-- return prev, next
|
||||
-- end
|
||||
-- end
|
||||
--- cut text from position with number character
|
||||
---@param line string text
|
||||
---@param col number position of text
|
||||
---@param prev_count number number char previous
|
||||
---@param next_count number number char next
|
||||
---@param is_regex boolean if it is regex then will cut all
|
||||
M.text_cusor_line = function(line, col, prev_count, next_count, is_regex)
|
||||
if is_regex then
|
||||
prev_count = col
|
||||
|
62
tests/afterquote_spec.lua
Normal file
62
tests/afterquote_spec.lua
Normal file
@ -0,0 +1,62 @@
|
||||
local npairs = require('nvim-autopairs')
|
||||
|
||||
_G.npairs = npairs
|
||||
|
||||
local data = {
|
||||
{
|
||||
name = 'add bracket after quote ',
|
||||
filepath = './tests/endwise/init.lua',
|
||||
filetype = 'lua',
|
||||
linenr = 5,
|
||||
key = [[(]],
|
||||
before = [[const abc=|"visudsa" ]],
|
||||
after = [[const abc=(|"visudsa") ]],
|
||||
},
|
||||
{
|
||||
name = 'add bracket after quote ',
|
||||
filepath = './tests/endwise/init.lua',
|
||||
filetype = 'lua',
|
||||
linenr = 5,
|
||||
key = [[(]],
|
||||
before = [[|"visudsa" ]],
|
||||
after = [[(|"visudsa") ]],
|
||||
},
|
||||
|
||||
{
|
||||
name = 'add bracket after quote ',
|
||||
filepath = './tests/endwise/init.lua',
|
||||
filetype = 'lua',
|
||||
linenr = 5,
|
||||
key = [[(]],
|
||||
before = [[const abc=|"visu\"dsa" ]],
|
||||
after = [[const abc=(|"visu\"dsa") ]],
|
||||
},
|
||||
{
|
||||
name = 'not add on exist quote',
|
||||
filepath = './tests/endwise/init.lua',
|
||||
filetype = 'lua',
|
||||
linenr = 5,
|
||||
key = [[(]],
|
||||
before = [[const abc=|"visu\"dsa") ]],
|
||||
after = [[const abc=(|"visu\"dsa") ]],
|
||||
},
|
||||
|
||||
{
|
||||
name = 'test add close quote on match',
|
||||
filepath = './tests/endwise/init.lua',
|
||||
filetype = 'lua',
|
||||
linenr = '5',
|
||||
key = [[(]],
|
||||
before = [[const abc=|"visu\"dsa" ]],
|
||||
after = [[const abc=(|"visu\"dsa") ]],
|
||||
},
|
||||
}
|
||||
|
||||
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)
|
@ -9,6 +9,7 @@ local utils = require('nvim-autopairs.utils')
|
||||
_G.npairs = npairs;
|
||||
local eq=_G.eq
|
||||
|
||||
|
||||
npairs.add_rules({
|
||||
Rule("u%d%d%d%d$", "number", "lua")
|
||||
:use_regex(true),
|
||||
@ -58,6 +59,13 @@ local data = {
|
||||
before = [[{|} ]],
|
||||
after = [[{{|}} ]]
|
||||
},
|
||||
{
|
||||
name = "test single quote ",
|
||||
filetype = "lua",
|
||||
key = "'",
|
||||
before = [[data,|) ]],
|
||||
after = [[data,'|') ]]
|
||||
},
|
||||
{
|
||||
name = "add normal bracket" ,
|
||||
key = [[(]],
|
||||
@ -71,7 +79,6 @@ local data = {
|
||||
after = [[aa"|" aa]]
|
||||
},
|
||||
{
|
||||
-- only = true,
|
||||
name = "add python quote" ,
|
||||
filetype = "python",
|
||||
key = [["]],
|
||||
@ -328,7 +335,6 @@ local function Test(test_data)
|
||||
npairs.on_attach(vim.api.nvim_get_current_buf())
|
||||
vim.fn.setline(line , before)
|
||||
vim.fn.setpos('.' ,{0, line, p_before , 0})
|
||||
-- log.debug("insert: " .. value.key)
|
||||
helpers.insert(value.key)
|
||||
vim.wait(10)
|
||||
helpers.feed("<esc>")
|
||||
|
@ -1,4 +1,5 @@
|
||||
local utils = require('nvim-autopairs.utils')
|
||||
local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils')
|
||||
local log = require('nvim-autopairs._log')
|
||||
local api = vim.api
|
||||
|
||||
@ -53,15 +54,15 @@ _G.Test_withfile = function(test_data, cb)
|
||||
if not vim.tbl_islist(value.before) then
|
||||
value.before = {value.before}
|
||||
end
|
||||
local numlnr = 0
|
||||
for _, text in pairs(value.before) do
|
||||
for index, text in pairs(value.before) do
|
||||
local txt = string.gsub(text, '%|' , "")
|
||||
table.insert(text_before, txt )
|
||||
if string.match( text, "%|") then
|
||||
pos_before.colnr = string.find(text, '%|')
|
||||
pos_before.linenr = pos_before.linenr + numlnr
|
||||
if string.find(text,'%|') then
|
||||
pos_before.colnr = string.find(text, '%|')
|
||||
pos_before.linenr = value.linenr + index-1
|
||||
end
|
||||
end
|
||||
numlnr = numlnr + 1
|
||||
end
|
||||
local after = string.gsub(value.after, '%|' , "")
|
||||
local p_after = string.find(value.after , '%|')
|
||||
@ -74,7 +75,7 @@ _G.Test_withfile = function(test_data, cb)
|
||||
vim.bo.filetype = value.filetype
|
||||
vim.cmd(":e")
|
||||
end
|
||||
vim.api.nvim_buf_set_lines(0, pos_before.linenr -1, pos_before.linenr +#text_before, false, text_before)
|
||||
vim.api.nvim_buf_set_lines(0, value.linenr -1, value.linenr +#text_before, false, text_before)
|
||||
vim.fn.cursor(pos_before.linenr, pos_before.colnr)
|
||||
log.debug("insert:"..value.key)
|
||||
helpers.insert(value.key)
|
||||
@ -85,6 +86,7 @@ _G.Test_withfile = function(test_data, cb)
|
||||
local pos = vim.fn.getpos('.')
|
||||
eq(pos_before.linenr + 1, pos[2], '\n\n breakline error:' .. value.name .. "\n")
|
||||
eq(after, result , "\n\n text error: " .. value.name .. "\n")
|
||||
|
||||
else
|
||||
local result = vim.fn.getline(pos_before.linenr)
|
||||
local pos = vim.fn.getpos('.')
|
||||
@ -98,3 +100,22 @@ _G.Test_withfile = function(test_data, cb)
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
_G.dump_node = function(node)
|
||||
local text=ts_utils.get_node_text(node)
|
||||
for _, txt in pairs(text) do
|
||||
print(txt)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
_G.dump_node_text = function(target)
|
||||
for node in target:iter_children() do
|
||||
local node_type = node:type()
|
||||
local text = ts_utils.get_node_text(node)
|
||||
log.debug("type:" .. node_type .. " ")
|
||||
log.debug(text)
|
||||
end
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user