Changeset 2161


Ignore:
Timestamp:
12/22/08 11:26:37 (3 years ago)
Author:
azawawi
Message:

[Padre::Plugin::Perl6] Syntax checking is up & running with full error messages from STD.pm ;-)

Location:
trunk/Padre-Plugin-Perl6/lib/Padre
Files:
3 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Padre-Plugin-Perl6/lib/Padre/Document/Perl6.pm

    r2155 r2161  
    1414our @ISA     = 'Padre::Document'; 
    1515 
    16 my $keywords; 
    17 my $issues = []; 
     16sub text_with_one_nl { 
     17    my $self = shift; 
     18    my $text = $self->text_get; 
     19    my $nlchar = "\n"; 
     20    if ( $self->get_newline_type eq 'WIN' ) { 
     21        $nlchar = "\r\n"; 
     22    } 
     23    elsif ( $self->get_newline_type eq 'MAC' ) { 
     24        $nlchar = "\r"; 
     25    } 
     26    $text =~ s/$nlchar/\n/g; 
     27    return $text; 
     28} 
    1829 
    1930# Naive way to parse and colorize perl6 files 
     
    2233 
    2334    my $editor = $self->editor; 
    24     my $text   = $self->text_get; 
     35    my $text   = $self->text_with_one_nl; 
    2536 
    2637    my $t0 = Benchmark->new; 
     
    3142    my @tokens; 
    3243    eval { @tokens = $p->tokens;   1; }; 
     44    $self->{issues} = []; 
    3345    if($EVAL_ERROR) { 
    3446        say "Parsing error, bye bye ->colorize " . $EVAL_ERROR; 
    3547        my @errors = split /\n/, $EVAL_ERROR; 
    36         my $lineno = -1; 
    37         my $error_msg = ""; 
     48        my $lineno = undef; 
    3849        for my $error (@errors) { 
    39             if($error =~ /error.+line (\d+)/) { 
     50            if($error =~ /line (\d+):$/) { 
    4051                $lineno = $1; 
    41                 $error_msg = $error; 
     52            } 
     53            if($lineno) { 
     54                push @{$self->{issues}}, { line => $lineno, msg => $error, severity => 'E', }; 
    4255            } 
    4356        } 
    44         # update errors 
    45         $issues = [ { line => $lineno, msg => $error_msg, severity => 'E', desc => $EVAL_ERROR, } ]; 
    4657        return; 
    47     } else { 
    48         # no errors... 
    49         $issues = []; 
    50     } 
     58    }  
    5159 
    5260    $self->remove_color; 
     
    113121} 
    114122 
     123# Checks the syntax of a Perl document. 
     124# Documented in Padre::Document! 
     125# Implemented as a task. See Padre::Task::SyntaxChecker::Perl6 
    115126sub check_syntax { 
     127    my $self  = shift; 
     128    my %args  = @ARG; 
     129    $args{background} = 0; 
     130    return $self->_check_syntax_internals(\%args); 
     131} 
     132 
     133sub check_syntax_in_background { 
     134    my $self  = shift; 
     135    my %args  = @ARG; 
     136    $args{background} = 1; 
     137    return $self->_check_syntax_internals(\%args); 
     138} 
     139 
     140sub _check_syntax_internals { 
    116141    my $self = shift; 
    117     return $self->_check_syntax_internal; 
    118 } 
    119  
    120 sub check_syntax_in_background { 
     142    my $args  = shift; 
     143 
     144    my $text = $self->text_with_one_nl; 
     145    unless ( defined $text and $text ne '' ) { 
     146        return []; 
     147    } 
     148 
     149    # Do we really need an update? 
     150    require Digest::MD5; 
     151    use Encode qw(encode_utf8); 
     152    my $md5 = Digest::MD5::md5(encode_utf8($text)); 
     153    unless ( $args->{force} ) { 
     154        if ( defined( $self->{last_checked_md5} ) 
     155             && $self->{last_checked_md5} eq $md5 
     156        ) { 
     157            return; 
     158        } 
     159    } 
     160    $self->{last_checked_md5} = $md5; 
     161 
     162    require Padre::Task::SyntaxChecker::Perl6; 
     163    my $task = Padre::Task::SyntaxChecker::Perl6->new( 
     164        notebook_page => $self->editor, 
     165        text => $text, 
     166        issues => $self->{issues}, 
     167        ( exists $args->{on_finish} ? (on_finish => $args->{on_finish}) : () ), 
     168    ); 
     169    if ($args->{background}) { 
     170        # asynchroneous execution (see on_finish hook) 
     171        $task->schedule(); 
     172        return(); 
     173    } 
     174    else { 
     175        # serial execution, returning the result 
     176        return() if $task->prepare() =~ /^break$/; 
     177        $task->run(); 
     178        return $task->{syntax_check}; 
     179    } 
     180    return; 
     181} 
     182 
     183sub keywords { 
    121184    my $self = shift; 
    122     return $self->_check_syntax_internal; 
    123 } 
    124  
    125 sub _check_syntax_internal { 
    126     my $self = shift; 
    127     return $issues; 
    128 } 
    129  
    130 sub keywords { 
    131     if (! defined $keywords) { 
    132         $keywords = YAML::Tiny::LoadFile( 
     185    if (! defined $self->{keywords}) { 
     186        $self->{keywords} = YAML::Tiny::LoadFile( 
    133187            Padre::Util::sharefile( 'languages', 'perl6', 'perl6.yml' ) 
    134188        ); 
    135189    } 
    136     return $keywords; 
     190    return $self->{keywords}; 
    137191} 
    138192 
  • trunk/Padre-Plugin-Perl6/lib/Padre/Plugin/Perl6.pm

    r2153 r2161  
    5959} 
    6060 
     61sub text_with_one_nl { 
     62    my $self = shift; 
     63    my $doc = shift; 
     64    my $text = $doc->text_get // ''; 
     65     
     66    my $nlchar = "\n"; 
     67    if ( $doc->get_newline_type eq 'WIN' ) { 
     68        $nlchar = "\r\n"; 
     69    } 
     70    elsif ( $doc->get_newline_type eq 'MAC' ) { 
     71        $nlchar = "\r"; 
     72    } 
     73    $text =~ s/$nlchar/\n/g; 
     74    return $text; 
     75} 
     76 
    6177sub export_html { 
    6278    my ($self, $type) = @ARG; 
    6379 
    64     if(!defined Padre::Documents->current) { 
     80    my $doc = Padre::Documents->current; 
     81    if(!defined $doc) { 
    6582        return; 
    6683    } 
    67  
    68     my $text = Padre::Documents->current->text_get() // ''; 
    69  
     84    if($doc->get_mimetype ne q{application/x-perl6}) { 
     85        return; 
     86    } 
     87     
     88    my $text = $self->text_with_one_nl($doc); 
    7089    my $p = Syntax::Highlight::Perl6->new( 
    7190        text => $text, 
Note: See TracChangeset for help on using the changeset viewer.