Skip to content

Commit

Permalink
(mini.surround) Update gen_spec.input.treesitter to be compatible w…
Browse files Browse the repository at this point in the history
…ith built-in `vim.treesitter` on Neovim>=0.9.
  • Loading branch information
echasnovski committed Jan 23, 2024
1 parent 7bf8915 commit 3c98c6b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
11 changes: 6 additions & 5 deletions doc/mini-surround.txt
Original file line number Diff line number Diff line change
Expand Up @@ -742,11 +742,12 @@ region pair is a difference between regions of outer and inner captures.

In order for this to work, apart from working treesitter parser for desired
language, user should have a reachable language-specific 'textobjects'
query (see |get_query()|). The most straightforward way for this is to have
'textobjects.scm' query file with treesitter captures stored in some
recognized path. This is primarily designed to be compatible with
'nvim-treesitter/nvim-treesitter-textobjects' plugin, but can be used
without it.
query (see |vim.treesitter.query.get()| or |get_query()|, depending on your
Neovim version).
The most straightforward way for this is to have 'textobjects.scm' query
file with treesitter captures stored in some recognized path. This is
primarily designed to be compatible with plugin
'nvim-treesitter/nvim-treesitter-textobjects', but can be used without it.

Two most common approaches for having a query file:
- Install 'nvim-treesitter/nvim-treesitter-textobjects'. It has curated and
Expand Down
17 changes: 9 additions & 8 deletions lua/mini/surround.lua
Original file line number Diff line number Diff line change
Expand Up @@ -920,11 +920,12 @@ MiniSurround.gen_spec = { input = {}, output = {} }
---
--- In order for this to work, apart from working treesitter parser for desired
--- language, user should have a reachable language-specific 'textobjects'
--- query (see |get_query()|). The most straightforward way for this is to have
--- 'textobjects.scm' query file with treesitter captures stored in some
--- recognized path. This is primarily designed to be compatible with
--- 'nvim-treesitter/nvim-treesitter-textobjects' plugin, but can be used
--- without it.
--- query (see |vim.treesitter.query.get()| or |get_query()|, depending on your
--- Neovim version).
--- The most straightforward way for this is to have 'textobjects.scm' query
--- file with treesitter captures stored in some recognized path. This is
--- primarily designed to be compatible with plugin
--- 'nvim-treesitter/nvim-treesitter-textobjects', but can be used without it.
---
--- Two most common approaches for having a query file:
--- - Install 'nvim-treesitter/nvim-treesitter-textobjects'. It has curated and
Expand Down Expand Up @@ -984,7 +985,7 @@ MiniSurround.gen_spec.input.treesitter = function(captures, opts)

return function()
-- Get array of matched treesitter nodes
local has_nvim_treesitter, _ = pcall(require, 'nvim-treesitter')
local has_nvim_treesitter = pcall(require, 'nvim-treesitter') and pcall(require, 'nvim-treesitter.query')
local node_pair_querier = (has_nvim_treesitter and opts.use_nvim_treesitter) and H.get_matched_node_pairs_plugin
or H.get_matched_node_pairs_builtin
local matched_node_pairs = node_pair_querier(captures)
Expand Down Expand Up @@ -1450,7 +1451,6 @@ H.prepare_captures = function(captures)
end

H.get_matched_node_pairs_plugin = function(captures)
-- Hope that 'nvim-treesitter.query' is stable enough
local ts_queries = require('nvim-treesitter.query')
local ts_parsers = require('nvim-treesitter.parsers')

Expand Down Expand Up @@ -1489,7 +1489,8 @@ H.get_matched_node_pairs_builtin = function(captures)
local ok, parser = pcall(vim.treesitter.get_parser, 0, lang)
if not ok then H.error_treesitter('parser', lang) end

local query = vim.treesitter.get_query(lang, 'textobjects')
local get_query = vim.fn.has('nvim-0.9') == 1 and vim.treesitter.query.get or vim.treesitter.get_query
local query = get_query(lang, 'textobjects')
if query == nil then H.error_treesitter('query', lang) end

-- Remove leading '@'
Expand Down

0 comments on commit 3c98c6b

Please sign in to comment.