ref(helpers) Improve get_node_text deprecation

This commit is contained in:
Daniel Mathiot
2022-06-14 19:08:46 +02:00
parent f5fbf3130c
commit 08c88e2a85
3 changed files with 17 additions and 5 deletions

View File

@@ -1,4 +1,5 @@
local nodes_utils = require("neogen.utilities.nodes") local nodes_utils = require("neogen.utilities.nodes")
local helpers = require("neogen.utilities.helpers")
local extractors = require("neogen.utilities.extractors") local extractors = require("neogen.utilities.extractors")
local locator = require("neogen.locators.default") local locator = require("neogen.locators.default")
local template = require("neogen.template") local template = require("neogen.template")
@@ -109,7 +110,7 @@ return {
-- Check if function is a static method. If so, will not remove the first parameter -- Check if function is a static method. If so, will not remove the first parameter
if node:parent():type() == "decorated_definition" then if node:parent():type() == "decorated_definition" then
local decorator = nodes_utils:matching_child_nodes(node:parent(), "decorator") local decorator = nodes_utils:matching_child_nodes(node:parent(), "decorator")
decorator = vim.treesitter.query.get_node_text(decorator[1], 0) decorator = helpers.get_node_text(decorator[1])
if decorator == "@staticmethod" then if decorator == "@staticmethod" then
remove_identifier = false remove_identifier = false
end end
@@ -128,7 +129,7 @@ return {
results[i.Return] = res.return_statement results[i.Return] = res.return_statement
results[i.ReturnTypeHint] = res[i.ReturnTypeHint] results[i.ReturnTypeHint] = res[i.ReturnTypeHint]
results[i.HasReturn] = (res.return_statement or res.anonymous_return or res[i.ReturnTypeHint]) results[i.HasReturn] = (res.return_statement or res.anonymous_return or res[i.ReturnTypeHint])
and { true } and { true }
or nil or nil
results[i.ArbitraryArgs] = res[i.ArbitraryArgs] results[i.ArbitraryArgs] = res[i.ArbitraryArgs]
results[i.Kwargs] = res[i.Kwargs] results[i.Kwargs] = res[i.Kwargs]
@@ -184,7 +185,7 @@ return {
for _, assignment in pairs(nodes["assignment"]) do for _, assignment in pairs(nodes["assignment"]) do
local left_side = assignment:field("left")[1] local left_side = assignment:field("left")[1]
local left_attribute = left_side:field("attribute")[1] local left_attribute = left_side:field("attribute")[1]
left_attribute = vim.treesitter.query.get_node_text(left_attribute, 0) left_attribute = helpers.get_node_text(left_attribute)
if left_attribute and not vim.startswith(left_attribute, "_") then if left_attribute and not vim.startswith(left_attribute, "_") then
table.insert(results[i.ClassAttribute], left_attribute) table.insert(results[i.ClassAttribute], left_attribute)
end end
@@ -232,7 +233,7 @@ return {
if child:type() == "comment" then if child:type() == "comment" then
local start_row = child:start() local start_row = child:start()
if start_row == 0 then if start_row == 0 then
if vim.startswith(vim.treesitter.query.get_node_text(node, 0), "#!") then if vim.startswith(helpers.get_node_text(node), "#!") then
return 1, 0 return 1, 0
end end
end end

View File

@@ -1,3 +1,5 @@
local helpers = require("neogen.utilities.helpers")
return { return {
--- Extract the content from each node from data --- Extract the content from each node from data
--- @param _ any self --- @param _ any self
@@ -12,7 +14,7 @@ return {
return node:type() return node:type()
end end
local get_text = function(node) local get_text = function(node)
return vim.treesitter.query.get_node_text(node, 0) return helpers.get_node_text(node)
end end
if opts.type then if opts.type then
result[k] = vim.tbl_map(get_type, v) result[k] = vim.tbl_map(get_type, v)

View File

@@ -22,4 +22,13 @@ return {
split = function(s, sep, plain) split = function(s, sep, plain)
return vim.fn.has("nvim-0.6") == 1 and vim.split(s, sep, { plain = plain }) or vim.split(s, sep, plain) return vim.fn.has("nvim-0.6") == 1 and vim.split(s, sep, { plain = plain }) or vim.split(s, sep, plain)
end, end,
--- Gets the text from the node
---@private
---@param node userdata node to fetch text from
---@param bufnr? number originated buffer number. Defaults to 0
---@return table newline separated list of text
get_node_text = function(node, bufnr)
return vim.split(vim.treesitter.query.get_node_text(node, bufnr or 0), "\n")
end,
} }