From 9e152f39d10d9a94e3c5beeb2c08be77d058d2c6 Mon Sep 17 00:00:00 2001 From: TheLeoP Date: Sun, 29 Jun 2025 14:43:52 -0500 Subject: [PATCH] feat!(treesitter): parse range around cursor based on g:matchup_treesitter_stopline --- autoload/matchup.vim | 1 + lua/match-up.lua | 1 + lua/treesitter-matchup/internal.lua | 13 +++++++++---- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/autoload/matchup.vim b/autoload/matchup.vim index 06e4d29..f159a40 100644 --- a/autoload/matchup.vim +++ b/autoload/matchup.vim @@ -82,6 +82,7 @@ function! s:init_options() call s:init_option('matchup_treesitter_include_match_words', v:false) call s:init_option('matchup_treesitter_enable_quotes', v:true) call s:init_option('matchup_treesitter_disable_virtual_text', v:true) + call s:init_option('matchup_treesitter_stopline', 400) endfunction function! s:init_option(option, default) diff --git a/lua/match-up.lua b/lua/match-up.lua index 8a3cf46..b6887fe 100644 --- a/lua/match-up.lua +++ b/lua/match-up.lua @@ -73,6 +73,7 @@ local M = {} ---@field include_match_words boolean ---@field disable_virtual_text boolean ---@field enable_quotes boolean +---@field stopline integer ---@class matchup.Config ---@field delim matchup.DelimConfig diff --git a/lua/treesitter-matchup/internal.lua b/lua/treesitter-matchup/internal.lua index ad8fec5..ffd9e63 100644 --- a/lua/treesitter-matchup/internal.lua +++ b/lua/treesitter-matchup/internal.lua @@ -134,10 +134,15 @@ M.get_matches = function(bufnr) local matches = {} ---@type matchup.treesitter.Match[] if parser then - -- TODO: g:matchup_delim_stopline could be used, but this functions needs to - -- know on which window it should look for in order to get the current - -- cursor position of that window - parser:parse(nil) + -- NOTE: assummes that we are always parsing the current window. May cause + -- issues if that's not always the case + local win = api.nvim_get_current_win() + local cur_row = unpack(api.nvim_win_get_cursor(win)) + local stopline = vim.g.matchup_treesitter_stopline ---@type integer + local start_row = math.max(cur_row - stopline, 0) + local end_row = math.min(cur_row + stopline, api.nvim_buf_line_count(bufnr)) + + parser:parse({start_row, end_row}) parser:for_each_tree(function(tree, lang_tree) if not tree or lang_tree:lang() == 'comment' then return