fix(tree): stop swapping Offset start and end#676
Open
c-tonneslan wants to merge 1 commit into
Open
Conversation
Tree.Offset(start, end) is documented (via list.Offset) so that start counts from the beginning and end counts from the end, e.g. Offset(1, -1) on [A,B,C,D] shows [B,C,D]. The previous implementation swapped start and end whenever start > end, which made Offset(2, 1) on the duckduckgoose example render the first two Ducks instead of the third Duck and the Goose. The two arguments aren't interchangeable coordinates, so the swap was wrong. Closes charmbracelet#535. Also tightened the bounds handling: start and end are now both clamped to [0, length], and end=-1 (the "skip nothing from end" idiom shown in the docs) is normalized to 0 instead of being treated as "skip everything" which had its own off-by-everything bug. Added two unit tests, one for the asymmetric case from the issue and one for the negative-clamp invariant. Signed-off-by: Charlie Tonneslan <[email protected]>
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.
Closes #535.
`Tree.Offset(start, end)` is documented (via `list.Offset`) such that `start` counts from the beginning and `end` counts from the end:
The implementation swapped `start` and `end` whenever `start > end`, so `Offset(2, 1)` on the duckduckgoose example (["Duck", "Duck", "Duck", "Goose", "Duck"]) ended up acting like `Offset(1, 2)` and rendered the first two Ducks instead of the third Duck plus the Goose. The two arguments aren't interchangeable coordinates, so the swap was wrong.
While I was in there I also tightened the bounds: `start` and `end` are both clamped to `[0, length]`, and `end = -1` (the "skip nothing from end" idiom in the docs example) is normalized to `0` instead of clamping to `length` (which previously made the loop in render iterate over zero elements).
Test plan
```
go test ./...
```
Added two new unit tests in `tree/tree_test.go`: