App-PRT

 view release on metacpan or  search on metacpan

lib/App/PRT/Command/AddMethod.pm  view on Meta::CPAN

    my $document = PPI::Document->new($file);

    my $statements = $document->find('PPI::Statement');
    die 'statement not found' unless $statements;
    my $after = $statements->[-1];

    my $code_document = PPI::Document->new(\$self->code);
    my $code_statement = $code_document->find_first('PPI::Statement::Sub');

    my @comments;
    my $cursor = $code_statement->first_token->previous_token;
    while (defined $cursor && (ref $cursor eq 'PPI::Token::Comment' || ref $cursor eq 'PPI::Token::Whitespace')) {
        unshift @comments, $cursor;
        $cursor = $cursor->previous_token;
    }

    while (ref $comments[0] eq 'PPI::Token::Whitespace') {
        shift @comments;
    }

    $after->insert_before($_) for @comments;

    $after->insert_before($code_statement);

lib/App/PRT/Command/DeleteMethod.pm  view on Meta::CPAN

    return unless $package->namespace eq $self->target_class_name;

    my $subs = $document->find('PPI::Statement::Sub');

    my $replaced = 0;
    for my $sub (@$subs) {
        next unless $sub->name eq $self->target_method_name;
        my @garbages;

        # comment before method
        my $cursor = $sub->first_token->previous_token;
        while (defined $cursor && ref $cursor eq 'PPI::Token::Comment') {
            unshift @garbages, $cursor;
            $cursor = $cursor->previous_token;
        }

        # method body
        push @garbages, $sub;

        # whitespace after method
        $cursor = $sub->last_token->next_token;
        while (defined $cursor && ref $cursor eq 'PPI::Token::Whitespace') {
            push @garbages, $cursor;
            $cursor = $cursor->next_token;
        }

        $self->{deleted_code} = join '', @garbages;

        $_->remove for @garbages;
        $replaced++;
    }

    $document->save($file) if $replaced;
}



( run in 0.231 second using v1.01-cache-2.11-cpan-4d50c553e7e )