From 08e9ada20856f0ece0387282813e69ace208cdd3 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Wed, 15 May 2024 08:07:00 -0500 Subject: [PATCH] feat!: overhaul the config setup This deprecates the old setup layout Closes https://github.com/PriceHiller/nvim-ts-autotag/issues/5 --- README.md | 65 +++++---- lua/nvim-ts-autotag.lua | 21 +-- lua/nvim-ts-autotag/config/ft.lua | 94 ++++++++++++ lua/nvim-ts-autotag/config/init.lua | 78 ++++++++++ lua/nvim-ts-autotag/config/plugin.lua | 199 ++++++++++++++++++++++++++ lua/nvim-ts-autotag/internal.lua | 190 +++--------------------- tests/minimal_init.lua | 9 +- 7 files changed, 438 insertions(+), 218 deletions(-) create mode 100644 lua/nvim-ts-autotag/config/ft.lua create mode 100644 lua/nvim-ts-autotag/config/init.lua create mode 100644 lua/nvim-ts-autotag/config/plugin.lua diff --git a/README.md b/README.md index c1858f2..332536b 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ It works with: - vue - xml +and more + ## Usage ```text @@ -42,6 +44,44 @@ require('nvim-ts-autotag').setup() > If you are setting up via `nvim-treesitter.configs` it has been deprecated! Please migrate to the > new way. It will be removed in `1.0.0`. +### Extending the default config + +Let's say that there's a language that `nvim-ts-autotag` doesn't currently support and you'd like to support it in your +config. While it would be the preference of the author that you upstream your changes, perhaps you would rather not 😢. + +For example, if you have a language that has a very similar layout in its Treesitter Queries as `html`, you could add an +alias like so: + +```lua +require('nvim-ts-autotag').setup({ + aliases = { + ["your language here"] = "html", + } +}) + +-- or +local TagConfigs = require("nvim-ts-autotag.config.init") +TagConfigs:add_alias("your language here", "html") +``` + +That will make `nvim-ts-autotag` close tags according to the rules of the `html` config in the given language. + +But what if a parser breaks for whatever reason, for example the upstream Treesitter tree changes its node names and now +the default queries that `nvim-ts-autotag` provides no longer work. + +Fear not! You can directly extend and override the existing configs. For example, let's say the start and end tag +patterns have changed for `xml`. We can directly override the `xml` config: + +```lua +local TagConfigs = require("nvim-ts-autotag.config.init") +TagConfigs:update(TagConfigs:get("xml"):override("xml", { + start_tag_pattern = { "STag" }, + end_tag_pattern = { "ETag" }, +})) +``` + +In fact, this very nearly what we do during our own internal initialization phase for `nvim-ts-autotag`. + ### Enable update on insert If you have that issue @@ -61,31 +101,6 @@ vim.lsp.handlers['textDocument/publishDiagnostics'] = vim.lsp.with( ) ``` -## Default values - -```lua -local filetypes = { - 'html', 'javascript', 'typescript', 'javascriptreact', 'typescriptreact', 'svelte', 'vue', 'tsx', 'jsx', 'rescript', - 'xml', - 'php', - 'markdown', - 'astro', 'glimmer', 'handlebars', 'hbs', 'twig' -} -local skip_tag = { - 'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'slot', - 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr','menuitem' -} - -``` - -### Override default values - -```lua -require('nvim-ts-autotag').setup({ - filetypes = { "html" , "xml" }, -}) -``` - ## Fork Status This is forked from https://github.com/windwp/nvim-ts-autotag due to the primary maintainer's disappearance. Any diff --git a/lua/nvim-ts-autotag.lua b/lua/nvim-ts-autotag.lua index 41012b3..4bcd187 100644 --- a/lua/nvim-ts-autotag.lua +++ b/lua/nvim-ts-autotag.lua @@ -11,7 +11,7 @@ function M.init() end nvim_ts.define_modules({ autotag = { - attach = function(bufnr, lang) + attach = function(bufnr, _) vim.deprecate( "Nvim Treesitter Setup", "`require('nvim-ts-autotag').setup()`", @@ -19,7 +19,7 @@ function M.init() "nvim-ts-autotag", true ) - internal.attach(bufnr, lang) + internal.attach(bufnr) end, detach = function(bufnr) internal.detach(bufnr) @@ -32,21 +32,6 @@ function M.init() end end -function M.setup(opts) - internal.setup(opts) - 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 +M.setup = require("nvim-ts-autotag.config.plugin").setup return M diff --git a/lua/nvim-ts-autotag/config/ft.lua b/lua/nvim-ts-autotag/config/ft.lua new file mode 100644 index 0000000..a227996 --- /dev/null +++ b/lua/nvim-ts-autotag/config/ft.lua @@ -0,0 +1,94 @@ +---@alias nvim-ts-autotag.FiletypeConfigPattern string[] A single array of patterns + +---@alias nvim-ts-autotag.FiletypeConfig.filetype string The supported filetype for a given Filetype Config + +-- WARN: IF YOU MESS WITH THE FIELDS IN THIS, MAKE ABSOLUTELY SURE YOU UPDATE THE PARTIAL PATTERNS +-- IN THE NEXT CLASS AS WELL! +-- +---@class nvim-ts-autotag.FiletypeConfig.patterns The patterns used in a tag search +---@field start_tag_pattern nvim-ts-autotag.FiletypeConfigPattern +---@field start_name_tag_pattern nvim-ts-autotag.FiletypeConfigPattern +---@field end_tag_pattern nvim-ts-autotag.FiletypeConfigPattern +---@field end_name_tag_pattern nvim-ts-autotag.FiletypeConfigPattern +---@field close_tag_pattern nvim-ts-autotag.FiletypeConfigPattern +---@field close_name_tag_pattern nvim-ts-autotag.FiletypeConfigPattern +---@field element_tag nvim-ts-autotag.FiletypeConfigPattern +---@field skip_tag_pattern nvim-ts-autotag.FiletypeConfigPattern + +---@class nvim-ts-autotag.FiletypeConfig.patterns.p Partial patterns, lua_ls doesn't have partial support so this has to copied 🙁 +---@field start_tag_pattern nvim-ts-autotag.FiletypeConfigPattern? +---@field start_name_tag_pattern nvim-ts-autotag.FiletypeConfigPattern? +---@field end_tag_pattern nvim-ts-autotag.FiletypeConfigPattern? +---@field end_name_tag_pattern nvim-ts-autotag.FiletypeConfigPattern? +---@field close_tag_pattern nvim-ts-autotag.FiletypeConfigPattern? +---@field close_name_tag_pattern nvim-ts-autotag.FiletypeConfigPattern? +---@field element_tag nvim-ts-autotag.FiletypeConfigPattern? +---@field skip_tag_pattern nvim-ts-autotag.FiletypeConfigPattern? + +---@class nvim-ts-autotag.FiletypeConfig +---@field filetype nvim-ts-autotag.FiletypeConfig.filetype If nil, then this is acting as a base +---@field patterns nvim-ts-autotag.FiletypeConfig.patterns +local FiletypeConfig = { + filetype = "", + patterns = { + start_tag_pattern = {}, + start_name_tag_pattern = {}, + end_tag_pattern = {}, + end_name_tag_pattern = {}, + close_tag_pattern = {}, + close_name_tag_pattern = {}, + element_tag = {}, + skip_tag_pattern = {}, + }, +} + +---@param filetype nvim-ts-autotag.FiletypeConfig.filetype +---@param patterns nvim-ts-autotag.FiletypeConfig.patterns +---@return nvim-ts-autotag.FiletypeConfig +function FiletypeConfig.new(filetype, patterns) + local new = { + filetype = filetype, + patterns = patterns, + } + return setmetatable(new, { + __index = FiletypeConfig, + }) +end + +--- Creates a deep copy of the given FiletypeConfig +---@private +---@return nvim-ts-autotag.FiletypeConfig +function FiletypeConfig:clone() + return vim.deepcopy(self) +end + +--- Allows overriding of patterns for a config and returns the overriden config +---@param filetype nvim-ts-autotag.FiletypeConfig.filetype +---@param patterns nvim-ts-autotag.FiletypeConfig.patterns.p +---@return nvim-ts-autotag.FiletypeConfig +function FiletypeConfig:override(filetype, patterns) + local new = self:clone() + new.filetype = filetype + new.patterns = vim.tbl_deep_extend("force", new.patterns, patterns or {}) + return new +end + +--- Adds additional values to a given config's patterns, updates the supported filetype, and +--- returns a new extended cfg +---@param filetype nvim-ts-autotag.FiletypeConfig.filetype +---@param patterns nvim-ts-autotag.FiletypeConfig.patterns.p? +---@return nvim-ts-autotag.FiletypeConfig +function FiletypeConfig:extend(filetype, patterns) + local new = self:clone() + new.filetype = filetype + for pat_key, pats in pairs(patterns or {}) do + for _, pat in ipairs(pats) do + if not vim.list_contains(new.patterns[pat_key], pat) then + table.insert(new.patterns[pat_key], pat) + end + end + end + return new +end + +return FiletypeConfig diff --git a/lua/nvim-ts-autotag/config/init.lua b/lua/nvim-ts-autotag/config/init.lua new file mode 100644 index 0000000..222968e --- /dev/null +++ b/lua/nvim-ts-autotag/config/init.lua @@ -0,0 +1,78 @@ +---@class nvim-ts-autotag.TagConfigs +---@field private _cfgs { [string]: nvim-ts-autotag.FiletypeConfig } +local TagConfigs = { + _cfgs = {}, +} + +---@param filetype string The filetype to get the tag config for +---@return nvim-ts-autotag.FiletypeConfig? +function TagConfigs:get(filetype) + return self._cfgs[filetype] +end + +--- Get tag patterns by a filetype +---@param filetype string The filetype to get the tag patterns for +---@return nvim-ts-autotag.FiletypeConfig.patterns? +function TagConfigs:get_patterns(filetype) + local cfg = self._cfgs[filetype] + if cfg then + return cfg.patterns + end +end + +---@param tag_config nvim-ts-autotag.FiletypeConfig +function TagConfigs:add(tag_config) + self._cfgs[tag_config.filetype] = tag_config +end + +--- Directly updates a given tag config stored in the TagConfigs +---@param tag_config nvim-ts-autotag.FiletypeConfig +function TagConfigs:update(tag_config) + self._cfgs[tag_config] = tag_config +end + +--- Get a list of supported filetypes +---@return string[] +function TagConfigs:get_supported_filetypes() + local supported_fts = {} + for ft, _ in pairs(self._cfgs) do + table.insert(supported_fts, ft) + end + return supported_fts +end + +--- Adds an alias for to an existing tag config for a new filetype +--- +--- For example, to an add an alias for say a filetype named `quill` that we want to use the `html` +--- tag config for we can do the following: +--- +--- ```lua +--- TagConfigs:add_alias("quill", "html") +--- ``` +---@param new_filetype string The new filetype to alias for +---@param existing_filetype string The existing filetype to link to +function TagConfigs:add_alias(new_filetype, existing_filetype) + local existing_cfg = self:get(existing_filetype) + if not existing_cfg then + error(("No existing filetype '%s' to alias to!"):format(existing_filetype)) + end + local new = existing_cfg:extend(new_filetype) + self:add(new) +end + +--- A simple wrapper around `TagConfigs.add_alias`. +--- +--- Adds multiple aliases to an existing tag config for new filetypes. +--- +--- ```lua +--- TagConfigs:add_aliases({ "quill", "vue"}, "html") +-- ``` +---@param new_filetypes string[] The new filetypes to alias for +---@param existing_filetype string The existing filetype to link to +function TagConfigs:add_aliases(new_filetypes, existing_filetype) + for _, new_ft in ipairs(new_filetypes) do + self:add_alias(new_ft, existing_filetype) + end +end + +return TagConfigs diff --git a/lua/nvim-ts-autotag/config/plugin.lua b/lua/nvim-ts-autotag/config/plugin.lua new file mode 100644 index 0000000..29765a5 --- /dev/null +++ b/lua/nvim-ts-autotag/config/plugin.lua @@ -0,0 +1,199 @@ +local FiletypeConfig = require("nvim-ts-autotag.config.ft") +local TagConfigs = require("nvim-ts-autotag.config.init") + +local function setup_tag_configs() + ---@diagnostic disable-next-line: param-type-mismatch + local base_cfg = FiletypeConfig:extend(nil, { + skip_tag_pattern = { + "area", + "base", + "br", + "col", + "command", + "embed", + "hr", + "img", + "slot", + "input", + "keygen", + "link", + "meta", + "param", + "source", + "track", + "wbr", + "menuitem", + }, + }) + + local html_tag_cfg = base_cfg:extend("html", { + start_tag_pattern = { "start_tag", "STag" }, + start_name_tag_pattern = { "tag_name", "Name" }, + end_tag_pattern = { "end_tag", "ETag" }, + end_name_tag_pattern = { "tag_name", "Name" }, + close_tag_pattern = { "erroneous_end_tag" }, + close_name_tag_pattern = { "erroneous_end_tag_name" }, + element_tag = { "element" }, + skip_tag_pattern = { "quoted_attribute_value", "end_tag" }, + }) + + TagConfigs:add(html_tag_cfg) + TagConfigs:add(html_tag_cfg:override("xml", { + start_tag_pattern = { "STag" }, + end_tag_pattern = { "ETag" }, + })) + + TagConfigs:add(base_cfg:extend("typescriptreact", { + start_tag_pattern = { "jsx_opening_element", "start_tag" }, + start_name_tag_pattern = { + "identifier", + "nested_identifier", + "tag_name", + "member_expression", + "jsx_identifier", + }, + end_tag_pattern = { "jsx_closing_element", "end_tag" }, + end_name_tag_pattern = { "identifier", "tag_name" }, + close_tag_pattern = { "jsx_closing_element", "nested_identifier" }, + close_name_tag_pattern = { "member_expression", "nested_identifier", "jsx_identifier", "identifier", ">" }, + element_tag = { "jsx_element", "element" }, + skip_tag_pattern = { + "jsx_closing_element", + "jsx_expression", + "string", + "jsx_attribute", + "end_tag", + "string_fragment", + }, + })) + + TagConfigs:add(base_cfg:extend("glimmer", { + start_tag_pattern = { "element_node_start" }, + start_name_tag_pattern = { "tag_name" }, + end_tag_pattern = { "element_node_end" }, + end_name_tag_pattern = { "tag_name" }, + close_tag_pattern = { "element_node_end" }, + close_name_tag_pattern = { "tag_name" }, + element_tag = { "element_node" }, + skip_tag_pattern = { "element_node_end", "attribute_node", "concat_statement" }, + })) + + TagConfigs:add(base_cfg:extend("svelte", { + start_tag_pattern = { "start_tag" }, + start_name_tag_pattern = { "tag_name" }, + end_tag_pattern = { "end_tag" }, + end_name_tag_pattern = { "tag_name" }, + close_tag_pattern = { "erroneous_end_tag" }, + close_name_tag_pattern = { "erroneous_end_tag_name" }, + element_tag = { "element" }, + skip_tag_pattern = { "quoted_attribute_value", "end_tag" }, + })) + + TagConfigs:add(base_cfg:extend("templ", { + start_tag_pattern = { "tag_start" }, + start_name_tag_pattern = { "element_identifier" }, + end_tag_pattern = { "tag_end" }, + end_name_tag_pattern = { "element_identifier" }, + close_tag_pattern = { "erroneous_end_tag" }, + close_name_tag_pattern = { "erroneous_end_tag_name" }, + element_tag = { "element" }, + skip_tag_pattern = { "quoted_attribute_value", "tag_end" }, + })) +end + +---@class nvim-ts-autotag.Opts +---@field enable_rename boolean? Whether or not to auto rename paired tags +---@field enable_close boolean? Whether or not to auto close tags +---@field enable_close_on_slash boolean? Whether or not to auto close tags when a `/` is inserted +local Opts = { + enable_rename = true, + enable_close = true, + enable_close_on_slash = false, +} + +---@class nvim-ts-autotag.PluginSetup +---@field opts nvim-ts-autotag.Opts? +---@field aliases { [string]: string }? Aliases a filetype to an existing filetype tag config +---@field per_filetype { [string]: nvim-ts-autotag.Opts }? Per filetype config overrides +local Setup = { + did_setup = false, + opts = Opts, + aliases = { + ["astro"] = "html", + ["eruby"] = "html", + ["vue"] = "html", + ["htmldjango"] = "html", + ["markdown"] = "html", + ["php"] = "html", + ["twig"] = "html", + ["blade"] = "html", + ["javascriptreact"] = "typescriptreact", + ["javascript.jsx"] = "typescriptreact", + ["typescript.tsx"] = "typescriptreact", + ["javascript"] = "typescriptreact", + ["typescript"] = "typescriptreact", + ["rescript"] = "typescriptreact", + ["handlebars"] = "glimmer", + ["hbs"] = "glimmer", + }, + per_filetype = {}, +} + +--- Do general plugin setup +---@param opts nvim-ts-autotag.PluginSetup +function Setup.setup(opts) + if Setup.did_setup then + return + end + if opts and not opts.opts then + vim.notify( + "nvim-ts-autotag: Using the legacy setup opts! Please migrate to the new setup options layout as this will eventually have its support removed in 1.0.0!", + vim.log.levels.WARN + ) + opts = { + ---@diagnostic disable-next-line: assign-type-mismatch + opts = opts, + } + end + Setup = vim.tbl_deep_extend("force", Setup, opts or {}) + + if not Setup.did_setup then + Setup.did_setup = true + + setup_tag_configs() + for new_ft, existing_ft in pairs(Setup.aliases) do + TagConfigs:add_alias(new_ft, existing_ft) + 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 +end + +--- Get the defined options for the given filetype or in general +---@param filetype string? +---@return nvim-ts-autotag.Opts +function Setup.get_opts(filetype) + if not filetype then + return Setup.opts + end + + local per_ft_conf = Setup.per_filetype[filetype] + if per_ft_conf then + return vim.tbl_deep_extend("force", Setup.opts or {}, per_ft_conf) + end + + return Setup.opts +end + +return Setup diff --git a/lua/nvim-ts-autotag/internal.lua b/lua/nvim-ts-autotag/internal.lua index 1baabcb..029de73 100644 --- a/lua/nvim-ts-autotag/internal.lua +++ b/lua/nvim-ts-autotag/internal.lua @@ -1,163 +1,10 @@ local log = require("nvim-ts-autotag._log") +local TagConfigs = require("nvim-ts-autotag.config.init") +local Setup = require("nvim-ts-autotag.config.plugin") local utils = require("nvim-ts-autotag.utils") local M = {} -M.tbl_filetypes = { - "html", - "javascript", - "typescript", - "javascriptreact", - "typescriptreact", - "svelte", - "vue", - "tsx", - "jsx", - "rescript", - "xml", - "php", - "markdown", - "astro", - "glimmer", - "handlebars", - "hbs", - "twig", - "htmldjango", - "eruby", - "templ", - "blade", -} - -M.tbl_skipTag = { - "area", - "base", - "br", - "col", - "command", - "embed", - "hr", - "img", - "slot", - "input", - "keygen", - "link", - "meta", - "param", - "source", - "track", - "wbr", - "menuitem", -} - -local HTML_TAG = { - filetypes = { - "astro", - "html", - "htmldjango", - "markdown", - "php", - "twig", - "xml", - "blade", - }, - start_tag_pattern = { "start_tag", "STag" }, - start_name_tag_pattern = { "tag_name", "Name" }, - end_tag_pattern = { "end_tag", "ETag" }, - end_name_tag_pattern = { "tag_name", "Name" }, - close_tag_pattern = { "erroneous_end_tag" }, - close_name_tag_pattern = { "erroneous_end_tag_name" }, - element_tag = { "element" }, - skip_tag_pattern = { "quoted_attribute_value", "end_tag" }, -} - -local JSX_TAG = { - filetypes = { - "typescriptreact", - "javascriptreact", - "javascript.jsx", - "typescript.tsx", - "javascript", - "typescript", - "rescript", - }, - start_tag_pattern = { "jsx_opening_element", "start_tag" }, - start_name_tag_pattern = { "identifier", "nested_identifier", "tag_name", "member_expression", "jsx_identifier" }, - end_tag_pattern = { "jsx_closing_element", "end_tag" }, - end_name_tag_pattern = { "identifier", "tag_name" }, - close_tag_pattern = { "jsx_closing_element", "nested_identifier" }, - close_name_tag_pattern = { "member_expression", "nested_identifier", "jsx_identifier", "identifier", ">" }, - element_tag = { "jsx_element", "element" }, - skip_tag_pattern = { - "jsx_closing_element", - "jsx_expression", - "string", - "jsx_attribute", - "end_tag", - "string_fragment", - }, -} - -local HBS_TAG = { - filetypes = { "glimmer", "handlebars", "hbs", "htmldjango" }, - start_tag_pattern = { "element_node_start" }, - start_name_tag_pattern = { "tag_name" }, - end_tag_pattern = { "element_node_end" }, - end_name_tag_pattern = { "tag_name" }, - close_tag_pattern = { "element_node_end" }, - close_name_tag_pattern = { "tag_name" }, - element_tag = { "element_node" }, - skip_tag_pattern = { "element_node_end", "attribute_node", "concat_statement" }, -} - -local SVELTE_TAG = { - filetypes = { "svelte" }, - start_tag_pattern = { "start_tag" }, - start_name_tag_pattern = { "tag_name" }, - end_tag_pattern = { "end_tag" }, - end_name_tag_pattern = { "tag_name" }, - close_tag_pattern = { "erroneous_end_tag" }, - close_name_tag_pattern = { "erroneous_end_tag_name" }, - element_tag = { "element" }, - skip_tag_pattern = { "quoted_attribute_value", "end_tag" }, -} - -local TEMPL_TAG = { - filetypes = { - "templ", - }, - start_tag_pattern = { "tag_start" }, - start_name_tag_pattern = { "element_identifier" }, - end_tag_pattern = { "tag_end" }, - end_name_tag_pattern = { "element_identifier" }, - close_tag_pattern = { "erroneous_end_tag" }, - close_name_tag_pattern = { "erroneous_end_tag_name" }, - element_tag = { "element" }, - skip_tag_pattern = { "quoted_attribute_value", "tag_end" }, -} - -local all_tag = { - HBS_TAG, - SVELTE_TAG, - JSX_TAG, - TEMPL_TAG, -} - -M.enable_rename = true -M.enable_close = true -M.enable_close_on_slash = false - -local did_setup = false - -M.setup = function(opts) - opts = opts or {} - M.tbl_filetypes = opts.filetypes or M.tbl_filetypes - M.tbl_skipTag = opts.skip_tag or M.tbl_skipTag - M.enable_rename = opts.enable_rename or M.enable_rename - M.enable_close = opts.enable_close or M.enable_close - M.enable_close_on_slash = opts.enable_close_on_slash or M.enable_close_on_slash - did_setup = true -end - local function is_in_table(tbl, val) if tbl == nil then return false @@ -171,20 +18,20 @@ local function is_in_table(tbl, val) end M.is_supported = function(lang) - return is_in_table(M.tbl_filetypes, lang) + return TagConfigs:get(lang) ~= nil end local buffer_tag = {} local setup_ts_tag = function() local bufnr = vim.api.nvim_get_current_buf() - for _, value in pairs(all_tag) do - if is_in_table(value.filetypes, vim.bo.filetype) then - buffer_tag[bufnr] = value - return value - end + local tag_pats = TagConfigs:get_patterns(vim.bo.filetype) + if tag_pats then + buffer_tag[bufnr] = tag_pats + else + -- HACK: Crappy fallback + buffer_tag[bufnr] = TagConfigs:get_patterns("html") end - buffer_tag[bufnr] = HTML_TAG end local function is_in_template_tag() @@ -210,9 +57,10 @@ local function is_in_template_tag() return has_element and has_template_string end +---@return nvim-ts-autotag.FiletypeConfig.patterns? local function get_ts_tag() if is_in_template_tag() then - return HTML_TAG + return TagConfigs:get_patterns("html") else return buffer_tag[vim.api.nvim_get_current_buf()] end @@ -366,7 +214,7 @@ local function check_close_tag(close_slash_tag) }) if tag_node ~= nil then local tag_name = get_tag_name(tag_node) - if tag_name ~= nil and is_in_table(M.tbl_skipTag, tag_name) then + if tag_name ~= nil and vim.list_contains(ts_tag.skip_tag_pattern, tag_name) then return false end if tag_node ~= nil then @@ -592,17 +440,17 @@ M.rename_tag = function() end end -M.attach = function(bufnr, lang) +M.attach = function(bufnr) bufnr = bufnr or vim.api.nvim_get_current_buf() - if not did_setup then + if not Setup.did_setup then local config = require("nvim-treesitter.configs").get_module("autotag") - M.setup(config) + Setup.setup(config) end - if is_in_table(M.tbl_filetypes, vim.bo.filetype) then + if TagConfigs:get(vim.bo.filetype) ~= nil then setup_ts_tag() local group = vim.api.nvim_create_augroup("nvim-ts-autotag", { clear = true }) - if M.enable_close == true then + if Setup.get_opts(vim.bo.filetype).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, { ">" }) @@ -614,7 +462,7 @@ M.attach = function(bufnr, lang) buffer = bufnr, }) end - if M.enable_close_on_slash == true then + if Setup.get_opts(vim.bo.filetype).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, { "/" }) @@ -630,7 +478,7 @@ M.attach = function(bufnr, lang) buffer = bufnr, }) end - if M.enable_rename == true then + if Setup.get_opts(vim.bo.filetype).enable_rename then vim.api.nvim_create_autocmd("InsertLeave", { group = group, buffer = bufnr, diff --git a/tests/minimal_init.lua b/tests/minimal_init.lua index eba0f28..9e25550 100644 --- a/tests/minimal_init.lua +++ b/tests/minimal_init.lua @@ -128,10 +128,11 @@ function M.setup(plugins) -- Ensure `nvim-ts-autotag` is registed on the runtimepath and set it up utils.rtp_register_ts_autotag() require("nvim-ts-autotag").setup({ - enable = true, - enable_rename = true, - enable_close = true, - enable_close_on_slash = true, + opts = { + enable_rename = true, + enable_close = true, + enable_close_on_slash = true, + }, }) print("[SETUP] Finished setting up minimal init")