diff --git a/lua/nvim-ts-autotag/internal.lua b/lua/nvim-ts-autotag/internal.lua index 26c21c8..ab3e572 100644 --- a/lua/nvim-ts-autotag/internal.lua +++ b/lua/nvim-ts-autotag/internal.lua @@ -1,5 +1,5 @@ local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils') -local get_node_text = vim.treesitter.query.get_node_text +local get_node_text = require('nvim-ts-autotag.utils').get_node_text local configs = require'nvim-treesitter.configs' local parsers = require'nvim-treesitter.parsers' @@ -163,7 +163,7 @@ local function find_parent_match(opts) local function get_tag_name(node) local tag_name = nil if node ~= nil then - tag_name = get_node_text(node, vim.api.nvim_get_current_buf()) + tag_name = get_node_text(node, vim.api.nvim_get_current_buf())[1] end return tag_name end diff --git a/lua/nvim-ts-autotag/utils.lua b/lua/nvim-ts-autotag/utils.lua index 00a1454..f269215 100644 --- a/lua/nvim-ts-autotag/utils.lua +++ b/lua/nvim-ts-autotag/utils.lua @@ -1,9 +1,15 @@ -local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils') local log = require('nvim-ts-autotag._log') -local M={} +local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils') +local get_node_text = vim.treesitter.query.get_node_text or ts_utils.get_node_text +local M = {} + +M.get_node_text = function(...) + local txt = get_node_text(...) + return vim.split(txt, '\n') +end M.dump_node = function(node) - local text=ts_utils.get_node_text(node) + local text = M.get_node_text(node) for _, txt in pairs(text) do print(txt) end @@ -12,19 +18,18 @@ 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] + local text = M.get_node_text(node) + tag_name = text[#text - 1] end - return tag_name:match("%<%/%>$") + return tag_name:match('%<%/%>$') end - M.dump_node_text = function(target) log.debug('=============================') for node in target:iter_children() do local node_type = node:type() - local text = ts_utils.get_node_text(node) - log.debug("type:" .. node_type .. " ") + local text = M.get_node_text(node) + log.debug('type:' .. node_type .. ' ') log.debug(text) end log.debug('=============================')