From 6c4f0ffbcf77bc14655e8bfafa0a64811345c01c Mon Sep 17 00:00:00 2001 From: windwp Date: Mon, 19 Apr 2021 18:50:38 +0700 Subject: [PATCH] fix #14 --- lua/nvim-ts-autotag/internal.lua | 57 ++++++++++++++++++++------------ lua/nvim-ts-autotag/utils.lua | 20 +++++++++++ sample/index.tsx | 8 ++++- tests/minimal.vim | 7 ++-- tests/renametag_spec.lua | 42 ++++++++++++++++++++++- 5 files changed, 107 insertions(+), 27 deletions(-) create mode 100644 lua/nvim-ts-autotag/utils.lua diff --git a/lua/nvim-ts-autotag/internal.lua b/lua/nvim-ts-autotag/internal.lua index 10c2209..fc838c7 100644 --- a/lua/nvim-ts-autotag/internal.lua +++ b/lua/nvim-ts-autotag/internal.lua @@ -1,6 +1,7 @@ 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 M = {} @@ -126,7 +127,6 @@ 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 @@ -169,7 +169,7 @@ local function find_tag_node(opt) return name_node end -local function find_close_tag_node(opt) +local function find_child_tag_node(opt) opt.find_child = true return find_tag_node(opt) end @@ -194,7 +194,7 @@ local function check_close_tag() max_depth = 2 }) if tag_node ~= nil then - local close_tag_node = find_close_tag_node({ + local close_tag_node = find_child_tag_node({ target = element_node, tag_pattern = ts_tag.end_tag_pattern, name_tag_pattern = ts_tag.end_name_tag_pattern, @@ -231,15 +231,29 @@ local function replace_text_node(node, tag_name) end end -local function check_tag_correct(node) + +local function validate_tag_regex(node,start_regex,end_regex) if node == nil then return false end local texts = ts_utils.get_node_text(node) - if string.match(texts[1],"^%<") and string.match(texts[#texts],"%>$") then - return true - end + if + string.match(texts[1],start_regex) + and string.match(texts[#texts],end_regex) + then return true end return false end +-- local function validate_tag(node) +-- return validate_tag_regex(node,"^%<","%>$") +-- end + +local function validate_start_tag(node) + return validate_tag_regex(node,"^%<%w","%>$") +end + +local function validate_close_tag(node) + return validate_tag_regex(node,"^%<%/%w","%>$") +end + local function rename_start_tag() local ts_tag = get_ts_tag() local tag_node = find_tag_node({ @@ -248,7 +262,7 @@ local function rename_start_tag() }) if tag_node == nil then return end - if not check_tag_correct(tag_node:parent()) then return end + if not validate_start_tag(tag_node:parent()) then return end local tag_name = get_tag_name(tag_node) local parent_node = tag_node @@ -260,25 +274,26 @@ local function rename_start_tag() if tag_node == nil then return end - local close_tag_node = find_close_tag_node({ + local close_tag_node = find_child_tag_node({ target = tag_node, tag_pattern = ts_tag.close_tag_pattern, name_tag_pattern = ts_tag.close_name_tag_pattern, }) + if close_tag_node ~= nil then - local close_tag_name = get_tag_name(close_tag_node) - if tag_name ~=close_tag_name then - replace_text_node(close_tag_node, tag_name) - end - else - close_tag_node = find_child_match({ + local error_node = find_child_match({ target = tag_node, pattern = ERROR_TAG }) - if close_tag_node ~=nil then + if error_node == nil then local close_tag_name = get_tag_name(close_tag_node) - if close_tag_name=='' then - replace_text_node(close_tag_node, "") + if tag_name ~= close_tag_name then + 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 end end @@ -292,7 +307,7 @@ local function rename_end_tag() }) if tag_node == nil then return end - if not check_tag_correct(tag_node:parent()) then return end + if not validate_close_tag(tag_node:parent()) then return end local tag_name = get_tag_name(tag_node) tag_node = find_parent_match({ target = tag_node, @@ -300,12 +315,12 @@ local function rename_end_tag() max_depth = 2 }) if tag_node == nil then return end - local start_tag_node = find_close_tag_node({ + local start_tag_node = find_child_tag_node({ target = tag_node, tag_pattern = ts_tag.start_tag_pattern, name_tag_pattern = ts_tag.start_name_tag_pattern, }) - if not check_tag_correct(start_tag_node:parent()) then return end + if not validate_start_tag(start_tag_node:parent()) then return end if start_tag_node ~= nil then local start_tag_name = get_tag_name(start_tag_node) if tag_name ~= start_tag_name then diff --git a/lua/nvim-ts-autotag/utils.lua b/lua/nvim-ts-autotag/utils.lua new file mode 100644 index 0000000..bf35126 --- /dev/null +++ b/lua/nvim-ts-autotag/utils.lua @@ -0,0 +1,20 @@ +local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils') +local M={} + +M.dump_node = function(node) + local text=ts_utils.get_node_text(node) + for _, txt in pairs(text) do + print(txt) + end +end + +M.is_close_empty_node = function(node) + local tag_name = '' + if node ~= nil then + local text = ts_utils.get_node_text(node) + tag_name = text[#text-1] + end + return tag_name:match("%<%/%>$") +end + +return M diff --git a/sample/index.tsx b/sample/index.tsx index a9a1812..51b677b 100644 --- a/sample/index.tsx +++ b/sample/index.tsx @@ -8,7 +8,13 @@ const SamplePage: React.FC = () => { return (
-
+ + + + + + + diff --git a/tests/minimal.vim b/tests/minimal.vim index 6aa8821..a94b845 100644 --- a/tests/minimal.vim +++ b/tests/minimal.vim @@ -3,10 +3,11 @@ 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 @@ -20,9 +21,7 @@ set indentexpr= lua << EOF - -local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils') -_G.T=ts_utils +_G.__is_log=true _G.test_rename = true _G.test_close = true require("plenary/busted") diff --git a/tests/renametag_spec.lua b/tests/renametag_spec.lua index b088b2a..b0f3897 100644 --- a/tests/renametag_spec.lua +++ b/tests/renametag_spec.lua @@ -132,7 +132,47 @@ local data = { key = [[ciwlala]], before = [[ ]], after = [[| ]] - } + }, + { + name = "18 rename empty node " , + filepath = './sample/index.tsx', + filetype = "typescriptreact", + linenr = 12, + key = [[ilala]], + before = [[<|>
]], + after = [[<|lala>
]] + }, + -- { + -- only = true, + -- name = "18 rename node to empty node " , + -- filepath = './sample/index.tsx', + -- filetype = "typescriptreact", + -- linenr = 12, + -- key = [[ciw]], + -- before = [[
]], + -- after = [[<|>
]] + -- }, + + -- TODO close tag with empty + -- { + -- only = true, + -- name = "18 rename end empty node " , + -- filepath = './sample/index.tsx', + -- filetype = "typescriptreact", + -- linenr = 12, + -- key = [[ilala]], + -- before = [[<>
]], + -- after = [[<|lala>
]] + -- }, + -- { + -- name = "19 rename end empty node " , + -- filepath = './sample/index.tsx', + -- filetype = "typescriptreact", + -- linenr = 12, + -- key = [[ilala]], + -- before = [[<>
]], + -- after = [[<|lala>
]] + -- } } local run_data = {}