Skip to content

Commit 12a669f

Browse files
authored
fix: update treesitter functions (#534)
1 parent b2a675e commit 12a669f

File tree

10 files changed

+71
-35
lines changed

10 files changed

+71
-35
lines changed

lua/orgmode/colors/markup_highlighter.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local ts = require('orgmode.treesitter.compat')
12
local config = require('orgmode.config')
23
local ts_utils = require('nvim-treesitter.ts_utils')
34
local query = nil
@@ -226,7 +227,7 @@ local function load_deps()
226227
if query then
227228
return
228229
end
229-
query = vim.treesitter.get_query('org', 'markup')
230+
query = ts.get_query('org', 'markup')
230231
vim.treesitter.query.add_predicate('org-is-valid-markup-range?', is_valid_markup_range)
231232
vim.treesitter.query.add_predicate('org-is-valid-hyperlink-range?', is_valid_hyperlink_range)
232233
vim.treesitter.query.add_predicate('org-is-valid-latex-range?', is_valid_latex_range)

lua/orgmode/colors/todo_highlighter.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
local ts = require('orgmode.treesitter.compat')
12
local config = require('orgmode.config')
23
local highlights = require('orgmode.colors.highlights')
34
local tree_utils = require('orgmode.utils.treesitter')
45
local utils = require('orgmode.utils')
56

67
local function add_todo_keyword_highlights()
7-
local query_files = vim.treesitter.get_query_files('org', 'highlights')
8+
local query_files = ts.get_query_files('org', 'highlights')
89
if not query_files or #query_files == 0 then
910
return
1011
end
@@ -51,7 +52,7 @@ local function add_todo_keyword_highlights()
5152
for _, v in ipairs(lines) do
5253
table.insert(all_lines, v)
5354
end
54-
vim.treesitter.set_query('org', 'highlights', table.concat(all_lines, '\n'))
55+
ts.set_query('org', 'highlights', table.concat(all_lines, '\n'))
5556
if vim.bo.filetype == 'org' then
5657
tree_utils.restart_highlights()
5758
end

lua/orgmode/org/indent.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local ts = require('orgmode.treesitter.compat')
12
local config = require('orgmode.config')
23
local ts_utils = require('nvim-treesitter.ts_utils')
34
local query = nil
@@ -25,7 +26,7 @@ local get_matches = ts_utils.memoize_by_buf_tick(function(bufnr)
2526
}
2627

2728
if type == 'headline' then
28-
opts.stars = vim.treesitter.query.get_node_text(node:field('stars')[1], bufnr):len()
29+
opts.stars = ts.get_node_text(node:field('stars')[1], bufnr):len()
2930
opts.indent = opts.indent + opts.stars + 1
3031
matches[range.start.line + 1] = opts
3132
end
@@ -52,7 +53,7 @@ local get_matches = ts_utils.memoize_by_buf_tick(function(bufnr)
5253
end
5354
if parent then
5455
local headline = parent:named_child('headline')
55-
local stars = vim.treesitter.query.get_node_text(headline:field('stars')[1], bufnr):len()
56+
local stars = ts.get_node_text(headline:field('stars')[1], bufnr):len()
5657
opts.indent = stars + 1
5758
for i = range.start.line, range['end'].line - 1 do
5859
matches[i + 1] = opts
@@ -67,7 +68,7 @@ end)
6768

6869
local prev_section = nil
6970
local function foldexpr()
70-
query = query or vim.treesitter.get_query('org', 'org_indent')
71+
query = query or ts.get_query('org', 'org_indent')
7172
local matches = get_matches(0)
7273
local match = matches[vim.v.lnum]
7374
local next_match = matches[vim.v.lnum + 1]
@@ -114,7 +115,7 @@ end
114115

115116
local function indentexpr()
116117
local noindent_mode = config.org_indent_mode == 'noindent'
117-
query = query or vim.treesitter.get_query('org', 'org_indent')
118+
query = query or ts.get_query('org', 'org_indent')
118119

119120
local prev_linenr = vim.fn.prevnonblank(vim.v.lnum - 1)
120121

lua/orgmode/org/mappings.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ local constants = require('orgmode.utils.constants')
1111
local ts_utils = require('nvim-treesitter.ts_utils')
1212
local utils = require('orgmode.utils')
1313
local ts_org = require('orgmode.treesitter')
14+
local ts = require('orgmode.treesitter.compat')
1415
local ts_table = require('orgmode.treesitter.table')
1516
local EventManager = require('orgmode.events')
1617
local Promise = require('orgmode.utils.promise')
@@ -599,7 +600,7 @@ function OrgMappings:handle_return(suffix)
599600
local counter = 1
600601
while next_sibling do
601602
local bullet = next_sibling:child(0)
602-
local text = vim.treesitter.query.get_node_text(bullet, 0)
603+
local text = ts.get_node_text(bullet, 0)
603604
local new_text = tostring(tonumber(text:match('%d+')) + 1) .. closer
604605

605606
if counter == 1 then

lua/orgmode/treesitter/compat.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
-- Shim module to address deprecations across nvim versions
2+
local ts = vim.treesitter
3+
local tsq = ts.query
4+
5+
local M = {}
6+
7+
function M.get_query_files(lang, query_group, is_included)
8+
return (tsq.get_files or tsq.get_query_files)(lang, query_group, is_included)
9+
end
10+
11+
function M.get_query(lang, query_name)
12+
return (tsq.get or tsq.get_query)(lang, query_name)
13+
end
14+
15+
function M.set_query(lang, query_name, text)
16+
return (tsq.set or tsq.set_query)(lang, query_name, text)
17+
end
18+
19+
function M.parse_query(lang, query)
20+
return (tsq.parse or tsq.parse_query)(lang, query)
21+
end
22+
23+
function M.get_range(node, source, metadata)
24+
return (ts.get_range or tsq.get_range)(node, source, metadata)
25+
end
26+
27+
function M.get_node_text(node, source, opts)
28+
return (ts.get_node_text or tsq.get_node_text)(node, source, opts)
29+
end
30+
31+
return M

lua/orgmode/treesitter/headline.lua

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local tree_utils = require('orgmode.utils.treesitter')
44
local Date = require('orgmode.objects.date')
55
local Range = require('orgmode.parser.range')
66
local config = require('orgmode.config')
7-
local query = vim.treesitter.query
7+
local ts = require('orgmode.treesitter.compat')
88

99
---@class Headline
1010
---@field headline userdata
@@ -46,7 +46,7 @@ end
4646
---@return number
4747
function Headline:level()
4848
local stars = self:stars()
49-
return query.get_node_text(stars, 0):len()
49+
return ts.get_node_text(stars, 0):len()
5050
end
5151

5252
function Headline:priority()
@@ -94,7 +94,7 @@ end
9494

9595
function Headline:_handle_promote_demote(recursive, modifier)
9696
local whole_subtree = function()
97-
local text = query.get_node_text(self.headline:parent(), 0)
97+
local text = ts.get_node_text(self.headline:parent(), 0)
9898
local lines = modifier(vim.split(text, '\n', true))
9999
tree_utils.set_node_lines(self.headline:parent(), lines)
100100
return self:refresh()
@@ -128,7 +128,7 @@ function Headline:tags()
128128
local node = self.headline:field('tags')[1]
129129
local text = ''
130130
if node then
131-
text = query.get_node_text(node, 0)
131+
text = ts.get_node_text(node, 0)
132132
end
133133
return node, text
134134
end
@@ -142,7 +142,7 @@ function Headline:set_tags(tags)
142142
end
143143
end
144144

145-
local txt = query.get_node_text(predecessor, 0)
145+
local txt = ts.get_node_text(predecessor, 0)
146146
local pred_end_row, pred_end_col, _ = predecessor:end_()
147147
local line = vim.fn.getline(pred_end_row + 1)
148148
local stars = line:match('^%*+%s*')
@@ -190,13 +190,13 @@ function Headline:set_priority(priority)
190190

191191
local todo = self:todo()
192192
if todo then
193-
local text = query.get_node_text(todo, 0)
193+
local text = ts.get_node_text(todo, 0)
194194
tree_utils.set_node_text(todo, ('%s [#%s]'):format(text, priority))
195195
return
196196
end
197197

198198
local stars = self:stars()
199-
local text = query.get_node_text(stars, 0)
199+
local text = ts.get_node_text(stars, 0)
200200
tree_utils.set_node_text(stars, ('%s [#%s]'):format(text, priority))
201201
end
202202

@@ -209,7 +209,7 @@ function Headline:set_todo(keyword)
209209
end
210210

211211
local stars = self:stars()
212-
local text = query.get_node_text(stars, 0)
212+
local text = ts.get_node_text(stars, 0)
213213
tree_utils.set_node_text(stars, string.format('%s %s', text, keyword))
214214
end
215215

@@ -231,7 +231,7 @@ function Headline:todo()
231231
return nil
232232
end
233233

234-
local text = query.get_node_text(todo_node, 0)
234+
local text = ts.get_node_text(todo_node, 0)
235235
for _, word in ipairs(keywords) do
236236
-- there may be multiple substitutions necessary
237237
local escaped_word = vim.pesc(word)
@@ -272,7 +272,7 @@ function Headline:is_done()
272272
end
273273

274274
function Headline:title()
275-
local title = query.get_node_text(self:item(), 0) or ''
275+
local title = ts.get_node_text(self:item(), 0) or ''
276276
local todo, word = self:todo()
277277
if todo then
278278
title = title:gsub('^' .. vim.pesc(word) .. '%s*', '')
@@ -334,11 +334,11 @@ function Headline:get_property(property_name)
334334
for _, node in ipairs(ts_utils.get_named_children(properties)) do
335335
local name = node:field('name')[1]
336336
local value = node:field('value')[1]
337-
if name and query.get_node_text(name, 0):lower() == property_name:lower() then
337+
if name and ts.get_node_text(name, 0):lower() == property_name:lower() then
338338
return {
339339
node = node,
340340
name = name,
341-
value = value and query.get_node_text(value, 0),
341+
value = value and ts.get_node_text(value, 0),
342342
}
343343
end
344344
end
@@ -369,7 +369,7 @@ function Headline:dates()
369369
end
370370

371371
for _, node in ipairs(ts_utils.get_named_children(plan)) do
372-
local name = query.get_node_text(node:named_child(0), 0)
372+
local name = ts.get_node_text(node:named_child(0), 0)
373373
dates[name] = node
374374
end
375375
return dates
@@ -445,7 +445,7 @@ function Headline:update_cookie(list_node)
445445
local cookie = self:cookie()
446446
if cookie then
447447
local new_cookie_val
448-
if query.get_node_text(cookie, 0):find('%%') then
448+
if ts.get_node_text(cookie, 0):find('%%') then
449449
new_cookie_val = ('[%d%%]'):format((#checked_boxes / #total_boxes) * 100)
450450
else
451451
new_cookie_val = ('[%d/%d]'):format(#checked_boxes, #total_boxes)
@@ -456,7 +456,7 @@ end
456456

457457
function Headline:child_checkboxes(list_node)
458458
return vim.tbl_map(function(node)
459-
local text = query.get_node_text(node, 0)
459+
local text = ts.get_node_text(node, 0)
460460
return text:match('%[.%]')
461461
end, ts_utils.get_named_children(list_node))
462462
end
@@ -465,7 +465,7 @@ end
465465
function Headline:parse(pattern)
466466
local match = ''
467467
local matching_nodes = vim.tbl_filter(function(node)
468-
local text = query.get_node_text(node, 0) or ''
468+
local text = ts.get_node_text(node, 0) or ''
469469
local m = text:match(pattern)
470470
if m then
471471
match = text:match(pattern)
@@ -487,7 +487,7 @@ function Headline:_get_date(type)
487487
if not timestamp_node then
488488
return nil
489489
end
490-
local parsed_date = Date.from_org_date(query.get_node_text(timestamp_node, 0), {
490+
local parsed_date = Date.from_org_date(ts.get_node_text(timestamp_node, 0), {
491491
range = Range.from_node(timestamp_node),
492492
})
493493
return parsed_date and parsed_date[1] or nil
@@ -522,7 +522,7 @@ function Headline:_add_date(type, date, active)
522522
break
523523
end
524524
end
525-
local ptext = query.get_node_text(last_child, 0)
525+
local ptext = ts.get_node_text(last_child, 0)
526526
tree_utils.set_node_text(last_child, ptext .. ' ' .. text)
527527
return self:refresh()
528528
end

lua/orgmode/treesitter/listitem.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local ts_utils = require('nvim-treesitter.ts_utils')
22
local tree_utils = require('orgmode.utils.treesitter')
3-
local query = vim.treesitter.query
3+
local ts = require('orgmode.treesitter.compat')
44
local Headline = require('orgmode.treesitter.headline')
55

66
---@class Listitem
@@ -36,7 +36,7 @@ function Listitem:checkbox()
3636
if not checkbox then
3737
return nil
3838
end
39-
local text = query.get_node_text(checkbox, 0)
39+
local text = ts.get_node_text(checkbox, 0)
4040
return { text = text, range = { checkbox:range() } }
4141
end
4242

@@ -79,7 +79,7 @@ function Listitem:child_checkboxes()
7979
for _, content in ipairs(contents) do
8080
if content:type() == 'list' then
8181
return vim.tbl_map(function(node)
82-
local text = query.get_node_text(node, 0)
82+
local text = ts.get_node_text(node, 0)
8383
return text:match('%[.%]')
8484
end, ts_utils.get_named_children(content))
8585
end
@@ -94,7 +94,7 @@ function Listitem:cookie()
9494
return nil
9595
end
9696

97-
local text = query.get_node_text(cookie_node, 0)
97+
local text = ts.get_node_text(cookie_node, 0)
9898
if text:match('%[%d*/%d*%]') or text:match('%[%d?%d?%d?%%%]') then
9999
return cookie_node
100100
end
@@ -104,7 +104,7 @@ function Listitem:update_cookie(total_child_checkboxes, checked_child_checkboxes
104104
local cookie = self:cookie()
105105
if cookie then
106106
local new_cookie_val
107-
if query.get_node_text(cookie, 0):find('%%') then
107+
if ts.get_node_text(cookie, 0):find('%%') then
108108
new_cookie_val = ('[%d%%]'):format((#checked_child_checkboxes / #total_child_checkboxes) * 100)
109109
else
110110
new_cookie_val = ('[%d/%d]'):format(#checked_child_checkboxes, #total_child_checkboxes)

lua/orgmode/treesitter/table.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local ts_utils = require('orgmode.utils.treesitter')
22
local Table = require('orgmode.parser.table')
33
local utils = require('orgmode.utils')
44
local config = require('orgmode.config')
5-
local query = vim.treesitter.query
5+
local ts = require('orgmode.treesitter.compat')
66

77
---@class TsTable
88
---@field node userdata
@@ -54,7 +54,7 @@ function TsTable:_parse_data()
5454
local cell_val = ''
5555
local cell_content = cell:field('contents')
5656
if cell_content and #cell_content > 0 then
57-
cell_val = query.get_node_text(cell_content[1], 0)
57+
cell_val = ts.get_node_text(cell_content[1], 0)
5858
end
5959
table.insert(row_data, cell_val)
6060
end

lua/orgmode/utils/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local ts = require('vim.treesitter.query')
1+
local ts = require('orgmode.treesitter.compat')
22
local ts_utils = require('nvim-treesitter.ts_utils')
33
local Promise = require('orgmode.utils.promise')
44
local uv = vim.loop

lua/orgmode/utils/treesitter.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local ts = require('orgmode.treesitter.compat')
12
local ts_utils = require('nvim-treesitter.ts_utils')
23
local parsers = require('nvim-treesitter.parsers')
34
local M = {}
@@ -21,7 +22,7 @@ end
2122
function M.parse_query(query)
2223
local ts_query = query_cache[query]
2324
if not ts_query then
24-
ts_query = vim.treesitter.query.parse_query('org', query)
25+
ts_query = ts.parse_query('org', query)
2526
query_cache[query] = ts_query
2627
end
2728
return query_cache[query]

0 commit comments

Comments
 (0)