Basic matchit support#15
Conversation
|
I agree, but I can't seem to reproduce the issue -- I tried a couple of if-clauses nested within the if, the else, etc, but they all seem to correctly respect nesting. Here's one example that works for me: if one:
if three:
print("foo 1")
elif four:
print("foo 2")
else:
print("foo 3")
elif two:
print("bar")
else:
print("baz")With the cursor on the outer if, tapping :packadd matchit
:let b:match_words = '\<if\>:\<elif\>:\<else\>'
:let b:match_skip = 'R:^\s*'If this particular example does work for you as well, could you give me one that doesn't that I can test with? |
|
Hi @AndrewRadev, sorry about the delay in getting back to you. Yes, that example does work on my end. If you remove the
Is it possible to have matchit handle these fall-through cases? |
|
Hm, you're right, that's definitely a problem. Unfortunately, I can't figure out a solution. For starters, there's no way to tell matchit to match the same whitespase in all cases. Putting backreferences is supported, but they're mechanically replaced. So, for instance, I did come up with this: 😀 autocmd CursorMoved <buffer> let b:match_words = s:BuildMatchWords()
function! s:BuildMatchWords()
if indent('.') > 0
let start_pattern = '\%(^'.repeat(' ', indent('.')).'\)\@<='
else
let start_pattern = '^'
endif
return join([
\ start_pattern.'if\>',
\ start_pattern.'elif\>',
\ start_pattern.'else\>',
\ ], ':')
endfunctionWhich essentially updates if three:
print("foo 1")
elif four:
print("foo 2")The matchit plugin expects either 2 or 3 components -- beginning + end, or beginning + middle + end. Unfortunately, while ruby has a literal The bottom-line is that I guess this would only be a useful tool if you have if-clauses with |
|
Since there are no end markers in Python, defining a pair in matchit itself always falls short, but the author provided https://github.com/vim-scripts/python_match.vim which lacks the pair |

Matchit is a built-in plugin that allows the
%key to jump between language constructs. This PR adds support for jumping between if, elif and else:It's pretty basic -- there are lots of other constructs that this could be useful for. But it's at least a start. Here's what the Ruby support uses: https://github.com/vim-ruby/vim-ruby/blob/4788a08433c3c90e131fc7d110d82577e1234a86/ftplugin/ruby.vim#L21-L41