Skip to content

Commit 2bac23e

Browse files
authored
feat: migrate to nvim-treesitter 'main' (#34)
1 parent 8bc68e1 commit 2bac23e

3 files changed

Lines changed: 38 additions & 70 deletions

File tree

lua/python/treesitter/init.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local tsutil = require("nvim-treesitter.ts_utils")
21
local nodes = require("python.treesitter.nodes")
32

43
local PythonTreeSitter = {
@@ -8,7 +7,7 @@ local PythonTreeSitter = {
87
}
98

109
function PythonTreeSitter.test_ts_queries()
11-
local current_node = tsutil.get_node_at_cursor()
10+
local current_node = vim.treesitter.get_node()
1211
if not current_node then
1312
return
1413
end

lua/python/treesitter/nodes.lua

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
local PythonTreeSitterNodes = {}
22

33
-- part of the code from polarmutex/contextprint.nvim
4-
local ts_utils = require("nvim-treesitter.ts_utils")
5-
local ts_query = require("nvim-treesitter.query")
6-
local parsers = require("nvim-treesitter.parsers")
7-
local locals = require("nvim-treesitter.locals")
8-
-- local vim_query = require("vim.treesitter.query")
94
local api = vim.api
105
local fn = vim.fn
116
local get_node_text = vim.treesitter.get_node_text
127
local parse = vim.treesitter.query.parse
13-
if parse == nil then
14-
parse = vim.treesitter.query.parse_query
8+
9+
-- Helper function to convert 0-indexed treesitter range to 1-indexed vim range
10+
local function get_vim_range(node)
11+
local start_row, start_col, end_row, end_col = node:range()
12+
return start_row + 1, start_col + 1, end_row + 1, end_col + 1
13+
end
14+
15+
-- Helper function to recurse through match captures (replaces locals.recurse_local_nodes)
16+
local function recurse_captures(match, query, callback)
17+
for id, node in pairs(match) do
18+
local name = query.captures[id]
19+
if name then
20+
callback(id, node, name)
21+
end
22+
end
1523
end
1624

1725
PythonTreeSitterNodes.count_parents = function(node)
@@ -54,21 +62,19 @@ PythonTreeSitterNodes.get_nodes = function(query, lang, defaults, bufnr)
5462
return nil
5563
end
5664

57-
local parser = parsers.get_parser(bufnr, lang)
65+
local parser = vim.treesitter.get_parser(bufnr, lang)
5866
local root = parser:parse()[1]:root()
5967
local start_row, _, end_row, _ = root:range()
6068
local results = {}
61-
for match in ts_query.iter_prepared_matches(parsed_query, root, bufnr, start_row, end_row) do
69+
for pattern, match, metadata in parsed_query:iter_matches(root, bufnr, start_row, end_row) do
6270
local sRow, sCol, eRow, eCol
6371
local declaration_node
6472
local type = "nil"
6573
local name = "nil"
66-
locals.recurse_local_nodes(match, function(_, node, path)
74+
recurse_captures(match, parsed_query, function(_, node, path)
6775
local idx = string.find(path, ".", 1, true)
6876
local op = string.sub(path, idx + 1, #path)
6977

70-
-- local a1, b1, c1, d1 = vim.treesitter.get_node_range(node)
71-
7278
type = string.sub(path, 1, idx - 1)
7379
if name == nil then
7480
name = defaults[type] or "empty"
@@ -78,11 +84,7 @@ PythonTreeSitterNodes.get_nodes = function(query, lang, defaults, bufnr)
7884
name = get_node_text(node, bufnr)
7985
elseif op == "declaration" then
8086
declaration_node = node
81-
sRow, sCol, eRow, eCol = node:range()
82-
sRow = sRow + 1
83-
eRow = eRow + 1
84-
sCol = sCol + 1
85-
eCol = eCol + 1
87+
sRow, sCol, eRow, eCol = get_vim_range(node)
8688
end
8789
end)
8890

@@ -124,12 +126,12 @@ PythonTreeSitterNodes.get_all_nodes = function(query, lang, defaults, bufnr, pos
124126
return nil
125127
end
126128

127-
local parser = parsers.get_parser(bufnr, lang)
129+
local parser = vim.treesitter.get_parser(bufnr, lang)
128130
local root = parser:parse()[1]:root()
129131
local start_row, _, end_row, _ = root:range()
130132
local results = {}
131133
local node_type
132-
for match in ts_query.iter_prepared_matches(parsed_query, root, bufnr, start_row, end_row) do
134+
for pattern, match, metadata in parsed_query:iter_matches(root, bufnr, start_row, end_row) do
133135
local sRow, sCol, eRow, eCol
134136
local declaration_node
135137
local type_node
@@ -139,14 +141,13 @@ PythonTreeSitterNodes.get_all_nodes = function(query, lang, defaults, bufnr, pos
139141
-- local method_receiver = ""
140142
-- ulog(match)
141143

142-
locals.recurse_local_nodes(match, function(_, node, path)
144+
recurse_captures(match, parsed_query, function(_, node, path)
143145
-- local idx = string.find(path, ".", 1, true)
144146
-- The query may return multiple nodes, e.g.
145147
-- (type_declaration (type_spec name:(type_identifier)@type_decl.name type:(type_identifier)@type_decl.type))@type_decl.declaration
146148
-- returns { { @type_decl.name, @type_decl.type, @type_decl.declaration} ... }
147149
local idx = string.find(path, ".[^.]*$") -- find last `.`
148150
op = string.sub(path, idx + 1, #path)
149-
local a1, b1, c1, d1 = vim.treesitter.get_node_range(node)
150151
local dbg_txt = get_node_text(node, bufnr) or ""
151152
if #dbg_txt > 100 then
152153
dbg_txt = string.sub(dbg_txt, 1, 100) .. "..."
@@ -172,8 +173,7 @@ PythonTreeSitterNodes.get_all_nodes = function(query, lang, defaults, bufnr, pos
172173
type_node = node
173174
elseif op == 'declaration' or op == 'clause' then
174175
declaration_node = node
175-
sRow, sCol, eRow, eCol =
176-
ts_utils.get_vim_range({ vim.treesitter.get_node_range(node) }, bufnr)
176+
sRow, sCol, eRow, eCol = get_vim_range(node)
177177
else
178178
-- ulog('unknown op: ' .. op)
179179
end
@@ -194,7 +194,7 @@ PythonTreeSitterNodes.get_all_nodes = function(query, lang, defaults, bufnr, pos
194194
end
195195
if type_node ~= nil and ntype then
196196
-- ulog('type_only')
197-
sRow, sCol, eRow, eCol = ts_utils.get_vim_range({ vim.treesitter.get_node_range(type_node) }, bufnr)
197+
sRow, sCol, eRow, eCol = get_vim_range(type_node)
198198
table.insert(results, {
199199
type_node = type_node,
200200
dim = { s = { r = sRow, c = sCol }, e = { r = eRow, c = eCol } },
@@ -266,7 +266,7 @@ PythonTreeSitterNodes.nodes_at_cursor = function(query, default, bufnr, ntype)
266266
end
267267

268268
function PythonTreeSitterNodes.inside_function()
269-
local current_node = ts_utils.get_node_at_cursor()
269+
local current_node = vim.treesitter.get_node()
270270
if not current_node then
271271
return false
272272
end

scripts/minimal_init.lua

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,51 +18,20 @@ vim.cmd("set rtp+=" .. runtime_path)
1818
require("luasnip").setup()
1919
require("luasnip.extras.fmt")
2020
require("luasnip.nodes.absolute_indexer")
21-
require("nvim-treesitter.locals")
22-
require("nvim-treesitter").setup()
2321
require("mini.test").setup()
2422
require("mini.doc").setup()
25-
require("nvim-treesitter.configs").setup({
26-
modules = {
27-
"highlight",
28-
},
29-
sync_install = false,
30-
auto_install = true,
31-
ignore_install = {},
32-
ensure_installed = {},
33-
highlight = {
34-
enable = true,
35-
},
36-
})
3723

38-
-- Clean path for use in a prefix comparison
39-
---@param input string
40-
---@return string
41-
local function clean_path(input)
42-
local pth = vim.fn.fnamemodify(input, ":p")
43-
if vim.fn.has("win32") == 1 then
44-
pth = pth:gsub("/", "\\")
45-
end
46-
return pth
47-
end
24+
-- Setup nvim-treesitter
25+
require("nvim-treesitter").install("python")
4826

49-
local function ts_is_installed(lang)
50-
local matched_parsers = vim.api.nvim_get_runtime_file("parser/" .. lang .. ".so", true) or {}
51-
local configs = require("nvim-treesitter.configs")
52-
local install_dir = configs.get_parser_install_dir()
53-
if not install_dir then
54-
return false
55-
end
56-
install_dir = clean_path(install_dir)
57-
for _, path in ipairs(matched_parsers) do
58-
local abspath = clean_path(path)
59-
if vim.startswith(abspath, install_dir) then
60-
return true
61-
end
62-
end
63-
return false
64-
end
27+
vim.api.nvim_create_autocmd("FileType", {
28+
group = vim.api.nvim_create_augroup("PythonTreesitter", { clear = true }),
29+
pattern = "python",
30+
desc = "Enable treesitter highlighting and indentation",
31+
callback = function(event)
32+
local buf = event.buf
6533

66-
if not ts_is_installed("python") then
67-
vim.cmd("TSInstallSync python")
68-
end
34+
-- Start highlighting
35+
pcall(vim.treesitter.start, buf, "python")
36+
end,
37+
})

0 commit comments

Comments
 (0)