From 7f9f55b285fa3893cd866e69ebec30f0a216dadf Mon Sep 17 00:00:00 2001 From: Mikhail Morozov Date: Tue, 16 Jul 2024 14:22:57 +0300 Subject: [PATCH] chore: move text node filtering to utils function --- lua/nvim-ts-autotag/internal.lua | 14 +------------- lua/nvim-ts-autotag/utils.lua | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/lua/nvim-ts-autotag/internal.lua b/lua/nvim-ts-autotag/internal.lua index 551e99e..98ce103 100644 --- a/lua/nvim-ts-autotag/internal.lua +++ b/lua/nvim-ts-autotag/internal.lua @@ -287,20 +287,8 @@ local function validate_tag_regex(node, start_regex, end_regex) if node == nil then return false end - local texts = utils.get_node_text(node) - local filtered = {} - - -- For some nodes (tsx) 'vim.treesitter.get_node_text' can return empty lines or lines with spaces - -- We have to exclude them - for i = 1, #texts do - local text = texts[i]:gsub("^%s*(.-)%s*$", "%1") - if text ~= "" then - filtered[#filtered + 1] = text - end - end - - if string.match(filtered[1], start_regex) and string.match(filtered[#filtered], end_regex) then + if string.match(texts[1], start_regex) and string.match(texts[#texts], end_regex) then return true end return false diff --git a/lua/nvim-ts-autotag/utils.lua b/lua/nvim-ts-autotag/utils.lua index 6b5fbdc..4bffa26 100644 --- a/lua/nvim-ts-autotag/utils.lua +++ b/lua/nvim-ts-autotag/utils.lua @@ -63,7 +63,20 @@ end M.get_node_text = function(node) local _, txt = pcall(get_node_text, node, vim.api.nvim_get_current_buf()) - return vim.split(txt, "\n") or {} + + local texts = vim.split(txt, "\n") + local filtered = {} + + -- For some nodes (tsx) 'vim.treesitter.get_node_text' can return empty lines or lines with spaces + -- We have to exclude them + for i = 1, #texts do + local text = texts[i]:gsub("^%s*(.-)%s*$", "%1") + if text ~= "" then + filtered[#filtered + 1] = text + end + end + + return filtered or {} end -- Stolen from nvim `0.10.0` for `0.9.5` users