vim-matchup icon indicating copy to clipboard operation
vim-matchup copied to clipboard

Treesitter: matching fails when only open and scope captures are defined

Open micampe opened this issue 9 months ago • 4 comments

Explain the issue

According to the "Supporting a new language (treesitter)" wiki page, only the open and scope captures are required, but this is not working: without a mid or close capture the scopes are not matched.

This is happening in nvim 0.11 with the latest nvim-treesitter and vim-matchup.

Minimal working example

For example, in a Python file:

# This doesn't show the  "< if" end marker after the pass statement
if True:
    pass

# This shows the "< if" end marker after the second pass statement
if True:
    pass
else:
    pass

The same issue happens in a def without a return or yield.

Minimal vimrc file

This is a minimal lua init file to show the issue.

However, this also exposes another issue, that also explains why the "minimal vimrc" template needs a workaround to add the after directory to rtp, which shouldn't be necessary: the queries directory and its siblings should not be inside after, but at the root.

So this minimal file also requires moving those directories to the appropriate place or using the same workaround and adding after to rtp.

-- Run this file as `nvim --clean -u minimal.lua`

for name, url in pairs {
    "https://github.com/andymass/vim-matchup",
    "https://github.com/nvim-treesitter/nvim-treesitter",
} do
  local install_path = vim.fn.fnamemodify('nvim_issue/' .. name, ':p')
  if vim.fn.isdirectory(install_path) == 0 then
    vim.fn.system { 'git', 'clone', '--depth=1', url, install_path }
  end
  vim.opt.runtimepath:append(install_path)
end

require'nvim-treesitter.configs'.setup {
    ensure_installed = { "python" },
    matchup = {enable = true},
}

Sorry but I wasn't able to test with the treesitter rewrite in progress in #330.

micampe avatar Apr 16 '25 13:04 micampe

https://github.com/andymass/vim-matchup/blob/ea2ff43e09e68b63fc6d9268fc5d82d82d433cb3/autoload/matchup/delim.vim#L140 is where this is encoded

Removing

    if get(l:opts, 'highlighting', 0) && len(l:matching_list) <= 2
      return []
    endif

yields result you're looking for

Image

This is a deliberate choice, likely due to if not having any counterpart to jump to. This bleeds into hinting, removing it in such cases. It does look to be safe to remove, but I'll have to leave proper analysis to others for now due to time constraints.

Slotos avatar Apr 16 '25 14:04 Slotos

Interesting, thank you.

It would be nice to have even without jumping, to indicate the end of the block.

I discovered this while implementing support (that I will be submitting) for Fennel (a lisp): I wanted to use it in places where there never is a mid/end specifically to highlight the end of the block.

This is useful in languages with no end marker in case of longer blocks or busy code.

micampe avatar Apr 16 '25 14:04 micampe

Oh, it's definitely nice to have. I work with python code where this would be a nice QoL feature.

I'd suggest making the change, adding a new test case numbered after this issue (see test/issues for examples), and creating a PR. Docker test runner commit from tree sitter rework can help with local testing, just remember to remove it before pushing the final version - I broke CI runner with my makefiles changes.

I'd do it myself, but there're only so many hours a week I can dedicate to open source work.

Slotos avatar Apr 16 '25 15:04 Slotos

I'd do it myself, but there're only so many hours a week I can dedicate to open source work.

your responses have already been very useful. thank you.

micampe avatar Apr 16 '25 15:04 micampe