From 7a9843f4696e2495a0fb237155ecf065b6ecac79 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Wed, 15 May 2024 02:53:46 -0500 Subject: [PATCH] fix: correctly attach to supported buffers Closes https://github.com/PriceHiller/nvim-ts-autotag/issues/7 --- lua/nvim-ts-autotag.lua | 18 +++++++++++++----- lua/nvim-ts-autotag/internal.lua | 12 +++++------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lua/nvim-ts-autotag.lua b/lua/nvim-ts-autotag.lua index c155b0f..41012b3 100644 --- a/lua/nvim-ts-autotag.lua +++ b/lua/nvim-ts-autotag.lua @@ -34,11 +34,19 @@ end function M.setup(opts) internal.setup(opts) - vim.cmd([[augroup nvim_ts_xmltag]]) - vim.cmd([[autocmd!]]) - vim.cmd([[autocmd FileType * call v:lua.require('nvim-ts-autotag.internal').attach()]]) - vim.cmd([[autocmd BufDelete * lua require('nvim-ts-autotag.internal').detach(vim.fn.expand(''))]]) - vim.cmd([[augroup end]]) + local augroup = vim.api.nvim_create_augroup("nvim_ts_xmltag", { clear = true }) + vim.api.nvim_create_autocmd("Filetype", { + group = augroup, + callback = function(args) + require("nvim-ts-autotag.internal").attach(args.buf) + end, + }) + vim.api.nvim_create_autocmd("BufDelete", { + group = augroup, + callback = function(args) + require("nvim-ts-autotag.internal").detach(args.buf) + end, + }) end return M diff --git a/lua/nvim-ts-autotag/internal.lua b/lua/nvim-ts-autotag/internal.lua index 80b71da..1baabcb 100644 --- a/lua/nvim-ts-autotag/internal.lua +++ b/lua/nvim-ts-autotag/internal.lua @@ -593,8 +593,7 @@ M.rename_tag = function() end M.attach = function(bufnr, lang) - M.lang = lang - + bufnr = bufnr or vim.api.nvim_get_current_buf() if not did_setup then local config = require("nvim-treesitter.configs").get_module("autotag") M.setup(config) @@ -606,33 +605,32 @@ M.attach = function(bufnr, lang) if M.enable_close == true then vim.keymap.set("i", ">", 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, { ">" }) + 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 M.enable_close_on_slash == true then vim.keymap.set("i", "/", function() - M.enable_close_on_slash = false 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, { "/" }) + 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 }) - M.enable_close_on_slash = true end, { noremap = true, silent = true, + buffer = bufnr, }) end if M.enable_rename == true then - bufnr = bufnr or vim.api.nvim_get_current_buf() vim.api.nvim_create_autocmd("InsertLeave", { group = group, buffer = bufnr,