mirror of
https://github.com/windwp/nvim-ts-autotag.git
synced 2025-01-01 12:49:15 -06:00
add some case rename tag
This commit is contained in:
parent
be64636cfb
commit
1e1c4ce4d9
@ -210,7 +210,16 @@ local function replaceTextNode(node, tag_name)
|
||||
end
|
||||
end
|
||||
|
||||
local function checkStartTag()
|
||||
local function check_tag_correct(node)
|
||||
if node == nil then return false end
|
||||
local texts = ts_utils.get_node_text(node)
|
||||
if string.match(texts[1],"^%<") and string.match(texts[#texts],"%>$") then
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
local function rename_start_tag()
|
||||
local ts_tag = get_ts_tag()
|
||||
local tag_node = find_tag_node({
|
||||
tag_pattern = ts_tag.start_tag_pattern,
|
||||
@ -218,8 +227,9 @@ local function checkStartTag()
|
||||
})
|
||||
|
||||
if tag_node == nil then return end
|
||||
|
||||
if not check_tag_correct(tag_node:parent()) then return end
|
||||
local tag_name = get_tag_name(tag_node)
|
||||
|
||||
tag_node = find_parent_match({
|
||||
target = tag_node,
|
||||
pattern = ts_tag.element_tag,
|
||||
@ -227,6 +237,7 @@ local function checkStartTag()
|
||||
})
|
||||
|
||||
if tag_node == nil then return end
|
||||
|
||||
local close_tag_node = find_close_tag_node({
|
||||
target = tag_node,
|
||||
tag_pattern = ts_tag.close_tag_pattern,
|
||||
@ -251,7 +262,7 @@ local function checkStartTag()
|
||||
end
|
||||
end
|
||||
|
||||
local function checkEndTag()
|
||||
local function rename_end_tag()
|
||||
local ts_tag = get_ts_tag()
|
||||
local tag_node = find_tag_node({
|
||||
tag_pattern = ts_tag.close_tag_pattern,
|
||||
@ -259,6 +270,7 @@ local function checkEndTag()
|
||||
})
|
||||
|
||||
if tag_node == nil then return end
|
||||
if not check_tag_correct(tag_node:parent()) then return end
|
||||
local tag_name = get_tag_name(tag_node)
|
||||
tag_node = find_parent_match({
|
||||
target = tag_node,
|
||||
@ -271,6 +283,7 @@ local function checkEndTag()
|
||||
tag_pattern = ts_tag.start_tag_pattern,
|
||||
name_tag_pattern = ts_tag.start_name_tag_pattern,
|
||||
})
|
||||
if not check_tag_correct(start_tag_node:parent()) then return end
|
||||
if start_tag_node ~= nil then
|
||||
local start_tag_name = get_tag_name(start_tag_node)
|
||||
if tag_name ~= start_tag_name then
|
||||
@ -292,8 +305,8 @@ end
|
||||
|
||||
M.renameTag = function ()
|
||||
if validate_rename() then
|
||||
checkStartTag()
|
||||
checkEndTag()
|
||||
rename_start_tag()
|
||||
rename_end_tag()
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -38,6 +38,15 @@ local data = {
|
||||
before = [[<di|v class="lla"> dsadsa </div> ]],
|
||||
after = [[<lala| class="lla"> dsadsa </lala|> ]]
|
||||
},
|
||||
{
|
||||
name = "html rename close tag with attr" ,
|
||||
filepath = './sample/index.html',
|
||||
filetype = "html",
|
||||
linenr = 10,
|
||||
key = [[ciwlala]],
|
||||
before = [[<div class="lla"> dsadsa </di|v> ]],
|
||||
after = [[<lala class="lla"> dsadsa </lala|> ]]
|
||||
},
|
||||
{
|
||||
name = "html not rename close tag on char <" ,
|
||||
filepath = './sample/index.html',
|
||||
@ -48,13 +57,27 @@ local data = {
|
||||
after = [[<div| class="lla"> dsadsa |</button> ]]
|
||||
},
|
||||
{
|
||||
name = "html rename close tag with attr" ,
|
||||
name = "html not rename close tag with not valid" ,
|
||||
filepath = './sample/index.html',
|
||||
filetype = "html",
|
||||
linenr = 10,
|
||||
linenr = 12,
|
||||
key = [[ciwlala]],
|
||||
before = [[<div class="lla"> dsadsa </di|v> ]],
|
||||
after = [[<lala class="lla"> dsadsa </lala|> ]]
|
||||
before = {
|
||||
[[<di|v class="lla" ]],
|
||||
[[ dsadsa </div>]]
|
||||
},
|
||||
after = [[<lala class="lla" ]]
|
||||
},
|
||||
{
|
||||
name = "html not rename close tag with not valid" ,
|
||||
filepath = './sample/index.html',
|
||||
filetype = "html",
|
||||
linenr = 12,
|
||||
key = [[ciwlala]],
|
||||
before = {
|
||||
[[<div class="lla" </d|iv>]],
|
||||
},
|
||||
after = [[<div class="lla" </l|ala>]]
|
||||
},
|
||||
{
|
||||
name = "typescriptreact rename open tag" ,
|
||||
@ -99,21 +122,36 @@ autotag.test = true
|
||||
local function Test(test_data)
|
||||
for _, value in pairs(test_data) do
|
||||
it("test "..value.name, function()
|
||||
local before = string.gsub(value.before , '%|' , "")
|
||||
local after = string.gsub(value.after , '%|' , "")
|
||||
local p_before = string.find(value.before , '%|')
|
||||
local p_after = string.find(value.after , '%|')
|
||||
local line =value.linenr
|
||||
local text_before={}
|
||||
local pos_before={
|
||||
linenr = value.linenr,
|
||||
colnr=0
|
||||
}
|
||||
if not vim.tbl_islist(value.before) then
|
||||
value.before = {value.before}
|
||||
end
|
||||
local numlnr = 0
|
||||
for _, 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
|
||||
end
|
||||
numlnr = numlnr + 1
|
||||
end
|
||||
local after = string.gsub(value.after, '%|' , "")
|
||||
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.fn.setline(line , before)
|
||||
vim.fn.cursor(line, p_before)
|
||||
local bufnr=vim.api.nvim_get_current_buf()
|
||||
vim.api.nvim_buf_set_lines(bufnr, pos_before.linenr -1, pos_before.linenr +#text_before, false, text_before)
|
||||
vim.fn.cursor(pos_before.linenr, pos_before.colnr)
|
||||
-- autotag.renameTag()
|
||||
helpers.feed(value.key,'x')
|
||||
helpers.feed(value.key, 'x')
|
||||
helpers.feed("<esc>",'x')
|
||||
local result = vim.fn.getline(line)
|
||||
local result = vim.fn.getline(pos_before.linenr)
|
||||
eq(after, result , "\n\n ERROR: " .. value.name .. "\n")
|
||||
else
|
||||
eq(false, true, "\n\n file not exist " .. value.filepath .. "\n")
|
||||
|
Loading…
Reference in New Issue
Block a user