diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b829749..642fd59 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,12 +18,12 @@ jobs: - name: Prepare run: | - mkdir -p ~/.vim/autoload - git clone --depth 1 https://github.com/nvim-lua/plenary.nvim ~/.vim/autoload/plenary.nvim - git clone --depth 1 https://github.com/nvim-lua/popup.nvim ~/.vim/autoload/popup.nvim - git clone --depth 1 https://github.com/nvim-treesitter/nvim-treesitter ~/.vim/autoload/nvim-treesitter - git clone --depth 1 https://github.com/nvim-treesitter/playground ~/.vim/autoload/playground - ln -s $(pwd) ~/.vim/autoload + mkdir -p ~/.local/share/nvim/site/pack/vendor/start + git clone --depth 1 https://github.com/nvim-lua/plenary.nvim ~/.local/share/nvim/site/pack/vendor/start/plenary.nvim + git clone --depth 1 https://github.com/nvim-lua/popup.nvim ~/.local/share/nvim/site/pack/vendor/start/popup.nvim + git clone --depth 1 https://github.com/nvim-treesitter/nvim-treesitter ~/.local/share/nvim/site/pack/vendor/start/nvim-treesitter + git clone --depth 1 https://github.com/nvim-treesitter/playground ~/.local/share/nvim/site/pack/vendor/start/playground + ln -s $(pwd) ~/.local/share/nvim/site/pack/vendor/start - name: Run tests run: | curl -OL https://raw.githubusercontent.com/norcalli/bot-ci/master/scripts/github-actions-setup.sh diff --git a/lua/nvim-ts-autotag/internal.lua b/lua/nvim-ts-autotag/internal.lua index 78ea3ec..fabd63c 100644 --- a/lua/nvim-ts-autotag/internal.lua +++ b/lua/nvim-ts-autotag/internal.lua @@ -2,6 +2,7 @@ local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils') local configs = require'nvim-treesitter.configs' +local parsers = require'nvim-treesitter.parsers' local M = {} @@ -26,13 +27,14 @@ local HTML_TAG = { skip_tag_pattern = {'quoted_attribute_value', 'end_tag'}, } +local ERROR_TAG = "ERROR" local JSX_TAG = { start_tag_pattern = 'jsx_opening_element', - start_name_tag_pattern = 'identifier', + start_name_tag_pattern = 'identifier|nested_identifier', end_tag_pattern = "jsx_closing_element", end_name_tag_pattern = "identifier", close_tag_pattern = 'jsx_closing_element', - close_name_tag_pattern = 'identifier', + close_name_tag_pattern = 'identifier|nested_identifier', element_tag = 'jsx_element', skip_tag_pattern = {'jsx_closing_element','jsx_expression', 'string', 'jsx_attribute'}, } @@ -62,7 +64,9 @@ M.is_supported = function (lang) end local function is_jsx() - return is_in_table({'typescriptreact', 'javascriptreact', 'javascript.jsx', 'typescript.tsx', 'javascript', 'typescript'}, vim.bo.filetype) + return is_in_table({ + 'typescriptreact', 'javascriptreact', 'javascript.jsx', 'typescript.tsx', 'javascript', 'typescript'}, + vim.bo.filetype) end local function get_ts_tag() @@ -97,17 +101,20 @@ local function find_parent_match(opts) assert(target ~= nil, "find parent target not nil :" .. pattern) local cur_depth = 0 local cur_node = target - while cur_node ~= nil do - local node_type = cur_node:type() - if is_in_table(skip_tag_pattern,node_type) then return nil end - if node_type ~= nil and node_type == pattern then - return cur_node - elseif cur_depth < max_depth then - cur_depth = cur_depth + 1 - cur_node = cur_node:parent() - else - return nil - end + local tbl_pattern = vim.split(pattern, "|") + for _,ptn in pairs(tbl_pattern) do + while cur_node ~= nil do + local node_type = cur_node:type() + if is_in_table(skip_tag_pattern, node_type) then return nil end + if node_type ~= nil and node_type == ptn then + return cur_node + elseif cur_depth < max_depth then + cur_depth = cur_depth + 1 + cur_node = cur_node:parent() + else + cur_node = nil + end + end end return nil end @@ -121,6 +128,7 @@ end local function find_tag_node(opt) local target = opt.target or ts_utils.get_node_at_cursor() + local tag_pattern = opt.tag_pattern local name_tag_pattern = opt.name_tag_pattern local skip_tag_pattern = opt.skip_tag_pattern @@ -140,8 +148,20 @@ local function find_tag_node(opt) }) end if node == nil then return nil end - local tbl_name_pattern = vim.split(name_tag_pattern, '>') local name_node = node + 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 + name_node = find_child_match({ + target = node, + pattern = pattern + }) + if name_node then return name_node end + end + end + + tbl_name_pattern = vim.split(name_tag_pattern, '>') for _, pattern in pairs(tbl_name_pattern) do name_node = find_child_match({ target = name_node, @@ -152,7 +172,7 @@ local function find_tag_node(opt) end local function find_close_tag_node(opt) - opt.find_child=true + opt.find_child = true return find_tag_node(opt) end @@ -195,6 +215,7 @@ local function checkCloseTag() end M.closeTag = function () + parsers.get_parser():parse() local result, tag_name = checkCloseTag() if result == true and tag_name ~= nil then vim.cmd(string.format([[normal! a]],tag_name)) @@ -231,10 +252,11 @@ local function rename_start_tag() if tag_node == nil then return end if not check_tag_correct(tag_node:parent()) then return end local tag_name = get_tag_name(tag_node) + local parent_node = tag_node tag_node = find_parent_match({ - target = tag_node, - pattern = ts_tag.element_tag, + target = parent_node, + pattern = ts_tag.element_tag .. "|" .. ERROR_TAG, max_depth = 2 }) @@ -253,7 +275,7 @@ local function rename_start_tag() else close_tag_node = find_child_match({ target = tag_node, - pattern = 'ERROR' + pattern = ERROR_TAG }) if close_tag_node ~=nil then local close_tag_name = get_tag_name(close_tag_node) @@ -307,14 +329,16 @@ end M.renameTag = function () if validate_rename() then + parsers.get_parser():parse() rename_start_tag() rename_end_tag() end end -M.attach = function (bufnr) - local config = configs.get_module('autotag') - M.setup(config) +M.attach = function (bufnr,lang) + M.lang = lang + local config = configs.get_module('autotag') + M.setup(config) if is_in_table(M.tbl_filetypes, vim.bo.filetype) then if M.enable_close == true then vim.cmd[[inoremap > >:lua require('nvim-ts-autotag.internal').closeTag()a]] diff --git a/tests/closetag_spec.lua b/tests/closetag_spec.lua index 1a2637b..d378113 100644 --- a/tests/closetag_spec.lua +++ b/tests/closetag_spec.lua @@ -168,6 +168,15 @@ local data = { before = [[const data:Array| ]] }, + { + name = "17 typescriptreact nested indentifer " , + filepath = './sample/index.tsx', + filetype = "typescriptreact", + linenr = 12, + key = [[>]], + before = [[| ]] + }, } local run_data = {} for _, value in pairs(data) do @@ -180,6 +189,7 @@ if #run_data == 0 then run_data = data end local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils') _G.TU=ts_utils + local function Test(test_data) for _, value in pairs(test_data) do it("test "..value.name, function() @@ -193,7 +203,7 @@ local function Test(test_data) vim.cmd(":e " .. value.filepath) vim.bo.filetype = value.filetype vim.fn.setline(line , before) - vim.fn.cursor(line, p_before-1) + vim.fn.cursor(line, p_before -1) -- autotag.closeTag() helpers.insert(value.key) local result = vim.fn.getline(line) diff --git a/tests/minimal.vim b/tests/minimal.vim index a759fc6..6aa8821 100644 --- a/tests/minimal.vim +++ b/tests/minimal.vim @@ -1,11 +1,12 @@ set rtp +=. -set rtp +=~/.vim/autoload/plenary.nvim/ -set rtp +=~/.vim/autoload/nvim-treesitter -set rtp +=~/.vim/autoload/playground/ +set rtp +=../plenary.nvim/ +set rtp +=../nvim-treesitter +set rtp +=../playground/ runtime! plugin/plenary.vim runtime! plugin/nvim-treesitter.vim runtime! plugin/playground.vim +runtime! plugin/nvim-ts-autotag.vim set noswapfile set nobackup diff --git a/tests/renametag_spec.lua b/tests/renametag_spec.lua index f2d764d..b088b2a 100644 --- a/tests/renametag_spec.lua +++ b/tests/renametag_spec.lua @@ -124,6 +124,15 @@ local data = { before = [[
dsadsa ]], after = [[ dsadsa ]] }, + { + name = "17 typescriptreact nested indentifer " , + filepath = './sample/index.tsx', + filetype = "typescriptreact", + linenr = 12, + key = [[ciwlala]], + before = [[ ]], + after = [[| ]] + } } local run_data = {} @@ -154,7 +163,7 @@ local function Test(test_data) table.insert(text_before, txt ) if string.match( text, "%|") then pos_before.colnr = string.find(text, '%|') - pos_before.linenr = pos_before.linenr + numlnr + pos_before.linenr = pos_before.linenr + numlnr end numlnr = numlnr + 1 end