47 lines
1.1 KiB
Rust
47 lines
1.1 KiB
Rust
use tree_sitter::{Point, Range};
|
|
|
|
pub fn editor_quote(s: impl AsRef<str>) -> String {
|
|
// TODO
|
|
format!("'{}'", s.as_ref())
|
|
}
|
|
|
|
pub fn range_from_byte_offsets(content: impl AsRef<[u8]>, start: usize, end: usize) -> Range {
|
|
// Kakoune's line indices are 1-based
|
|
let mut start_row = 1;
|
|
let mut start_column = 1;
|
|
let mut end_row = 1;
|
|
let mut end_column = 1;
|
|
|
|
for (i, byte) in content.as_ref().iter().enumerate() {
|
|
if i < start {
|
|
if *byte == b'\n' {
|
|
start_row += 1;
|
|
start_column = 1;
|
|
} else {
|
|
start_column += 1;
|
|
}
|
|
}
|
|
|
|
if i < end {
|
|
if *byte == b'\n' {
|
|
end_row += 1;
|
|
end_column = 1;
|
|
} else {
|
|
end_column += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
Range {
|
|
start_byte: start,
|
|
end_byte: end,
|
|
start_point: Point {
|
|
row: start_row,
|
|
column: start_column,
|
|
},
|
|
end_point: Point {
|
|
row: end_row,
|
|
column: end_column,
|
|
},
|
|
}
|
|
}
|