From 36dbb2018915ba38314eef80027e38cbf3fff9f7 Mon Sep 17 00:00:00 2001 From: JoeDaBu Date: Tue, 17 Dec 2024 16:21:07 -0800 Subject: [PATCH] toggle created remove cleanup --- lua/nvim-ts-autotag/config/plugin.lua | 12 ++++- lua/nvim-ts-autotag/internal.lua | 74 +++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/lua/nvim-ts-autotag/config/plugin.lua b/lua/nvim-ts-autotag/config/plugin.lua index e665826..72de152 100644 --- a/lua/nvim-ts-autotag/config/plugin.lua +++ b/lua/nvim-ts-autotag/config/plugin.lua @@ -130,6 +130,7 @@ local Opts = { enable_rename = true, enable_close = true, enable_close_on_slash = false, + enable = true, } ---@class nvim-ts-autotag.PluginSetup @@ -171,8 +172,13 @@ end --- Do general plugin setup ---@param opts nvim-ts-autotag.PluginSetup? -function Setup.setup(opts) - opts = opts or {} +function Setup.setup(config) + if config then + Setup.opts = config.opts + opts = Setup.opts + else + opts = {} + end if Setup.did_setup() then return end @@ -197,6 +203,7 @@ function Setup.setup(opts) TagConfigs:add_alias(new_ft, existing_ft) end local augroup = vim.api.nvim_create_augroup("nvim_ts_xmltag", { clear = true }) + -- initializes autotag vim.api.nvim_create_autocmd("InsertEnter", { group = augroup, once = true, @@ -204,6 +211,7 @@ function Setup.setup(opts) require("nvim-ts-autotag.internal").attach(args.buf) end, }) + -- adds autotag to every new file opened vim.api.nvim_create_autocmd("Filetype", { group = augroup, callback = function(args) diff --git a/lua/nvim-ts-autotag/internal.lua b/lua/nvim-ts-autotag/internal.lua index 807bc18..291e39b 100644 --- a/lua/nvim-ts-autotag/internal.lua +++ b/lua/nvim-ts-autotag/internal.lua @@ -26,6 +26,9 @@ local buffer_tag = {} local setup_ts_tag = function() local bufnr = vim.api.nvim_get_current_buf() local tag_pats = TagConfigs:get_patterns(vim.bo.filetype) + tag_pats["enable_close"] = Setup.get_opts(vim.bo.filetype).enable_close + tag_pats["enable_close_on_slash"] = Setup.get_opts(vim.bo.filetype).enable_close_on_slash + tag_pats["enable_rename"] = Setup.get_opts(vim.bo.filetype).enable_rename if tag_pats then buffer_tag[bufnr] = tag_pats else @@ -511,5 +514,76 @@ M.detach = function(bufnr) buffer_tag[bufnr] = nil end +M.enable = function() + for bufnr, tag in pairs(buffer_tag) do + if tag ~= nil then + local group = vim.api.nvim_create_augroup("nvim-ts-autotag-" .. bufnr, { clear = true }) + if tag.enable_close then + vim.keymap.set("i", ">", function() + local row, col = unpack(vim.api.nvim_win_get_cursor(0)) + vim.api.nvim_buf_set_text(bufnr, row - 1, col, row - 1, col, { ">" }) + M.close_tag() + vim.api.nvim_win_set_cursor(0, { row, col + 1 }) + end, { + noremap = true, + silent = true, + buffer = bufnr, + }) + end + if tag.enable_close_on_slash then + vim.keymap.set("i", "/", function() + local row, col = unpack(vim.api.nvim_win_get_cursor(0)) + vim.api.nvim_buf_set_text(bufnr, row - 1, col, row - 1, col, { "/" }) + if is_before_arrow() then + log.debug("is_before_arrow") + M.close_slash_tag() + end + local new_row, new_col = unpack(vim.api.nvim_win_get_cursor(0)) + vim.api.nvim_win_set_cursor(0, { new_row, new_col + 1 }) + end, { + noremap = true, + silent = true, + buffer = bufnr, + }) + end + if tag.enable_rename then + vim.api.nvim_create_autocmd("InsertLeave", { + group = group, + buffer = bufnr, + callback = M.rename_tag, + }) + end + end + end +end + +M.disable = function() + for bufnr, _ in pairs(buffer_tag) do + vim.keymap.set("i", ">", ">", { + noremap = true, + silent = true, + buffer = bufnr, + }) + vim.keymap.set("i", "/", "/", { + noremap = true, + silent = true, + buffer = bufnr, + }) + vim.cmd("autocmd! nvim-ts-autotag-" .. bufnr) + end +end +function M.buffers() +end + +M.toggle = function() + Setup.opts.enable = not Setup.opts.enable + if Setup.opts.enable then + M.enable() + else + M.disable() + end +end + + -- _G.AUTO = M return M