diff --git a/lua/nvim-ts-autotag.lua b/lua/nvim-ts-autotag.lua index 0ebdc69..f75b6bc 100644 --- a/lua/nvim-ts-autotag.lua +++ b/lua/nvim-ts-autotag.lua @@ -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 > >:lua require('nvim-ts-autotag').closeTag()]] + vim.cmd[[inoremap > :lua require('nvim-ts-autotag').closeTag()]] local bufnr = vim.api.nvim_get_current_buf() - vim.cmd("augroup nvim_ts_xmltag_" .. bufnr) - vim.cmd[[autocmd!]] - vim.cmd[[autocmd InsertLeave 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 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]],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>]],tag_name)) vim.cmd[[normal! T>]] + else + vim.cmd(string.format([[normal! a>]],tag_name)) end end diff --git a/sample/index.html b/sample/index.html index 87a66ce..ad99728 100644 --- a/sample/index.html +++ b/sample/index.html @@ -8,12 +8,6 @@ -
- - - - - diff --git a/tests/closetag_spec.lua b/tests/closetag_spec.lua index a341478..5b06aa6 100644 --- a/tests/closetag_spec.lua +++ b/tests/closetag_spec.lua @@ -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 = [[ ]] + before = [[]] }, { name = "html not close on input tag" , @@ -44,17 +44,27 @@ local data = { before = [[
]], after = [[
]] }, + { + only=true, + name = "html not close on exist tag" , + filepath = './sample/index.html', + filetype = "html", + linenr = 10, + key = [[>]], + before = [[ ]] + }, { name = "typescriptreact auto close tag" , filepath = './sample/index.tsx', filetype = "typescriptreact", linenr = 12, key = [[>]], - before = [[| ]] + before = [[|]] }, { - 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 = [[| ]] }, { - 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 ]] + before = [[ ]], + after = [[ ]] }, { name = "typescriptreact not close on script" , @@ -80,25 +90,34 @@ local data = { before = [[const data:Array ]] }, - -- { - -- only = true, - -- name = "vue auto close tag" , - -- filepath = './sample/index.vue', - -- filetype = "vue", - -- linenr = 4, - -- key = [[>]], - -- before = [[| ]] - -- }, - -- { - -- name = "vue not close on script", - -- filepath = './sample/index.vue', - -- filetype = "vue", - -- linenr = 12, - -- key = [[>]], - -- before = [[const data:Array ]] - -- }, + + { + name = "typescriptreact not close on script" , + filepath = './sample/index.tsx', + filetype = "typescriptreact", + linenr = 6, + key = [[>]], + before = [[{(card.data | 0) &&
}]], + after = [[{(card.data >| 0) &&
}]] + }, + { + name = "vue auto close tag" , + filepath = './sample/index.vue', + filetype = "vue", + linenr = 4, + key = [[>]], + before = [[|]] + }, + { + name = "vue not close on script", + filepath = './sample/index.vue', + filetype = "vue", + linenr = 12, + key = [[>]], + before = [[const data:Array ]] + }, } 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("") local result = vim.fn.getline(line) eq(after, result , "\n\n text error: " .. value.name .. "\n") else diff --git a/tests/minimal.vim b/tests/minimal.vim index 88a8f81..311a47d 100644 --- a/tests/minimal.vim +++ b/tests/minimal.vim @@ -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 diff --git a/tests/renametag_spec.lua b/tests/renametag_spec.lua index be60258..6e2929e 100644 --- a/tests/renametag_spec.lua +++ b/tests/renametag_spec.lua @@ -105,7 +105,6 @@ local function Test(test_data) helpers.feed(value.key,'x') helpers.feed("",'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")