Subject: | calculate document char offset when indexing locations |
i've mentioned this to adam in a seperate email, but wanted to file an
enhancement request as well. i'd like for ppi to calculate the character
offset of tokens within a document, ie:
package Foo;
use strict;
1;
the package token would start at offset of 0, while the word token
representing 'Foo' would start at offset 8. the 'use' statement offset
would have an offset of 14, and so on.
attached you'll find a quick diff that illustrates the changes i made to
to the PPI::Document and PPI::Dumper modules to add this enhancement.
Subject: | Dumper.patch |
--- Dumper.pm 2007-11-04 11:53:24.000000000 -0500
+++ Dumper.pm.OLD 2007-11-04 11:52:32.000000000 -0500
@@ -253,7 +253,7 @@
if ( $Element->isa('PPI::Token') ) {
my $location = $Element->location;
if ($location) {
- $loc_string = sprintf("[ % 4d, % 3d, % 3d, % 4d ] ", @$location);
+ $loc_string = sprintf("[ % 4d, % 3d, % 3d ] ", @$location);
}
}
# Output location or pad with 20 spaces
Subject: | Document.patch |
--- Document.pm.OLD 2007-11-04 11:48:31.000000000 -0500
+++ Document.pm 2007-11-04 12:00:49.000000000 -0500
@@ -146,6 +146,7 @@
my $self = $class->SUPER::new;
$self->{readonly} = ! 1;
$self->{tab_width} = 1;
+ $self->{offset} = 0;
return $self;
}
@@ -585,7 +586,7 @@
# Calculate the new location if needed.
$location = $_
? $self->_add_location( $location, $Tokens[$_ - 1], \$heredoc )
- : [ 1, 1, 1 ];
+ : [ 1, 1, 1, 0 ];
$first = $_;
last;
}
@@ -609,6 +610,8 @@
my ($self, $start, $Token, $heredoc) = @_;
my $content = $Token->{content};
+ $self->{offset} += length($content);
+
# Does the content contain any newlines
my $newlines =()= $content =~ /\n/g;
unless ( $newlines ) {
@@ -616,13 +619,14 @@
return [
$start->[0],
$start->[1] + length($content),
- $start->[2] + $self->_visual_length($content, $start->[2])
+ $start->[2] + $self->_visual_length($content, $start->[2]),
+ $self->{offset},
];
}
# This is the more complex case where we hit or
# span a newline boundary.
- my $location = [ $start->[0] + $newlines, 1, 1 ];
+ my $location = [ $start->[0] + $newlines, 1, 1, $self->{offset} ];
if ( $heredoc and $$heredoc ) {
$location->[0] += $$heredoc;
$$heredoc = 0;
@@ -633,6 +637,7 @@
if ( $content =~ /\n([^\n]+?)\z/ ) {
$location->[1] += length($1);
$location->[2] += $self->_visual_length($1, $location->[2]);
+ $location->[3] += length($1);
}
$location;