mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-01 19:17:02 -04:00
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.
This commit is contained in:
parent
bd2f5d770d
commit
44b7c81002
2 changed files with 35 additions and 32 deletions
|
|
@ -3,7 +3,6 @@ local utils = require "nvim-treesitter.utils"
|
|||
local info = require "nvim-treesitter.info"
|
||||
local configs = require "nvim-treesitter.configs"
|
||||
local parsers = require "nvim-treesitter.parsers"
|
||||
local ts_query = vim.treesitter.query
|
||||
local ts_utils = require "nvim-treesitter.ts_utils"
|
||||
|
||||
-- Registers all query predicates
|
||||
|
|
@ -22,23 +21,6 @@ function M.define_modules(...)
|
|||
configs.define_modules(...)
|
||||
end
|
||||
|
||||
local get_line_for_node = function(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(ts_query.get_node_text(node, bufnr) or ""))
|
||||
-- Escape % to avoid statusline to evaluate content as expression
|
||||
return line:gsub("%%", "%%%%")
|
||||
end
|
||||
|
||||
-- Trim spaces and opening brackets from end
|
||||
local transform_line = function(line)
|
||||
return line:gsub("%s*[%[%(%{]*%s*$", "")
|
||||
|
|
@ -67,7 +49,7 @@ function M.statusline(opts)
|
|||
local expr = current_node
|
||||
|
||||
while expr do
|
||||
local line = get_line_for_node(expr, type_patterns, transform_fn, bufnr)
|
||||
local line = ts_utils._get_line_for_node(expr, type_patterns, transform_fn, bufnr)
|
||||
if line ~= "" and not vim.tbl_contains(lines, line) then
|
||||
table.insert(lines, 1, line)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -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") }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue