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

Is it possible to match corresponding angle brackets in template C++ code?

Open Aster89 opened this issue 5 years ago • 2 comments
trafficstars

Is your feature request related to a problem? Please describe. Matching corresponding angle brackets in C++ template code. The following toy example is a demo of the issue:

#include <type_traits>
class a {};
bool operator<(a,a) { return true; };
bool b = std::is_same_v<decltype(a{} < a{}), bool>;

I've also asked a question on vi.stackexchange.

Describe the solution you'd like Matching corresponding angle brackets in C++ template code.

Describe alternatives you've considered Does one even exist? However, I'm asking for this feature, but I suspect it might be impossibile to implement it without a semantic-aware strategy.

Additional context Related: #69

Aster89 avatar Nov 11 '20 20:11 Aster89

You can try this. It's not perfect, but may work.

let g:matchup_matchpref = {'cpp': {'template': 1}}

Doing it properly is going to require vim to understand syntax much better. I have a lot of ideas about this but no specific timeline.

andymass avatar Nov 27 '20 15:11 andymass

I'm not sure recognizing the syntax perfectly would actually solve the problem. For instance, in the following B<i>b; has a totally different meaning depending on the different meaning of B, i, and b.

class A {};
A operator<(A,A) { return A{}; };
A operator>(A,A) { return A{}; };

template<typename T>
class B {};

class i {};

int main() {
    {
        B<i>b; // pair of angle brackets
    }
    {
        A B, i, b;
        B<i>b; // two unrelated comparison operators
    }
}

Indeed, % jumps between < and > on both lines, even if I've set let g:matchup_matchpref = {'cpp': {'template': 1}}.

Aster89 avatar Nov 28 '20 08:11 Aster89

Based on this example, I think the current behavior is close to the best that we can do, so I'll close this issue for now.

The neovim/tree-sitter version is slightly smarter but has similar bad parses. C++ after all is highly context sensitive..

andymass avatar Apr 29 '23 18:04 andymass