Vi does not handle curly braces correctly in my perl script
Vee messed up my Perl script! Although {
none of the closing curly braces match at the end of the code, {
at the end of lines 27 and 28 it matches }
at position 30.
Here is a screen video of how Vi relates to my script behavior.
(source: abbasinasab.com )
Here's also my piece of troublemaker code:
#CODE
while ($data =~ m{
^foo_\s+ $X \s* \{
( (?: [^{}]+ | \{(?1)\} )* )
\}
}mgx)
{
my $Y = $1;
next if $Y !~ m{
bar_$Z \s* \{
( (?: [^{}]+ | \{(?1)\} )*? )
\}
}mx;
my $DO = $1;
#CODE
}
My questions:
- How and why Vi can't handle curly braces in this situation.
- How can I rewrite my ugly piece of code to avoid this confusion for Vi.
Vim has two functionalities that can be tricked with complex syntaxes:
- Highlighting
'matchpairs'
and jumping uses an internal heuristic. This may have a slight effect (cp.:help cpo-M
):
:set cpo+=M
Doing so %
correctly navigates to the expected closing parenthesis (if you don't have a plugin like matchpairs.vim, override the command %
). Also note that the setting 'cpoptions'
is global, so it may negatively affect other file types.
- Syntax highlighting is based on regular expression. Thus, it will fail with corner cases and complex syntaxes (like C ++ and Perl) that cannot be accurately modeled with regexes (but require their own parser).
In general, I would not rewrite the code so that the editor is happy. Other people may use different editors, and soon the code will be heavily mangled by various "workarounds". The only exception is that the code is clearly complex and cumbersome, and the overall readability will improve after the restructuring.
source to share