CC: | Mark Grimes <mgrimes [...] cpan.org> |
Subject: | [PATCH 2/2] Added a package for Vim |
Date: | Mon, 15 Jun 2009 21:26:54 -0700 |
To: | bug-Devel-IntelliPerl [...] rt.cpan.org |
From: | Mark Grimes <mgrimes [...] cpan.org> |
This probably isn't the most efficient vim scripting, but it
works for me. I would be happy to release this separately, but
It looks like D::IP is young enough that the API might be changing
regularly so it is probably better off in the core dist. I'd
love to hear some feedback.
(Sorry for the rather impersonal patches. I'm using Git::CPAN::Patch
which is very efficient, but not particularly friendly.)
---
lib/Devel/IntelliPerl/Editor/Vim.pm | 119 +++++++++++++++++++++++++++++++++++
1 files changed, 119 insertions(+), 0 deletions(-)
create mode 100644 lib/Devel/IntelliPerl/Editor/Vim.pm
diff --git a/lib/Devel/IntelliPerl/Editor/Vim.pm b/lib/Devel/IntelliPerl/Editor/Vim.pm
new file mode 100644
index 0000000..02a163d
--- /dev/null
+++ b/lib/Devel/IntelliPerl/Editor/Vim.pm
@@ -0,0 +1,119 @@
+package Devel::IntelliPerl::Editor::Vim;
+
+use Moose;
+
+use Exporter qw(import);
+use Devel::IntelliPerl;
+use Text::Table;
+
+extends 'Devel::IntelliPerl::Editor';
+
+our @EXPORT = qw(run);
+
+has editor => ( isa => 'Str', is => 'ro', default => 'Vim' );
+
+sub run {
+ my ($self) = @_;
+ my @source;
+ my ( $line_number, $column, $filename ) = @ARGV;
+ push( @source, $_ ) while (<STDIN>);
+ my $ip = Devel::IntelliPerl->new(
+ line_number => $line_number,
+ column => $column + 1,
+ source => join( '', @source ),
+ filename => $filename
+ );
+ print length( $ip->prefix ), "\n";
+
+ my @methods = $ip->methods;
+ if ( @methods > 1 ) {
+ print join( "\n", @methods );
+ } elsif ( my $method = shift @methods ) {
+ print substr( $method, length $ip->prefix );
+ } elsif ( my $error = $ip->error ) {
+ print "The following error occured:\n" . $error;
+ }
+
+}
+
+__PACKAGE__->meta->make_immutable;
+
+=head1 NAME
+
+Devel::IntelliPerl::Editor::Vim - IntelliPerl integration for Vim
+
+=head1 SYNOPSIS
+
+ package Test;
+ use Moose;
+
+ has a_varaible => ( is => 'rw', isa => 'Str' );
+ sub a_method {
+ my $self = shift;
+
+ $self->a^^
+ }
+
+Pressing <c-x><c-o> from insert mode in Vim will offer up a menu to
+auto complete with either C<a_method> or C<a_varaible>.
+
+=head1 DESCRIPTION
+
+Vim uses the omnifunction to autocomplete most languages. This is typically
+mapped to C<<c-x><c-o>> which is vim speak for (ctrl-x followed by crtl-o).
+Add the following to your C<~/.vim/ftplugin/perl.vim> file and the
+omnifunction should run L<Devel::IntelliPerl> for perl method completion.
+
+ setlocal omnifunc=Ocf_devel_intelliperl
+ function! Ocf_devel_intelliperl(findstart, base)
+ " This function is called twice, once with a:findstart and immediately
+ " thereafter without a:findstart
+ " With a:findstart, return the col where the prefix starts
+ " Without a:findstart, return the method options
+ " We run Devel::IntelliPerl only once and cache the results
+ if a:findstart
+
+ " Get some info for the command
+ let line = line('.')
+ let column = col('.') - 1
+ let filename = expand("%")
+
+ " Defined the Devel::IntelliPerl command
+ let command = "perl -I/home/mgrimes/.cpanplus/5.10.0/build/Devel-IntelliPerl-0.01/lib -MDevel::IntelliPerl::Editor::Vim -e 'run' " . line . ' ' . column . ' ' . filename
+
+ " Get the current contents of the buffer (we don't want to have to write the file)
+ let buffer_contents = join( getline(1,"$"), "\n" )
+
+ " Run the command and munge the results into a list
+ let result_str = system(command, buffer_contents )
+ let s:ofc_results = split( result_str, " *\n" )
+ let prefix_len = s:ofc_results[0]
+
+ return col('.') - prefix_len - 1
+ endif
+
+ return s:ofc_results[1:-1]
+ endfunction
+
+=head1 METHODS
+
+=head2 editor
+
+Set to C<Vim>.
+
+=head2 run
+
+This method is exported and invokes L<Devel::IntelliPerl>.
+
+=head1 SEE ALSO
+
+L<Devel::IntelliSense>
+
+=head1 COPYRIGHT & LICENSE
+
+Copyright 2009 Mark Grimes, all rights reserved.
+
+This program is free software; you can redistribute it and/or modify it
+under the same terms as Perl itself.
+
+=cut
--
1.6.3.2