-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
RFC: Enforce Point
and Offset
invariants at the type level
#8081
Comments
I think cleaning up this code is a good idea in general, and I really like the idea of making Point and DisplayPoint less confusing by having the type-name be self-descriptive. (I would also really like to add a For a significant refactor like this, we'd like you to spend some time pairing with someone at Zed to get started (so we can quickly answer the questions that come up as we get into implementation, and keep knowledge sharing high). I'd be happy to work with you on this for a bit: https://calendly.com/conradirwin/pairing, but you can also poke around in the Zed channels https://zed.dev/channel/zed-283 and find people to talk it through with. |
Also fix a few other places we were using max_buffer_row() instead of max_point().row() Related: #8081
This function was operating in the wrong co-ordinate space (c.f. #8081)
This function was operating in the wrong co-ordinate space (c.f. #8081) Release Notes: - Fixed a panic on `ctrl-m` in a multibuffer
I took a dive into this after #8870. A few learnings:
So probably the place to start is to introduce a new abstraction at the
|
I haven’t had much time to look at this, but played around with it a bit last weekend. I came to the same conclusion - rope and buffer need to have the same coordinate space. I successfully got the rope crate to compile with a new |
This function was operating in the wrong co-ordinate space (c.f. #8081) Release Notes: - Fixed a panic on `ctrl-m` in a multibuffer
This function was operating in the wrong co-ordinate space (c.f. #8081) Release Notes: - Fixed a panic on `ctrl-m` in a multibuffer
zed-industries#11656) Part of zed-industries#8081 To avoid confusion and bugs when converting between various row `u32`'s, use different types for each. Further PRs should split `Point` into buffer and multi buffer variants and make the code more readable. Release Notes: - N/A --------- Co-authored-by: Piotr <piotr@zed.dev>
Check for existing issues
Describe the feature
Offsets and points are used extensively throughout Zed. Each instance of these implicitly has some "space" they are associated with. For example, any given offset may be a MultiBuffer offset, a Buffer offset, a Display offset, and potentially others.
This has a few drawbacks:
When first diving into the codebase, I'd see many functions asking for
offset: usize
orpoint: Point
, and at first it was quite hard understanding which text space they were asking for.I ran into this working on my (WIP) Vim Arguments Text Object pull request. I'm using the syntax tree to select the correct argument node, and converted the syntax offsets to
DisplayPoints
. The syntax offset is relative to the buffer, so this was a bug. These bugs can be subtle, as the normal use case is a singleton buffer. New contributors may not think to test in a MultiBuffer, or to test with inlay hints, folds, and other DisplayBuffer features.We have two versions of
ToOffset
andToPoint
andToPointUtf16
. We also haveDisplayPoint
,InlayPoint
,InlayOffset
,FoldPoint
,WrapPoint
, and offset variants of some of these. Many of these have duplicated implementations of traits such assum_tree::Dimension
.There's inconsistency in converting between different spaces, with methods scattered across types and spaces. Discovering the correct conversion method is non-trivial.
Proposed Solution
Offset
type, a newtype wrapper around the currentusize
we are usingPoint
,Offset
, andOffsetUtf16
generic over the space they are associated with, allowing us to enforce space invariants at compile time.Instead of
usize
andPoint
, we'd haveOffset<Buffer>
,Point<Buffer>
,Offset<MultiBuffer>
,Point<MultiBuffer>
, etc. Instead ofDisplayPoint
andInlayPoint
, we'd usePoint<DisplayMap>
andPoint<InlayMap>
.ToOffset
andToPoint
generic over the space they are converting to. This gives us a single mechanism for converting between spaces.Drawbacks
offset: Offset<MultiBuffer>
instead ofoffset: usize
is a bit more to take inIf applicable, add mockups / screenshots to help present your vision of the feature
No response
The text was updated successfully, but these errors were encountered: