[tpm] Follow-up links and stuff for Neovim

Yanick Champoux yanick at babyl.ca
Thu Sep 28 16:46:36 PDT 2017


For those who are interested to know more:

Neovim-RPC is on CPAN and at https://github.com/yanick/Neovim-RPC

A new version that moves to IO::Async to deal with the 
asynchronous/network part will be out soon.


I also have a few blog entries about it:

http://techblog.babyl.ca/entry/neovim-plugins-part-1
http://techblog.babyl.ca/entry/neovim-plugins-part-2

Before I began to play with Neovim, I toyed with the same principle with 
Vim itself: see Vim::X on CPAN, and http://techblog.babyl.ca/entry/vim-x 
   (note that this was waaaay before Vim 8 and the new asynchronous 
interface feature that came with it, which I will probably use if I ever 
revisit Vim::X)

Oh yeah, and as for flipping I didn't show in details in the demo, the 
code you saw was

             code      => [ split "\n", $self->flip_snippet(join "\n", 
@$code ) ],

The part that lies behind that and that was not shown is copied below. 
It's nothing out of this world, just some good ol' regex brutality. If I 
was a more graceful soul, I'd probably use PPI, but... meh. :-)

Joy!
`/anick


sub flip_snippet($self,$snippet) {

     $snippet =~ s/^\s*\n//gm;
     my ($indent) = $snippet =~ /^(\s*)/;
     $snippet =~ s/^$indent//gm;

     my $operators = join '|', qw/ if unless while for until /;

     my $block_re = qr/
         ^
         (?<operator>$operators)
             \s* (?:my \s+ (?<variable>\$\w+) \s* )?
             \( \s* (?<array>[^)]+) \) \s* {
                 (?<inner>.*)
             }
             \s* $
     /xs;

     my $postfix_re = qr/
         ^
         (?<inner>[^;]+?)
         \s+ (?<operator>$operators)
         \s+ (?<array>[^;]+?)
         \s* ;
         $
     /xs;

     if ( $snippet =~ $block_re ) {
         $snippet = block_to_postifx( $snippet, %+ );
     }
     elsif( $snippet =~ $postfix_re ) {
         $snippet = postfix_to_block( $snippet, %+ );
     }

     $snippet =~ s/^\s*?\n//;

     $snippet =~ s/^/$indent/gm;

     return $snippet;
}

sub postfix_to_block {
     my( $snippet, %capture ) = @_;

     $snippet = $capture{inner};
     chomp $capture{array};
     $snippet = "$capture{operator} ( $capture{array} ) {\n    $snippet\n}";

}

sub block_to_postifx {
     my( $snippet, %capture ) = @_;

     # more than one statement? Don't touch it
     return $snippet if $capture{inner} =~ /(;)/ > 1;

     $snippet = $capture{inner};
     $snippet =~ s/;\s*$//;

     $snippet =~ s/\Q$capture{variable}/\$_/g;
     $snippet =~ s/\$_\s*=~\s*//g;

     $capture{array} =~ s/\s*$//;

     $snippet .= " $capture{operator} $capture{array};";

     return $snippet;
}


More information about the toronto-pm mailing list