|
78 | 78 | ---@param lang string |
79 | 79 | ---@return matchup.treesitter.Match[] |
80 | 80 | local get_memoized_matches = memoize(function(bufnr, root, lang) |
81 | | - local query_name = 'matchup' |
82 | | - local query = ts.query.get(lang, query_name) |
| 81 | + local query = ts.query.get(lang, 'matchup') |
83 | 82 |
|
84 | 83 | if not query then |
85 | 84 | return {} |
@@ -137,28 +136,31 @@ end, buf_root_lang_hash) |
137 | 136 | ---@param bufnr integer |
138 | 137 | ---@return matchup.treesitter.Match[] |
139 | 138 | M.get_matches = function(bufnr) |
140 | | - local parser = ts.get_parser(bufnr) |
| 139 | + local lang_tree = ts.get_parser(bufnr) |
141 | 140 | local matches = {} ---@type matchup.treesitter.Match[] |
142 | 141 |
|
143 | | - if parser then |
| 142 | + if lang_tree then |
144 | 143 | -- NOTE: assummes that we are always parsing the current window. May cause |
145 | 144 | -- issues if that's not always the case |
146 | | - local win = api.nvim_get_current_win() |
147 | | - local cur_row = unpack(api.nvim_win_get_cursor(win)) |
| 145 | + local cursor = vim.api.nvim_win_get_cursor(0) |
148 | 146 | local stopline = vim.g.matchup_treesitter_stopline ---@type integer |
149 | | - local start_row = math.max(cur_row - stopline, 0) |
150 | | - local end_row = math.min(cur_row + stopline, api.nvim_buf_line_count(bufnr)) |
151 | | - |
152 | | - parser:parse({start_row, end_row}) |
153 | | - parser:for_each_tree(function(tree, lang_tree) |
154 | | - if not tree or lang_tree:lang() == 'comment' then |
155 | | - return |
| 147 | + local start_row = math.max(cursor[1] - stopline, 0) |
| 148 | + local end_row = math.min(cursor[1] + stopline, api.nvim_buf_line_count(bufnr)) |
| 149 | + |
| 150 | + lang_tree:parse({ start_row, end_row }) |
| 151 | + ---@type vim.treesitter.LanguageTree? |
| 152 | + local nested_lang_tree = lang_tree:language_for_range({ cursor[1]-1, cursor[2], cursor[1]-1, cursor[2] }) |
| 153 | + while vim.tbl_isempty(matches) and nested_lang_tree ~= nil do |
| 154 | + local lang = nested_lang_tree:lang() |
| 155 | + if lang ~= 'comment' then |
| 156 | + for _, tree in ipairs(nested_lang_tree:trees()) do |
| 157 | + local group_results = get_memoized_matches(bufnr, tree:root(), lang) |
| 158 | + vim.list_extend(matches, group_results) |
| 159 | + end |
156 | 160 | end |
157 | 161 |
|
158 | | - local lang = lang_tree:lang() |
159 | | - local group_results = get_memoized_matches(bufnr, tree:root(), lang) |
160 | | - vim.list_extend(matches, group_results) |
161 | | - end) |
| 162 | + nested_lang_tree = nested_lang_tree:parent() |
| 163 | + end |
162 | 164 | end |
163 | 165 |
|
164 | 166 | return matches |
|
0 commit comments