Skip to content

Commit

Permalink
fix: get_range shim for playground
Browse files Browse the repository at this point in the history
  • Loading branch information
dtomvan committed Mar 24, 2023
1 parent cb568af commit fd25dad
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
7 changes: 6 additions & 1 deletion lua/nvim-treesitter/compat.lua
@@ -1,5 +1,6 @@
-- Shim module to address deprecations across nvim versions
local tsq = vim.treesitter.query
local ts = vim.treesitter
local tsq = ts.query

local M = {}

Expand All @@ -15,4 +16,8 @@ function M.parse_query(lang, query)
return (tsq.parse or tsq.parse_query)(lang, query)
end

function M.get_range(node, source, metadata)
return (ts.get_range or tsq.get_range)(node, source, metadata)
end

return M
14 changes: 5 additions & 9 deletions lua/nvim-treesitter/locals.lua
Expand Up @@ -4,7 +4,7 @@

local queries = require "nvim-treesitter.query"
local ts_utils = require "nvim-treesitter.ts_utils"
local ts_query = vim.treesitter.query
local ts = vim.treesitter
local api = vim.api

local M = {}
Expand Down Expand Up @@ -170,7 +170,7 @@ M.get_definitions_lookup_table = ts_utils.memoize_by_buf_tick(function(bufnr)
local scopes = M.get_definition_scopes(node_entry.node, bufnr, node_entry.scope)
-- Always use the highest valid scope
local scope = scopes[#scopes]
local node_text = ts_query.get_node_text(node_entry.node, bufnr)
local node_text = ts.get_node_text(node_entry.node, bufnr)
local id = M.get_definition_id(scope, node_text)

result[id] = node_entry
Expand Down Expand Up @@ -223,7 +223,7 @@ end
---@return string|nil kind
function M.find_definition(node, bufnr)
local def_lookup = M.get_definitions_lookup_table(bufnr)
local node_text = ts_query.get_node_text(node, bufnr)
local node_text = ts.get_node_text(node, bufnr)

for scope in M.iter_scope_tree(node, bufnr) do
local id = M.get_definition_id(scope, node_text)
Expand All @@ -244,7 +244,7 @@ end
---@return TSNode[]: a list of nodes
function M.find_usages(node, scope_node, bufnr)
bufnr = bufnr or api.nvim_get_current_buf()
local node_text = ts_query.get_node_text(node, bufnr)
local node_text = ts.get_node_text(node, bufnr)

if not node_text or #node_text < 1 then
return {}
Expand All @@ -254,11 +254,7 @@ function M.find_usages(node, scope_node, bufnr)
local usages = {}

for match in M.iter_locals(bufnr, scope_node) do
if
match.reference
and match.reference.node
and ts_query.get_node_text(match.reference.node, bufnr) == node_text
then
if match.reference and match.reference.node and ts.get_node_text(match.reference.node, bufnr) == node_text then
local def_node, _, kind = M.find_definition(match.reference.node, bufnr)

if kind == nil or def_node == node then
Expand Down
2 changes: 1 addition & 1 deletion queries/lua/injections.scm
Expand Up @@ -14,7 +14,7 @@
((function_call
name: (_) @_vimcmd_identifier
arguments: (arguments (string content: _ @query) .))
(#any-of? @_vimcmd_identifier "vim.treesitter.query.set" "vim.treesitter.query.parse_query"))
(#any-of? @_vimcmd_identifier "vim.treesitter.query.set" "vim.treesitter.query.parse_query" "vim.treesitter.query.parse"))

;; highlight string as query if starts with `;; query`
((string ("string_content") @query) (#lua-match? @query "^%s*;+%s?query"))
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/ts_utils_spec.lua
Expand Up @@ -71,8 +71,8 @@ describe("swap_nodes", function()
local function swap(case)
vim.api.nvim_buf_set_lines(0, 0, -1, false, case.lines)
vim.opt.filetype = case.filetype
local a = vim.treesitter.get_node_at_pos(0, case.a[1], case.a[2], {})
local b = vim.treesitter.get_node_at_pos(0, case.b[1], case.b[2], {})
local a = vim.treesitter.get_node(0, case.a[1], case.a[2], {})
local b = vim.treesitter.get_node(0, case.b[1], case.b[2], {})
tsutils.swap_nodes(a, b, 0, true)
end

Expand Down

0 comments on commit fd25dad

Please sign in to comment.