rakudo-debugger icon indicating copy to clipboard operation
rakudo-debugger copied to clipboard

Crash when code to highlight includes a completely blank line

Open raiph opened this issue 11 years ago • 1 comments

http://irclog.perlgeek.de/perl6/2014-04-23#i_8625892

Using Rakudo/MoarVM and debugger built a couple days ago.

raiph avatar Apr 23 '14 07:04 raiph

The bug was in the highlighted_lines sub which starts:

sub highlighted_lines(@lines, $start_line_pos, $end_line_pos) {
    @lines.map: {
        state $line = 0;
        NEXT $line++;
        my $safe_start_pos = [min] $start_line_pos, .chars - 1;
        my $safe_end_pos   = [min] $end_line_pos, .chars - 1;

.chars - 1 is -1 on zero length lines. So the $safe_*_pos variables ironically weren't safe.

Also, the NEXT had no effect in my tests.

The sub as it stands had highlighting oddities that seem to be due to the NEXT issue.

The sub also seemed unnecessarily complex, to the degree I found it difficult to reason about. I rewrote it to the following version which seems to more directly express what the sub has to do and which has been working in my testing:

sub highlighted_lines(@lines, $start_line_pos, $end_line_pos) {
    @lines.map: {
        state $line = 0;
        my $start_pos = ($line == 0)
                               ?? [min] $start_line_pos, .chars
                               !! 0;
        my $end_pos   = ($line == @lines.end)
                               ?? [min] $end_line_pos, .chars
                               !! .chars;
        my $rendered = colored('| ', 'blue');
        if $line == 0 {
            $rendered ~= .substr(0, $start_pos);
        }
        $rendered ~= colored(.substr($start_pos, $end_pos - $start_pos), 'bold yellow');
        if $line == @lines.end {
            $rendered ~= .substr($end_pos, .chars - $end_pos);
        }
        $line++;
        $rendered.subst("\r", "")
    }
}

raiph avatar Apr 24 '14 02:04 raiph