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.

mini-screencast demonstrating the issue
(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:

  1. How and why Vi can't handle curly braces in this situation.
  2. How can I rewrite my ugly piece of code to avoid this confusion for Vi.
+3


source to share


1 answer


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.

+4


source







All Articles