Skip to content

Use line-index crate for offset to position conversions#325

Merged
vinistock merged 1 commit into
mainfrom
11-14-use_line-index_crate_for_offset_to_position_conversions
Nov 18, 2025
Merged

Use line-index crate for offset to position conversions#325
vinistock merged 1 commit into
mainfrom
11-14-use_line-index_crate_for_offset_to_position_conversions

Conversation

@vinistock

@vinistock vinistock commented Nov 14, 2025

Copy link
Copy Markdown
Member

In #126, I made a bit of a faux pas. I totally forgot to call attention to the fact that Rust Analyzer already publishes a number of crates for LSP related things (such as translating offsets into line/col information) and that we should check what's available.

Indeed, they do publish the line-index crate, which does everything we need to translate locations including all of the different encodings.

I propose that we switch to using the crate so that we can avoid having to maintain this translation. The crate supports translating in both directions and it supports handling incremental edits as well.

In the future, we can make each Document instance hold their own LineIndex instance and we'll be able to easily translate positions in both directions - regardless of whether the document has been committed to disk or not.

vinistock commented Nov 14, 2025

Copy link
Copy Markdown
Member Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@vinistock vinistock mentioned this pull request Nov 14, 2025
@vinistock vinistock self-assigned this Nov 14, 2025
@vinistock
vinistock marked this pull request as ready for review November 14, 2025 18:27
@vinistock
vinistock requested a review from a team as a code owner November 14, 2025 18:27
None => (0, 0, 0, 0),
};

let line_index = LineIndex::new(&source);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to think about how to structure this API in the future, but for now I'm creating a new line index every time.

@jesse-shopify jesse-shopify left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very nice change. Good find.

@vinistock
vinistock merged commit fca6864 into main Nov 18, 2025
20 checks passed
@vinistock
vinistock deleted the 11-14-use_line-index_crate_for_offset_to_position_conversions branch November 18, 2025 14:43
}

fn parse_location_positions(location: &str) -> (String, Position, Position) {
fn parse_location_positions(location: &str) -> (String, LineCol, LineCol) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we keep our Position abstraction on top of LineCol so we can add the features we need to Position and also keep the same naming through the Rust and Ruby APIs?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we currently have any custom functionality we need on top? Or will a type alias do for now?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a type alias may suffice 👍

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