From 50410bf1d3f7519ac433b4027486a9fe3273049b Mon Sep 17 00:00:00 2001 From: windwp Date: Thu, 11 Mar 2021 09:24:35 +0700 Subject: [PATCH] fix some case on closetag --- lua/nvim-ts-autotag/internal.lua | 66 ++++++++++++++++++++++---------- sample/index.html | 4 ++ sample/index.tsx | 7 ++-- tests/closetag_spec.lua | 60 ++++++++++++++--------------- 4 files changed, 82 insertions(+), 55 deletions(-) diff --git a/lua/nvim-ts-autotag/internal.lua b/lua/nvim-ts-autotag/internal.lua index 6c96443..732a7a9 100644 --- a/lua/nvim-ts-autotag/internal.lua +++ b/lua/nvim-ts-autotag/internal.lua @@ -1,3 +1,5 @@ + + local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils') local configs = require'nvim-treesitter.configs' @@ -53,20 +55,17 @@ local function is_in_table(tbl, val) return false end -M.is_supported=function (lang) +M.is_supported = function (lang) return is_in_table(M.tbl_filetypes,lang) end local function is_jsx() - if is_in_table({'typescriptreact', 'javascriptreact', 'javascript', 'typescript'}, vim.bo.filetype) then - return true - end - return false + return is_in_table({'typescriptreact', 'javascriptreact', 'javascript', 'typescript'}, vim.bo.filetype) end -local function get_ts_tag() +local function get_ts_tag() local ts_tag = HTML_TAG - if(is_jsx()) then ts_tag = JSX_TAG end + if is_jsx() then ts_tag = JSX_TAG end return ts_tag end @@ -123,7 +122,7 @@ local function find_tag_node(opt) local tag_pattern = opt.tag_pattern local name_tag_pattern = opt.name_tag_pattern local skip_tag_pattern = opt.skip_tag_pattern - local find_child = opt.find_child or false + local find_child = opt.find_child or false local node if find_child then node = find_child_match({ @@ -143,7 +142,7 @@ local function find_tag_node(opt) local name_node = node for _, pattern in pairs(tbl_name_pattern) do name_node = find_child_match({ - target = name_node, + target = name_node, pattern = pattern }) end @@ -165,18 +164,42 @@ local function checkCloseTag() }) 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 is_in_table(M.tbl_skipTag, tag_name) then return false end + -- case 6,9 check close on exist node + local element_node = find_parent_match({ + target = tag_node, + pattern = ts_tag.element_tag, + max_depth = 2 + }) + if tag_node ~= nil then + local close_tag_node = find_close_tag_node({ + target = element_node, + tag_pattern = ts_tag.end_tag_pattern, + name_tag_pattern = ts_tag.end_name_tag_pattern, + }) + if close_tag_node ~= nil then + local start_row = tag_node:range() + local close_start_row = close_tag_node:range() + if start_row == close_start_row and tag_name == get_tag_name(close_tag_node) then + return false + end + end + end return true,tag_name end return false end + +function M.code(cmd) + return vim.api.nvim_replace_termcodes(cmd, true, false, true) +end M.closeTag = function () local result, tag_name = checkCloseTag() if result == true and tag_name ~= nil then vim.cmd(string.format([[normal! a]],tag_name)) - vim.cmd[[normal! T>]] + vim.cmd[[normal! F>]] end end @@ -191,20 +214,21 @@ local function replaceTextNode(node, tag_name) end local function checkStartTag() - local ts_tag = HTML_TAG - if(is_jsx()) then ts_tag = JSX_TAG end - local tag_node = find_tag_node({ - tag_pattern = ts_tag.start_tag_pattern, + local ts_tag = get_ts_tag() + local tag_node = find_tag_node({ + tag_pattern = ts_tag.start_tag_pattern, name_tag_pattern = ts_tag.start_name_tag_pattern, }) if tag_node == nil then return end + local tag_name = get_tag_name(tag_node) - tag_node = find_parent_match({ - target = tag_node, - pattern = ts_tag.element_tag, + tag_node = find_parent_match({ + target = tag_node, + pattern = ts_tag.element_tag, max_depth = 2 }) + if tag_node == nil then return end local close_tag_node = find_close_tag_node({ target = tag_node, @@ -263,11 +287,11 @@ M.renameTag = function () checkEndTag() end -M.attach = function (bufnr, lang) +M.attach = function (bufnr) local config = configs.get_module('autotag') M.setup(config) if is_in_table(M.tbl_filetypes,vim.bo.filetype) then - vim.cmd[[inoremap > >:lua require('nvim-ts-autotag.internal').closeTag()i]] + vim.cmd[[inoremap > >:lua require('nvim-ts-autotag.internal').closeTag()a]] bufnr = bufnr or vim.api.nvim_get_current_buf() if M.enableRename == true then vim.cmd("augroup nvim_ts_xmltag_" .. bufnr) @@ -278,7 +302,7 @@ M.attach = function (bufnr, lang) end end -M.detach = function (bufnr ) +M.detach = function ( ) end diff --git a/sample/index.html b/sample/index.html index f54b007..f38ed81 100644 --- a/sample/index.html +++ b/sample/index.html @@ -7,10 +7,14 @@ Document +
+ + +
diff --git a/sample/index.tsx b/sample/index.tsx index 3ebbf66..a9a1812 100644 --- a/sample/index.tsx +++ b/sample/index.tsx @@ -4,14 +4,13 @@ import React, { useCallback, useEffect } from 'react' const SamplePage: React.FC = () => { const [state, setstate] = useState(initialState) - useEffect(() => { - },[]) return (
- -
+
+ +
diff --git a/tests/closetag_spec.lua b/tests/closetag_spec.lua index a5c4db7..3264508 100644 --- a/tests/closetag_spec.lua +++ b/tests/closetag_spec.lua @@ -18,7 +18,7 @@ end local data = { { - name = "html close tag" , + name = "1 html close tag" , filepath = './sample/index.html', filetype = "html", linenr = 10, @@ -27,7 +27,7 @@ local data = { after = [[
|
]] }, { - name = "html close tag" , + name = "2 html close tag" , filepath = './sample/index.html', filetype = "html", linenr = 10, @@ -36,7 +36,7 @@ local data = { after = [[
|
]] }, { - name = "html not close tag on close tag" , + name = "3 html not close tag on close tag" , filepath = './sample/index.html', filetype = "html", linenr = 10, @@ -45,7 +45,7 @@ local data = { after = [[
aa
|]] }, { - name = "html not close on input tag" , + name = "4 html not close on input tag" , filepath = './sample/index.html', filetype = "html", linenr = 10, @@ -54,7 +54,7 @@ local data = { after = [[| ]] }, { - name = "html not close inside quote" , + name = "5 html not close inside quote" , filepath = './sample/index.html', filetype = "html", linenr = 10, @@ -63,16 +63,16 @@ local data = { after = [[
]] }, { - name = "html not close on exist tag" , + name = "6 html not close on exist tag" , filepath = './sample/index.html', filetype = "html", linenr = 10, key = [[>]], - before = [[]], - after = [[
|
]] + before = [[
]], + after = [[
|
]] }, { - name = "typescriptreact close tag" , + name = "7 typescriptreact close tag" , filepath = './sample/index.tsx', filetype = "typescriptreact", linenr = 12, @@ -81,35 +81,35 @@ local data = { after = [[|]] }, { - name = "typescriptreact not close on exist tag" , + name = "8 typescriptreact close" , filepath = './sample/index.tsx', filetype = "typescriptreact", linenr = 12, key = [[>]], - before = [[|
]] + before = [[
|
]] }, { - name = "typescriptreact not close on exist tag" , + name = "9 typescriptreact not close on exist tag" , filepath = './sample/index.tsx', filetype = "typescriptreact", linenr = 12, key = [[>]], + before = [[
]], + after = [[
|
]] + }, + { + name = "10 typescriptreact close on inline script" , + filepath = './sample/index.tsx', + filetype = "typescriptreact", + linenr = 9, + key = [[>]], before = [[const a = () =>
|
]] }, + { - { - name = "typescriptreact close tag" , - filepath = './sample/index.tsx', - filetype = "typescriptreact", - linenr = 12, - key = [[>]], - before = [[|]] - }, - { - name = "typescriptreact not close on close tag" , + name = "11 typescriptreact not close on close tag" , filepath = './sample/index.tsx', filetype = "typescriptreact", linenr = 12, @@ -118,7 +118,7 @@ local data = { after = [[|]] }, { - name = "typescriptreact not close on expresion" , + name = "12 typescriptreact not close on expresion" , filepath = './sample/index.tsx', filetype = "typescriptreact", linenr = 12, @@ -127,7 +127,7 @@ local data = { after = [[ ]] }, { - name = "typescriptreact not close on script" , + name = "13 typescriptreact not close on typescript" , filepath = './sample/index.tsx', filetype = "typescriptreact", linenr = 6, @@ -137,7 +137,7 @@ local data = { }, { - name = "typescriptreact not close on script" , + name = "14 typescriptreact not close on script" , filepath = './sample/index.tsx', filetype = "typescriptreact", linenr = 6, @@ -146,7 +146,7 @@ local data = { after = [[{(card.data >| 0) &&
}]] }, { - name = "vue auto close tag" , + name = "15 vue auto close tag" , filepath = './sample/index.vue', filetype = "vue", linenr = 4, @@ -155,7 +155,7 @@ local data = { after = [[|]] }, { - name = "vue not close on script", + name = "16 vue not close on script", filepath = './sample/index.vue', filetype = "vue", linenr = 12, @@ -197,7 +197,7 @@ local function Test(test_data) local result = vim.fn.getline(line) local pos = vim.fn.getpos('.') eq(after, result , "\n\n [ERROR TEXT]: " .. value.name .. "\n") - eq(p_after, pos[3] + 1, "\n\n [ERROR POS]: " .. value.name .. "\n") + eq(p_after, pos[3] +1, "\n\n [ERROR POS]: " .. value.name .. "\n") else eq(false, true, "\n\n file not exist " .. value.filepath .. "\n") end