Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding skip_blank option #36

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions stack_data/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ def range_from_node(
return None
else:
range_end = len(self.text)
if range_start == range_end == 0:
return None

return RangeInLine(range_start, range_end, data)

Expand Down
25 changes: 24 additions & 1 deletion stack_data/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def __init__(
strip_leading_indent=True,
html=False,
chain=True,
collapse_repeated_frames=True
collapse_repeated_frames=True,
skip_blank = True
):
if options is None:
options = Options()
Expand Down Expand Up @@ -64,6 +65,8 @@ def __init__(
self.chain = chain
self.options = options
self.collapse_repeated_frames = collapse_repeated_frames
self.skip_blank = skip_blank
self.last_lineno_printed = None

def set_hook(self):
def excepthook(_etype, evalue, _tb):
Expand Down Expand Up @@ -137,10 +140,14 @@ def format_frame(self, frame: Union[FrameInfo, FrameType, TracebackType]) -> Ite

for line in frame.lines:
if isinstance(line, Line):
if self.show_linenos and not self.skip_blank:
yield self.perhaps_add_blank_lines(line)
self.last_lineno_printed = line.lineno
yield self.format_line(line)
else:
assert_(line is LINE_GAP)
yield self.line_gap_string + "\n"
self.last_lineno_printed = None

if self.show_variables:
try:
Expand Down Expand Up @@ -189,9 +196,25 @@ def format_line(self, line: Line) -> str:
+ self.executing_node_underline * (end - start)
+ "\n"
)
return result


def perhaps_add_blank_lines(self, line):
if self.last_lineno_printed is None:
return ""
gap = line.lineno - self.last_lineno_printed
if line.executing_node_ranges:
self.last_lineno_printed = None
if gap <= 1:
return ""
leading_space = " " * (len(self.current_line_indicator) + 1)
if gap == 2:
result = leading_space + "{:4} |\n".format(self.last_lineno_printed + 1)
else:
result = leading_space + " : |\n"
return result


def format_variables(self, frame_info: FrameInfo) -> Iterable[str]:
for var in sorted(frame_info.variables, key=lambda v: v.name):
try:
Expand Down