diff --git a/lua/nvim-ts-autotag/internal.lua b/lua/nvim-ts-autotag/internal.lua index 2285958..de8e110 100644 --- a/lua/nvim-ts-autotag/internal.lua +++ b/lua/nvim-ts-autotag/internal.lua @@ -129,8 +129,35 @@ local setup_ts_tag = function() buffer_tag[bufnr] = HTML_TAG end +local function is_in_template_tag() + local cursor_node = ts_utils.get_node_at_cursor() + if not cursor_node then + return false + end + + local has_element = false + local has_template_string = false + + local current_node = cursor_node + while not (has_element and has_template_string) and current_node do + if not has_element and current_node:type() == 'element' then + has_element = true + end + if not has_template_string and current_node:type() == 'template_string' then + has_template_string = true + end + current_node = current_node:parent() + end + + return has_element and has_template_string +end + local function get_ts_tag() + if is_in_template_tag() then + return HTML_TAG + else return buffer_tag[vim.api.nvim_get_current_buf()] + end end local function find_child_match(opts) @@ -502,18 +529,26 @@ M.attach = function(bufnr, lang) if is_in_table(M.tbl_filetypes, vim.bo.filetype) then setup_ts_tag() if M.enable_close == true then - vim.cmd( - [[inoremap > >:lua require('nvim-ts-autotag.internal').close_tag()a]] - ) + vim.api.nvim_buf_set_keymap(bufnr or 0, 'i', ">", ">", { + noremap = true, + silent = true, + callback = function() + local row, col = unpack(vim.api.nvim_win_get_cursor(0)) + vim.api.nvim_buf_set_text(bufnr or 0, row-1, col, row-1, col, { '>' }) + M.close_tag() + vim.api.nvim_win_set_cursor(0, {row, col+1}) + end + }) end if M.enable_rename == true then bufnr = bufnr or vim.api.nvim_get_current_buf() - vim.cmd( - string.format( - [[autocmd! InsertLeave call v:lua.require('nvim-ts-autotag.internal').rename_tag() ]], - bufnr - ) - ) + vim.api.nvim_create_autocmd('InsertLeave', { + group = vim.api.nvim_create_augroup('ts-autotag-rename', { clear = true }), + buffer = bufnr, + callback = function() + M.rename_tag() + end + }) end end end diff --git a/sample/index.ts b/sample/index.ts new file mode 100644 index 0000000..60561d9 --- /dev/null +++ b/sample/index.ts @@ -0,0 +1,6 @@ +const html = x => x; +html` + + + +` diff --git a/tests/closetag_spec.lua b/tests/closetag_spec.lua index c04e7bd..4c00633 100644 --- a/tests/closetag_spec.lua +++ b/tests/closetag_spec.lua @@ -194,6 +194,16 @@ local data = { -- before = [[| ]], -- }, + { + + name = '19 lit template div', + filepath = './sample/index.ts', + filetype = 'typescript', + linenr = 3, + key = [[>]], + before = [[| ]], + }, } local autotag = require('nvim-ts-autotag')