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

Conversation

godlygeek
Copy link
Contributor

Update tree reporter snapshots for Textual 0.49 (the TextArea no longer shows a cursor - yay!) and fix two bugs I noticed while looking at the snapshot diff: sometimes we don't show any lines, and sometimes we highlight the wrong line! Whoops.

@godlygeek godlygeek self-assigned this Feb 8, 2024
@godlygeek godlygeek force-pushed the fix_tree_reporter_line_highlighting branch from 42630b3 to dadc007 Compare February 8, 2024 02:28
There were two different problems here:

- When fetching lines from the file, we could choose a negative start
  index for the first line to display from the list of lines, resulting
  in no lines being shown.
- When highlighting a line close to the start of the file, we failed to
  offset the highlighted line by the number of context lines we would
  have shown but couldn't because we ran out of lines.

Signed-off-by: Matt Wozniski <[email protected]>
@godlygeek godlygeek force-pushed the fix_tree_reporter_line_highlighting branch from dadc007 to ad3ccee Compare February 8, 2024 02:31
@godlygeek
Copy link
Contributor Author

My test program for making sure the line number handling is now right was:

import mmap


def a():
    b()
    b()


def b():
    c()


def c():
    e()


def e():
    f()


def f():
    g()


def g():
    h()


def h():
    i()


def i():
    j()


def j():
    mmap.mmap(-1, 1024 * 1024)


a()

That made it easy to test both the case where there's not enough lines above the active frame's line to fill out our context and the case where there are (and the case where the active frame's line is close to the end of the file rather than the start, too, but that doesn't need any special handling).

After fixing the bug where an empty list would be returned if there were
not enough lines before the cursor line in the file, another bug was
revealed by the test suite: we've been calling `linecache.getlines()`
even for the root node. That's only noticeable in the test suite because
we mock the return value from that function. In a real run of the
tree reporter, we call `linecache.getlines()` with an empty string as
the file name, and it always returns an empty list in that case.

Still, better to fix this to recognize the root node and not attempt to
fetch lines for it at all.

Signed-off-by: Matt Wozniski <[email protected]>
This accounts for both the two bugs fixed in the previous two commits,
as well as a change in Textual 0.49: the cursor is no longer shown for
unfocusable `TextArea` widgets.

Signed-off-by: Matt Wozniski <[email protected]>
@godlygeek godlygeek force-pushed the fix_tree_reporter_line_highlighting branch from ad3ccee to d6f05d1 Compare February 8, 2024 02:35
@codecov-commenter
Copy link

codecov-commenter commented Feb 8, 2024

Codecov Report

Attention: 4 lines in your changes are missing coverage. Please review.

Comparison is base (41248ed) 92.55% compared to head (fec7d8c) 92.82%.
Report is 6 commits behind head on main.

Files Patch % Lines
src/memray/reporters/tree.py 72.72% 3 Missing ⚠️
tests/conftest.py 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #544      +/-   ##
==========================================
+ Coverage   92.55%   92.82%   +0.27%     
==========================================
  Files          91       91              
  Lines       11304    11119     -185     
  Branches     1581     2022     +441     
==========================================
- Hits        10462    10321     -141     
+ Misses        837      798      -39     
+ Partials        5        0       -5     
Flag Coverage Δ
cpp 92.82% <85.18%> (+6.88%) ⬆️
python_and_cython 92.82% <85.18%> (-2.90%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

The latest versions of Textual only support Python 3.8 and higher, so
differences from the snapshot are expected when running Python 3.7.

Signed-off-by: Matt Wozniski <[email protected]>
@pablogsal pablogsal merged commit 01ebcaf into bloomberg:main Feb 8, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants