Skip to content

Commit

Permalink
Close #271 by displaying line numbers.
Browse files Browse the repository at this point in the history
Also avoid errors when the turtle text font can't be parsed.
  • Loading branch information
donkirkby committed Jul 3, 2020
1 parent 893fc79 commit b229f32
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 7 deletions.
14 changes: 12 additions & 2 deletions plugin/PySrc/space_tracer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ def parse_args(command_args=None):
parser.add_argument('--end_line',
type=int,
help='last line number to trace')
parser.add_argument('--line_numbers',
'-l',
action='store_true',
help='include line numbers with source code')
parser.add_argument('--live',
action='store_true',
help='load main module as %s instead of %s.' %
Expand Down Expand Up @@ -480,6 +484,8 @@ def trace_command(self, command_args=None):
indent = 0
start_char = -args.source_indent
reported_source_lines = []
last_line = max(last for first, last in builder.reported_blocks)
number_width = len(str(last_line))
for first_line, last_line in builder.reported_blocks:
if first_line is None:
first_line = 1
Expand All @@ -489,8 +495,12 @@ def trace_command(self, command_args=None):
if line_number > len(source_lines):
reported_source_lines.append('')
else:
reported_source_lines.append(
source_lines[line_number-1][start_char:])
line = source_lines[line_number - 1][start_char:]
if args.line_numbers:
line = '{:{}}) {}'.format(line_number,
number_width,
line)
reported_source_lines.append(line)
max_source_width = max(map(len, reported_source_lines))
if source_width is None:
source_width = max_source_width + indent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.donkirkby.livecanvas;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
Expand Down Expand Up @@ -94,19 +96,26 @@ public int[] getYCoordinates() {
}
return copy;
}
@NotNull
public FontOptions getFontOptions(String name) {
String value = options.get(name);
if (value == null) {
return null;
value = "";
}
Pattern pattern = Pattern.compile("\\('([^']*)', (\\d+), '([^']*)'\\)");
Matcher matcher = pattern.matcher(value);
String fontName;
int size;
String[] styleNames;
if ( ! matcher.matches()) {
return null;
fontName = "Arial";
size = 8;
styleNames = new String[] {"normal"};
} else {
fontName = matcher.group(1);
size = Integer.parseInt(matcher.group(2));
styleNames = matcher.group(3).split(" ");
}
String fontName = matcher.group(1);
int size = Integer.parseInt(matcher.group(2));
String[] styleNames = matcher.group(3).split(" ");
return new FontOptions(fontName, size, styleNames);
}
}
50 changes: 50 additions & 0 deletions test/PySrc/tests/test_traced.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,3 +694,53 @@ def foo(n): | n = 3
'example.py'])

assert report == expected_report


def test_show_line_numbers():
code = """\
a = 1
b = 2
print(a + b)
"""
expected_report = """\
2) b = 2 | b = 2
3) print(a + b) | print('3')"""

with replace_input(code):
report = TraceRunner().trace_command([
'space_tracer',
'--start_line', '2',
'--end_line', '3',
'--line_numbers',
'--traced_file', 'example.py',
'example.py'])

assert report == expected_report


def test_show_line_numbers_pad_right():
code = """\
a = 1
b = 2
c = 3
d = 4
e = 5
f = 6
g = 7
h = 8
i = 9
print(f*g)"""
expected_report = """\
9) i = 9 | i = 9
10) print(f*g) | print('42')"""

with replace_input(code):
report = TraceRunner().trace_command([
'space_tracer',
'--start_line', '9',
'--end_line', '10',
'--line_numbers',
'--traced_file', 'example.py',
'example.py'])

assert report == expected_report

0 comments on commit b229f32

Please sign in to comment.