diff --git a/lua/nvim-ts-autotag/internal.lua b/lua/nvim-ts-autotag/internal.lua index 994086e..826017f 100644 --- a/lua/nvim-ts-autotag/internal.lua +++ b/lua/nvim-ts-autotag/internal.lua @@ -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 diff --git a/tests/renametag_spec.lua b/tests/renametag_spec.lua index 427b1e0..3fff427 100644 --- a/tests/renametag_spec.lua +++ b/tests/renametag_spec.lua @@ -38,6 +38,15 @@ local data = { before = [[ dsadsa ]], after = [[ dsadsa ]] }, + { + name = "html rename close tag with attr" , + filepath = './sample/index.html', + filetype = "html", + linenr = 10, + key = [[ciwlala]], + before = [[
dsadsa ]], + after = [[ dsadsa ]] + }, { name = "html not rename close tag on char <" , filepath = './sample/index.html', @@ -48,13 +57,27 @@ local data = { after = [[ dsadsa | ]] }, { - 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 = [[
dsadsa ]], - after = [[ dsadsa ]] + before = { + [[]] + }, + after = [[]], + }, + after = [[
]] }, { 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("",'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")