From 07e607f0a903fdd64c78261146f1e237a23440be Mon Sep 17 00:00:00 2001 From: windwp Date: Sat, 8 May 2021 07:34:31 +0700 Subject: [PATCH] fix svelte bug --- lua/nvim-ts-autotag.lua | 1 + lua/nvim-ts-autotag/internal.lua | 67 +++++++++++++++++++++++++------- lua/nvim-ts-autotag/utils.lua | 2 + sample/index.hbs | 4 ++ sample/index.svelte | 25 ++++++++++++ tests/renametag_spec.lua | 18 +++++++++ 6 files changed, 102 insertions(+), 15 deletions(-) create mode 100644 sample/index.hbs create mode 100644 sample/index.svelte diff --git a/lua/nvim-ts-autotag.lua b/lua/nvim-ts-autotag.lua index 9172542..c1c3a2b 100644 --- a/lua/nvim-ts-autotag.lua +++ b/lua/nvim-ts-autotag.lua @@ -19,6 +19,7 @@ function M.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 * call v:lua.require('nvim-ts-autotag.internal').detach()]] vim.cmd[[augroup end]] end diff --git a/lua/nvim-ts-autotag/internal.lua b/lua/nvim-ts-autotag/internal.lua index c6c2d2f..eae3148 100644 --- a/lua/nvim-ts-autotag/internal.lua +++ b/lua/nvim-ts-autotag/internal.lua @@ -1,7 +1,8 @@ local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils') local configs = require'nvim-treesitter.configs' local parsers = require'nvim-treesitter.parsers' --- local log = require('nvim-ts-autotag._log') +local log = require('nvim-ts-autotag._log') +-- local utils=require('nvim-ts-autotag.utils') local M = {} @@ -15,8 +16,10 @@ M.tbl_skipTag = { 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr','menuitem' } +local ERROR_TAG = "ERROR" local HTML_TAG = { + filetypes = {'html'}, start_tag_pattern = 'start_tag', start_name_tag_pattern = 'tag_name', end_tag_pattern = "end_tag", @@ -27,8 +30,11 @@ local HTML_TAG = { skip_tag_pattern = {'quoted_attribute_value', 'end_tag'}, } -local ERROR_TAG = "ERROR" local JSX_TAG = { + filetypes = { + 'typescriptreact', 'javascriptreact', 'javascript.jsx', + 'typescript.tsx', 'javascript', 'typescript'}, + start_tag_pattern = 'jsx_opening_element', start_name_tag_pattern = 'identifier|nested_identifier', end_tag_pattern = "jsx_closing_element", @@ -41,6 +47,7 @@ local JSX_TAG = { local HBS_TAG = { + filetypes = {'glimmer', 'handlebars','hbs'}, start_tag_pattern = 'element_node_start', start_name_tag_pattern = 'tag_name', end_tag_pattern = "element_node_end", @@ -51,6 +58,24 @@ local HBS_TAG = { 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 = 'ERROR', + close_name_tag_pattern = 'ERROR', + element_tag = 'element', + skip_tag_pattern = {'quoted_attribute_value', 'end_tag'}, +} + +local all_tag = { + HBS_TAG, + SVELTE_TAG, + JSX_TAG +} M.enable_rename = true M.enable_close = true @@ -74,22 +99,22 @@ M.is_supported = function (lang) return is_in_table(M.tbl_filetypes,lang) end +local buffer_tag={} local function get_ts_tag() - if is_in_table({ - 'typescriptreact', 'javascriptreact', 'javascript.jsx', - 'typescript.tsx', 'javascript', 'typescript'}, - vim.bo.filetype) - then - return JSX_TAG + local bufnr = vim.api.nvim_get_current_buf() + + if buffer_tag[bufnr] then + return buffer_tag[bufnr] end - if is_in_table({'glimmer', 'handlebars','hbs'}, - vim.bo.filetype) - then - return HBS_TAG + for _,value in pairs(all_tag) do + if is_in_table(value.filetypes,vim.bo.filetype) then + buffer_tag[bufnr] = value + return value + end end - + buffer_tag[bufnr] = HTML_TAG return HTML_TAG end @@ -166,7 +191,7 @@ local function find_tag_node(opt) end if node == nil then return nil end local name_node = node - local tbl_name_pattern={} + local tbl_name_pattern = {} if string.match(name_tag_pattern,"%|") then tbl_name_pattern = vim.split(name_tag_pattern, '|') for _, pattern in pairs(tbl_name_pattern) do @@ -185,6 +210,11 @@ local function find_tag_node(opt) pattern = pattern }) end + + -- check current node is have same name of tag_match + if is_in_table(tbl_name_pattern, node:type()) + then return node + end return name_node end @@ -310,11 +340,17 @@ local function rename_start_tag() replace_text_node(close_tag_node, tag_name) end else + local error_tag = get_tag_name(error_node) if error_tag=='' then replace_text_node(error_node, "") end + -- have both parent node and child node is error + if close_tag_node:type() == ERROR_TAG then + replace_text_node(error_node, "") + end end + end end @@ -386,7 +422,8 @@ M.attach = function (bufnr,lang) end M.detach = function ( ) - + local bufnr = vim.api.nvim_get_current_buf() + buffer_tag[bufnr] = nil end -- _G.AUTO = M diff --git a/lua/nvim-ts-autotag/utils.lua b/lua/nvim-ts-autotag/utils.lua index 74d9311..00a1454 100644 --- a/lua/nvim-ts-autotag/utils.lua +++ b/lua/nvim-ts-autotag/utils.lua @@ -20,11 +20,13 @@ end M.dump_node_text = function(target) + log.debug('=============================') for node in target:iter_children() do local node_type = node:type() local text = ts_utils.get_node_text(node) log.debug("type:" .. node_type .. " ") log.debug(text) end + log.debug('=============================') end return M diff --git a/sample/index.hbs b/sample/index.hbs new file mode 100644 index 0000000..c1b7322 --- /dev/null +++ b/sample/index.hbs @@ -0,0 +1,4 @@ +
+ + +
diff --git a/sample/index.svelte b/sample/index.svelte new file mode 100644 index 0000000..6fc25e2 --- /dev/null +++ b/sample/index.svelte @@ -0,0 +1,25 @@ + + + +
+
+ + + + + + + + +
+
diff --git a/tests/renametag_spec.lua b/tests/renametag_spec.lua index fcee203..e87a567 100644 --- a/tests/renametag_spec.lua +++ b/tests/renametag_spec.lua @@ -144,6 +144,24 @@ local data = { before = [[<|>
]], after = [[<|lala>
]] }, + { + name = "19 rename start tag on svelte " , + filepath = './sample/index.svelte', + filetype = "svelte", + linenr = 18, + key = [[ciwlala]], + before = [[<|data>]], + after = [[<|lala>]] + }, + { + name = "20 rename end tag on svelte " , + filepath = './sample/index.svelte', + filetype = "svelte", + linenr = 18, + key = [[ciwlala]], + before = [[]], + after = [[]] + }, -- { -- only = true, -- name = "18 rename node to empty node " ,