cessen / ropey

A utf8 text rope for manipulating and editing large texts.
MIT License
1.04k stars 46 forks source link

[Qustion]: How to get the col number of byte index #85

Closed Solo-steven closed 1 year ago

Solo-steven commented 1 year ago

Thanks for building this crate, it is really helpful!

I have a question when I use ropey API, although there is byte_to_line API I can get line number by index, but seems there is no method I can get the col number from the index. Did I miss something or ropey API does not going to support this operation? or there are another way I can combine ropey API to get col number by index. Thanks for any help ~~

pascalkuthe commented 1 year ago

Depends on how you define the column number. If you define it as the utf-8 offset from the start of the line:

let col = byte - rope.line_to_byte(rope.byte_to_line(byte))

If you define it as the utf-16 offset from the start of the line:

let line = doc.byte_to_line(byte);
let line_start = doc.char_to_utf16_cu(doc.line_to_char(line));
let col = doc.char_to_utf16_cu(doc.byte_to_char(byte)) - line_start;

If you define it as the utf-32 offset from the start of the line:

let line = doc.byte_to_line(byte);
let line_start = doc.line_to_char(line);
let col = doc.byte_to_char(byte) - line_start;

If you define it as the actual visual column (for example for vertical movement in the editor) that will depend on how you render the text and will likely require some form of grapheme segmentation and using something like the unicode-width crate. The first three options are already possible with the current API. The last option is not uniformly defined and out of scope (but also possible with the current API by using the chunks iterator, its just more work)