diff options
| author | Steven Sojka <steelsojka@users.noreply.github.com> | 2020-08-17 11:39:22 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-17 11:39:22 -0500 |
| commit | b796f0725d913cba0a292bb7b750f0403b07c94a (patch) | |
| tree | 6645909fcf422b0d77f513514ece4a539135aba4 /lua/nvim-treesitter/query.lua | |
| parent | 0b3cd6c131a5ee9e3377c6b348c2b8995a848967 (diff) | |
| parent | 52168114594d791a3ae6092ab2489758da7b3ae8 (diff) | |
Merge pull request #305 from theHamsta/textobjects-submodules
Textobjects submodules
Diffstat (limited to 'lua/nvim-treesitter/query.lua')
| -rw-r--r-- | lua/nvim-treesitter/query.lua | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/lua/nvim-treesitter/query.lua b/lua/nvim-treesitter/query.lua index ec93d5bb..4a2ce57d 100644 --- a/lua/nvim-treesitter/query.lua +++ b/lua/nvim-treesitter/query.lua @@ -213,6 +213,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 |
