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

Fix tree reporter line highlighting #544

Merged
Merged
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
1 change: 1 addition & 0 deletions news/544.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug where the tree reporter would fail to populate the code pane with relevant lines if the line where the allocation occurred was too near the start of the file.
19 changes: 11 additions & 8 deletions src/memray/reporters/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ async def update_text_area(self) -> None:

text = self.query_one("#textarea", TextArea)

if self.frame.location is None:
if self.frame.location is None or self.frame.location == ROOT_NODE:
text.clear()
return

_, file, line = self.frame.location
delta = text.size.height // 2
lines = linecache.getlines(file)[line - delta : line + delta]
lines = linecache.getlines(file)[max(line - delta, 0) : line + delta]

text.text = "\n".join(tuple(line.rstrip() for line in lines))
text.select_line((delta - 1))
text.select_line(line - 1 if delta >= line else delta - 1)
text.show_line_numbers = False

def _get_content_by_label_id(self) -> Dict[str, str]:
Expand Down Expand Up @@ -166,18 +166,21 @@ def watch_frame(self) -> None:
def compose(self) -> ComposeResult:
if self.frame is None:
return

delta = 3

if self.frame.location is not None:
_, file, line = self.frame.location
lines = linecache.getlines(file)[line - delta : line + delta]
else:
if self.frame.location is None or self.frame.location == ROOT_NODE:
lines = []
selected_line = 0
else:
_, file, line = self.frame.location
lines = linecache.getlines(file)[max(line - delta, 0) : line + delta]
selected_line = line - 1 if delta >= line else delta - 1

text = TextArea(
"\n".join(lines), language="python", theme="dracula", id="textarea"
)
text.select_line(delta + 1)
text.select_line(selected_line)
text.show_line_numbers = False
text.can_focus = False
text.cursor_blink = False
Expand Down
Loading
Loading