fix: correctly attach to supported buffers

Closes https://github.com/PriceHiller/nvim-ts-autotag/issues/7
This commit is contained in:
Price Hiller 2024-05-15 02:53:46 -05:00 committed by windwp
parent 1550f128bd
commit 7a9843f469
2 changed files with 18 additions and 12 deletions

View File

@ -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('<abuf>'))]])
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

View File

@ -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,