Basic matchit support - #15
Conversation
tpict
commented
Feb 23, 2021
Hey, thanks for the PR! Is there any way of getting matchit to only jump between keywords at the same indentation level? At the moment this seems to slip up on nested |
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: ifone:
ifthree:
print("foo 1")
eliffour:
print("foo 2")
else:
print("foo 3")
eliftwo:
print("bar")
else:
print("baz")With the cursor on the outer if, tapping :packaddmatchit
:letb:match_words='\<if\>:\<elif\>:\<else\>'
:letb: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? |
tpict
commented
Apr 22, 2021
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: 😀 autocmdCursorMoved<buffer>letb:match_words=s:BuildMatchWords()
function!s:BuildMatchWords()
ifindent('.') > 0let start_pattern ='\%(^'.repeat('', indent('.')).'\)\@<='elselet start_pattern ='^'endifreturnjoin([
\ start_pattern.'if\>',
\ start_pattern.'elif\>',
\ start_pattern.'else\>',
\ ], ':')
endfunctionWhich essentially updates ifthree:
print("foo 1")
eliffour:
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 |
Konfekt
commented
Mar 19, 2024
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 |
Konfekt
commented
Dec 1, 2024
@tpict How about including the matchit support by the authoer of matchit, @benjifisher ? |
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