andymass / vim-matchup

vim match-up: even better % :facepunch: navigate and highlight matching words :facepunch: modern matchit and matchparen. Supports both vim and neovim + tree-sitter.
https://www.vim.org/scripts/script.php?script_id=5624
MIT License
1.69k stars 71 forks source link

R double brackets `[[` nested with `[` and `[[` #171

Open BertrandSim opened 3 years ago

BertrandSim commented 3 years ago

Hello, thank you for this plugin! I installed your plugin recently, and wanted to add my own support for R, and in particular, for [[...]].

In the R language, one has the [[ (extraction) operator, and the [ (subset) operator. As there is no support for the R filetype, I proceeded to add my own ftplugin:

" ~/.vim/after/ftplugin/r/match.vim
let b:match_words .= '\V[[:]]'

As there is already vim's built-in support for [ by default, I only needed to add support for [[.

However, some parts of [[ are confused with a single [. In addition, when the [['s and ['s are nested together, the double and single brackets are not matched properly.
Here's a minimal example:

[abc] # ok
[[abc]] # the second '[' should be part of '[[', instead of a single '['. Similarly for the last ']'.
x[a[b]] # the first ']' does not have a matching '['
x[a[[b]]] # the second ']' does not have a matching '['. 
x[[a[b]]] # first and second '['s wrongly matched with first and second ']'s
x[[a[[b]]]] # first and second '['s wrongly matched with second and third ']'s

I also tried to use the priority of b:match_words, by preferring to match [[ over [. That is,

" ~/.vim/after/ftplugin/r/match.vim
let b:match_words .= '\V[[:]],[:]'
set matchpairs-=[:]

but the result was the same.

andymass commented 3 years ago

just checking- are you using classic or tree-sitter (nvim only)?

BertrandSim commented 3 years ago

Oh yes, I forgot to mention. I'm using classic vim 8.1 (ie. no tree-sitter)