Skip to content

Commit

Permalink
fix: migrate deprecated ts_util.get_node_text call (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Apr 18, 2022
1 parent 3c64c5b commit 1bd3cf1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
run_tests:
strategy:
matrix:
nvim_tag: [v0.5.0, v0.5.1, v0.6.0, v0.6.1]
nvim_tag: [v0.5.0, v0.5.1, v0.6.0, v0.6.1, v0.7.0]

name: Run tests
runs-on: ubuntu-latest
Expand Down
26 changes: 17 additions & 9 deletions lua/aerial/backends/treesitter/extensions.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
-- This file is used by the markdown backend as well.
-- We pcall(require) so it doesn't error when nvim-treesitter isn't installed.
local _, ts_utils = pcall(require, "nvim-treesitter.ts_utils")
local has_ts_utils, ts_utils = pcall(require, "nvim-treesitter.ts_utils")
local _, utils = pcall(require, "nvim-treesitter.utils")
local get_node_text
if vim.treesitter.query and vim.treesitter.query.get_node_text then
get_node_text = vim.treesitter.query.get_node_text
elseif has_ts_utils then
get_node_text = function(node, buf)
return ts_utils.get_node_text(node, buf)[1]
end
end
local M = {}

local default_methods = {
Expand Down Expand Up @@ -82,9 +90,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 = ts_utils.get_node_text(type, bufnr)[1] or "<parse error>"
local name = get_node_text(type, bufnr) or "<parse error>"
if trait_node then
local trait = ts_utils.get_node_text(trait_node, bufnr)[1] 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 @@ -96,7 +104,7 @@ M.ruby = {
postprocess = function(bufnr, item, match)
local method = (utils.get_at_path(match, "method") or {}).node
if method then
local fn = ts_utils.get_node_text(method, bufnr)[1] or "<parse error>"
local fn = get_node_text(method, bufnr) or "<parse error>"
if fn ~= "before" and fn ~= "after" then
item.name = fn .. " " .. item.name
end
Expand All @@ -108,7 +116,7 @@ M.lua = {
postprocess = function(bufnr, item, match)
local method = (utils.get_at_path(match, "method") or {}).node
if method then
local fn = ts_utils.get_node_text(method, bufnr)[1] 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 @@ -122,11 +130,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 = ts_utils.get_node_text(method, bufnr)[1] or "<parse error>"
local fn = get_node_text(method, bufnr) or "<parse error>"
if modifier then
fn = fn .. "." .. (ts_utils.get_node_text(modifier, bufnr)[1] or "<parse error>")
fn = fn .. "." .. (get_node_text(modifier, bufnr) or "<parse error>")
end
local str = ts_utils.get_node_text(string, bufnr)[1] or "<parse error>"
local str = get_node_text(string, bufnr) or "<parse error>"
item.name = fn .. " " .. str
end
end,
Expand All @@ -145,7 +153,7 @@ local function c_postprocess(bufnr, item, match)
-- Search the declarator downwards until you hit the identifier
root = root:field("declarator")[1]
end
item.name = ts_utils.get_node_text(root, bufnr)[1] or "<parse error>"
item.name = get_node_text(root, bufnr) or "<parse error>"
end
end

Expand Down
12 changes: 10 additions & 2 deletions lua/aerial/backends/treesitter/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ M.fetch_symbols_sync = function(bufnr)
local extensions = require("aerial.backends.treesitter.extensions")
local parsers = require("nvim-treesitter.parsers")
local query = require("nvim-treesitter.query")
local ts_utils = require("nvim-treesitter.ts_utils")
local get_node_text
if 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 utils = require("nvim-treesitter.utils")
local include_kind = config.get_filter_kind_map(bufnr)
local parser = parsers.get_parser(bufnr)
Expand Down Expand Up @@ -78,7 +86,7 @@ M.fetch_symbols_sync = function(bufnr)
local end_row, end_col = end_node:end_()
local name
if name_node then
name = ts_utils.get_node_text(name_node, bufnr)[1] or "<parse error>"
name = get_node_text(name_node, bufnr) or "<parse error>"
else
name = "<Anonymous>"
end
Expand Down
2 changes: 1 addition & 1 deletion tests/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ vim.bo.swapfile = false
vim.cmd([[autocmd BufRead,BufNewFile *.jl setfiletype julia]])

require("nvim-treesitter.configs").setup({
ensure_installed = "maintained",
ensure_installed = "all",
})

0 comments on commit 1bd3cf1

Please sign in to comment.