Wednesday, January 26, 2011

Re: Show method name in gutter

On Wed, Jan 26, 2011 at 04:08, Ovid <publiustemp-vim@yahoo.com> wrote:
> I'm working on some old legacy code with "methods" which are thousands of lines long. When jumping through a file, it's not always clear to me which method I'm in. I'd love to find a way to show the current line numbers method name in the gutter in much the same way that numbers are shown there with "set number".
>
> Is this possible or is there another option?
>
> Note that this is Perl code and I'm well-aware of the general "unparseable" nature of Perl. A "most works" solution would be fine.

See any of the following:

http://search.cpan.org/dist/PPIx-IndexOffsets
http://search.cpan.org/dist/PPIx-LineToSub/
http://search.cpan.org/dist/PPIx-IndexLines/
(notice, the last one is mine--you've inspired me to actually finish
up some problems and submit the updates to the cpan)

Each of those do what you want to some degree or another. The major
reason why I wrote my own is because the other two will do the
following:

sub somesub {
some code
}

some code between subs

sub anothersub {
some code
}

If your cursor is between those subs the other functions will return
'somesub' whereas mine will return 'main' instead. Also, my function
returns 'BEGIN', 'CHECK', 'UNITCHECK', 'INIT', 'END', 'POD',
'__DATA__' and '__END__' as well as PackageName[::SubName] as
appropriate.

This works ok, but only for one file being edited per session. I know
why, but I don't know how to fix it. Hopefully the list can help.

I have this in my ~/.vim/after/ftplugin/perl.vim file (see
https://github.com/harleypig/dot_vim for everything in context):

" This makes the status line work *only* for the first perl file
loaded, but
" if I don't do it this way, the statusline has this check appended
each time
" the buffer is visited. How to fix this?

if ! exists("g:did_perl_statusline")
setlocal statusline+=%(\ %{StatusLineIndexLine()}%)
let g:did_perl_statusline = 1
endif

if has( 'perl' )
perl << EOP
use strict;

eval "use PPIx::IndexLines";
my $error = $@;

sub index_line {

if ( $error ) {

VIM::DoCommand "let subName='$error'";

} else {

my $curwin = $main::curwin;
my $curbuf = $main::curbuf;

# There's got to be a better way to slurp in the current buffer!
my $document = join "\n", map { $curbuf->Get( $_ ) } 0 ..
$curbuf->Count;

my $ppi = PPIx::IndexLines->new( \$document );
$ppi->index_lines;
( my $line, undef ) = $curwin->Cursor;
my $sub_name = $ppi->line_type( $line );
VIM::DoCommand "let subName='$line: $sub_name'";

}
}

EOP

function! StatusLineIndexLine()
perl index_line()
return subName
endfunction
endif
--
Alan Young

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: