add spec undo

This commit is contained in:
zztrieuzz 2021-10-20 08:36:54 +07:00
parent f9d8914a7c
commit f0c10f5265
4 changed files with 173 additions and 93 deletions

View File

@ -67,7 +67,7 @@ local data = {
name = 'test add close quote on match',
filepath = './tests/endwise/init.lua',
filetype = 'lua',
linenr = '5',
linenr = 5,
key = [[(]],
before = [[const abc=|"visu\"dsa" ]],
after = [[const abc=(|"visu\"dsa") ]],
@ -77,28 +77,28 @@ local data = {
name = 'test add close quote inside string',
filepath = './tests/endwise/init.lua',
filetype = 'lua',
linenr = '5',
linenr = 5,
key = [[(]],
before = [[const abc="visu|"dsa"" ]],
after = [[const abc="visu(|"dsa"" ]],
after = [[const abc="visu(|"dsa"" ]],
},
{
name = 'not add bracket with quote have comma',
filepath = './tests/endwise/init.lua',
filetype = 'lua',
linenr = '5',
linenr = 5,
key = [[(]],
before = [[|"data", abcdef]],
after = [[(|"data", abcdef]],
after = [[(|"data", abcdef]],
},
{
name = 'not add bracket with quote have comma',
filepath = './tests/endwise/init.lua',
filetype = 'lua',
linenr = '5',
linenr = 5,
key = [[(]],
before = [[|"data", "abcdef"]],
after = [[(|"data", "abcdef"]],
after = { [[(|"data", "abcdef"]] },
},
}
@ -108,5 +108,6 @@ local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils')
_G.TU = ts_utils
describe('[afterquote tag]', function()
_G.Test_withfile(run_data, {})
_G.Test_withfile(run_data, {
})
end)

View File

@ -1,56 +1,71 @@
local npairs = require('nvim-autopairs')
local ts = require 'nvim-treesitter.configs'
local ts = require('nvim-treesitter.configs')
local log = require('nvim-autopairs._log')
ts.setup {
ensure_installed = {'lua'},
highlight = {enable = true},
}
_G.npairs = npairs;
vim.api.nvim_set_keymap('i' , '<CR>','v:lua.npairs.check_break_line_char()', {expr = true , noremap = true})
ts.setup({
ensure_installed = { 'lua' },
highlight = { enable = true },
})
_G.npairs = npairs
vim.api.nvim_set_keymap(
'i',
'<CR>',
'v:lua.npairs.autopairs_cr()',
{ expr = true, noremap = true }
)
local data = {
{
name = "lua function add endwise" ,
name = 'lua function add endwise',
filepath = './tests/endwise/init.lua',
filetype = "lua",
linenr = 5,
key = [[<cr>]],
before = [[function a()| ]],
after = [[ end ]]
filetype = 'lua',
linenr = 5,
key = [[<cr>]],
before = [[function a()| ]],
after = {
[[function a() ]],
[[| ]],
[[ end ]],
},
},
{
name = "lua function add endwise" ,
name = 'lua function add endwise',
filepath = './tests/endwise/init.lua',
filetype = "lua",
linenr = 5,
key = [[<cr>]],
before = [[function a()|x ab ]],
after = [[]]
filetype = 'lua',
linenr = 5,
key = [[<cr>]],
before = [[function a()|x ab ]],
after = {
[[function a() ]],
[[|x ab]],
},
},
{
name = "add if endwise" ,
name = 'add if endwise',
filepath = './tests/endwise/init.lua',
filetype = "lua",
linenr = 5,
key = [[<cr>]],
before = [[if data== 'fdsafdsa' then| ]],
after = [[end ]]
filetype = 'lua',
linenr = 5,
key = [[<cr>]],
before = [[if data== 'fdsafdsa' then| ]],
after = {
[[if data== 'fdsafdsa' then ]],
[[|]],
[[end ]],
},
},
{
name = 'undo on<cr> key',
filepath = './tests/endwise/init.lua',
filetype = 'lua',
linenr = 5,
key = [[{<cr><esc>uu]],
before = [[local abc = | ]],
after = {
[[local abc = {|} ]],
[[]],
[[]],
},
},
-- {
-- name = "don't add endwise on match rule" ,
-- filepath = './tests/endwise/init.lua',
-- filetype = "lua",
-- linenr = 5,
-- key = [[<cr>]],
-- before ={
-- [[if data == 'xdsad' then| ]],
-- [[ local abde='das' ]],
-- [[end]]
-- },
-- after = [[ local abde='das' ]]
-- },
}
local run_data = _G.Test_filter(data)
@ -58,12 +73,14 @@ 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,{
_G.Test_withfile(run_data, {
-- need to understand this ??? new line make change cursor zzz
cursor_add = 1,
before = function(value)
npairs.clear_rules()
npairs.add_rules(require('nvim-autopairs.rules.endwise-'..value.filetype))
end
npairs.add_rules(
require('nvim-autopairs.rules.endwise-' .. value.filetype)
)
end,
})
end)

View File

@ -508,6 +508,22 @@ local data = {
before = [[a | ]],
after = [[a x| ]]
},
{
filetype = 'vim',
name='undo on quote',
key = [[{123<esc>u]],
end_cursor=12,
before = [[local abc=| ]],
after = [[local abc={|} ]]
},
{
filetype = 'vim',
name='undo on bracket',
key = [['123<esc>u]],
end_cursor=12,
before = [[local abc=| ]],
after = [[local abc='|' ]]
}
}
local run_data = {}

View File

@ -14,22 +14,24 @@ function helpers.insert(text)
helpers.feed('i' .. text, 'x')
end
utils.insert_char = function(text)
api.nvim_put({text}, "c", true, true)
api.nvim_put({ text }, 'c', true, true)
end
utils.feed = function(text,num)
utils.feed = function(text, num)
local result = ''
for _ = 1, num, 1 do
result = result .. text
end
api.nvim_feedkeys (api.nvim_replace_termcodes(
result, true, false, true),
"x", true)
api.nvim_feedkeys(
api.nvim_replace_termcodes(result, true, false, true),
'x',
true
)
end
_G.eq = assert.are.same
_G.Test_filter = function (data)
_G.Test_filter = function(data)
local run_data = {}
for _, value in pairs(data) do
if value.only == true then
@ -37,85 +39,129 @@ _G.Test_filter = function (data)
break
end
end
if #run_data == 0 then run_data = data end
if #run_data == 0 then
run_data = data
end
return run_data
end
local compare_text = function(linenr, text_after, name, cursor_add)
cursor_add = cursor_add or 0
print(cursor_add)
local new_text = vim.api.nvim_buf_get_lines(
0,
linenr - 1,
linenr + #text_after,
true
)
if #new_text ~= #text_after + 1 then
eq(#new_text, #text_after, '\n\n text error: ' .. name .. '\n')
return false
end
for i = 1, #text_after, 1 do
local t = string.gsub(text_after[i], '%|', '')
if t:gsub('%s+$', '') ~= new_text[i]:gsub('%s+$', '') then
eq(t, new_text[i], '\n\n text error: ' .. name .. '\n')
end
local p_after = string.find(text_after[i], '%|')
if p_after then
-- log.debug(p_after)
local row, col = utils.get_cursor()
eq(row, linenr + i - 2, '\n\n cursor row error: ' .. name .. '\n')
p_after = p_after + cursor_add
eq(col, p_after -2, '\n\n cursor column error : ' .. name .. '\n')
end
end
return true
end
_G.Test_withfile = function(test_data, cb)
for _, value in pairs(test_data) do
it("test "..value.name, function()
it('test ' .. value.name, function()
local text_before = {}
local pos_before = {
linenr = value.linenr,
colnr = 0
colnr = 0,
}
if not vim.tbl_islist(value.before) then
value.before = {value.before}
value.before = { value.before }
end
for index, text in pairs(value.before) do
local txt = string.gsub(text, '%|' , "")
table.insert(text_before, txt )
if string.match( text, "%|") then
if string.find(text,'%|') then
local txt = string.gsub(text, '%|', '')
table.insert(text_before, txt)
if string.match(text, '%|') then
if string.find(text, '%|') then
pos_before.colnr = string.find(text, '%|')
pos_before.linenr = value.linenr + index-1
pos_before.linenr = value.linenr + index - 1
end
end
end
local after = string.gsub(value.after, '%|' , "")
local p_after = string.find(value.after , '%|')
if not vim.tbl_islist(value.after) then
value.after = { value.after }
end
vim.bo.filetype = value.filetype
if vim.fn.filereadable(vim.fn.expand(value.filepath)) == 1 then
vim.cmd(":bd!")
if cb.before then cb.before(value) end
vim.cmd(":e " .. value.filepath)
vim.cmd(':bd!')
if cb.before then
cb.before(value)
end
vim.cmd(':e ' .. value.filepath)
if value.filetype then
vim.bo.filetype = value.filetype
vim.cmd(":e")
vim.cmd(':e')
end
vim.api.nvim_buf_set_lines(0, value.linenr -1, value.linenr +#text_before, false, text_before)
vim.api.nvim_buf_set_lines(
0,
value.linenr - 1,
value.linenr + #text_before,
true,
text_before
)
---@diagnostic disable-next-line: redundant-parameter
vim.fn.cursor(pos_before.linenr, pos_before.colnr)
log.debug("insert:"..value.key)
log.debug('insert:' .. value.key)
helpers.insert(value.key)
vim.wait(10)
helpers.feed("<esc>")
helpers.feed('<esc>')
if value.key == '<cr>' then
local result = vim.fn.getline(pos_before.linenr + 2)
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")
local row, col = utils.get_cursor()
compare_text(
value.linenr,
value.after,
value.name,
cb.cursor_add
)
else
local result = vim.fn.getline(pos_before.linenr)
local pos = vim.fn.getpos('.')
eq(after, result , "\n\n text error: " .. value.name .. "\n")
eq(p_after, pos[3] + 1, "\n\n pos error: " .. value.name .. "\n")
compare_text(
value.linenr,
value.after,
value.name,
cb.cursor_add
)
end
if cb.after then
cb.after(value)
end
if cb.after then cb.after(value) end
else
eq(false, true, "\n\n file not exist " .. value.filepath .. "\n")
eq(false, true, '\n\n file not exist ' .. value.filepath .. '\n')
end
end)
end
end
_G.dump_node = function(node)
local text=ts_utils.get_node_text(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('type:' .. node_type .. ' ')
log.debug(text)
end
end