From 80d427af7b898768c8d8538663d52dee133da86f Mon Sep 17 00:00:00 2001 From: zztrieuzz Date: Sat, 9 Oct 2021 12:06:16 +0700 Subject: [PATCH] fix: make function detach working fix #29 #25 require('nvim-ts-autotag.internal').detach() --- lua/nvim-ts-autotag/internal.lua | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/lua/nvim-ts-autotag/internal.lua b/lua/nvim-ts-autotag/internal.lua index 46aff08..38e6526 100644 --- a/lua/nvim-ts-autotag/internal.lua +++ b/lua/nvim-ts-autotag/internal.lua @@ -103,13 +103,8 @@ end local buffer_tag={} -local function get_ts_tag() +local setup_ts_tag = function() local bufnr = vim.api.nvim_get_current_buf() - - if buffer_tag[bufnr] then - return buffer_tag[bufnr] - end - for _,value in pairs(all_tag) do if is_in_table(value.filetypes,vim.bo.filetype) then buffer_tag[bufnr] = value @@ -117,11 +112,12 @@ local function get_ts_tag() end end buffer_tag[bufnr] = HTML_TAG - return HTML_TAG end -M.on_file_type = function () +local function get_ts_tag() + return buffer_tag[vim.api.nvim_get_current_buf()] end + local function find_child_match(opts) local target = opts.target local pattern = opts.pattern @@ -228,6 +224,7 @@ end local function check_close_tag() local ts_tag = get_ts_tag() + if not ts_tag then return false end local tag_node = find_tag_node({ tag_pattern = ts_tag.start_tag_pattern, name_tag_pattern = ts_tag.start_name_tag_pattern, @@ -264,7 +261,6 @@ local function check_close_tag() end M.close_tag = function () - parsers.get_parser():parse() local buf_parser = parsers.get_parser() if not buf_parser then return end buf_parser:parse() @@ -310,6 +306,7 @@ end local function rename_start_tag() local ts_tag = get_ts_tag() + if not ts_tag then return end local tag_node = find_tag_node({ tag_pattern = ts_tag.start_tag_pattern, name_tag_pattern = ts_tag.start_name_tag_pattern, @@ -361,6 +358,7 @@ end local function rename_end_tag() local ts_tag = get_ts_tag() + if not ts_tag then return end local tag_node = find_tag_node({ tag_pattern = ts_tag.close_tag_pattern, name_tag_pattern = ts_tag.close_name_tag_pattern, @@ -413,15 +411,16 @@ M.attach = function (bufnr,lang) local config = configs.get_module('autotag') M.setup(config) 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]] end if M.enable_rename == true then bufnr = bufnr or 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.internal').rename_tag() ]] - vim.cmd[[augroup end]] + vim.cmd(string.format( + [[autocmd! InsertLeave call v:lua.require('nvim-ts-autotag.internal').rename_tag() ]], + bufnr + )) end end end