From 44b7c8100269161e20d585f24bce322f6dcdf8d2 Mon Sep 17 00:00:00 2001 From: francisco souza <108725+fsouza@users.noreply.github.com> Date: Thu, 21 Apr 2022 10:23:36 -0400 Subject: fix(ts_utils): fix swap_nodes after get_node_text change After some discussion, it looks like the easiest thing to do for now is to keep a private copy of get_node_text (just to skip the deprecation message) and invoke that, until core provides an equivalent function that can return the node content in a table representing the node "lines". Also fixes the statusline by calling the private version for get_node_text until a change is made in core. --- lua/nvim-treesitter/ts_utils.lua | 47 +++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 13 deletions(-) (limited to 'lua/nvim-treesitter/ts_utils.lua') diff --git a/lua/nvim-treesitter/ts_utils.lua b/lua/nvim-treesitter/ts_utils.lua index c95ddf54..8200e78e 100644 --- a/lua/nvim-treesitter/ts_utils.lua +++ b/lua/nvim-treesitter/ts_utils.lua @@ -2,20 +2,10 @@ local api = vim.api local parsers = require "nvim-treesitter.parsers" local utils = require "nvim-treesitter.utils" -local ts_query = vim.treesitter.query local M = {} ---- Gets the actual text content of a node --- @deprecated Use vim.treesitter.query.get_node_text --- @param node the node to get the text from --- @param bufnr the buffer containing the node --- @return list of lines of text of the node -function M.get_node_text(node, bufnr) - vim.notify_once( - "nvim-treesitter.ts_utils.get_node_text is deprecated: use vim.treesitter.query.get_node_text", - vim.log.levels.WARN - ) +local function get_node_text(node, bufnr) local bufnr = bufnr or api.nvim_get_current_buf() if not node then return {} @@ -39,6 +29,37 @@ function M.get_node_text(node, bufnr) end end +---@private +function M._get_line_for_node(node, type_patterns, transform_fn, bufnr) + local node_type = node:type() + local is_valid = false + for _, rgx in ipairs(type_patterns) do + if node_type:find(rgx) then + is_valid = true + break + end + end + if not is_valid then + return "" + end + local line = transform_fn(vim.trim(get_node_text(node, bufnr)[1] or "")) + -- Escape % to avoid statusline to evaluate content as expression + return line:gsub("%%", "%%%%") +end + +--- Gets the actual text content of a node +-- @deprecated Use vim.treesitter.query.get_node_text +-- @param node the node to get the text from +-- @param bufnr the buffer containing the node +-- @return list of lines of text of the node +function M.get_node_text(node, bufnr) + vim.notify_once( + "nvim-treesitter.ts_utils.get_node_text is deprecated: use vim.treesitter.query.get_node_text", + vim.log.levels.WARN + ) + return get_node_text(node, bufnr) +end + --- Determines whether a node is the parent of another -- @param dest the possible parent -- @param source the possible child node @@ -344,8 +365,8 @@ function M.swap_nodes(node_or_range1, node_or_range2, bufnr, cursor_to_second) local range1 = M.node_to_lsp_range(node_or_range1) local range2 = M.node_to_lsp_range(node_or_range2) - local text1 = ts_query.get_node_text(node_or_range1) - local text2 = ts_query.get_node_text(node_or_range2) + local text1 = get_node_text(node_or_range1, bufnr) + local text2 = get_node_text(node_or_range2, bufnr) local edit1 = { range = range1, newText = table.concat(text2, "\n") } local edit2 = { range = range2, newText = table.concat(text1, "\n") } -- cgit v1.2.3