summaryrefslogtreecommitdiff
path: root/lua/nvim-treesitter/query.lua
diff options
context:
space:
mode:
authorStephan Seitz <stephan.seitz@fau.de>2020-08-03 00:25:12 +0200
committerStephan Seitz <stephan.seitz@fau.de>2020-08-17 17:44:40 +0200
commitf6681c230f0eb8be0f3f9375b2da205681ea1fc9 (patch)
treed7d14c395cd62d687297a3fa0d080cd3b95da72d /lua/nvim-treesitter/query.lua
parente629efafd8f529ff9b1297b947b4438bf4d2265c (diff)
chore(textobject): use query.find_best_match to find next/previous textobject
Diffstat (limited to 'lua/nvim-treesitter/query.lua')
-rw-r--r--lua/nvim-treesitter/query.lua30
1 files changed, 30 insertions, 0 deletions
diff --git a/lua/nvim-treesitter/query.lua b/lua/nvim-treesitter/query.lua
index c630b366..034df223 100644
--- a/lua/nvim-treesitter/query.lua
+++ b/lua/nvim-treesitter/query.lua
@@ -212,6 +212,36 @@ function M.get_capture_matches(bufnr, capture_string, query_group)
return matches
end
+function M.find_best_match(bufnr, capture_string, query_group, filter_predicate, scoring_function)
+ if not string.sub(capture_string, 1,2) == '@' then
+ api.nvim_err_writeln('capture_string must start with "@"')
+ return
+ end
+
+ --remove leading "@"
+ capture_string = string.sub(capture_string, 2)
+
+ local best
+ local best_score
+
+ for maybe_match in M.iter_group_results(bufnr, query_group) do
+ local match = utils.get_at_path(maybe_match, capture_string)
+
+ if match and filter_predicate(match) then
+ local current_score = scoring_function(match)
+ if not best then
+ best = match
+ best_score = current_score
+ end
+ if current_score > best_score then
+ best = match
+ best_score = current_score
+ end
+ end
+ end
+ return best
+end
+
-- Iterates matches from a query file.
-- @param bufnr the buffer
-- @param query_group the query file to use