Skip to content

Commit

Permalink
fix: deprecated treesitter API in nvim nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Mar 25, 2023
1 parent 9ee7184 commit a6b86fd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
43 changes: 28 additions & 15 deletions lua/aerial/backends/treesitter/extensions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
local _, utils = pcall(require, "nvim-treesitter.utils")
local M = {}

local get_node_text
if vim.treesitter.get_node_text then
-- Neovim 0.9
get_node_text = vim.treesitter.get_node_text
elseif vim.treesitter.query and vim.treesitter.query.get_node_text then
get_node_text = vim.treesitter.query.get_node_text
else
local ts_utils = require("nvim-treesitter.ts_utils")
get_node_text = function(node, buf)
return ts_utils.get_node_text(node, buf)[1]
end
end

local default_methods = {
get_parent = function(stack, match, node)
local ts_utils = require("nvim-treesitter.ts_utils")
Expand Down Expand Up @@ -71,7 +84,7 @@ M.elixir = {
postprocess = function(bufnr, item, match)
local identifier = (utils.get_at_path(match, "identifier") or {}).node
if identifier then
local name = vim.treesitter.query.get_node_text(identifier, bufnr) or "<parse error>"
local name = get_node_text(identifier, bufnr) or "<parse error>"
if name == "defp" then
item.scope = "private"
end
Expand All @@ -83,7 +96,7 @@ M.elixir = {
return false
elseif name == "defimpl" then
local protocol = (utils.get_at_path(match, "protocol") or {}).node
local protocol_name = vim.treesitter.query.get_node_text(protocol, bufnr) or "<parse error>"
local protocol_name = get_node_text(protocol, bufnr) or "<parse error>"
item.name = string.format("%s > %s", item.name, protocol_name)
elseif name == "test" then
item.name = string.format("test %s", item.name)
Expand Down Expand Up @@ -124,7 +137,7 @@ M.go = {
postprocess = function(bufnr, item, match)
local receiver = (utils.get_at_path(match, "receiver") or {}).node
if receiver then
local receiver_text = vim.treesitter.query.get_node_text(receiver, bufnr) or "<parse error>"
local receiver_text = get_node_text(receiver, bufnr) or "<parse error>"
item.name = string.format("%s %s", receiver_text, item.name)
end
return true
Expand Down Expand Up @@ -161,7 +174,7 @@ M.help = {
if vim.startswith(match.type.node:type(), "h") then
while node and node:type() == "word" do
local row, col = node:start()
table.insert(pieces, 1, vim.treesitter.query.get_node_text(node, bufnr))
table.insert(pieces, 1, get_node_text(node, bufnr))
node = node:prev_sibling()
item.lnum = row + 1
item.col = col
Expand Down Expand Up @@ -192,9 +205,9 @@ M.rust = {
if item.kind == "Class" then
local trait_node = (utils.get_at_path(match, "trait") or {}).node
local type = (utils.get_at_path(match, "rust_type") or {}).node
local name = vim.treesitter.query.get_node_text(type, bufnr) or "<parse error>"
local name = get_node_text(type, bufnr) or "<parse error>"
if trait_node then
local trait = vim.treesitter.query.get_node_text(trait_node, bufnr) or "<parse error>"
local trait = get_node_text(trait_node, bufnr) or "<parse error>"
name = string.format("%s > %s", name, trait)
end
item.name = name
Expand All @@ -206,7 +219,7 @@ M.ruby = {
postprocess = function(bufnr, item, match)
local method = (utils.get_at_path(match, "method") or {}).node
if method then
local fn = vim.treesitter.query.get_node_text(method, bufnr) or "<parse error>"
local fn = get_node_text(method, bufnr) or "<parse error>"
if fn ~= item.name then
item.name = fn .. " " .. item.name
end
Expand All @@ -218,7 +231,7 @@ M.lua = {
postprocess = function(bufnr, item, match)
local method = (utils.get_at_path(match, "method") or {}).node
if method then
local fn = vim.treesitter.query.get_node_text(method, bufnr) or "<parse error>"
local fn = get_node_text(method, bufnr) or "<parse error>"
if fn == "it" or fn == "describe" then
item.name = fn .. " " .. string.sub(item.name, 2, string.len(item.name) - 1)
end
Expand All @@ -232,11 +245,11 @@ M.javascript = {
local modifier = (utils.get_at_path(match, "modifier") or {}).node
local string = (utils.get_at_path(match, "string") or {}).node
if method and string then
local fn = vim.treesitter.query.get_node_text(method, bufnr) or "<parse error>"
local fn = get_node_text(method, bufnr) or "<parse error>"
if modifier then
fn = fn .. "." .. (vim.treesitter.query.get_node_text(modifier, bufnr) or "<parse error>")
fn = fn .. "." .. (get_node_text(modifier, bufnr) or "<parse error>")
end
local str = vim.treesitter.query.get_node_text(string, bufnr) or "<parse error>"
local str = get_node_text(string, bufnr) or "<parse error>"
item.name = fn .. " " .. str
end
end,
Expand All @@ -263,7 +276,7 @@ local function c_postprocess(bufnr, item, match)
break
end
end
item.name = vim.treesitter.query.get_node_text(root, bufnr) or "<parse error>"
item.name = get_node_text(root, bufnr) or "<parse error>"
end
end

Expand Down Expand Up @@ -292,11 +305,11 @@ M.typescript = {
local modifier = (utils.get_at_path(match, "modifier") or {}).node
local string = (utils.get_at_path(match, "string") or {}).node
if method and string then
local fn = vim.treesitter.query.get_node_text(method, bufnr) or "<parse error>"
local fn = get_node_text(method, bufnr) or "<parse error>"
if modifier then
fn = fn .. "." .. (vim.treesitter.query.get_node_text(modifier, bufnr) or "<parse error>")
fn = fn .. "." .. (get_node_text(modifier, bufnr) or "<parse error>")
end
local str = vim.treesitter.query.get_node_text(string, bufnr) or "<parse error>"
local str = get_node_text(string, bufnr) or "<parse error>"
item.name = fn .. " " .. str
end
end,
Expand Down
4 changes: 3 additions & 1 deletion lua/aerial/backends/treesitter/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ M.fetch_symbols_sync = function(bufnr)
local parsers = require("nvim-treesitter.parsers")
local query = require("nvim-treesitter.query")
local get_node_text
if vim.treesitter.query and vim.treesitter.query.get_node_text then
if vim.treesitter.get_node_text then
get_node_text = vim.treesitter.get_node_text
elseif vim.treesitter.query and vim.treesitter.query.get_node_text then
get_node_text = vim.treesitter.query.get_node_text
else
local ts_utils = require("nvim-treesitter.ts_utils")
Expand Down

0 comments on commit a6b86fd

Please sign in to comment.