fix(tree): remove incorrect start/end swap in Offset#704
Closed
xbrxr03 wants to merge 1 commit into
Closed
Conversation
…ping The Offset method incorrectly swapped start and end when start > end. This made Offset(2, 1) behave like Offset(1, 2), hiding rows from the wrong end of the tree. The original issue (charmbracelet#535) demonstrates: Offset(2, 1) on [Duck, Duck, Goose, Duck, Duck] should skip 2 from the start and 1 from the end, but the swap inverted the meaning. The fix removes the swap entirely and adds proper bounds clamping in Children() so that out-of-range offsets produce empty results instead of panics. Negative values in Offset() are now clamped to zero. Includes 9 test cases covering: zero offset, skip from start, skip from end, skip from both, start > end (no swap), offset larger than children, negative values, offset that swallows all items, and equal offset. Closes charmbracelet#535
Author
|
Closing this PR due to lack of maintainer engagement. I'm happy to reopen if there's interest. Thanks for the great project. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Tree.Offset(start, end)incorrectly swapsstartandendwhenstart > end. This makesOffset(2, 1)behave likeOffset(1, 2), hiding rows from the wrong end of the tree.Reported in #535 — calling
Offset(2, 1)on[Duck, Duck, Goose, Duck, Duck]should skip 2 from the start and 1 from the end, but the swap inverts the meaning so you end up skipping 1 from the start and 2 from the end instead.Fix
Offset(). The parameters now mean what they say:start= rows to skip from the beginning,end= rows to skip from the end. No magic reordering.Children()so out-of-range offsets produce empty results instead of panicking on negative loop bounds.Offset().Offset()to explain what the parameters actually do.Comparison with #612
This PR builds on the same core insight as #612 (remove the swap), but goes further:
Children()method could produce negative loop bounds ifoffset[1] > children.Length(). This PR adds explicit clamping so the iteration is always safe.endas "use children length" — this treatedOffset(0, -1)as "show everything". The new behavior treats all negative values as zero, which is more intuitive.start > end(the core bug), offset larger than children, negative values, offset that swallows all items, and equal offsets.Test Results
Full suite passes:
ok charm.land/lipgloss/v2/tree 0.216sCloses #535