diff --git a/lua/orgmode/colors/markup_highlighter.lua b/lua/orgmode/colors/markup_highlighter.lua index e3ff06c2f..ac86f23ee 100644 --- a/lua/orgmode/colors/markup_highlighter.lua +++ b/lua/orgmode/colors/markup_highlighter.lua @@ -1,3 +1,4 @@ +local ts = require('orgmode.treesitter.compat') local config = require('orgmode.config') local ts_utils = require('nvim-treesitter.ts_utils') local query = nil @@ -226,7 +227,7 @@ local function load_deps() if query then return end - query = vim.treesitter.get_query('org', 'markup') + query = ts.get_query('org', 'markup') vim.treesitter.query.add_predicate('org-is-valid-markup-range?', is_valid_markup_range) vim.treesitter.query.add_predicate('org-is-valid-hyperlink-range?', is_valid_hyperlink_range) vim.treesitter.query.add_predicate('org-is-valid-latex-range?', is_valid_latex_range) diff --git a/lua/orgmode/colors/todo_highlighter.lua b/lua/orgmode/colors/todo_highlighter.lua index 0e23c70f6..037a3c17a 100644 --- a/lua/orgmode/colors/todo_highlighter.lua +++ b/lua/orgmode/colors/todo_highlighter.lua @@ -1,10 +1,11 @@ +local ts = require('orgmode.treesitter.compat') local config = require('orgmode.config') local highlights = require('orgmode.colors.highlights') local tree_utils = require('orgmode.utils.treesitter') local utils = require('orgmode.utils') local function add_todo_keyword_highlights() - local query_files = vim.treesitter.get_query_files('org', 'highlights') + local query_files = ts.get_query_files('org', 'highlights') if not query_files or #query_files == 0 then return end @@ -51,7 +52,7 @@ local function add_todo_keyword_highlights() for _, v in ipairs(lines) do table.insert(all_lines, v) end - vim.treesitter.set_query('org', 'highlights', table.concat(all_lines, '\n')) + ts.set_query('org', 'highlights', table.concat(all_lines, '\n')) if vim.bo.filetype == 'org' then tree_utils.restart_highlights() end diff --git a/lua/orgmode/org/indent.lua b/lua/orgmode/org/indent.lua index 297f536bd..8651e617e 100644 --- a/lua/orgmode/org/indent.lua +++ b/lua/orgmode/org/indent.lua @@ -1,3 +1,4 @@ +local ts = require('orgmode.treesitter.compat') local config = require('orgmode.config') local ts_utils = require('nvim-treesitter.ts_utils') local query = nil @@ -25,7 +26,7 @@ local get_matches = ts_utils.memoize_by_buf_tick(function(bufnr) } if type == 'headline' then - opts.stars = vim.treesitter.query.get_node_text(node:field('stars')[1], bufnr):len() + opts.stars = ts.get_node_text(node:field('stars')[1], bufnr):len() opts.indent = opts.indent + opts.stars + 1 matches[range.start.line + 1] = opts end @@ -52,7 +53,7 @@ local get_matches = ts_utils.memoize_by_buf_tick(function(bufnr) end if parent then local headline = parent:named_child('headline') - local stars = vim.treesitter.query.get_node_text(headline:field('stars')[1], bufnr):len() + local stars = ts.get_node_text(headline:field('stars')[1], bufnr):len() opts.indent = stars + 1 for i = range.start.line, range['end'].line - 1 do matches[i + 1] = opts @@ -67,7 +68,7 @@ end) local prev_section = nil local function foldexpr() - query = query or vim.treesitter.get_query('org', 'org_indent') + query = query or ts.get_query('org', 'org_indent') local matches = get_matches(0) local match = matches[vim.v.lnum] local next_match = matches[vim.v.lnum + 1] @@ -114,7 +115,7 @@ end local function indentexpr() local noindent_mode = config.org_indent_mode == 'noindent' - query = query or vim.treesitter.get_query('org', 'org_indent') + query = query or ts.get_query('org', 'org_indent') local prev_linenr = vim.fn.prevnonblank(vim.v.lnum - 1) diff --git a/lua/orgmode/org/mappings.lua b/lua/orgmode/org/mappings.lua index a0423fb2c..cf5ba07b8 100644 --- a/lua/orgmode/org/mappings.lua +++ b/lua/orgmode/org/mappings.lua @@ -11,6 +11,7 @@ local constants = require('orgmode.utils.constants') local ts_utils = require('nvim-treesitter.ts_utils') local utils = require('orgmode.utils') local ts_org = require('orgmode.treesitter') +local ts = require('orgmode.treesitter.compat') local ts_table = require('orgmode.treesitter.table') local EventManager = require('orgmode.events') local Promise = require('orgmode.utils.promise') @@ -599,7 +600,7 @@ function OrgMappings:handle_return(suffix) local counter = 1 while next_sibling do local bullet = next_sibling:child(0) - local text = vim.treesitter.query.get_node_text(bullet, 0) + local text = ts.get_node_text(bullet, 0) local new_text = tostring(tonumber(text:match('%d+')) + 1) .. closer if counter == 1 then diff --git a/lua/orgmode/treesitter/compat.lua b/lua/orgmode/treesitter/compat.lua new file mode 100644 index 000000000..df6cd610c --- /dev/null +++ b/lua/orgmode/treesitter/compat.lua @@ -0,0 +1,31 @@ +-- Shim module to address deprecations across nvim versions +local ts = vim.treesitter +local tsq = ts.query + +local M = {} + +function M.get_query_files(lang, query_group, is_included) + return (tsq.get_files or tsq.get_query_files)(lang, query_group, is_included) +end + +function M.get_query(lang, query_name) + return (tsq.get or tsq.get_query)(lang, query_name) +end + +function M.set_query(lang, query_name, text) + return (tsq.set or tsq.set_query)(lang, query_name, text) +end + +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 + +function M.get_node_text(node, source, opts) + return (ts.get_node_text or tsq.get_node_text)(node, source, opts) +end + +return M diff --git a/lua/orgmode/treesitter/headline.lua b/lua/orgmode/treesitter/headline.lua index dbcab5e52..b34624fdb 100644 --- a/lua/orgmode/treesitter/headline.lua +++ b/lua/orgmode/treesitter/headline.lua @@ -4,7 +4,7 @@ local tree_utils = require('orgmode.utils.treesitter') local Date = require('orgmode.objects.date') local Range = require('orgmode.parser.range') local config = require('orgmode.config') -local query = vim.treesitter.query +local ts = require('orgmode.treesitter.compat') ---@class Headline ---@field headline userdata @@ -46,7 +46,7 @@ end ---@return number function Headline:level() local stars = self:stars() - return query.get_node_text(stars, 0):len() + return ts.get_node_text(stars, 0):len() end function Headline:priority() @@ -94,7 +94,7 @@ end function Headline:_handle_promote_demote(recursive, modifier) local whole_subtree = function() - local text = query.get_node_text(self.headline:parent(), 0) + local text = ts.get_node_text(self.headline:parent(), 0) local lines = modifier(vim.split(text, '\n', true)) tree_utils.set_node_lines(self.headline:parent(), lines) return self:refresh() @@ -128,7 +128,7 @@ function Headline:tags() local node = self.headline:field('tags')[1] local text = '' if node then - text = query.get_node_text(node, 0) + text = ts.get_node_text(node, 0) end return node, text end @@ -142,7 +142,7 @@ function Headline:set_tags(tags) end end - local txt = query.get_node_text(predecessor, 0) + local txt = ts.get_node_text(predecessor, 0) local pred_end_row, pred_end_col, _ = predecessor:end_() local line = vim.fn.getline(pred_end_row + 1) local stars = line:match('^%*+%s*') @@ -190,13 +190,13 @@ function Headline:set_priority(priority) local todo = self:todo() if todo then - local text = query.get_node_text(todo, 0) + local text = ts.get_node_text(todo, 0) tree_utils.set_node_text(todo, ('%s [#%s]'):format(text, priority)) return end local stars = self:stars() - local text = query.get_node_text(stars, 0) + local text = ts.get_node_text(stars, 0) tree_utils.set_node_text(stars, ('%s [#%s]'):format(text, priority)) end @@ -209,7 +209,7 @@ function Headline:set_todo(keyword) end local stars = self:stars() - local text = query.get_node_text(stars, 0) + local text = ts.get_node_text(stars, 0) tree_utils.set_node_text(stars, string.format('%s %s', text, keyword)) end @@ -231,7 +231,7 @@ function Headline:todo() return nil end - local text = query.get_node_text(todo_node, 0) + local text = ts.get_node_text(todo_node, 0) for _, word in ipairs(keywords) do -- there may be multiple substitutions necessary local escaped_word = vim.pesc(word) @@ -272,7 +272,7 @@ function Headline:is_done() end function Headline:title() - local title = query.get_node_text(self:item(), 0) or '' + local title = ts.get_node_text(self:item(), 0) or '' local todo, word = self:todo() if todo then title = title:gsub('^' .. vim.pesc(word) .. '%s*', '') @@ -334,11 +334,11 @@ function Headline:get_property(property_name) for _, node in ipairs(ts_utils.get_named_children(properties)) do local name = node:field('name')[1] local value = node:field('value')[1] - if name and query.get_node_text(name, 0):lower() == property_name:lower() then + if name and ts.get_node_text(name, 0):lower() == property_name:lower() then return { node = node, name = name, - value = value and query.get_node_text(value, 0), + value = value and ts.get_node_text(value, 0), } end end @@ -369,7 +369,7 @@ function Headline:dates() end for _, node in ipairs(ts_utils.get_named_children(plan)) do - local name = query.get_node_text(node:named_child(0), 0) + local name = ts.get_node_text(node:named_child(0), 0) dates[name] = node end return dates @@ -445,7 +445,7 @@ function Headline:update_cookie(list_node) local cookie = self:cookie() if cookie then local new_cookie_val - if query.get_node_text(cookie, 0):find('%%') then + if ts.get_node_text(cookie, 0):find('%%') then new_cookie_val = ('[%d%%]'):format((#checked_boxes / #total_boxes) * 100) else new_cookie_val = ('[%d/%d]'):format(#checked_boxes, #total_boxes) @@ -456,7 +456,7 @@ end function Headline:child_checkboxes(list_node) return vim.tbl_map(function(node) - local text = query.get_node_text(node, 0) + local text = ts.get_node_text(node, 0) return text:match('%[.%]') end, ts_utils.get_named_children(list_node)) end @@ -465,7 +465,7 @@ end function Headline:parse(pattern) local match = '' local matching_nodes = vim.tbl_filter(function(node) - local text = query.get_node_text(node, 0) or '' + local text = ts.get_node_text(node, 0) or '' local m = text:match(pattern) if m then match = text:match(pattern) @@ -487,7 +487,7 @@ function Headline:_get_date(type) if not timestamp_node then return nil end - local parsed_date = Date.from_org_date(query.get_node_text(timestamp_node, 0), { + local parsed_date = Date.from_org_date(ts.get_node_text(timestamp_node, 0), { range = Range.from_node(timestamp_node), }) return parsed_date and parsed_date[1] or nil @@ -522,7 +522,7 @@ function Headline:_add_date(type, date, active) break end end - local ptext = query.get_node_text(last_child, 0) + local ptext = ts.get_node_text(last_child, 0) tree_utils.set_node_text(last_child, ptext .. ' ' .. text) return self:refresh() end diff --git a/lua/orgmode/treesitter/listitem.lua b/lua/orgmode/treesitter/listitem.lua index c825a00de..ed7ead036 100644 --- a/lua/orgmode/treesitter/listitem.lua +++ b/lua/orgmode/treesitter/listitem.lua @@ -1,6 +1,6 @@ local ts_utils = require('nvim-treesitter.ts_utils') local tree_utils = require('orgmode.utils.treesitter') -local query = vim.treesitter.query +local ts = require('orgmode.treesitter.compat') local Headline = require('orgmode.treesitter.headline') ---@class Listitem @@ -36,7 +36,7 @@ function Listitem:checkbox() if not checkbox then return nil end - local text = query.get_node_text(checkbox, 0) + local text = ts.get_node_text(checkbox, 0) return { text = text, range = { checkbox:range() } } end @@ -79,7 +79,7 @@ function Listitem:child_checkboxes() for _, content in ipairs(contents) do if content:type() == 'list' then return vim.tbl_map(function(node) - local text = query.get_node_text(node, 0) + local text = ts.get_node_text(node, 0) return text:match('%[.%]') end, ts_utils.get_named_children(content)) end @@ -94,7 +94,7 @@ function Listitem:cookie() return nil end - local text = query.get_node_text(cookie_node, 0) + local text = ts.get_node_text(cookie_node, 0) if text:match('%[%d*/%d*%]') or text:match('%[%d?%d?%d?%%%]') then return cookie_node end @@ -104,7 +104,7 @@ function Listitem:update_cookie(total_child_checkboxes, checked_child_checkboxes local cookie = self:cookie() if cookie then local new_cookie_val - if query.get_node_text(cookie, 0):find('%%') then + if ts.get_node_text(cookie, 0):find('%%') then new_cookie_val = ('[%d%%]'):format((#checked_child_checkboxes / #total_child_checkboxes) * 100) else new_cookie_val = ('[%d/%d]'):format(#checked_child_checkboxes, #total_child_checkboxes) diff --git a/lua/orgmode/treesitter/table.lua b/lua/orgmode/treesitter/table.lua index dfbddbfe4..a66434315 100644 --- a/lua/orgmode/treesitter/table.lua +++ b/lua/orgmode/treesitter/table.lua @@ -2,7 +2,7 @@ local ts_utils = require('orgmode.utils.treesitter') local Table = require('orgmode.parser.table') local utils = require('orgmode.utils') local config = require('orgmode.config') -local query = vim.treesitter.query +local ts = require('orgmode.treesitter.compat') ---@class TsTable ---@field node userdata @@ -54,7 +54,7 @@ function TsTable:_parse_data() local cell_val = '' local cell_content = cell:field('contents') if cell_content and #cell_content > 0 then - cell_val = query.get_node_text(cell_content[1], 0) + cell_val = ts.get_node_text(cell_content[1], 0) end table.insert(row_data, cell_val) end diff --git a/lua/orgmode/utils/init.lua b/lua/orgmode/utils/init.lua index d22da3d81..39c281feb 100644 --- a/lua/orgmode/utils/init.lua +++ b/lua/orgmode/utils/init.lua @@ -1,4 +1,4 @@ -local ts = require('vim.treesitter.query') +local ts = require('orgmode.treesitter.compat') local ts_utils = require('nvim-treesitter.ts_utils') local Promise = require('orgmode.utils.promise') local uv = vim.loop diff --git a/lua/orgmode/utils/treesitter.lua b/lua/orgmode/utils/treesitter.lua index 4d620ae85..262c752b4 100644 --- a/lua/orgmode/utils/treesitter.lua +++ b/lua/orgmode/utils/treesitter.lua @@ -1,3 +1,4 @@ +local ts = require('orgmode.treesitter.compat') local ts_utils = require('nvim-treesitter.ts_utils') local parsers = require('nvim-treesitter.parsers') local M = {} @@ -21,7 +22,7 @@ end function M.parse_query(query) local ts_query = query_cache[query] if not ts_query then - ts_query = vim.treesitter.query.parse_query('org', query) + ts_query = ts.parse_query('org', query) query_cache[query] = ts_query end return query_cache[query]