Skip to content

Commit

Permalink
chore: move text node filtering to utils function
Browse files Browse the repository at this point in the history
  • Loading branch information
inferst committed Jul 16, 2024
1 parent 0782035 commit 7f9f55b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
14 changes: 1 addition & 13 deletions lua/nvim-ts-autotag/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion lua/nvim-ts-autotag/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7f9f55b

Please sign in to comment.