From 9fbb0932c08c36aa8361be64d80d6b073a54dd1d Mon Sep 17 00:00:00 2001 From: Cormac Relf Date: Wed, 17 Mar 2021 15:20:27 +1100 Subject: [PATCH] tree-sitter: ignore matches in inner scopes Previously, placing the cursor on line 1 would highlight every elif and else in the whole block of code. % would jump to line 4, then % from there would cycle between lines 2, 4 and 6. 1 if noice: 2 if yeah: 3 pass 4 elif no: 5 pass 6 else: 7 pass 8 elif blah: 9 pass 10 else: 11 pass With the change, which I think the code had contemplated given there was already an unused `M.containing_scope(node, bufnr, info.key)` call, % will only move between ifs and elses that are in the same `@scope.if_`, and not to any inner scopes. Hence the example will have two mutually exclusive %-cycles: [1, 8, 10] and [2, 4, 6]. --- lua/treesitter-matchup/internal.lua | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lua/treesitter-matchup/internal.lua b/lua/treesitter-matchup/internal.lua index 2f89605..37a0263 100644 --- a/lua/treesitter-matchup/internal.lua +++ b/lua/treesitter-matchup/internal.lua @@ -248,13 +248,14 @@ function M.get_matching(delim, down, bufnr) and (row >= info.search_range[1] and row <= info.search_range[3]) then - local scope = M.containing_scope(node, bufnr, info.key) + local target_scope = M.containing_scope(node, bufnr, info.key) + if info.scope == target_scope then + local text = ts_utils.get_node_text(node, bufnr)[1] + table.insert(matches, {text, row + 1, col + 1}) - local text = ts_utils.get_node_text(node, bufnr)[1] - table.insert(matches, {text, row + 1, col + 1}) - - if side == 'close' then - got_close = true + if side == 'close' then + got_close = true + end end end end