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

could add support for python

Open huaihuaiVV opened this issue 6 years ago • 11 comments
trafficstars

could add support for python ?

like python's if:

elif:

try:

exception:

block etc ...

huaihuaiVV avatar Aug 29 '19 06:08 huaihuaiVV

The question is where would % go in this example, starting on the first if?

if foo:
    Bar()
else:
    Bas()
if bar:
    Fro()

andymass avatar Aug 29 '19 11:08 andymass

just match the first if / else pair is ok .

huaihuaiVV avatar Aug 30 '19 01:08 huaihuaiVV

@andymass i would say dependig in which scope the cursor is located.

astier avatar Jan 22 '20 00:01 astier

@astier can you elaborate please? I think the question here is how to deal with the end of a block which has no symbol for the cursor to be placed on.

andymass avatar Jan 22 '20 12:01 andymass

Given your example above I would expect the cursor to jump to the else. The last any-block would be treated like the close-block. Or another way to look at it would be if no close-block can be found go back to open-block.

astier avatar Jan 22 '20 18:01 astier

I would expect the first if/else to match each other because they're part of the same compound statement. The second if could match nothing -- matching the implicit end of a compound statement would be helpful, but I imagine that makes it substantially more complicated, and IMO is not necessary to make it useful.

Here's a very rudimentary fist pass at this, using a regex to match whitespace supporting if/elif/else and try/except/else blocks:

autocmd FileType python let b:match_words = '^\(\s*\)\<if\>:^\1\<elif\>:^\1\<else\>,^\(\s*\)\<try\>:^\1\<except\>:^\1\<else\>'

This example with various permutations works as I would expect except for the issues noted in comments:

if foo:
    foo

# The first if above correctly matches nothing, but this one incorrectly matches
# the else belonging to the final try:
if foo:
    foo

# Zero indentation works, but puts a warning in the status bar:
# "match-up: requested invalid backreference \1"
if foo:
    # Cycling back here from the final else will put cursor at the start of the
    # line, instead of at the keyword:
    if foo:
        if foo:
            try:
                foo
            except SomeException:
                foo
            except:
                foo
            # This else gets highlighted correctly when the cursor is on
            # the try/excepts above, but when cycling it gets stuck here:
            else:
                foo
            # try/except without else doesn't work at all:
            try:
                foo
            except:
                foo
        else:
            foo
    elif:
        foo
    else:
        foo
    if foo:
        foo
    # This elif is not recognized because there's not terminating else:
    elif foo:
        foo
elif:
    foo
else:
    foo

try:
    foo
# This else cycles back to the first if up top instead of the try:
else:
    foo

I don't know the insides of this well enough to tell if there's some obvious solution to these issues (in which case it should be relatively straightforward to expand this with other syntax elements?), or if this is barking up the wrong tree entirely.

Thank you for your work @andymass.

mattias-lidman avatar Feb 28 '20 22:02 mattias-lidman

seems like this is In progress?

momadacoding avatar Jul 15 '21 06:07 momadacoding

Would love to see this as well!

dsully avatar Sep 12 '21 12:09 dsully

FYI this is already complete in neovim+treesitter (see here.) Unfortunately, I think it is too difficult to achieve in vim right now and suspect is not likely to happen any time soon so I am tempted to close this issue.

andymass avatar Sep 12 '21 13:09 andymass

Thanks - so with tree-sitter integration enabled, this functionality should work?

dsully avatar Sep 12 '21 13:09 dsully

Thanks - so with tree-sitter integration enabled, this functionality should work?

@dsully right, if you try out python with tree-sitter and there are any problems, please let me know in another issue.

andymass avatar Sep 12 '21 13:09 andymass

Closing as it's supported with tree-sitter. Unfortunately, we can't support this in vanilla vim today.

andymass avatar May 18 '23 15:05 andymass