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

clarify cursor API initial offsets #628

Closed
OmarTawfik opened this issue Oct 31, 2023 · 1 comment
Closed

clarify cursor API initial offsets #628

OmarTawfik opened this issue Oct 31, 2023 · 1 comment
Assignees

Comments

@OmarTawfik
Copy link
Contributor

OmarTawfik commented Oct 31, 2023

functions vs getters

I also suggest also not using getters, but functions, with a prefix like create_ instead of get_, since cursors returned should be persisted long-term and reused. Additionally, it is meant to be mutated, so it is no longer a property of the node that creates it. And last, that prevents bugs because of patterns like below, where users can wrongly expect the cursor to be persisted on the node:

root_node.cursor.next();
root_node.cursor.next();

initial cursor offset

currently calling node.cursor would create one at offset ZERO. This is correct for the root node, but problematic for any sub-nodes, and can cause user confusion. Let's replace this API with a few distinct methods:

  • node.create_cursor_at(offset)
  • parse_result.create_root_cursor() that would call self.root_node.create_cursor_at(ZERO) for convenience.

This way, the user is informed with the expected offset during creation.

fast cursors (no offsets)

There are use cases that need to analyze the CST without tracking offsets. This means they shouldn't pay the cost of maintaining these numbers as the cursor is running. One idea is to provide another node.create_*() API that creates a fast Cursor that can implement all same API, but won't track or provide offset values.

However, this is a nice-to-have feature, and we can put it aside for later if it will result in two divergent implementations.

@Xanewok Xanewok removed this from Slang - 2023 Q3 Nov 2, 2023
@OmarTawfik OmarTawfik assigned OmarTawfik and unassigned OmarTawfik Nov 8, 2023
github-merge-queue bot pushed a commit that referenced this issue Nov 10, 2023
- rename `with_colour` to `with_color` as we are using a single `en-US`
locale in the entire project.
- use `SUPPORTED_VERSIONS.binary_search()` in `Language::new()`
- use functions instead of properties for expensive operations -> #628
- require specifying an initial offset when creating a CST cursor ->
#628
github-merge-queue bot pushed a commit that referenced this issue Nov 22, 2023
Closes #583
Ref #628 for the modified cursor/offset API

Changes are best reviewed commit-by-commit.
@Xanewok
Copy link
Contributor

Xanewok commented Dec 2, 2023

We'd have to think a bit more how we could expose different cursors as the different logic would have to impact the Path(Rule)Node types used internally by the cursor. I've separated #691 as this is a nice-to-have at the moment and the core issue outlined here has been fixed in #647 and #666.

@Xanewok Xanewok closed this as completed Dec 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: No status
Development

No branches or pull requests

2 participants