fix vue file and improve tsx

This commit is contained in:
windwp 2021-03-10 06:41:24 +07:00
parent 5bd6b70563
commit f4417a71f5
5 changed files with 70 additions and 55 deletions

View File

@ -11,7 +11,9 @@ M.tbl_skipTag = {
'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr','menuitem'
}
M.test = false
M.test = false
M.enableRename = true
M.enableClose = true
M.setup = function (opts)
opts = opts or {}
@ -40,12 +42,14 @@ local function isJsX()
end
M.on_file_type = function ()
if is_in_table(M.tbl_filetypes,vim.bo.filetype) then
vim.cmd[[inoremap <silent> <buffer> > ><c-o>:lua require('nvim-ts-autotag').closeTag()<CR>]]
vim.cmd[[inoremap <silent> <buffer> > <c-o>:lua require('nvim-ts-autotag').closeTag()<CR>]]
local bufnr = vim.api.nvim_get_current_buf()
vim.cmd("augroup nvim_ts_xmltag_" .. bufnr)
vim.cmd[[autocmd!]]
vim.cmd[[autocmd InsertLeave <buffer> call v:lua.require('nvim-ts-autotag').renameTag() ]]
vim.cmd[[augroup end]]
if M.enableRename==true then
vim.cmd("augroup nvim_ts_xmltag_" .. bufnr)
vim.cmd[[autocmd!]]
vim.cmd[[autocmd InsertLeave <buffer> call v:lua.require('nvim-ts-autotag').renameTag() ]]
vim.cmd[[augroup end]]
end
end
end
local function find_child_match(opts)
@ -101,13 +105,6 @@ local function find_tag_node(start_tag_pattern, name_tag_pattern,skip_tag_patter
pattern = start_tag_pattern,
skip_tag_pattern = skip_tag_pattern
})
if(M.test and start_tag_node == nil) then
start_tag_node = find_child_match({
target = cur_node,
pattern = start_tag_pattern,
skip_tag_pattern = skip_tag_pattern
})
end
if start_tag_node== nil then return nil end
local tbl_name_pattern = vim.split(name_tag_pattern, '>')
local name_node = start_tag_node
@ -150,9 +147,12 @@ M.closeTag = function ()
end
local tag_node = find_tag_node(start_tag_pattern, name_tag_pattern,skip_tag_pattern)
local tag_name = get_tag_name(tag_node)
if tag_name ~= nil and not is_in_table(M.tbl_skipTag,tag_name) then
vim.cmd(string.format([[normal! a</%s>]],tag_name))
-- check if already have exist tag
if tag_name ~= nil and not is_in_table(M.tbl_skipTag, tag_name) then
vim.cmd(string.format([[normal! a></%s>]],tag_name))
vim.cmd[[normal! T>]]
else
vim.cmd(string.format([[normal! a>]],tag_name))
end
end

View File

@ -8,12 +8,6 @@
</head>
<body>
<div></div>
<lala>
</lala>
</body>
</html>

View File

@ -18,13 +18,13 @@ end
local data = {
{
name = "html auto close tag" ,
name = "html close tag" ,
filepath = './sample/index.html',
filetype = "html",
linenr = 10,
key = [[>]],
before = [[<div| ]],
after = [[<div> </div>]]
before = [[<div|]],
after = [[<div></div>]]
},
{
name = "html not close on input tag" ,
@ -44,17 +44,27 @@ local data = {
before = [[<div class="aa|"> </div> ]],
after = [[<div class="aa>|"> </div> ]]
},
{
only=true,
name = "html not close on exist tag" ,
filepath = './sample/index.html',
filetype = "html",
linenr = 10,
key = [[>]],
before = [[<div| </div]],
after = [[<div> </div>]]
},
{
name = "typescriptreact auto close tag" ,
filepath = './sample/index.tsx',
filetype = "typescriptreact",
linenr = 12,
key = [[>]],
before = [[<Img| ]],
after = [[<Img>| </Img>]]
before = [[<Img|]],
after = [[<Img>|</Img>]]
},
{
name = "typescriptreact auto close tag" ,
name = "typescriptreact don't close closing tag" ,
filepath = './sample/index.tsx',
filetype = "typescriptreact",
linenr = 12,
@ -63,13 +73,13 @@ local data = {
after = [[<button className="btn " onClick={()}> </button>| ]]
},
{
name = "typescriptreact not close on script" ,
name = "typescriptreact not close on expresion" ,
filepath = './sample/index.tsx',
filetype = "typescriptreact",
linenr = 6,
key = [[>]],
before = [[const data:Array<string| ]],
after = [[const data:Array<string> ]]
before = [[<button className="btn " onClick={(|)}> </button> ]],
after = [[<button className="btn " onClick={(>|)}> </button> ]]
},
{
name = "typescriptreact not close on script" ,
@ -80,25 +90,34 @@ local data = {
before = [[const data:Array<string| ]],
after = [[const data:Array<string> ]]
},
-- {
-- only = true,
-- name = "vue auto close tag" ,
-- filepath = './sample/index.vue',
-- filetype = "vue",
-- linenr = 4,
-- key = [[>]],
-- before = [[<Img| ]],
-- after = [[<Img>| </Img>]]
-- },
-- {
-- name = "vue not close on script",
-- filepath = './sample/index.vue',
-- filetype = "vue",
-- linenr = 12,
-- key = [[>]],
-- before = [[const data:Array<string| ]],
-- after = [[const data:Array<string> ]]
-- },
{
name = "typescriptreact not close on script" ,
filepath = './sample/index.tsx',
filetype = "typescriptreact",
linenr = 6,
key = [[>]],
before = [[{(card.data | 0) && <div></div>}]],
after = [[{(card.data >| 0) && <div></div>}]]
},
{
name = "vue auto close tag" ,
filepath = './sample/index.vue',
filetype = "vue",
linenr = 4,
key = [[>]],
before = [[<Img|]],
after = [[<Img>|</Img>]]
},
{
name = "vue not close on script",
filepath = './sample/index.vue',
filetype = "vue",
linenr = 12,
key = [[>]],
before = [[const data:Array<string| ]],
after = [[const data:Array<string> ]]
},
}
local run_data = {}
for _, value in pairs(data) do
@ -110,7 +129,10 @@ end
if #run_data == 0 then run_data = data end
local autotag = require('nvim-ts-autotag')
autotag.test = true
autotag.enableRename = false
local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils')
_G.TU=ts_utils
local function Test(test_data)
for _, value in pairs(test_data) do
it("test "..value.name, function()
@ -119,15 +141,14 @@ local function Test(test_data)
local p_before = string.find(value.before , '%|')
local p_after = string.find(value.after , '%|')
local line =value.linenr
vim.bo.filetype = value.filetype
if vim.fn.filereadable(vim.fn.expand(value.filepath)) == 1 then
vim.cmd(":bd!")
vim.cmd(":e " .. value.filepath)
vim.bo.filetype = value.filetype
vim.fn.setline(line , before)
vim.fn.cursor(line, p_before)
vim.fn.cursor(line, p_before -1)
-- autotag.closeTag()
helpers.insert(value.key)
helpers.feed("<esc>")
local result = vim.fn.getline(line)
eq(after, result , "\n\n text error: " .. value.name .. "\n")
else

View File

@ -1,9 +1,11 @@
set rtp +=.
set rtp +=~/.vim/autoload/plenary.nvim/
set rtp +=~/.vim/autoload/nvim-treesitter/
set rtp +=~/.vim/autoload/nvim-treesitter
set rtp +=~/.vim/autoload/playground/
runtime! plugin/plenary.vim
runtime! plugin/nvim-treesitter.vim
runtime! plugin/playground.vim
set noswapfile
set nobackup
@ -20,5 +22,4 @@ local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils')
_G.T=ts_utils
require("plenary/busted")
require("nvim-ts-autotag").setup()
EOF

View File

@ -105,7 +105,6 @@ local function Test(test_data)
helpers.feed(value.key,'x')
helpers.feed("<esc>",'x')
local result = vim.fn.getline(line)
print("AAAAAAAAAAAAAAAA")
eq(after, result , "\n\n ERROR: " .. value.name .. "\n")
else
eq(false, true, "\n\n file not exist " .. value.filepath .. "\n")